Skip to content

Commit 182e5fe

Browse files
committed
test(ops): add JUnit tests for unary math ops Square and Abs
Adds testUnaryMathOps() to GeneratedOperationsTest to verify that the Square and Abs operations correctly execute through the Java API and produce expected scalar TFloat32 results via the native TF engine.
1 parent 9266bdf commit 182e5fe

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/op/core/GeneratedOperationsTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.tensorflow.ndarray.Shape;
2626
import org.tensorflow.op.Op;
2727
import org.tensorflow.op.Ops;
28+
import org.tensorflow.types.TFloat32;
2829
import org.tensorflow.types.TInt32;
2930

3031
public final class GeneratedOperationsTest {
@@ -80,4 +81,30 @@ public void testControlDependencies() {
8081
}
8182
}
8283
}
84+
85+
/**
86+
* Test for basic unary math operations. Ensures that the JNI bridge correctly handles Square and
87+
* Absolute value functions.
88+
*/
89+
@Test
90+
public void testUnaryMathOps() {
91+
try (Graph g = new Graph();
92+
Session sess = new Session(g)) {
93+
Ops ops = Ops.create(g);
94+
95+
Operand<TFloat32> square = ops.math.square(ops.constant(4.0f));
96+
@SuppressWarnings("unchecked")
97+
TFloat32 squareRaw = (TFloat32) sess.runner().fetch(square).run().get(0);
98+
try (TFloat32 result = squareRaw) {
99+
assertEquals(16.0f, result.getFloat(), 0.0f);
100+
}
101+
102+
Operand<TFloat32> abs = ops.math.abs(ops.constant(-5.5f));
103+
@SuppressWarnings("unchecked")
104+
TFloat32 absRaw = (TFloat32) sess.runner().fetch(abs).run().get(0);
105+
try (TFloat32 result = absRaw) {
106+
assertEquals(5.5f, result.getFloat(), 0.0f);
107+
}
108+
}
109+
}
83110
}

0 commit comments

Comments
 (0)