From 56c41de353a570dca2899ad018a10644271d2074 Mon Sep 17 00:00:00 2001 From: Winfried Gerlach Date: Fri, 2 Jan 2026 12:36:14 +0100 Subject: [PATCH 1/4] add TBoolTest (mix of TStringTest and NumericTypesTestBase) --- .../java/org/tensorflow/types/TBoolTest.java | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java diff --git a/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java b/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java new file mode 100644 index 00000000000..8931490eba3 --- /dev/null +++ b/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java @@ -0,0 +1,142 @@ +package org.tensorflow.types; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.Test; +import org.tensorflow.EagerSession; +import org.tensorflow.ndarray.NdArray; +import org.tensorflow.ndarray.NdArrays; +import org.tensorflow.ndarray.Shape; +import org.tensorflow.ndarray.index.Indices; +import org.tensorflow.op.Ops; +import org.tensorflow.op.core.Constant; +import org.tensorflow.op.math.LogicalAnd; +import org.tensorflow.op.math.LogicalNot; +import org.tensorflow.op.math.LogicalOr; + +class TBoolTest { + + @Test + void createScalar() { + TBool tensorT = TBool.scalarOf(true); + assertNotNull(tensorT); + assertEquals(Shape.scalar(), tensorT.shape()); + assertEquals(true, tensorT.getObject()); + + TBool tensorF = TBool.scalarOf(false); + assertNotNull(tensorF); + assertEquals(Shape.scalar(), tensorF.shape()); + assertEquals(false, tensorF.getObject()); + } + + @Test + void createVector() { + TBool tensor = TBool.vectorOf(true, false); + assertNotNull(tensor); + assertEquals(Shape.of(2), tensor.shape()); + assertEquals(true, tensor.getObject(0)); + assertEquals(false, tensor.getObject(1)); + } + + @Test + void createCopy() { + NdArray bools = + NdArrays.ofObjects(Boolean.class, Shape.of(2, 2)) + .setObject(true, 0, 0) + .setObject(false, 0, 1) + .setObject(false, 1, 0) + .setObject(true, 1, 1); + + TBool tensor = TBool.tensorOf(bools); + assertNotNull(tensor); + bools + .scalars() + .forEachIndexed((idx, s) -> assertEquals(s.getObject(), tensor.getObject(idx))); + } + + @Test + void initializeTensorsWithBools() { + // Allocate a tensor of booleans of the shape (2, 3, 2) + TBool tensor = TBool.tensorOf(Shape.of(2, 3, 2)); + + assertEquals(3, tensor.rank()); + assertEquals(12, tensor.size()); + NdArray data = (NdArray) tensor; + + try (EagerSession session = EagerSession.create()) { + Ops tf = Ops.create(session); + + // Initialize tensor memory with falses and take a snapshot + data.scalars().forEach(scalar -> ((NdArray) scalar).setObject(false)); + Constant x = tf.constantOf(tensor); + + // Initialize the same tensor memory with trues and take a snapshot + data.scalars().forEach(scalar -> ((NdArray) scalar).setObject(true)); + Constant y = tf.constantOf(tensor); + + // Calculate x AND y and validate the result + LogicalAnd xAndY = tf.math.logicalAnd(x, y); + ((NdArray) xAndY.asTensor()) + .scalars() + .forEach(scalar -> assertEquals(false, scalar.getObject())); + + // Calculate x AND y and validate the result + LogicalOr xOrY = tf.math.logicalOr(x, y); + ((NdArray) xOrY.asTensor()) + .scalars() + .forEach(scalar -> assertEquals(true, scalar.getObject())); + + // Calculate !x and validate the result against y + LogicalNot notX = tf.math.logicalNot(x); + assertEquals(y.asTensor(), notX.asTensor()); + } + } + + @Test + void setAndCompute() { + NdArray heapData = + NdArrays.ofBooleans(Shape.of(4)) + .setObject(true, 0) + .setObject(false, 1) + .setObject(true, 2) + .setObject(false, 3); + + // Creates a 2x2 matrix + try (TBool tensor = TBool.tensorOf(Shape.of(2, 2))) { + NdArray data = (NdArray) tensor; + + // Copy first 2 values of the vector to the first row of the matrix + data.set(heapData.slice(Indices.range(0, 2)), 0); + + // Copy values at an odd position in the vector as the second row of the matrix + data.set(heapData.slice(Indices.odd()), 1); + + assertEquals(true, data.getObject(0, 0)); + assertEquals(false, data.getObject(0, 1)); + assertEquals(false, data.getObject(1, 0)); + assertEquals(false, data.getObject(1, 1)); + + // Read rows of the tensor in reverse order + NdArray flippedData = data.slice(Indices.flip(), Indices.flip()); + + assertEquals(false, flippedData.getObject(0, 0)); + assertEquals(false, flippedData.getObject(0, 1)); + assertEquals(false, flippedData.getObject(1, 0)); + assertEquals(true, flippedData.getObject(1, 1)); + + try (EagerSession session = EagerSession.create()) { + Ops tf = Ops.create(session); + + LogicalNot sub = tf.math.logicalNot(tf.constantOf(tensor)); + NdArray result = (NdArray) sub.asTensor(); + + assertEquals(false, result.getObject(0, 0)); + assertEquals(true, result.getObject(0, 1)); + assertEquals(true, result.getObject(1, 0)); + assertEquals(true, result.getObject(1, 1)); + } + } + } + +} \ No newline at end of file From 0e67a8b2c6ca19b57e7f210124f4f3a93f304079 Mon Sep 17 00:00:00 2001 From: Winfried Gerlach Date: Fri, 2 Jan 2026 12:38:55 +0100 Subject: [PATCH 2/4] minor cleanup --- .../src/main/java/org/tensorflow/NativeFunction.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/NativeFunction.java b/tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/NativeFunction.java index 245fff70d1a..4dbde8f2473 100644 --- a/tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/NativeFunction.java +++ b/tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/NativeFunction.java @@ -20,9 +20,7 @@ import com.google.protobuf.InvalidProtocolBufferException; import java.util.ArrayDeque; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -92,7 +90,7 @@ public synchronized List getDependencies() { } }); } - dependencies = Collections.unmodifiableList(new ArrayList<>(deps)); + dependencies = List.copyOf(deps); } return dependencies; From 7141c8ca495afb57b03be9670acd2df689b4054f Mon Sep 17 00:00:00 2001 From: Winfried Gerlach Date: Sun, 4 Jan 2026 14:09:33 +0100 Subject: [PATCH 3/4] add copyright comment, fix comment in test --- .../java/org/tensorflow/types/TBoolTest.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java b/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java index 8931490eba3..83968167a99 100644 --- a/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java +++ b/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java @@ -1,3 +1,20 @@ +/* + * Copyright 2020 The TensorFlow Authors. All Rights Reserved. + * + * 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 org.tensorflow.types; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -81,7 +98,7 @@ void initializeTensorsWithBools() { .scalars() .forEach(scalar -> assertEquals(false, scalar.getObject())); - // Calculate x AND y and validate the result + // Calculate x OR y and validate the result LogicalOr xOrY = tf.math.logicalOr(x, y); ((NdArray) xOrY.asTensor()) .scalars() From b87bfb213e00e5e52ec8fdbe0b65d67b2ae2d00b Mon Sep 17 00:00:00 2001 From: Winfried Gerlach Date: Sun, 4 Jan 2026 14:11:31 +0100 Subject: [PATCH 4/4] make class and test methods public to be aligned with other tests --- .../test/java/org/tensorflow/types/TBoolTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java b/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java index 83968167a99..91e91d343f0 100644 --- a/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java +++ b/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/types/TBoolTest.java @@ -32,10 +32,10 @@ import org.tensorflow.op.math.LogicalNot; import org.tensorflow.op.math.LogicalOr; -class TBoolTest { +public class TBoolTest { @Test - void createScalar() { + public void createScalar() { TBool tensorT = TBool.scalarOf(true); assertNotNull(tensorT); assertEquals(Shape.scalar(), tensorT.shape()); @@ -48,7 +48,7 @@ void createScalar() { } @Test - void createVector() { + public void createVector() { TBool tensor = TBool.vectorOf(true, false); assertNotNull(tensor); assertEquals(Shape.of(2), tensor.shape()); @@ -57,7 +57,7 @@ void createVector() { } @Test - void createCopy() { + public void createCopy() { NdArray bools = NdArrays.ofObjects(Boolean.class, Shape.of(2, 2)) .setObject(true, 0, 0) @@ -73,7 +73,7 @@ void createCopy() { } @Test - void initializeTensorsWithBools() { + public void initializeTensorsWithBools() { // Allocate a tensor of booleans of the shape (2, 3, 2) TBool tensor = TBool.tensorOf(Shape.of(2, 3, 2)); @@ -111,7 +111,7 @@ void initializeTensorsWithBools() { } @Test - void setAndCompute() { + public void setAndCompute() { NdArray heapData = NdArrays.ofBooleans(Shape.of(4)) .setObject(true, 0)