diff --git a/javatests/com/jnibind/test/ArrayTestHelpers.java b/javatests/com/jnibind/test/ArrayTestHelpers.java index b8a0c3fb..c2c11df0 100644 --- a/javatests/com/jnibind/test/ArrayTestHelpers.java +++ b/javatests/com/jnibind/test/ArrayTestHelpers.java @@ -17,12 +17,87 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; /** Helpers functions to assert array behaviour from native. */ final class ArrayTestHelpers { private ArrayTestHelpers() {} + static void assertBoolean1D(boolean testForTrue, boolean[] arr) { + for (int i = 0; i < arr.length; i++) { + if (testForTrue) { + assertTrue(arr[i]); + } else { + assertFalse(arr[i]); + } + } + } + + static void assertByte1D(byte baseOffset, byte[] arr) { + for (int i = 0; i < arr.length; i++) { + assertEquals(i + baseOffset, arr[i]); + } + } + + static void assertChar1D(char baseOffset, char[] arr) { + for (int i = 0; i < arr.length; i++) { + assertEquals(i + baseOffset, arr[i]); + } + } + + static void assertShort1D(short baseOffset, short[] arr) { + for (int i = 0; i < arr.length; i++) { + assertEquals(i + baseOffset, arr[i]); + } + } + + static void assertInt1D(int baseOffset, int[] arr) { + for (int i = 0; i < arr.length; i++) { + assertEquals(i + baseOffset, arr[i]); + } + } + + static void assertLong1D(long baseOffset, long[] arr) { + for (int i = 0; i < arr.length; i++) { + assertEquals(i + baseOffset, arr[i]); + } + } + + static void assertFloat1D(float baseOffset, float[] arr) { + for (int i = 0; i < arr.length; i++) { + assertEquals(i + baseOffset, arr[i], 1.f); + } + } + + static void assertDouble1D(double baseOffset, double[] arr) { + for (int i = 0; i < arr.length; i++) { + assertEquals((double) i + baseOffset, arr[i], 0); + } + } + + static void assertString1D(String[] arr) { + assertEquals(3, arr.length); + assertEquals("Foo", arr[0]); + assertEquals("Baz", arr[1]); + assertEquals("Bar", arr[2]); + } + + static void assertObject1D(int objectMemberOffset, ObjectTestHelper[] arr) { + for (int i = 0; i < arr.length; i++) { + int idxPlusOffset = i + objectMemberOffset; + ObjectTestHelper dummyThatMatchesIdx = + new ObjectTestHelper(idxPlusOffset, idxPlusOffset, idxPlusOffset); + assertTrue(dummyThatMatchesIdx.isEqualTo(arr[i])); + } + } + + static void assertObjectArrayOfNulls1D(ObjectTestHelper[] arr) { + for (ObjectTestHelper element : arr) { + assertNull(element); + } + } + /** 2D Tests RJNI declarations. * */ static void assertBoolean2D(boolean testForTrue, boolean[][] arr) { // All even are true (unless testForTrue when they're flipped). diff --git a/javatests/com/jnibind/test/ArrayTestMethodRank1.java b/javatests/com/jnibind/test/ArrayTestMethodRank1.java index 964a31e6..3c05974b 100644 --- a/javatests/com/jnibind/test/ArrayTestMethodRank1.java +++ b/javatests/com/jnibind/test/ArrayTestMethodRank1.java @@ -40,25 +40,25 @@ public static void doShutDown() { static native void jniTearDown(); - native void nativeBooleanTests(ArrayTestMethodRank1 arrayTest, boolean[] intArray); + native void nativeBooleanTests(boolean[] intArray); - native void nativeByteTests(ArrayTestMethodRank1 arrayTest, byte[] byteArray); + native void nativeByteTests(byte[] byteArray); - native void nativeCharTests(ArrayTestMethodRank1 arrayTest, char[] charArray); + native void nativeCharTests(char[] charArray); - native void nativeShortTests(ArrayTestMethodRank1 arrayTest, short[] shortArray); + native void nativeShortTests(short[] shortArray); - native void nativeIntTests(ArrayTestMethodRank1 arrayTest, int[] intArray); + native void nativeIntTests(int[] intArray); - native void nativeLongTests(ArrayTestMethodRank1 arrayTest, long[] longArray); + native void nativeLongTests(long[] longArray); - native void nativeFloatTests(ArrayTestMethodRank1 arrayTest, float[] floatArray); + native void nativeFloatTests(float[] floatArray); - native void nativeDoubleTests(ArrayTestMethodRank1 arrayTest, double[] doubleArray); + native void nativeDoubleTests(double[] doubleArray); - native void nativeStringTests(ArrayTestMethodRank1 arrayTest, String[] stringArray); + native void nativeStringTests(String[] stringArray); - native void nativeObjectTests(ArrayTestMethodRank1 arrayTest, ObjectTestHelper[] objectArray); + native void nativeObjectTests(ObjectTestHelper[] objectArray); void booleanArray(boolean testForTrue, boolean[] arr) { for (int i = 0; i < arr.length; i++) { @@ -137,55 +137,55 @@ void objectArrayArrayOfNulls(ObjectTestHelper[] arr) { @Test public void booleanTests() { boolean[] arr = {true, true, true}; - nativeBooleanTests(this, arr); + nativeBooleanTests(arr); } @Test public void byteTests() { byte[] arr = {0, 1, 2, 3, 4, 5}; - nativeByteTests(this, arr); + nativeByteTests(arr); } @Test public void charTests() { char[] arr = {0, 1, 2, 3, 4, 5}; - nativeCharTests(this, arr); + nativeCharTests(arr); } @Test public void shortTests() { short[] arr = {0, 1, 2, 3, 4, 5}; - nativeShortTests(this, arr); + nativeShortTests(arr); } @Test public void intTests() { int[] arr = {0, 1, 2, 3, 4, 5}; - nativeIntTests(this, arr); + nativeIntTests(arr); } @Test public void longTests() { long[] arr = {0, 1, 2, 3, 4, 5}; - nativeLongTests(this, arr); + nativeLongTests(arr); } @Test public void floatTests() { float[] arr = {0, 1, 2, 3, 4, 5}; - nativeFloatTests(this, arr); + nativeFloatTests(arr); } @Test public void doubleTests() { double[] arr = {0, 1, 2, 3, 4, 5}; - nativeDoubleTests(this, arr); + nativeDoubleTests(arr); } @Test public void stringTests() { String[] arr = {"Foo", "Baz", "Bar"}; - nativeStringTests(this, arr); + nativeStringTests(arr); } @Test @@ -194,6 +194,6 @@ public void objectTests() { new ObjectTestHelper(0, 0, 0), new ObjectTestHelper(1, 1, 1), new ObjectTestHelper(2, 2, 2) }; - nativeObjectTests(this, objectTestHelper); + nativeObjectTests(objectTestHelper); } } diff --git a/javatests/com/jnibind/test/BUILD b/javatests/com/jnibind/test/BUILD index c8112162..8e52d06c 100644 --- a/javatests/com/jnibind/test/BUILD +++ b/javatests/com/jnibind/test/BUILD @@ -37,16 +37,6 @@ java_test( ################################################################################ # Array Test Helpers. ################################################################################ -cc_library( - name = "array_test_helpers_native", - hdrs = ["array_test_helpers_native.h"], - deps = [ - ":object_test_helper_jni", - "//:jni_bind", - "//implementation:method", - ], -) - java_library( name = "array_test_helpers", testonly = True, @@ -58,6 +48,16 @@ java_library( ], ) +cc_library( + name = "array_test_helpers_native", + hdrs = ["array_test_helpers_native.h"], + deps = [ + ":object_test_helper_jni", + "//:jni_bind", + "//implementation:method", + ], +) + ################################################################################ # Array Test: Methods Rank 1. ################################################################################ @@ -66,6 +66,7 @@ cc_library( testonly = True, srcs = ["array_test_method_rank_1_jni.cc"], deps = [ + ":array_test_helpers_native", ":object_test_helper_jni", "//:jni_bind", "//metaprogramming:lambda_string", @@ -86,6 +87,9 @@ java_test( srcs = ["ArrayTestMethodRank1.java"], data = [":libarray_test_method_rank_1_jni.so"], jvm_flags = ["-Djava.library.path=./javatests/com/jnibind/test"], + runtime_deps = [ + ":array_test_helpers", + ], deps = [ ":object_test_helper", "@maven//:com_google_truth_truth_1_1", diff --git a/javatests/com/jnibind/test/array_test_helpers_native.h b/javatests/com/jnibind/test/array_test_helpers_native.h index c1fa8487..2c75852e 100644 --- a/javatests/com/jnibind/test/array_test_helpers_native.h +++ b/javatests/com/jnibind/test/array_test_helpers_native.h @@ -27,6 +27,31 @@ constexpr jni::Class kArrayTestHelperClass{ "com/jnibind/test/ArrayTestHelpers", ::jni::Static{ + // Rank 1 verification. + ::jni::Method{"assertBoolean1D", ::jni::Return{}, + ::jni::Params{jboolean{}, ::jni::Array{}}}, + ::jni::Method{"assertByte1D", ::jni::Return{}, + ::jni::Params{jbyte{}, ::jni::Array{}}}, + ::jni::Method{"assertChar1D", ::jni::Return{}, + ::jni::Params{jchar{}, ::jni::Array{}}}, + ::jni::Method{"assertShort1D", ::jni::Return{}, + ::jni::Params{jshort{}, ::jni::Array{}}}, + ::jni::Method{"assertInt1D", ::jni::Return{}, + ::jni::Params{jint{}, ::jni::Array{}}}, + ::jni::Method{"assertLong1D", ::jni::Return{}, + ::jni::Params{jlong{}, ::jni::Array{}}}, + ::jni::Method{"assertFloat1D", ::jni::Return{}, + ::jni::Params{jfloat{}, ::jni::Array{}}}, + ::jni::Method{"assertDouble1D", ::jni::Return{}, + ::jni::Params{jdouble{}, ::jni::Array{}}}, + ::jni::Method{"assertString1D", ::jni::Return{}, + ::jni::Params{::jni::Array{jstring{}}}}, + ::jni::Method{ + "assertObject1D", ::jni::Return{}, + ::jni::Params{jint{}, ::jni::Array{kObjectTestHelperClass}}}, + ::jni::Method{"assertObjectArrayOfNulls1D", ::jni::Return{}, + ::jni::Params{::jni::Array{kObjectTestHelperClass}}}, + // Rank 2 verification. ::jni::Method{"assertBoolean2D", ::jni::Return{}, ::jni::Params{jboolean{}, ::jni::Array{}}}, @@ -44,8 +69,6 @@ constexpr jni::Class kArrayTestHelperClass{ ::jni::Params{jfloat{}, ::jni::Array{}}}, ::jni::Method{"assertDouble2D", ::jni::Return{}, ::jni::Params{jdouble{}, ::jni::Array{}}}, - ::jni::Method{"object1D", ::jni::Return{}, - ::jni::Params{::jni::Array{kObjectTestHelperClass}}}, ::jni::Method{"assertObject2D", ::jni::Return{}, ::jni::Params{int{}, ::jni::Array{kObjectTestHelperClass, ::jni::Rank<2>{}}}}, diff --git a/javatests/com/jnibind/test/array_test_method_rank_1_jni.cc b/javatests/com/jnibind/test/array_test_method_rank_1_jni.cc index aed213dd..a530b616 100644 --- a/javatests/com/jnibind/test/array_test_method_rank_1_jni.cc +++ b/javatests/com/jnibind/test/array_test_method_rank_1_jni.cc @@ -16,6 +16,7 @@ #include +#include "array_test_helpers_native.h" #include "object_test_helper_jni.h" #include "jni_bind.h" #include "metaprogramming/lambda_string.h" @@ -30,39 +31,21 @@ using ::jni::Method; using ::jni::Params; using ::jni::RegularToArrayTypeMap_t; using ::jni::Return; +using ::jni::StaticRef; static std::unique_ptr> jvm; -// clang-format off -static constexpr Class kArrayTestMethodRank1 { - "com/jnibind/test/ArrayTestMethodRank1", - Method {"booleanArray", Return{}, Params{jboolean{}, Array{jboolean{}}}}, - Method {"byteArray", Return{}, Params{jbyte{}, Array{jbyte{}}}}, - Method {"charArray", Return{}, Params{jchar{}, Array{jchar{}}}}, - Method {"shortArray", Return{}, Params{jshort{}, Array{jshort{}}}}, - Method {"intArray", Return{}, Params{int{}, Array{jint{}}}}, - Method {"longArray", Return{}, Params{jlong{}, Array{jlong{}}}}, - Method {"floatArray", Return{}, Params{float{}, Array{jfloat{}}}}, - Method {"doubleArray", Return{}, Params{jdouble{}, Array{jdouble{}}}}, - Method {"stringArray", Return{}, Params{Array{jstring{}}}}, - Method {"objectArray", Return{}, Params{int{}, Array{kObjectTestHelperClass}}}, - - Method {"objectArrayArrayOfNulls", - Return{}, Params{Array{kObjectTestHelperClass}}}, -}; -// clang-format on - // Generic method test suitable for simple primitive types. // Strings are passed through lambdas as method indexing is compile time. template -void GenericMethodTest(LocalObject fixture, - LocalArray local_arr, SpanType base, +void GenericMethodTest(LocalArray local_arr, SpanType base, MethodNameLambda method_name_lambda) { // Simple lvalue pass through works as expected. - fixture(method_name_lambda(), base, local_arr); + StaticRef{}(method_name_lambda(), base, local_arr); // Simple rvalue pass through works as expected. - fixture(method_name_lambda(), base, std::move(local_arr)); + StaticRef{}(method_name_lambda(), base, + std::move(local_arr)); // Building a new array, and setting all the values by hand works. LocalArray new_array{8}; @@ -72,7 +55,7 @@ void GenericMethodTest(LocalObject fixture, array_view.ptr()[i] = base + static_cast(i); } } - fixture(method_name_lambda(), base, new_array); + StaticRef{}(method_name_lambda(), base, new_array); // You can pull the view multiple times. { @@ -90,7 +73,7 @@ void GenericMethodTest(LocalObject fixture, val = base + i; i++; } - fixture(method_name_lambda(), base, new_array); + StaticRef{}(method_name_lambda(), base, new_array); // You can build an array of null values and set the values manually. LocalArray arr_built_from_null{5}; @@ -101,7 +84,7 @@ void GenericMethodTest(LocalObject fixture, val = base + j; j++; } - fixture(method_name_lambda(), base, new_array); + StaticRef{}(method_name_lambda(), base, new_array); } extern "C" { @@ -118,78 +101,71 @@ JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestMethodRank1_jniTearDown( JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestMethodRank1_nativeBooleanTests( - JNIEnv* env, jclass, jobject test_fixture, jbooleanArray boolean_array) { - GenericMethodTest(LocalObject{test_fixture}, - LocalArray{boolean_array}, jboolean{true}, - STR("booleanArray")); + JNIEnv* env, jclass, jbooleanArray boolean_array) { + GenericMethodTest(LocalArray{boolean_array}, jboolean{true}, + STR("assertBoolean1D")); } JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestMethodRank1_nativeByteTests( - JNIEnv* env, jclass, jobject test_fixture, jbyteArray byte_array) { - GenericMethodTest(LocalObject{test_fixture}, - LocalArray{byte_array}, jbyte{0}, STR("byteArray")); + JNIEnv* env, jclass, jbyteArray byte_array) { + GenericMethodTest(LocalArray{byte_array}, jbyte{0}, + STR("assertByte1D")); } JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestMethodRank1_nativeCharTests( - JNIEnv* env, jclass, jobject test_fixture, jcharArray char_array) { - GenericMethodTest(LocalObject{test_fixture}, - LocalArray{char_array}, jchar{0}, STR("charArray")); + JNIEnv* env, jclass, jcharArray char_array) { + GenericMethodTest(LocalArray{char_array}, jchar{0}, + STR("assertChar1D")); } JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestMethodRank1_nativeShortTests( - JNIEnv* env, jclass, jobject test_fixture, jshortArray short_array) { - GenericMethodTest(LocalObject{test_fixture}, - LocalArray{short_array}, jshort{0}, - STR("shortArray")); + JNIEnv* env, jclass, jshortArray short_array) { + GenericMethodTest(LocalArray{short_array}, jshort{0}, + STR("assertShort1D")); } JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestMethodRank1_nativeIntTests(JNIEnv* env, jclass, - jobject test_fixture, + jintArray int_array) { - GenericMethodTest(LocalObject{test_fixture}, - LocalArray{int_array}, jint{0}, STR("intArray")); + GenericMethodTest(LocalArray{int_array}, jint{0}, STR("assertInt1D")); } JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestMethodRank1_nativeLongTests( - JNIEnv* env, jclass, jobject test_fixture, jlongArray long_array) { - GenericMethodTest(LocalObject{test_fixture}, - LocalArray{long_array}, jlong{0}, STR("longArray")); + JNIEnv* env, jclass, jlongArray long_array) { + GenericMethodTest(LocalArray{long_array}, jlong{0}, + STR("assertLong1D")); } JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestMethodRank1_nativeFloatTests( - JNIEnv* env, jclass, jobject test_fixture, jfloatArray float_array) { - GenericMethodTest(LocalObject{test_fixture}, - LocalArray{float_array}, jfloat{0}, - STR("floatArray")); + JNIEnv* env, jclass, jfloatArray float_array) { + GenericMethodTest(LocalArray{float_array}, jfloat{0}, + STR("assertFloat1D")); } JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestMethodRank1_nativeDoubleTests( - JNIEnv* env, jclass, jobject test_fixture, jdoubleArray double_array) { - GenericMethodTest(LocalObject{test_fixture}, - LocalArray{double_array}, jdouble{0}, - STR("doubleArray")); + JNIEnv* env, jclass, jdoubleArray double_array) { + GenericMethodTest(LocalArray{double_array}, jdouble{0}, + STR("assertDouble1D")); } JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestMethodRank1_nativeStringTests( - JNIEnv* env, jclass, jobject test_fixture, jobjectArray object_array) { - LocalObject fixture{test_fixture}; - + JNIEnv* env, jclass, jobjectArray object_array) { // Simple lvalue pass through works as expected. LocalArray local_arr{object_array}; - fixture("stringArray", local_arr); + StaticRef{}("assertString1D", local_arr); // TODO(b/143908983): Currently not possible to write. // Simple rvalue pass through works as expected. /* - fixture("stringArray", + StaticRef{}("assertString1D", LocalArray{ { "Foo", std::string{"baz"}, LocalString{"Baz"} } }); @@ -200,7 +176,7 @@ Java_com_jnibind_test_ArrayTestMethodRank1_nativeStringTests( new_array.Set(0, LocalString{"Foo"}); new_array.Set(1, LocalString{"Baz"}); new_array.Set(2, LocalString{"Bar"}); - fixture("stringArray", new_array); + StaticRef{}("assertString1D", new_array); // And it can be iterated over. std::size_t i = 0; @@ -210,26 +186,26 @@ Java_com_jnibind_test_ArrayTestMethodRank1_nativeStringTests( i++; } - fixture("stringArray", validator_array); + StaticRef{}("assertString1D", validator_array); } JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestMethodRank1_nativeObjectTests( - JNIEnv* env, jclass, jobject test_fixture, jobjectArray object_array) { - LocalObject fixture{test_fixture}; - + JNIEnv* env, jclass, jobjectArray object_array) { // Creating arrays of nulls with just size works. LocalArray local_arr_nulls{5}; - fixture("objectArrayArrayOfNulls", local_arr_nulls); + StaticRef{}("assertObjectArrayOfNulls1D", + local_arr_nulls); // Simple lvalue pass through works as expected. LocalArray local_arr{object_array}; - fixture("objectArray", 0, local_arr); + StaticRef{}("assertObject1D", 0, local_arr); // Simple rvalue pass through works as expected. - fixture("objectArray", 5, - LocalArray{ - 1, LocalObject{5, 5, 5}}); + StaticRef{}( + "assertObject1D", 5, + LocalArray{ + 1, LocalObject{5, 5, 5}}); // Building a new array, and setting all the values by hand works. LocalObject obj{0, 0, 0}; @@ -240,7 +216,7 @@ Java_com_jnibind_test_ArrayTestMethodRank1_nativeObjectTests( i, LocalObject{jint{i}, jint{i}, jint{i}}); } } - fixture("objectArray", 0, new_array); + StaticRef{}("assertObject1D", 0, new_array); // You can pull the view multiple times. { @@ -261,7 +237,7 @@ Java_com_jnibind_test_ArrayTestMethodRank1_nativeObjectTests( val["intVal3"].Set(val["intVal3"].Get() + 3); } - fixture("objectArray", 2 + 3, new_array); + StaticRef{}("assertObject1D", 2 + 3, new_array); } } // extern "C"