Skip to content

Commit

Permalink
Add Rank 1 helpers into static methods and convert rank 1 method tests.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 585692876
  • Loading branch information
jwhpryor authored and copybara-github committed Nov 27, 2023
1 parent 1688701 commit fcb83dd
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 103 deletions.
75 changes: 75 additions & 0 deletions javatests/com/jnibind/test/ArrayTestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
40 changes: 20 additions & 20 deletions javatests/com/jnibind/test/ArrayTestMethodRank1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
24 changes: 14 additions & 10 deletions javatests/com/jnibind/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
################################################################################
Expand All @@ -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",
Expand All @@ -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",
Expand Down
27 changes: 25 additions & 2 deletions javatests/com/jnibind/test/array_test_helpers_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<jboolean>{}}},
::jni::Method{"assertByte1D", ::jni::Return{},
::jni::Params{jbyte{}, ::jni::Array<jbyte>{}}},
::jni::Method{"assertChar1D", ::jni::Return{},
::jni::Params{jchar{}, ::jni::Array<jchar>{}}},
::jni::Method{"assertShort1D", ::jni::Return{},
::jni::Params{jshort{}, ::jni::Array<jshort>{}}},
::jni::Method{"assertInt1D", ::jni::Return{},
::jni::Params{jint{}, ::jni::Array<jint>{}}},
::jni::Method{"assertLong1D", ::jni::Return{},
::jni::Params{jlong{}, ::jni::Array<jlong>{}}},
::jni::Method{"assertFloat1D", ::jni::Return{},
::jni::Params{jfloat{}, ::jni::Array<jfloat>{}}},
::jni::Method{"assertDouble1D", ::jni::Return{},
::jni::Params{jdouble{}, ::jni::Array<jdouble>{}}},
::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<jboolean, 2>{}}},
Expand All @@ -44,8 +69,6 @@ constexpr jni::Class kArrayTestHelperClass{
::jni::Params{jfloat{}, ::jni::Array<jfloat, 2>{}}},
::jni::Method{"assertDouble2D", ::jni::Return{},
::jni::Params{jdouble{}, ::jni::Array<jdouble, 2>{}}},
::jni::Method{"object1D", ::jni::Return{},
::jni::Params{::jni::Array{kObjectTestHelperClass}}},
::jni::Method{"assertObject2D", ::jni::Return{},
::jni::Params{int{}, ::jni::Array{kObjectTestHelperClass,
::jni::Rank<2>{}}}},
Expand Down
Loading

0 comments on commit fcb83dd

Please sign in to comment.