From ee50ec01c683ae9d59dc0cdd37d1f1cdf641c23d Mon Sep 17 00:00:00 2001 From: jiajuwang Date: Sat, 21 Feb 2026 18:53:06 -0600 Subject: [PATCH 1/2] Test cases for binary tree added --- .../createbinarytree/NewBinaryTreeTest.java | 118 ++++++++++++++++++ .../createbinarytree/Node.java | 12 ++ 2 files changed, 130 insertions(+) create mode 100644 src/test/java/org/ogo/test/createdatastructure/createbinarytree/NewBinaryTreeTest.java diff --git a/src/test/java/org/ogo/test/createdatastructure/createbinarytree/NewBinaryTreeTest.java b/src/test/java/org/ogo/test/createdatastructure/createbinarytree/NewBinaryTreeTest.java new file mode 100644 index 0000000..85513f3 --- /dev/null +++ b/src/test/java/org/ogo/test/createdatastructure/createbinarytree/NewBinaryTreeTest.java @@ -0,0 +1,118 @@ +package org.ogo.test.createdatastructure.createbinarytree; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.ogo.client.OGO.query; +import static org.ogo.client.OGO.queryBool; +import static org.ogo.client.OGO.queryLong; + +import java.io.IOException; +import java.rmi.NotBoundException; +import java.rmi.RemoteException; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.ogo.client.OGO; + +public class NewBinaryTreeTest { + + @BeforeAll + public static void initQueryEngine() + throws RemoteException, InterruptedException, IOException, NotBoundException { + Thread.sleep(4000); + OGO.inMemory = false; + OGO.init(); + OGO.setWhiteList("ogo/test"); + } + + @Test + public void checkContains() throws RemoteException { + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + assertTrue(queryBool(root, "MATCH (n:" + nodeClass + " {value: 4}) RETURN TRUE")); + } + + @Test + public void checkSize() throws RemoteException { + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + assertEquals(7L, queryLong(root, "MATCH (n:" + nodeClass + ") RETURN COUNT(n)")); + } + + @Test + public void checkLeafCount() throws RemoteException { + // Leaves in {1,2,3,4,5,6,7} tree are: 1, 3, 5, 7 → 4 leaves + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + assertEquals( + 4L, + queryLong( + root, + "MATCH (n:" + + nodeClass + + ") WHERE NOT (n)-[:left]->() AND NOT (n)-[:right]->() RETURN COUNT(n)")); + } + + @Test + public void checkBSTInvariant() throws RemoteException { + // Every left child must be smaller than its parent, every right child must be larger + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + assertTrue( + queryBool( + root, + "MATCH (a:" + + nodeClass + + ")<-[:left]-(b:" + + nodeClass + + ") MATCH (c:" + + nodeClass + + ")-[:right]->(d:" + + nodeClass + + ") WITH COLLECT(a.valuec.value) AS m RETURN ALL(n in m WHERE n=true)")); + } + + @Test + public void checkParent() throws RemoteException { + // Parent of node with value 2 should be the root node with value 4 + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + Object[] result = + query( + root, + "MATCH (parent:" + + nodeClass + + ")-[:left|right]->(child:" + + nodeClass + + " {value: 2}) RETURN parent.value"); + assertEquals(1, result.length, "Expected exactly one parent"); + assertEquals(4, result[0], "Parent of node 2 should be node 4"); + } + + @Test + public void checkDepth() throws RemoteException { + // Shortest path from root (value=4) to leaf (value=1) should be 2 hops: 4->2->1 + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + assertEquals( + 2L, + queryLong( + root, + "MATCH p=shortestPath((r:" + + nodeClass + + " {value: 4})-[*]->(n:" + + nodeClass + + " {value: 1})) RETURN length(p)")); + } +} diff --git a/src/test/java/org/ogo/test/createdatastructure/createbinarytree/Node.java b/src/test/java/org/ogo/test/createdatastructure/createbinarytree/Node.java index 1d36633..d5bac6e 100644 --- a/src/test/java/org/ogo/test/createdatastructure/createbinarytree/Node.java +++ b/src/test/java/org/ogo/test/createdatastructure/createbinarytree/Node.java @@ -10,4 +10,16 @@ public class Node { left = null; right = null; } + + Node(int val) { + value = val; + left = null; + right = null; + } + + Node(Node leftNode, Node rightNode, int val) { + value = val; + left = leftNode; + right = rightNode; + } } From 0664ee3c93dfdaf240b8388f7c35c77f75f2f96b Mon Sep 17 00:00:00 2001 From: jiajuwang Date: Mon, 23 Feb 2026 18:13:44 -0600 Subject: [PATCH 2/2] Merge two test files --- .../createbinarytree/BinaryTreeTest.java | 127 ++++++++++++++---- .../createbinarytree/NewBinaryTreeTest.java | 118 ---------------- 2 files changed, 99 insertions(+), 146 deletions(-) delete mode 100644 src/test/java/org/ogo/test/createdatastructure/createbinarytree/NewBinaryTreeTest.java diff --git a/src/test/java/org/ogo/test/createdatastructure/createbinarytree/BinaryTreeTest.java b/src/test/java/org/ogo/test/createdatastructure/createbinarytree/BinaryTreeTest.java index 1a7b44e..a121f41 100644 --- a/src/test/java/org/ogo/test/createdatastructure/createbinarytree/BinaryTreeTest.java +++ b/src/test/java/org/ogo/test/createdatastructure/createbinarytree/BinaryTreeTest.java @@ -1,8 +1,10 @@ package org.ogo.test.createdatastructure.createbinarytree; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.ogo.client.OGO.query; import static org.ogo.client.OGO.queryBool; +import static org.ogo.client.OGO.queryLong; import java.io.IOException; import java.rmi.NotBoundException; @@ -13,17 +15,20 @@ import org.ogo.client.OGO; import org.ogo.util.Profile; -@Disabled +// @Disabled public class BinaryTreeTest { /** * @since 1.0.0 * @version 1.0.0 */ + String nodeClass = "`" + Node.class.getName() + "`"; + @BeforeAll public static void initQueryEngine() throws RemoteException, InterruptedException, IOException, NotBoundException { Thread.sleep(4000); + OGO.inMemory = false; OGO.init(); OGO.setWhiteList("ogo/test"); } @@ -32,35 +37,22 @@ public static void initQueryEngine() * @since 1.0.0 * @version 1.0.0 */ + @Disabled @Profile @Test public void addTwoNodes() throws RemoteException { - String nodeClass = "`org.ogo.test.createDataStructure.createBinaryTree.Node`"; OGO.inMemory = true; Object[] objects = query( - "CREATE (a:" - + nodeClass - + " {value: 10}), (b:" - + nodeClass - + " {value: 20}), (c:" - + nodeClass - + " {value: 30}), (d:" - + nodeClass - + " {value: 40}), (e:" - + nodeClass - + " {value: 50}), (f:" - + nodeClass - + " {value: 60}) MERGE (f)<-[:right]-(e)<-[:right]-(c)-[:left]->(a)-[:right]->(b)" - + " MERGE (e)-[:left]->(d) RETURN c"); + String.format( + "CREATE (a:%s {value: 10}), (b:%s {value: 20}), (c:%s {value: 30}), (d:%s {value: 40}), (e:%s {value: 50}), (f:%s {value: 60}) MERGE (f)<-[:right]-(e)<-[:right]-(c)-[:left]->(a)-[:right]->(b) MERGE (e)-[:left]->(d) RETURN c", + nodeClass, nodeClass, nodeClass, nodeClass, nodeClass, nodeClass)); OGO.inMemory = false; Object[] result = query( - "MATCH (n:" - + nodeClass - + " {value: 30})-[*2]->(m:" - + nodeClass - + " {value:40}) RETURN m.value"); + String.format( + "MATCH (n:%s {value: 30})-[*2]->(m:%s {value:40}) RETURN m.value", + nodeClass, nodeClass)); assertTrue(result.length == 1 && result[0] instanceof Integer && (Integer) result[0] == 40); } @@ -69,22 +61,101 @@ public void addTwoNodes() throws RemoteException { * @since 1.0.0 * @version 1.0.0 */ + @Disabled @Profile @Test public void testInvariant() throws RemoteException { int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; BinaryTree tree = new BinaryTree(); Node root = tree.createTree(arr, 0, arr.length); - String nodeClass = "`org.ogo.test.createDataStructure.createBinaryTree.Node`"; OGO.inMemory = false; assertTrue( queryBool( root, - "MATCH (a)<-[:left]-(b:" - + nodeClass - + ") MATCH (c:" - + nodeClass - + ")-[:right]->(d) WITH COLLECT(a.valuec.value) AS m RETURN" - + " ALL(n in m WHERE n=true)")); + String.format( + "MATCH (a)<-[:left]-(b:%s) MATCH (c:%s)-[:right]->(d) WITH COLLECT(a.valuec.value) AS m RETURN ALL(n in m WHERE n=true)", + nodeClass, nodeClass))); + } + + @Test + public void checkContains() throws RemoteException { + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + // String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + assertTrue(queryBool(root, String.format("MATCH (n:%s {value: 4}) RETURN TRUE", nodeClass))); + } + + @Test + public void checkSize() throws RemoteException { + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + // String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + assertEquals(7L, queryLong(root, String.format("MATCH (n:%s) RETURN COUNT(n)", nodeClass))); + } + + @Test + public void checkLeafCount() throws RemoteException { + // Leaves in {1,2,3,4,5,6,7} tree are: 1, 3, 5, 7 → 4 leaves + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + // String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + assertEquals( + 4L, + queryLong( + root, + String.format( + "MATCH (n:%s) WHERE NOT (n)-[:left]->() AND NOT (n)-[:right]->() RETURN COUNT(n)", + nodeClass))); + } + + @Test + public void checkBSTInvariant() throws RemoteException { + // Every left child must be smaller than its parent, every right child must be larger + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + // String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + assertTrue( + queryBool( + root, + String.format( + "MATCH (a:%s)<-[:left]-(b:%s) MATCH (c:%s)-[:right]->(d:%s) WITH COLLECT(a.valuec.value) AS m RETURN ALL(n in m WHERE n=true)", + nodeClass, nodeClass, nodeClass, nodeClass))); + } + + @Test + public void checkParent() throws RemoteException { + // Parent of node with value 2 should be the root node with value 4 + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + // String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + Object[] result = + query( + root, + String.format( + "MATCH (parent:%s)-[:left|right]->(child:%s {value: 2}) RETURN parent.value", + nodeClass, nodeClass)); + assertEquals(1, result.length, "Expected exactly one parent"); + assertEquals(4, result[0], "Parent of node 2 should be node 4"); + } + + @Test + public void checkDepth() throws RemoteException { + // Shortest path from root (value=4) to leaf (value=1) should be 2 hops: 4->2->1 + int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; + BinaryTree tree = new BinaryTree(); + Node root = tree.createTree(arr, 0, arr.length); + // String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; + assertEquals( + 2L, + queryLong( + root, + String.format( + "MATCH p=shortestPath((r:%s {value: 4})-[*]->(n:%s {value: 1})) RETURN length(p)", + nodeClass, nodeClass))); } } diff --git a/src/test/java/org/ogo/test/createdatastructure/createbinarytree/NewBinaryTreeTest.java b/src/test/java/org/ogo/test/createdatastructure/createbinarytree/NewBinaryTreeTest.java deleted file mode 100644 index 85513f3..0000000 --- a/src/test/java/org/ogo/test/createdatastructure/createbinarytree/NewBinaryTreeTest.java +++ /dev/null @@ -1,118 +0,0 @@ -package org.ogo.test.createdatastructure.createbinarytree; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.ogo.client.OGO.query; -import static org.ogo.client.OGO.queryBool; -import static org.ogo.client.OGO.queryLong; - -import java.io.IOException; -import java.rmi.NotBoundException; -import java.rmi.RemoteException; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.ogo.client.OGO; - -public class NewBinaryTreeTest { - - @BeforeAll - public static void initQueryEngine() - throws RemoteException, InterruptedException, IOException, NotBoundException { - Thread.sleep(4000); - OGO.inMemory = false; - OGO.init(); - OGO.setWhiteList("ogo/test"); - } - - @Test - public void checkContains() throws RemoteException { - int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; - BinaryTree tree = new BinaryTree(); - Node root = tree.createTree(arr, 0, arr.length); - String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; - assertTrue(queryBool(root, "MATCH (n:" + nodeClass + " {value: 4}) RETURN TRUE")); - } - - @Test - public void checkSize() throws RemoteException { - int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; - BinaryTree tree = new BinaryTree(); - Node root = tree.createTree(arr, 0, arr.length); - String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; - assertEquals(7L, queryLong(root, "MATCH (n:" + nodeClass + ") RETURN COUNT(n)")); - } - - @Test - public void checkLeafCount() throws RemoteException { - // Leaves in {1,2,3,4,5,6,7} tree are: 1, 3, 5, 7 → 4 leaves - int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; - BinaryTree tree = new BinaryTree(); - Node root = tree.createTree(arr, 0, arr.length); - String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; - assertEquals( - 4L, - queryLong( - root, - "MATCH (n:" - + nodeClass - + ") WHERE NOT (n)-[:left]->() AND NOT (n)-[:right]->() RETURN COUNT(n)")); - } - - @Test - public void checkBSTInvariant() throws RemoteException { - // Every left child must be smaller than its parent, every right child must be larger - int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; - BinaryTree tree = new BinaryTree(); - Node root = tree.createTree(arr, 0, arr.length); - String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; - assertTrue( - queryBool( - root, - "MATCH (a:" - + nodeClass - + ")<-[:left]-(b:" - + nodeClass - + ") MATCH (c:" - + nodeClass - + ")-[:right]->(d:" - + nodeClass - + ") WITH COLLECT(a.valuec.value) AS m RETURN ALL(n in m WHERE n=true)")); - } - - @Test - public void checkParent() throws RemoteException { - // Parent of node with value 2 should be the root node with value 4 - int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; - BinaryTree tree = new BinaryTree(); - Node root = tree.createTree(arr, 0, arr.length); - String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; - Object[] result = - query( - root, - "MATCH (parent:" - + nodeClass - + ")-[:left|right]->(child:" - + nodeClass - + " {value: 2}) RETURN parent.value"); - assertEquals(1, result.length, "Expected exactly one parent"); - assertEquals(4, result[0], "Parent of node 2 should be node 4"); - } - - @Test - public void checkDepth() throws RemoteException { - // Shortest path from root (value=4) to leaf (value=1) should be 2 hops: 4->2->1 - int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7}; - BinaryTree tree = new BinaryTree(); - Node root = tree.createTree(arr, 0, arr.length); - String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`"; - assertEquals( - 2L, - queryLong( - root, - "MATCH p=shortestPath((r:" - + nodeClass - + " {value: 4})-[*]->(n:" - + nodeClass - + " {value: 1})) RETURN length(p)")); - } -}