-
Notifications
You must be signed in to change notification settings - Fork 0
Test cases for binary tree added #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we please do a different Data Structure
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other BinaryTreeTest file is disabled so I add some new tests to see if OGO works for Binary tree data structure. |
||
|
|
||
| @BeforeAll | ||
| public static void initQueryEngine() | ||
| throws RemoteException, InterruptedException, IOException, NotBoundException { | ||
| Thread.sleep(4000); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need to sleep?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The sleep command gives time for Neo4JGraphQueryEngine to start before test runs. It also exists in all other test files. |
||
| 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`"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why don't we do
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will change hardcode class name to use reflection. |
||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need + in other tests/files or do we use String.format?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will change to String.format |
||
| + ")<-[:left]-(b:" | ||
| + nodeClass | ||
| + ") MATCH (c:" | ||
| + nodeClass | ||
| + ")-[:right]->(d:" | ||
| + nodeClass | ||
| + ") WITH COLLECT(a.value<b.value AND d.value>c.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)")); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we do not need a separate file for this; just keep in the same file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do