GP-1889 - cleanup

This commit is contained in:
dragonmacher
2022-04-04 13:49:18 -04:00
parent fc7e7d188b
commit 995772ff75
@@ -4,9 +4,9 @@
* 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.
@@ -15,279 +15,233 @@
*/
package ghidra.graph;
import org.junit.Assert;
import static org.junit.Assert.*;
import java.util.*;
import org.junit.Before;
import org.junit.Test;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class GraphPathTest {
// GraphPath object used for (almost) all tests.
GraphPath<Integer> GP;
private GraphPath<Integer> graphPath;
/**
* Setup function to have a new GraphPath instance for every test that is filled with 21
* vertices.
*/
@Before
public void setUp() {
GP = new GraphPath<>();
for (int i = 0 ; i <= 20; i++) {
GP.add(i);
}
}
/**
* Setup to have a new GraphPath instance for every test that is filled with 21 vertices.
*/
@Before
public void setUp() {
graphPath = new GraphPath<>();
for (int i = 0; i <= 20; i++) {
graphPath.add(i);
}
}
/**
* Test to verify if copy() function works correctly. Reinitializes the GraphPath object and
* adds three vertices to it. GraphPath is then copied and with assertions the test checks if
* all three vertices exist in the copy, and that they are the only vertices in the GraphPath.
*/
@Test
public void testCopy() {
GP = new GraphPath<>();
/**
* Test to verify if copy() function works correctly. Reinitializes the GraphPath object and
* adds three vertices to it. GraphPath is then copied and with assertions the test checks if
* all three vertices exist in the copy, and that they are the only vertices in the GraphPath.
*/
@Test
public void testCopy() {
graphPath = new GraphPath<>();
GP.add(1);
GP.add(2);
GP.add(3);
graphPath.add(1);
graphPath.add(2);
graphPath.add(3);
GraphPath<Integer> graphPathCopy = GP.copy();
GraphPath<Integer> graphPathCopy = graphPath.copy();
Assert.assertTrue(graphPathCopy.contains(1));
Assert.assertTrue(graphPathCopy.contains(2));
Assert.assertTrue(graphPathCopy.contains(3));
Assert.assertEquals(3, graphPathCopy.size());
}
assertTrue(graphPathCopy.contains(1));
assertTrue(graphPathCopy.contains(2));
assertTrue(graphPathCopy.contains(3));
assertEquals(3, graphPathCopy.size());
}
/**
* Test to verify if startsWith() function works correctly. A smaller GraphPath is given as an
* argument in this test. Asserts check if the startsWith() function returns true, when the
* GraphPath actually starts with the GraphPath passed in the parameter. Also check if false
* is returned with a GraphPath that the GraphPath object does not start with.
*/
@Test
public void testStartsWith_SmallerGraphPath() {
GraphPath<Integer> graphPathStart = new GraphPath<>();
for (int i = 0; i < 5; i++) {
graphPathStart.add(i);
}
/**
* Test to verify if startsWith() function works correctly. A smaller GraphPath is given as an
* argument in this test. Asserts check if the startsWith() function returns true, when the
* GraphPath actually starts with the GraphPath passed in the parameter. Also check if false
* is returned with a GraphPath that the GraphPath object does not start with.
*/
@Test
public void testStartsWith_SmallerGraphPath() {
GraphPath<Integer> graphPathStart = new GraphPath<>();
for (int i = 0; i < 5; i++) {
graphPathStart.add(i);
}
Assert.assertTrue(GP.startsWith(graphPathStart));
Assert.assertFalse(GP.startsWith(new GraphPath<>(6)));
}
assertTrue(graphPath.startsWith(graphPathStart));
assertFalse(graphPath.startsWith(new GraphPath<>(6)));
}
/**
* Test to verify if startsWith() function works correctly. A larger GraphPath is given as an
* argument in this test. Asserts check if the startsWith() function returns false, when the
* GraphPath in the argument is larger than the GraphPath object.
*/
@Test
public void testStartsWith_LargerGraphPath() {
GraphPath<Integer> largerGraphPath = new GraphPath<>();
for (int i = 0; i < 25; i++) {
largerGraphPath.add(i);
}
/**
* Test to verify if startsWith() function works correctly. A larger GraphPath is given as an
* argument in this test. Asserts check if the startsWith() function returns false, when the
* GraphPath in the argument is larger than the GraphPath object.
*/
@Test
public void testStartsWith_LargerGraphPath() {
GraphPath<Integer> largerGraphPath = new GraphPath<>();
for (int i = 0; i < 25; i++) {
largerGraphPath.add(i);
}
Assert.assertFalse(GP.startsWith(largerGraphPath));
}
assertFalse(graphPath.startsWith(largerGraphPath));
}
/**
* Test to verify if getCommonStartPath() function works correctly. A different GraphPath to GP
* is created and later on with asserts, the test checks if the commonStartPath contains the
* correct elements and has the right length.
*/
@Test
public void testGetCommonStartPath() {
GraphPath<Integer> commonStartGraphPath = new GraphPath<>();
for (int i = 0; i < 10; i++) {
commonStartGraphPath.add(i);
}
/**
* Test to verify if getCommonStartPath() function works correctly.
*/
@Test
public void testGetCommonStartPath() {
GraphPath<Integer> sharedStartPath = new GraphPath<>();
for (int i = 0; i < 10; i++) {
sharedStartPath.add(i);
}
GraphPath<Integer> differentGraphPath = commonStartGraphPath.copy();
GraphPath<Integer> differentPath = sharedStartPath.copy();
for (int i = 90; i < 100; i++) {
differentPath.add(i);
}
for (int i = 90; i < 100; i++) {
differentGraphPath.add(i);
}
GraphPath<Integer> commonStartPathResult =
sharedStartPath.getCommonStartPath(differentPath);
for (int i = 0; i < commonStartPathResult.size(); i++) {
assertEquals(sharedStartPath.get(i), commonStartPathResult.get(i));
}
GraphPath<Integer> commonStartPathResult =
commonStartGraphPath.getCommonStartPath(differentGraphPath);
assertEquals(sharedStartPath.size(), commonStartPathResult.size());
}
for (int i = 0; i < commonStartPathResult.size(); i++) {
Assert.assertEquals(commonStartGraphPath.get(i), commonStartPathResult.get(i));
}
@Test
public void testSize() {
graphPath = new GraphPath<>();
graphPath.add(1);
graphPath.add(2);
graphPath.add(3);
Assert.assertEquals(commonStartGraphPath.size(), commonStartPathResult.size());
}
assertEquals(3, graphPath.size());
}
/**
* Test to verify if getCommonStartPath() function works correctly. A different GraphPath to GP
* is created and later on with asserts, the test checks if the commonStartPath contains the
* correct elements and has the right length.
*/
@Test
public void getCommonStartPath2() {
GraphPath<Integer> commonStartGraphPath = new GraphPath<>();
for (int i = 0; i < 10; i++) {
commonStartGraphPath.add(i);
}
@Test
public void testContains() {
Random r = new Random();
int randomInt = r.nextInt(1000);
graphPath.add(randomInt);
assertTrue(graphPath.contains(randomInt));
assertFalse(graphPath.contains(1001));
for (int i = 0; i < 10; i++) {
commonStartGraphPath.add(i);
}
randomInt = r.nextInt(1000);
graphPath.add(randomInt);
assertTrue(graphPath.contains(randomInt));
assertFalse(graphPath.contains(1001));
}
GraphPath commonStartPathResult = commonStartGraphPath.getCommonStartPath(GP);
/**
* Test to verify if getLast() function works correctly. At setUp() a GraphPath is created
* containing 21 vertices, with the last vertex having integer 20. Assert checks if getLast()
* function returns 20, since it is the last vertex in the GraphPath.
*/
@Test
public void testGetLast() {
assertEquals(20, (int) graphPath.getLast());
}
/**
* Test to verify if depth() function works correctly. At setUp() a GraphPath is created
* containing 21 vertices, with the last vertex having integer 20. A random vertex is selected
* from the GraphPath. Since in the test cases the depth is equal to the vertex its integer,
* an assertEquals with the two is performed.
*/
@Test
public void testDepth() {
Random r = new Random();
int randomInt = r.nextInt(graphPath.size());
assertEquals(randomInt, graphPath.depth(randomInt));
}
for (int i = 0; i < commonStartPathResult.size(); i++) {
Assert.assertEquals(commonStartPathResult.get(i), GP.get(i));
}
/**
* Test to verify if get() function works correctly. At setUp() a GraphPath is created
* containing 21 vertices, with the last vertex having integer 20. A random vertex is selected
* from the GraphPath. Since in the test cases the index is equal to the vertex its integer,
* an assertEquals with the two is performed.
*/
@Test
public void testGet() {
Random r = new Random();
int randomInt = r.nextInt(graphPath.size());
assertEquals(randomInt, (int) graphPath.get(randomInt));
}
Assert.assertEquals(10, commonStartPathResult.size());
}
/**
* Test to verify if removeLast() function works correctly. At setUp() a GraphPath is created
* containing 21 vertices, with the last vertex having integer 20. Assert checks if this is the
* case. Second assert checks if 20 is again returned when removeLast() is called. Final assert
* checks if now getLast() returns 19, to check if removeLast() actually worked.
*/
@Test
public void tetRemoveLast() {
assertEquals(20, (int) graphPath.getLast());
assertEquals(20, (int) graphPath.removeLast());
assertEquals(19, (int) graphPath.getLast());
}
/**
* Test to verify if size() function works correctly. Reinitializes GP and adds three elements
* to it. Assert checks if size of GraphPath is then three.
*/
@Test
public void size() {
GP = new GraphPath<>();
GP.add(1);
GP.add(2);
GP.add(3);
/**
* Test to verify if getPredecessors() function works correctly. At setUp() a GraphPath is
* created containing 21 vertices, with the last vertex having integer 20. This test creates a
* new set with integers 0-10. Then assertEquals checks if the set returned by
* getPredecessors() is equal as this set.
*/
@Test
public void testGetPredecessors() {
Set<Integer> predecessors = new HashSet<>();
for (int i = 0; i <= 10; i++) {
predecessors.add(i);
}
Assert.assertEquals(3, GP.size());
}
Set<Integer> predecessorsSet = graphPath.getPredecessors(10);
assertEquals(predecessors, predecessorsSet);
}
/**
* Test to verify if contains() function works correctly. A vertex with a random integer is
* added to the GraphPath. Assertion checks if vertex is really added to the GraphPath. This is
* done twice.
*/
@Test
public void contains() {
Random r = new Random();
int randomInt = r.nextInt(100000);
GP.add(randomInt);
/**
* Test to verify if getPredecessors() function works correctly. At setUp() a GraphPath is
* created containing 21 vertices, with the last vertex having integer 20. Assert checks if
* getPredecessors() returns an empty HashSet whenever it is called with a larger index than
* the amount of vertices contained in the GraphPath.
*/
@Test
public void testGetPredecessors_LargerIndex() {
Set<Integer> predecessorsSet = graphPath.getPredecessors(21);
assertEquals(0, predecessorsSet.size());
}
Assert.assertTrue(GP.contains(randomInt));
/**
* Test to verify if getSuccessors() function works correctly. At setUp() a GraphPath is
* created containing 21 vertices, with the last vertex having integer 20. This test creates a
* new set with integers 10-20. Then assertEquals checks if the set returned by
* getSuccessors() is the same as this set.
*/
@Test
public void testGetSuccessors() {
Set<Integer> successors = new HashSet<>();
for (int i = 10; i <= 20; i++) {
successors.add(i);
}
randomInt = r.nextInt(100000);
GP.add(randomInt);
Set<Integer> successorsSet = graphPath.getSuccessors(10);
assertEquals(successors, successorsSet);
}
Assert.assertTrue(GP.contains(randomInt));
}
/**
* Test to verify if getLast() function works correctly. At setUp() a GraphPath is created
* containing 21 vertices, with the last vertex having integer 20. Assert checks if getLast()
* function returns 20, since it is the last vertex in the GraphPath.
*/
@Test
public void getLast() {
Assert.assertEquals(20, (int) GP.getLast());
}
/**
* Test to verify if depth() function works correctly. At setUp() a GraphPath is created
* containing 21 vertices, with the last vertex having integer 20. A random vertex is selected
* from the GraphPath. Since in the test cases the depth is equal to the vertex its integer,
* an assertEquals with the two is performed.
*/
@Test
public void depth() {
Random r = new Random();
int randomInt = r.nextInt(GP.size());
Assert.assertEquals(randomInt, GP.depth(randomInt));
}
/**
* Test to verify if get() function works correctly. At setUp() a GraphPath is created
* containing 21 vertices, with the last vertex having integer 20. A random vertex is selected
* from the GraphPath. Since in the test cases the index is equal to the vertex its integer,
* an assertEquals with the two is performed.
*/
@Test
public void get() {
Random r = new Random();
int randomInt = r.nextInt(GP.size());
Assert.assertEquals(randomInt, (int) GP.get(randomInt));
}
/**
* Test to verify if removeLast() function works correctly. At setUp() a GraphPath is created
* containing 21 vertices, with the last vertex having integer 20. Assert checks if this is the
* case. Second assert checks if 20 is again returned when removeLast() is called. Final assert
* checks if now getLast() returns 19, to check if removeLast() actually worked.
*/
@Test
public void removeLast() {
Assert.assertEquals(20, (int) GP.getLast());
Assert.assertEquals(20, (int) GP.removeLast());
Assert.assertEquals(19, (int) GP.getLast());
}
/**
* Test to verify if getPredecessors() function works correctly. At setUp() a GraphPath is
* created containing 21 vertices, with the last vertex having integer 20. Testcase creates a
* new HashSet with integers 0-10. Then assertEquals checks if HashSet returned by
* getPredecessors() is the same as this HashSet.
*/
@Test
public void getPredecessors() {
Set<Integer> predecessors = new HashSet<>();
for (int i = 0; i <= 10; i++) {
predecessors.add(i);
}
Set<Integer> predecessorsSet = GP.getPredecessors(10);
Assert.assertEquals(predecessors, predecessorsSet);
}
/**
* Test to verify if getPredecessors() function works correctly. At setUp() a GraphPath is
* created containing 21 vertices, with the last vertex having integer 20. Assert checks if
* getPredecessors() returns an empty HashSet whenever it is called with a larger index than
* the amount of vertices contained in the GraphPath.
*/
@Test
public void getPredecessors_LargerIndex() {
Set<Integer> predecessorsSet = GP.getPredecessors(21);
Assert.assertEquals(0, predecessorsSet.size());
}
/**
* Test to verify if getSuccessors() function works correctly. At setUp() a GraphPath is
* created containing 21 vertices, with the last vertex having integer 20. Testcase creates a
* new HashSet with integers 10-20. Then assertEquals checks if HashSet returned by
* getSuccessors() is the same as this HashSet.
*/
@Test
public void getSuccessors() {
Set<Integer> successors = new HashSet<>();
for (int i = 10; i <= 20; i++) {
successors.add(i);
}
Set<Integer> successorsSet = GP.getSuccessors(10);
Assert.assertEquals(successors, successorsSet);
}
/**
* Test to verify if getPredecessors() function works correctly. At setUp() a GraphPath is
* created containing 21 vertices, with the last vertex having integer 20. Assert checks if
* getSuccessors() returns an empty HashSet whenever it is called with a larger index than
* the amount of vertices contained in the GraphPath.
*/
@Test
public void getSuccessors_LargerIndex() {
Set<Integer> successorsSet = GP.getSuccessors(GP.size());
Assert.assertEquals(0, successorsSet.size());
}
/**
* Test to verify if getPredecessors() function works correctly. At setUp() a GraphPath is
* created containing 21 vertices, with the last vertex having integer 20. Assert checks if
* getSuccessors() returns an empty HashSet whenever it is called with a larger index than
* the amount of vertices contained in the GraphPath.
*/
@Test
public void testGetSuccessors_LargerIndex() {
Set<Integer> successorsSet = graphPath.getSuccessors(graphPath.size());
assertEquals(0, successorsSet.size());
}
}