diff --git a/javatests/com/jnibind/test/ArrayTestFieldRank2.java b/javatests/com/jnibind/test/ArrayTestFieldRank2.java new file mode 100644 index 00000000..e09913db --- /dev/null +++ b/javatests/com/jnibind/test/ArrayTestFieldRank2.java @@ -0,0 +1,175 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.jnibind.test; + +import org.junit.AfterClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Exercises array functionality arcross rJNI. */ +@RunWith(JUnit4.class) +public class ArrayTestFieldRank2 { + static { + System.loadLibrary("array_test_field_rank_2_jni"); + } + + @AfterClass + public static void doShutDown() { + jniTearDown(); + } + + static native void jniTearDown(); + + public boolean[][] booleanArrayField = { + {false, true, false}, + {true, false, true}, + {false, true, false} + }; + + byte[][] byteArrayField = { + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0} + }; + + char[][] charArrayField = { + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + }; + + short[][] shortArrayField = { + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0} + }; + + int[][] intArrayField = { + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0} + }; + + long[][] longArrayField = { + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0} + }; + + float[][] floatArrayField = { + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0} + }; + + double[][] doubleArrayField = { + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0} + }; + + String[][] stringArrayField = { + {"BAD", "BAD", "BAD"}, + {"BAD", "BAD", "BAD"}, + {"BAD", "BAD", "BAD"} + }; + + ObjectTestHelper[][] objectArrayField = { + {new ObjectTestHelper(0, 0, 0), new ObjectTestHelper(0, 0, 0), new ObjectTestHelper(0, 0, 0)}, + {new ObjectTestHelper(0, 0, 0), new ObjectTestHelper(0, 0, 0), new ObjectTestHelper(0, 0, 0)}, + {new ObjectTestHelper(0, 0, 0), new ObjectTestHelper(0, 0, 0), new ObjectTestHelper(0, 0, 0)} + }; + + native void nativeBooleanTests(ArrayTestFieldRank2 arrayTest); + + native void nativeByteTests(ArrayTestFieldRank2 arrayTest); + + native void nativeCharTests(ArrayTestFieldRank2 arrayTest); + + native void nativeShortTests(ArrayTestFieldRank2 arrayTest); + + native void nativeIntTests(ArrayTestFieldRank2 arrayTest); + + native void nativeLongTests(ArrayTestFieldRank2 arrayTest); + + native void nativeFloatTests(ArrayTestFieldRank2 arrayTest); + + native void nativeDoubleTests(ArrayTestFieldRank2 arrayTest); + + native void nativeStringTests(ArrayTestFieldRank2 arrayTest); + + native void nativeObjectTests(ArrayTestFieldRank2 arrayTest); + + @Test + public void booleanTests() { + boolean[] arr = {false, false, false}; + nativeBooleanTests(this); + } + + @Test + public void byteTests() { + byte[] arr = {0, 1, 2, 3, 4, 5}; + nativeByteTests(this); + } + + @Test + public void charTests() { + char[] arr = {0, 1, 2, 3, 4, 5}; + nativeCharTests(this); + } + + @Test + public void shortTests() { + short[] arr = {0, 1, 2, 3, 4, 5}; + nativeShortTests(this); + } + + @Test + public void intTests() { + int[] arr = {0, 1, 2, 3, 4, 5}; + nativeIntTests(this); + } + + @Test + public void longTests() { + long[] arr = {0, 1, 2, 3, 4, 5}; + nativeLongTests(this); + } + + @Test + public void floatTests() { + float[] arr = {0, 1, 2, 3, 4, 5}; + nativeFloatTests(this); + } + + @Test + public void doubleTests() { + double[] arr = {0, 1, 2, 3, 4, 5}; + nativeDoubleTests(this); + } + + @Test + public void stringTests() { + // TODO(b/143908983): This is broken, but using regular `jobject` works. + } + + @Test + public void objectTests() { + nativeObjectTests(this); + } +} diff --git a/javatests/com/jnibind/test/ArrayTestHelpers.java b/javatests/com/jnibind/test/ArrayTestHelpers.java index 4eb8dc74..e5bec070 100644 --- a/javatests/com/jnibind/test/ArrayTestHelpers.java +++ b/javatests/com/jnibind/test/ArrayTestHelpers.java @@ -121,7 +121,7 @@ static void assertObjectArrayOfNulls1D(ObjectTestHelper[] arr) { } /** Rank 2 Verification Methods. */ - static void assertBoolean2D(boolean testForTrue, boolean[][] arr) { + static void assertBoolean2D(boolean testForTrue, boolean unusedStride, boolean[][] arr) { // All even are true (unless testForTrue when they're flipped). for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { @@ -137,82 +137,88 @@ static void assertBoolean2D(boolean testForTrue, boolean[][] arr) { } } - static void assertByte2D(byte baseOffset, byte[][] arr) { + static void assertByte2D(byte baseOffset, byte stride, byte[][] arr) { byte curVal = baseOffset; for (byte[] element : arr) { for (int j = 0; j < element.length; j++) { assertEquals(curVal, element[j]); - curVal++; + curVal += stride; } } } - static void assertChar2D(char baseOffset, char[][] arr) { + static void assertChar2D(char baseOffset, char stride, char[][] arr) { char curVal = baseOffset; for (char[] element : arr) { for (int j = 0; j < element.length; j++) { assertEquals(curVal, element[j]); - curVal++; + curVal += stride; } } } - static void assertShort2D(short baseOffset, short[][] arr) { + static void assertShort2D(short baseOffset, short stride, short[][] arr) { short curVal = baseOffset; for (short[] element : arr) { for (int j = 0; j < element.length; j++) { assertEquals(curVal, element[j]); - curVal++; + curVal += stride; } } } - static void assertInt2D(int baseOffset, int[][] arr) { + static void assertInt2D(int baseOffset, int stride, int[][] arr) { int curVal = baseOffset; for (int[] element : arr) { for (int j = 0; j < element.length; j++) { assertEquals(curVal, element[j]); - curVal++; + curVal += stride; } } } - static void assertLong2D(long baseOffset, long[][] arr) { + static void assertLong2D(long baseOffset, long stride, long[][] arr) { long curVal = baseOffset; for (long[] element : arr) { for (int j = 0; j < element.length; j++) { assertEquals(curVal, element[j]); - curVal++; + curVal += stride; } } } - static void assertFloat2D(float baseOffset, float[][] arr) { + static void assertFloat2D(float baseOffset, float stride, float[][] arr) { float curVal = baseOffset; for (float[] element : arr) { for (int j = 0; j < element.length; j++) { assertEquals(curVal, element[j], 0.0f); - curVal++; + curVal += stride; } } } - static void assertDouble2D(double baseOffset, double[][] arr) { + static void assertDouble2D(double baseOffset, double stride, double[][] arr) { double curVal = baseOffset; for (double[] element : arr) { for (int j = 0; j < element.length; j++) { assertEquals(curVal, element[j], 0.0); - curVal++; + curVal += stride; } } } - static void assertObject2D(int objectMemberOffset, ObjectTestHelper[][] arr) { + static void assertObject2D(int objectMemberOffset, int stride, ObjectTestHelper[][] arr) { int curVal = objectMemberOffset; for (ObjectTestHelper[] objectList : arr) { for (ObjectTestHelper element : objectList) { - assertTrue(element.isEqualTo(new ObjectTestHelper(curVal, curVal, curVal))); - curVal++; + int curVal1 = curVal; + curVal += stride; + int curVal2 = curVal; + curVal += stride; + int curVal3 = curVal; + curVal += stride; + + assertTrue(element.isEqualTo(new ObjectTestHelper(curVal1, curVal2, curVal3))); } } } diff --git a/javatests/com/jnibind/test/ArrayTestMethodRank2.java b/javatests/com/jnibind/test/ArrayTestMethodRank2.java index a3942e46..5bfdc76b 100644 --- a/javatests/com/jnibind/test/ArrayTestMethodRank2.java +++ b/javatests/com/jnibind/test/ArrayTestMethodRank2.java @@ -139,9 +139,17 @@ public void doubleTests2D() { @Test public void objectTests2D() { ObjectTestHelper[][] arr = { - {new ObjectTestHelper(0, 0, 0), new ObjectTestHelper(1, 1, 1), new ObjectTestHelper(2, 2, 2)}, - {new ObjectTestHelper(3, 3, 3), new ObjectTestHelper(4, 4, 4), new ObjectTestHelper(5, 5, 5)}, - {new ObjectTestHelper(6, 6, 6), new ObjectTestHelper(7, 7, 7), new ObjectTestHelper(8, 8, 8)}, + {new ObjectTestHelper(0, 1, 2), new ObjectTestHelper(3, 4, 5), new ObjectTestHelper(6, 7, 8)}, + { + new ObjectTestHelper(9, 10, 11), + new ObjectTestHelper(12, 13, 14), + new ObjectTestHelper(15, 16, 17) + }, + { + new ObjectTestHelper(18, 19, 20), + new ObjectTestHelper(21, 22, 23), + new ObjectTestHelper(24, 25, 26) + }, }; nativeObjectTests2D(arr); diff --git a/javatests/com/jnibind/test/BUILD b/javatests/com/jnibind/test/BUILD index 7aee9d46..27d1b33a 100644 --- a/javatests/com/jnibind/test/BUILD +++ b/javatests/com/jnibind/test/BUILD @@ -38,6 +38,45 @@ java_test( ], ) +################################################################################ +# Array Test: Fields Rank 2. +################################################################################ +cc_library( + name = "array_test_field_rank_2_jni_impl", + testonly = True, + srcs = ["array_test_field_rank_2_jni.cc"], + deps = [ + ":array_test_helpers_native", + ":modulo", + ":object_test_helper_jni", + "//:jni_bind", + "//metaprogramming:lambda_string", + ], + alwayslink = True, +) + +cc_binary( + name = "libarray_test_field_rank_2_jni.so", + testonly = True, + linkshared = True, + deps = [":array_test_field_rank_2_jni_impl"], +) + +java_test( + name = "ArrayTestFieldRank2", + testonly = True, + srcs = ["ArrayTestFieldRank2.java"], + data = [":libarray_test_field_rank_2_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", + ], +) + ################################################################################ # Array Test Helpers. ################################################################################ diff --git a/javatests/com/jnibind/test/array_test_field_rank_2_jni.cc b/javatests/com/jnibind/test/array_test_field_rank_2_jni.cc new file mode 100644 index 00000000..49f779a9 --- /dev/null +++ b/javatests/com/jnibind/test/array_test_field_rank_2_jni.cc @@ -0,0 +1,275 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "array_test_helpers_native.h" +#include "modulo.h" +#include "object_test_helper_jni.h" +#include "jni_bind.h" +#include "metaprogramming/lambda_string.h" + +using ::jni::Array; +using ::jni::ArrayView; +using ::jni::Class; +using ::jni::Field; +using ::jni::LocalArray; +using ::jni::LocalObject; +using ::jni::LocalString; +using ::jni::Modulo; +using ::jni::Rank; +using ::jni::StaticRef; + +static std::unique_ptr> jvm; + +// clang-format off +static constexpr Class kArrayTestFieldRank2 { + "com/jnibind/test/ArrayTestFieldRank2", + + // Fields under test: Rank 1 Primitives. + Field {"booleanArrayField", Array{jboolean{}, Rank<2>{}}}, + Field {"byteArrayField", Array{jbyte{}, Rank<2>{}}}, + Field {"charArrayField", Array{jchar{}, Rank<2>{}}}, + Field {"shortArrayField", Array{jshort{}, Rank<2>{}}}, + Field {"intArrayField", Array{jint{}, Rank<2>{}}}, + Field {"longArrayField", Array{jlong{}, Rank<2>{}}}, + Field {"floatArrayField", Array{jfloat{}, Rank<2>{}}}, + Field {"doubleArrayField", Array{jdouble{}, Rank<2>{}}}, + Field {"stringArrayField", Array{jstring{}, Rank<2>{}}}, + + // Fields under test: Rank 1 Objects. + Field {"objectArrayField", Array{kObjectTestHelperClass, Rank<2>{}}}, +}; +// clang-format on + +// Generic field test suitable for simple primitive types. +// Strings are passed through lambdas as field indexing is compile time. +template +void GenericFieldTest(LocalObject fixture, + SpanType max_val, SpanType base, SpanType stride, + FieldNameLambda field_name_lambda, + MethodNameLambda method_name_lambda) { + LocalArray arr{fixture[field_name_lambda()].Get()}; + + // Field starts in default state. + StaticRef{}(method_name_lambda(), SpanType{0}, + SpanType{0}, + fixture[field_name_lambda()].Get()); + + // Updating the field manually works. + { + std::size_t i = 0; + for (LocalArray arr_rank_1 : arr.Pin()) { + ArrayView pin = arr_rank_1.Pin(); + for (int j = 0; j < arr_rank_1.Length(); j++) { + pin.ptr()[j] = Modulo(i, base, max_val); + i++; + } + } + + StaticRef{}(method_name_lambda(), base, SpanType{1}, + fixture[field_name_lambda()].Get()); + } + + // Updating the field repeatedly works. + { + std::size_t i = 0; + { + for (LocalArray arr_rank_1 : arr.Pin()) { + // Updating the field repeatedly works. + { + ArrayView pin = arr_rank_1.Pin(); + for (int j = 0; j < arr_rank_1.Length(); ++j) { + pin.ptr()[j] = + Modulo(stride * static_cast(i), base, max_val); + i++; + } + } + } + StaticRef{}(method_name_lambda(), base, stride, + fixture[field_name_lambda()].Get()); + } + } + + // Not pinning the value has no effect. + { + for (LocalArray arr_rank_1 : arr.Pin()) { + std::memset(arr_rank_1.Pin(false).ptr(), 0, + arr_rank_1.Length() * sizeof(SpanType)); + } + } + StaticRef{}(method_name_lambda(), base, stride, + fixture[field_name_lambda()].Get()); +} + +extern "C" { + +JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* pjvm, void* reserved) { + jvm.reset(new jni::JvmRef(pjvm)); + return JNI_VERSION_1_6; +} + +JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestFieldRank2_jniTearDown( + JavaVM* pjvm, void* reserved) { + jvm = nullptr; +} + +JNIEXPORT void JNICALL +Java_com_jnibind_test_ArrayTestFieldRank2_nativeBooleanTests( + JNIEnv* env, jclass, jobject test_fixture) { + GenericFieldTest(LocalObject{test_fixture}, jboolean{2}, + jboolean{0}, jboolean{1}, STR("booleanArrayField"), + STR("assertBoolean2D")); +} + +JNIEXPORT void JNICALL +Java_com_jnibind_test_ArrayTestFieldRank2_nativeByteTests( + JNIEnv* env, jclass, jobject test_fixture) { + GenericFieldTest(LocalObject{test_fixture}, + std::numeric_limits::max(), jbyte{10}, jbyte{2}, + STR("byteArrayField"), STR("assertByte2D")); +} + +JNIEXPORT void JNICALL +Java_com_jnibind_test_ArrayTestFieldRank2_nativeCharTests( + JNIEnv* env, jclass, jobject test_fixture) { + GenericFieldTest(LocalObject{test_fixture}, + std::numeric_limits::max(), jchar{0}, jchar{1}, + STR("charArrayField"), STR("assertChar2D")); +} + +JNIEXPORT void JNICALL +Java_com_jnibind_test_ArrayTestFieldRank2_nativeShortTests( + JNIEnv* env, jclass, jobject test_fixture) { + GenericFieldTest(LocalObject{test_fixture}, + std::numeric_limits::max(), jshort{0}, jshort{1}, + STR("shortArrayField"), STR("assertShort2D")); +} + +JNIEXPORT void JNICALL Java_com_jnibind_test_ArrayTestFieldRank2_nativeIntTests( + JNIEnv* env, jclass, jobject test_fixture) { + GenericFieldTest(LocalObject{test_fixture}, + std::numeric_limits::max(), jint{0}, jint{1}, + STR("intArrayField"), STR("assertInt2D")); +} + +JNIEXPORT void JNICALL +Java_com_jnibind_test_ArrayTestFieldRank2_nativeLongTests( + JNIEnv* env, jclass, jobject test_fixture) { + GenericFieldTest(LocalObject{test_fixture}, + std::numeric_limits::max(), jlong{0}, jlong{1}, + STR("longArrayField"), STR("assertLong2D")); +} + +JNIEXPORT void JNICALL +Java_com_jnibind_test_ArrayTestFieldRank2_nativeFloatTests( + JNIEnv* env, jclass, jobject test_fixture) { + GenericFieldTest(LocalObject{test_fixture}, + std::numeric_limits::max(), jfloat{0.f}, jfloat{1.f}, + STR("floatArrayField"), STR("assertFloat2D")); +} + +JNIEXPORT void JNICALL +Java_com_jnibind_test_ArrayTestFieldRank2_nativeDoubleTests( + JNIEnv* env, jclass, jobject test_fixture) { + GenericFieldTest(LocalObject{test_fixture}, + std::numeric_limits::max(), jdouble{0}, jdouble{1}, + STR("doubleArrayField"), STR("assertDouble2D")); +} + +// TODO(b/143908983): This is broken, but using regular `jobject` works, +// so there is still an alternative path to be take. +JNIEXPORT void JNICALL +Java_com_jnibind_test_ArrayTestFieldRank2_nativeStringTests( + JNIEnv* env, jclass, jobject test_fixture) { + /* + LocalObject fixture{test_fixture}; + + // Field starts in default state. + StaticRef{}( + "assertString1D", fixture["stringArrayField"].Get(), jboolean{false}); + + // Updating the field manually works. + LocalArray arr = fixture["stringArrayField"].Get(); + arr.Set(0, LocalString{"Foo"}); + arr.Set(1, LocalString{"Baz"}); + arr.Set(2, LocalString{"Bar"}); + StaticRef{}("assertString1D", arr, true); + + // Updating the field repeatedly works. + arr.Set(0, LocalString{"FAKE"}); + arr.Set(1, LocalString{"DEAD"}); + arr.Set(2, LocalString{"BEEF"}); + StaticRef{}("assertString1D", arr, false); + + // Updating the field repeatedly works. + arr.Set(0, "Foo"); + const char* kBaz = "Baz"; + arr.Set(1, kBaz); + arr.Set(2, std::string{"Bar"}); + StaticRef{}("assertString1D", arr, true); + + // Iterating over values works. + static constexpr std::array vals{"Foo", "Baz", "Bar"}; + int i = 0; + for (LocalString local_string : arr.Pin()) { + StaticRef{}("assertString", local_string, vals[i]); + ++i; + } + + // Iterating over passed values works. + i = 0; + for (LocalString local_string : LocalArray{object_array}.Pin()) { + StaticRef{}("assertString", local_string, vals[i]); + ++i; + } + */ +} + +void Java_com_jnibind_test_ArrayTestFieldRank2_nativeObjectTests( + JNIEnv* env, jclass, jobject test_fixture) { + LocalObject fixture{test_fixture}; + LocalArray arr = + fixture["objectArrayField"].Get(); + + int i = 0; + { + for (LocalArray arr_rank_1 : + arr.Pin()) { + for (LocalObject obj : arr_rank_1.Pin()) { + obj["intVal1"].Set(i); + i++; + obj["intVal2"].Set(i); + i++; + obj["intVal3"].Set(i); + i++; + } + } + } + + // Object on fixture is already updated. + StaticRef{}("assertObject2D", 0, 1, + fixture["objectArrayField"].Get()); + + // But using local reference works too. + StaticRef{}("assertObject2D", 0, 1, arr); +} + +} // extern "C" diff --git a/javatests/com/jnibind/test/array_test_helpers_native.h b/javatests/com/jnibind/test/array_test_helpers_native.h index 8e2c94e2..d8668bae 100644 --- a/javatests/com/jnibind/test/array_test_helpers_native.h +++ b/javatests/com/jnibind/test/array_test_helpers_native.h @@ -64,25 +64,33 @@ constexpr jni::Class kArrayTestHelperClass{ ::jni::Params{::jni::Array{kObjectTestHelperClass}}}, // Rank 2 Verification. - ::jni::Method{"assertBoolean2D", ::jni::Return{}, - ::jni::Params{jboolean{}, ::jni::Array{}}}, - ::jni::Method{"assertByte2D", ::jni::Return{}, - ::jni::Params{jbyte{}, ::jni::Array{}}}, - ::jni::Method{"assertChar2D", ::jni::Return{}, - ::jni::Params{jchar{}, ::jni::Array{}}}, - ::jni::Method{"assertShort2D", ::jni::Return{}, - ::jni::Params{jshort{}, ::jni::Array{}}}, + ::jni::Method{ + "assertBoolean2D", ::jni::Return{}, + ::jni::Params{jboolean{}, jboolean{}, ::jni::Array{}}}, + ::jni::Method{ + "assertByte2D", ::jni::Return{}, + ::jni::Params{jbyte{}, jbyte{}, ::jni::Array{}}}, + ::jni::Method{ + "assertChar2D", ::jni::Return{}, + ::jni::Params{jchar{}, jchar{}, ::jni::Array{}}}, + ::jni::Method{ + "assertShort2D", ::jni::Return{}, + ::jni::Params{jshort{}, jshort{}, ::jni::Array{}}}, ::jni::Method{"assertInt2D", ::jni::Return{}, - ::jni::Params{jint{}, ::jni::Array{}}}, - ::jni::Method{"assertLong2D", ::jni::Return{}, - ::jni::Params{jlong{}, ::jni::Array{}}}, - ::jni::Method{"assertFloat2D", ::jni::Return{}, - ::jni::Params{jfloat{}, ::jni::Array{}}}, - ::jni::Method{"assertDouble2D", ::jni::Return{}, - ::jni::Params{jdouble{}, ::jni::Array{}}}, + ::jni::Params{jint{}, jint{}, ::jni::Array{}}}, + ::jni::Method{ + "assertLong2D", ::jni::Return{}, + ::jni::Params{jlong{}, jlong{}, ::jni::Array{}}}, + ::jni::Method{ + "assertFloat2D", ::jni::Return{}, + ::jni::Params{jfloat{}, jfloat{}, ::jni::Array{}}}, + ::jni::Method{ + "assertDouble2D", ::jni::Return{}, + ::jni::Params{jdouble{}, jdouble{}, ::jni::Array{}}}, ::jni::Method{"assertObject2D", ::jni::Return{}, - ::jni::Params{int{}, ::jni::Array{kObjectTestHelperClass, - ::jni::Rank<2>{}}}}, + ::jni::Params{int{}, int{}, + ::jni::Array{kObjectTestHelperClass, + ::jni::Rank<2>{}}}}, }}; #endif // JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_ARRAY_TEST_HELPERS_NATIVE_H_ diff --git a/javatests/com/jnibind/test/array_test_method_rank_2_jni.cc b/javatests/com/jnibind/test/array_test_method_rank_2_jni.cc index 6e63328e..aac9599b 100644 --- a/javatests/com/jnibind/test/array_test_method_rank_2_jni.cc +++ b/javatests/com/jnibind/test/array_test_method_rank_2_jni.cc @@ -37,11 +37,12 @@ void GenericMethodTest( MethodNameLambda method_name_lambda, LocalArray arr, SpanType max_val = std::numeric_limits::max()) { // Simple lvalue pass through works as expected. - StaticRef{}(method_name_lambda(), SpanType{0}, arr); + StaticRef{}(method_name_lambda(), SpanType{0}, + SpanType{1}, arr); // Simple rvalue pass through works as expected. StaticRef{}(method_name_lambda(), SpanType{0}, - std::move(arr)); + SpanType{1}, std::move(arr)); // Building a new array, and setting all the values by hand works. LocalArray new_array{3}; @@ -67,7 +68,7 @@ void GenericMethodTest( new_array.Set(2, row3); StaticRef{}(method_name_lambda(), SpanType{0}, - new_array); + SpanType{1}, new_array); } // You can pull the view multiple times with iterators (each value ticked 1). @@ -78,7 +79,7 @@ void GenericMethodTest( } } StaticRef{}(method_name_lambda(), SpanType{1}, - new_array); + SpanType{1}, new_array); } // You can pull the view multiple times with raw loops. @@ -95,8 +96,8 @@ void GenericMethodTest( } // Each variant increments base by 1, so 2 is used here. - StaticRef{}(method_name_lambda(), - Modulo(2, {0}, max_val), new_array); + StaticRef{}( + method_name_lambda(), Modulo(2, {0}, max_val), SpanType{1}, new_array); } extern "C" { @@ -169,43 +170,49 @@ Java_com_jnibind_test_ArrayTestMethodRank2_nativeObjectTests2D( JNIEnv* env, jclass, jobjectArray arr_jobjectArray) { // Simple lvalue pass through works as expected. LocalArray arr{arr_jobjectArray}; - StaticRef{}("assertObject2D", 0, arr); + StaticRef{}("assertObject2D", 0, 1, arr); // Simple rvalue pass through works as expected. - StaticRef{}("assertObject2D", 0, std::move(arr)); + StaticRef{}("assertObject2D", 0, 1, std::move(arr)); // Building a new array, and setting all the values by hand works. LocalArray new_array{3, nullptr}; LocalArray row1{3}; - row1.Set(0, LocalObject{0, 0, 0}); - row1.Set(1, LocalObject{1, 1, 1}); - row1.Set(2, LocalObject{2, 2, 2}); + row1.Set(0, LocalObject{0, 1, 2}); + row1.Set(1, LocalObject{3, 4, 5}); + row1.Set(2, LocalObject{6, 7, 8}); LocalArray row2{3}; - row2.Set(0, LocalObject{3, 3, 3}); - row2.Set(1, LocalObject{4, 4, 4}); - row2.Set(2, LocalObject{5, 5, 5}); + row2.Set(0, LocalObject{9, 10, 11}); + row2.Set(1, LocalObject{12, 13, 14}); + row2.Set(2, LocalObject{15, 16, 17}); LocalArray row3{3}; - row3.Set(0, LocalObject{6, 6, 6}); - row3.Set(1, LocalObject{7, 7, 7}); - row3.Set(2, LocalObject{8, 8, 8}); + row3.Set(0, LocalObject{18, 19, 20}); + row3.Set(1, LocalObject{21, 22, 23}); + row3.Set(2, LocalObject{24, 25, 26}); new_array.Set(0, row1); new_array.Set(1, row2); new_array.Set(2, row3); - StaticRef{}("assertObject2D", 0, new_array); + StaticRef{}("assertObject2D", 0, 1, new_array); // You can pull the view multiple times with iterators (each value ticked 1). { + int i = 0; for (LocalArray inner_array : new_array.Pin()) { for (LocalObject obj : inner_array.Pin()) { - obj("increment", 1); + obj["intVal1"].Set(i); + i++; + obj["intVal2"].Set(i); + i++; + obj["intVal3"].Set(i); + i++; } } - StaticRef{}("assertObject2D", 1, new_array); + StaticRef{}("assertObject2D", 0, 1, new_array); } // You can pull the view multiple times with raw loops. @@ -219,7 +226,7 @@ Java_com_jnibind_test_ArrayTestMethodRank2_nativeObjectTests2D( } // Each variant increments base by 1, so 2 is used here. - StaticRef{}("assertObject2D", 2, new_array); + StaticRef{}("assertObject2D", 1, 1, new_array); } } // extern "C"