Skip to content

Commit

Permalink
Add Rank 1 helpers into static methods and convert rank 1 field tests.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 586219433
  • Loading branch information
jwhpryor authored and copybara-github committed Nov 29, 2023
1 parent 95419a7 commit e7ff3dc
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 176 deletions.
92 changes: 0 additions & 92 deletions javatests/com/jnibind/test/ArrayTestFieldRank1.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

package com.jnibind.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import org.junit.AfterClass;
import org.junit.Test;
Expand Down Expand Up @@ -74,94 +70,6 @@ public static void doShutDown() {

native void nativeObjectTests(ArrayTestFieldRank1 arrayTest, ObjectTestHelper[] objectArray);

void booleanArray(boolean baseOffset, boolean stride, boolean[] arr) {
// Booleans stride up until true (i.e. once) and then red line.
// This isn't intuitive, but allows for generalised testing.
for (int i = 0; i < arr.length; i++) {
if (baseOffset || (i > 0 && stride)) {
assertTrue(arr[i]);
} else {
assertFalse(arr[i]);
}
}
}

void byteArray(byte baseOffset, byte stride, byte[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i * stride + baseOffset, arr[i]);
}
}

void charArray(char baseOffset, char stride, char[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i * stride + baseOffset, arr[i]);
}
}

void shortArray(short baseOffset, short stride, short[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i * stride + baseOffset, arr[i]);
}
}

void intArray(int baseOffset, int stride, int[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i * stride + baseOffset, arr[i]);
}
}

void longArray(long baseOffset, long stride, long[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i * stride + baseOffset, arr[i]);
}
}

void floatArray(float baseOffset, float stride, float[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i * stride + baseOffset, arr[i], 1.f);
}
}

void doubleArray(double baseOffset, double stride, double[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals((double) i * stride + baseOffset, arr[i], 0);
}
}

void stringArray(String[] arr, boolean isFooBazBar) {
if (isFooBazBar) {
assertEquals(3, arr.length);
assertEquals("Foo", arr[0]);
assertEquals("Baz", arr[1]);
assertEquals("Bar", arr[2]);
} else {
assertNotEquals("Foo", arr[0]);
assertNotEquals("Baz", arr[1]);
assertNotEquals("Bar", arr[2]);
}
}

void stringEqual(boolean expectEqual, String lhs, String rhs) {
if (expectEqual) {
assertEquals(lhs, rhs);
} else {
assertNotEquals(lhs, rhs);
}
}

void object(int objectMemberOffset, ObjectTestHelper obj) {
assertEquals(objectMemberOffset, obj.intVal1);
}

void objectArray(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]));
}
}

@Test
public void booleanTests() {
boolean[] arr = {false, false, false};
Expand Down
66 changes: 44 additions & 22 deletions javatests/com/jnibind/test/ArrayTestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,94 @@
*/
package com.jnibind.test;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
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) {
/** Rank 0 Verification Methods. */
static void assertString(String lhs, String rhs) {
assertEquals(lhs, rhs);
}

static void assertObject(int objectMemberOffset, ObjectTestHelper arr) {
assertTrue(
arr.isEqualTo(
new ObjectTestHelper(objectMemberOffset, objectMemberOffset, objectMemberOffset)));
}

/** Rank 1 Verification Methods. */
static void assertBoolean1D(boolean baseOffset, boolean stride, boolean[] arr) {
// Booleans stride up until true (i.e. once) and then red line.
// This isn't intuitive, but allows for generalised testing.
for (int i = 0; i < arr.length; i++) {
if (testForTrue) {
if (baseOffset || (i > 0 && stride)) {
assertTrue(arr[i]);
} else {
assertFalse(arr[i]);
}
}
}

static void assertByte1D(byte baseOffset, byte[] arr) {
static void assertByte1D(byte baseOffset, byte stride, byte[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i + baseOffset, arr[i]);
assertEquals(i * stride + baseOffset, arr[i]);
}
}

static void assertChar1D(char baseOffset, char[] arr) {
static void assertChar1D(char baseOffset, char stride, char[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i + baseOffset, arr[i]);
assertEquals(i * stride + baseOffset, arr[i]);
}
}

static void assertShort1D(short baseOffset, short[] arr) {
static void assertShort1D(short baseOffset, short stride, short[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i + baseOffset, arr[i]);
assertEquals(i * stride + baseOffset, arr[i]);
}
}

static void assertInt1D(int baseOffset, int[] arr) {
static void assertInt1D(int baseOffset, int stride, int[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i + baseOffset, arr[i]);
assertEquals(i * stride + baseOffset, arr[i]);
}
}

static void assertLong1D(long baseOffset, long[] arr) {
static void assertLong1D(long baseOffset, long stride, long[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i + baseOffset, arr[i]);
assertEquals(i * stride + baseOffset, arr[i]);
}
}

static void assertFloat1D(float baseOffset, float[] arr) {
static void assertFloat1D(float baseOffset, float stride, float[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals(i + baseOffset, arr[i], 1.f);
assertEquals(i * stride + baseOffset, arr[i], 1.f);
}
}

static void assertDouble1D(double baseOffset, double[] arr) {
static void assertDouble1D(double baseOffset, double stride, double[] arr) {
for (int i = 0; i < arr.length; i++) {
assertEquals((double) i + baseOffset, arr[i], 0);
assertEquals((double) i * stride + 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 assertString1D(String[] arr, boolean isFooBazBar) {
if (isFooBazBar) {
assertThat(arr).hasLength(3);
assertEquals("Foo", arr[0]);
assertEquals("Baz", arr[1]);
assertEquals("Bar", arr[2]);
} else {
assertNotEquals("Foo", arr[0]);
assertNotEquals("Baz", arr[1]);
assertNotEquals("Bar", arr[2]);
}
}

static void assertObject1D(int objectMemberOffset, ObjectTestHelper[] arr) {
Expand All @@ -98,7 +120,7 @@ static void assertObjectArrayOfNulls1D(ObjectTestHelper[] arr) {
}
}

/** 2D Tests RJNI declarations. * */
/** Rank 2 Verification Methods. */
static void assertBoolean2D(boolean testForTrue, boolean[][] arr) {
// All even are true (unless testForTrue when they're flipped).
for (int i = 0; i < arr.length; i++) {
Expand Down
4 changes: 4 additions & 0 deletions javatests/com/jnibind/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cc_library(
testonly = True,
srcs = ["array_test_field_rank_1_jni.cc"],
deps = [
":array_test_helpers_native",
":object_test_helper_jni",
"//:jni_bind",
"//metaprogramming:lambda_string",
Expand All @@ -28,6 +29,9 @@ java_test(
srcs = ["ArrayTestFieldRank1.java"],
data = [":libarray_test_field_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
Loading

0 comments on commit e7ff3dc

Please sign in to comment.