Skip to content

Commit dd4805c

Browse files
Merge pull request #1 from EngineeringSoftware/new_tests
Test cases for binary tree added
2 parents 9ea1280 + 0664ee3 commit dd4805c

2 files changed

Lines changed: 111 additions & 28 deletions

File tree

Lines changed: 99 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.ogo.test.createdatastructure.createbinarytree;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.junit.jupiter.api.Assertions.assertTrue;
45
import static org.ogo.client.OGO.query;
56
import static org.ogo.client.OGO.queryBool;
7+
import static org.ogo.client.OGO.queryLong;
68

79
import java.io.IOException;
810
import java.rmi.NotBoundException;
@@ -13,17 +15,20 @@
1315
import org.ogo.client.OGO;
1416
import org.ogo.util.Profile;
1517

16-
@Disabled
18+
// @Disabled
1719
public class BinaryTreeTest {
1820

1921
/**
2022
* @since 1.0.0
2123
* @version 1.0.0
2224
*/
25+
String nodeClass = "`" + Node.class.getName() + "`";
26+
2327
@BeforeAll
2428
public static void initQueryEngine()
2529
throws RemoteException, InterruptedException, IOException, NotBoundException {
2630
Thread.sleep(4000);
31+
OGO.inMemory = false;
2732
OGO.init();
2833
OGO.setWhiteList("ogo/test");
2934
}
@@ -32,35 +37,22 @@ public static void initQueryEngine()
3237
* @since 1.0.0
3338
* @version 1.0.0
3439
*/
40+
@Disabled
3541
@Profile
3642
@Test
3743
public void addTwoNodes() throws RemoteException {
38-
String nodeClass = "`org.ogo.test.createDataStructure.createBinaryTree.Node`";
3944
OGO.inMemory = true;
4045
Object[] objects =
4146
query(
42-
"CREATE (a:"
43-
+ nodeClass
44-
+ " {value: 10}), (b:"
45-
+ nodeClass
46-
+ " {value: 20}), (c:"
47-
+ nodeClass
48-
+ " {value: 30}), (d:"
49-
+ nodeClass
50-
+ " {value: 40}), (e:"
51-
+ nodeClass
52-
+ " {value: 50}), (f:"
53-
+ nodeClass
54-
+ " {value: 60}) MERGE (f)<-[:right]-(e)<-[:right]-(c)-[:left]->(a)-[:right]->(b)"
55-
+ " MERGE (e)-[:left]->(d) RETURN c");
47+
String.format(
48+
"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",
49+
nodeClass, nodeClass, nodeClass, nodeClass, nodeClass, nodeClass));
5650
OGO.inMemory = false;
5751
Object[] result =
5852
query(
59-
"MATCH (n:"
60-
+ nodeClass
61-
+ " {value: 30})-[*2]->(m:"
62-
+ nodeClass
63-
+ " {value:40}) RETURN m.value");
53+
String.format(
54+
"MATCH (n:%s {value: 30})-[*2]->(m:%s {value:40}) RETURN m.value",
55+
nodeClass, nodeClass));
6456

6557
assertTrue(result.length == 1 && result[0] instanceof Integer && (Integer) result[0] == 40);
6658
}
@@ -69,22 +61,101 @@ public void addTwoNodes() throws RemoteException {
6961
* @since 1.0.0
7062
* @version 1.0.0
7163
*/
64+
@Disabled
7265
@Profile
7366
@Test
7467
public void testInvariant() throws RemoteException {
7568
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
7669
BinaryTree tree = new BinaryTree();
7770
Node root = tree.createTree(arr, 0, arr.length);
78-
String nodeClass = "`org.ogo.test.createDataStructure.createBinaryTree.Node`";
7971
OGO.inMemory = false;
8072
assertTrue(
8173
queryBool(
8274
root,
83-
"MATCH (a)<-[:left]-(b:"
84-
+ nodeClass
85-
+ ") MATCH (c:"
86-
+ nodeClass
87-
+ ")-[:right]->(d) WITH COLLECT(a.value<b.value AND d.value>c.value) AS m RETURN"
88-
+ " ALL(n in m WHERE n=true)"));
75+
String.format(
76+
"MATCH (a)<-[:left]-(b:%s) MATCH (c:%s)-[:right]->(d) WITH COLLECT(a.value<b.value AND d.value>c.value) AS m RETURN ALL(n in m WHERE n=true)",
77+
nodeClass, nodeClass)));
78+
}
79+
80+
@Test
81+
public void checkContains() throws RemoteException {
82+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
83+
BinaryTree tree = new BinaryTree();
84+
Node root = tree.createTree(arr, 0, arr.length);
85+
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
86+
assertTrue(queryBool(root, String.format("MATCH (n:%s {value: 4}) RETURN TRUE", nodeClass)));
87+
}
88+
89+
@Test
90+
public void checkSize() throws RemoteException {
91+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
92+
BinaryTree tree = new BinaryTree();
93+
Node root = tree.createTree(arr, 0, arr.length);
94+
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
95+
assertEquals(7L, queryLong(root, String.format("MATCH (n:%s) RETURN COUNT(n)", nodeClass)));
96+
}
97+
98+
@Test
99+
public void checkLeafCount() throws RemoteException {
100+
// Leaves in {1,2,3,4,5,6,7} tree are: 1, 3, 5, 7 → 4 leaves
101+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
102+
BinaryTree tree = new BinaryTree();
103+
Node root = tree.createTree(arr, 0, arr.length);
104+
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
105+
assertEquals(
106+
4L,
107+
queryLong(
108+
root,
109+
String.format(
110+
"MATCH (n:%s) WHERE NOT (n)-[:left]->() AND NOT (n)-[:right]->() RETURN COUNT(n)",
111+
nodeClass)));
112+
}
113+
114+
@Test
115+
public void checkBSTInvariant() throws RemoteException {
116+
// Every left child must be smaller than its parent, every right child must be larger
117+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
118+
BinaryTree tree = new BinaryTree();
119+
Node root = tree.createTree(arr, 0, arr.length);
120+
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
121+
assertTrue(
122+
queryBool(
123+
root,
124+
String.format(
125+
"MATCH (a:%s)<-[:left]-(b:%s) MATCH (c:%s)-[:right]->(d:%s) WITH COLLECT(a.value<b.value AND d.value>c.value) AS m RETURN ALL(n in m WHERE n=true)",
126+
nodeClass, nodeClass, nodeClass, nodeClass)));
127+
}
128+
129+
@Test
130+
public void checkParent() throws RemoteException {
131+
// Parent of node with value 2 should be the root node with value 4
132+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
133+
BinaryTree tree = new BinaryTree();
134+
Node root = tree.createTree(arr, 0, arr.length);
135+
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
136+
Object[] result =
137+
query(
138+
root,
139+
String.format(
140+
"MATCH (parent:%s)-[:left|right]->(child:%s {value: 2}) RETURN parent.value",
141+
nodeClass, nodeClass));
142+
assertEquals(1, result.length, "Expected exactly one parent");
143+
assertEquals(4, result[0], "Parent of node 2 should be node 4");
144+
}
145+
146+
@Test
147+
public void checkDepth() throws RemoteException {
148+
// Shortest path from root (value=4) to leaf (value=1) should be 2 hops: 4->2->1
149+
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
150+
BinaryTree tree = new BinaryTree();
151+
Node root = tree.createTree(arr, 0, arr.length);
152+
// String nodeClass = "`org.ogo.test.createdatastructure.createbinarytree.Node`";
153+
assertEquals(
154+
2L,
155+
queryLong(
156+
root,
157+
String.format(
158+
"MATCH p=shortestPath((r:%s {value: 4})-[*]->(n:%s {value: 1})) RETURN length(p)",
159+
nodeClass, nodeClass)));
89160
}
90161
}

src/test/java/org/ogo/test/createdatastructure/createbinarytree/Node.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,16 @@ public class Node {
1010
left = null;
1111
right = null;
1212
}
13+
14+
Node(int val) {
15+
value = val;
16+
left = null;
17+
right = null;
18+
}
19+
20+
Node(Node leftNode, Node rightNode, int val) {
21+
value = val;
22+
left = leftNode;
23+
right = rightNode;
24+
}
1325
}

0 commit comments

Comments
 (0)