Skip to content

Commit

Permalink
Add rank 2 E2E tests for fields.
Browse files Browse the repository at this point in the history
This should resolve #40.

PiperOrigin-RevId: 589589116
  • Loading branch information
jwhpryor authored and copybara-github committed Dec 10, 2023
1 parent cef8656 commit 2e7ca55
Show file tree
Hide file tree
Showing 7 changed files with 577 additions and 59 deletions.
175 changes: 175 additions & 0 deletions javatests/com/jnibind/test/ArrayTestFieldRank2.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
42 changes: 24 additions & 18 deletions javatests/com/jnibind/test/ArrayTestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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)));
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions javatests/com/jnibind/test/ArrayTestMethodRank2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 39 additions & 0 deletions javatests/com/jnibind/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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.
################################################################################
Expand Down
Loading

0 comments on commit 2e7ca55

Please sign in to comment.