Commit 656caae1 authored by Archie Shahidullah's avatar Archie Shahidullah :expressionless:
Browse files

Resolving new issues

parent 0681730f
1 merge request!1Review Tests for Project 3
Pipeline #36446 canceled with stage
in 0 seconds
Showing with 41 additions and 52 deletions
+41 -52
......@@ -296,10 +296,10 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests {
impl.addFront(num);
}
if (reference.size() > 1 && impl.size() > 1) {
if (num % 3 == 0) {
if (num % 5 == 0) {
reference.removeFirst();
impl.removeFront();
} else if (num % 4 == 0) {
} else if (num % 7 == 0) {
reference.removeLast();
impl.removeBack();
}
......@@ -331,10 +331,10 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests {
impl.addFront(num);
}
if (reference.size() > 1 && impl.size() > 1) {
if (num % 3 == 0) {
if (num % 5 == 0) {
reference.removeFirst();
impl.removeFront();
} else if (num % 4 == 0) {
} else if (num % 7 == 0) {
reference.removeLast();
impl.removeBack();
}
......
......@@ -11,7 +11,6 @@ import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.hamcrest.MatcherAssert.assertThat;
import org.hamcrest.collection.IsIterableContainingInOrder;
import edu.caltech.cs2.interfaces.IDeque;
......@@ -24,7 +23,7 @@ public class NodeChecker {
/**
* This method checks whether a given class is a linked node or not.
*
* @param clazz the class you want to check
* @param clazz the class you want to check
* @param doublyLinked whether or not the list <em>can</em> be doubly linked
*/
public static void isNode(Class clazz, boolean doublyLinked) {
......@@ -39,9 +38,8 @@ public class NodeChecker {
}
// Get fields
SortedSet<String> fields = new TreeSet<>(Stream.of(clazz.getDeclaredFields())
.map(x -> x.getName())
.collect(Collectors.toList()));
SortedSet<String> fields = new TreeSet<>(
Stream.of(clazz.getDeclaredFields()).map(x -> x.getName()).collect(Collectors.toList()));
boolean hasData = false;
boolean hasNode = false;
......@@ -65,22 +63,18 @@ public class NodeChecker {
}
// Linked to another node
hasNode = true;
}
else if (f.getType().toString().equals("class java.lang.Object")) {
} else if (f.getType().toString().equals("class java.lang.Object")) {
if (!Modifier.isFinal(f.getModifiers())) {
fail("Field \"" + field + "\" in class " + clazz.getName() +
" is not final");
fail("Field \"" + field + "\" in class " + clazz.getName() + " is not final");
}
// Has a generic type to store data
if (hasData) {
// Checks for multiple data fields
fail("Class " + clazz.getName() + " has multiple generic fields: \""
+ field + "\"");
fail("Class " + clazz.getName() + " has multiple generic fields: \"" + field + "\"");
return;
}
hasData = true;
}
else {
} else {
fail("Field \"" + field + "\" is not a generic type in " + clazz.getTypeName());
}
}
......@@ -99,11 +93,10 @@ public class NodeChecker {
return;
}
hasConstructor = true;
}
else if (!type.toString().equals("class " + clazz.getTypeName())) {
} else if (!type.toString().equals("class " + clazz.getTypeName())) {
// Check for invalid argument types
fail("Constructor \"" + c.getName() + "\" has an argument that is not a " +
"generic type in class " + clazz.getTypeName());
fail("Constructor \"" + c.getName() + "\" has an argument that is not a " + "generic type in class "
+ clazz.getTypeName());
}
}
}
......@@ -112,7 +105,7 @@ public class NodeChecker {
/**
* This method performs a node check on every internal class.
*
* @param clazz the class you want to check
* @param clazz the class you want to check
* @param doublyLinked whether or not the list <em>can</em> be doubly linked
*/
public static void checkInternalClasses(Class clazz, boolean doublyLinked) {
......@@ -138,7 +131,7 @@ public class NodeChecker {
/**
* This method gets a valid, internal node class from a given class.
*
* @param clazz the class you want to check
* @param clazz the class you want to check
* @param doublyLinked whether or not the list <em>can</em> be doubly linked
* @return the node class
*/
......@@ -162,7 +155,7 @@ public class NodeChecker {
* This method gets fields of specified type from a given class.
*
* @param clazz the class you want to check
* @param type the type of field you want
* @param type the type of field you want
* @return a list of fields matching the given type
*/
public static List<Field> getFields(Class clazz, Class type) {
......@@ -178,12 +171,15 @@ public class NodeChecker {
}
/**
* This method checks whether a given pointer permutation in a deque contains a cycle.
* This method checks whether a given pointer permutation in a deque contains a
* cycle.
*
* @param deque the deque you want to check
* @param nextField the field corresponding to the next pointer in a linked node
* @param dequeField the field corresponding to the head pointer in a linked deque
* @param <E> the generic type of the data field in a linked node
* @param deque the deque you want to check
* @param nextField the field corresponding to the next pointer in a linked
* node
* @param dequeField the field corresponding to the head pointer in a linked
* deque
* @param <E> the generic type of the data field in a linked node
* @return an array containing the indices of the cyclic nodes
*/
public static <E> int[] checkCycle(IDeque<E> deque, Field nextField, Field dequeField) {
......@@ -206,7 +202,7 @@ public class NodeChecker {
// Check if memory locations are equal
if (nodes[j] == nodes[i]) {
// Return indices of nodes that create a cycle
return new int[] {i, j};
return new int[] { i, j };
}
}
try {
......@@ -219,15 +215,15 @@ public class NodeChecker {
i++;
}
// No cycle
return new int[] {-1, -1};
return new int[] { -1, -1 };
}
/**
* This method checks whether a given deque contains a cycle.
*
* @param deque the deque you want to check
* @param deque the deque you want to check
* @param doublyLinked whether or not the list <em>can</em> be doubly linked
* @param <E> the generic type of the data field in a linked node
* @param <E> the generic type of the data field in a linked node
*/
public static <E> void cycleDetection(IDeque<E> deque, boolean doublyLinked) {
Class nodeClass = getNodeClass(deque.getClass(), doublyLinked);
......@@ -246,30 +242,27 @@ public class NodeChecker {
continue;
}
if (nodes[0] == deque.size() && nodes[1] == 0) {
fail("The last node is connected to the first node in " +
nodeClass.getName() + " object");
}
else {
fail("Node " + nodes[0] + " is connected to Node " + nodes[1] +
" in " + nodeClass.getName() + " object");
fail("The last node is connected to the first node in " + nodeClass.getName() + " object");
} else {
fail("Node " + nodes[0] + " is connected to Node " + nodes[1] + " in " + nodeClass.getName()
+ " object");
}
}
}
}
/**
* This method checks whether iterating through a list forwards and backwards returns the same values.
* This method checks whether iterating through a list forwards and backwards
* returns the same values.
*
* @param deque the deque you want to check
* @param <E> the generic type of the data field in a linked node
* @param <E> the generic type of the data field in a linked node
*/
public static <E> void checkReverse(IDeque<E> deque) {
// Grab the linked node class and possible pointers to the head and tail
Class nodeClass = getNodeClass(deque.getClass(), true);
List<Field> dequePointers = getFields(deque.getClass(), nodeClass);
assertEquals(2, dequePointers.size(), "List does not have one head and tail pointer");
// The tests should pass only twice (head/next and tail/previous combinations)
int flag = 2;
// Try all permutations of pointers
try {
for (int i = 0; i < 2; i++) {
......@@ -308,13 +301,9 @@ public class NodeChecker {
temp = prev.get(temp);
}
Collections.reverse(backwardValues);
try {
// Assert the reverse of the backwards equals the forwards
assertThat(backwardValues,
IsIterableContainingInOrder.contains(forwardValues.toArray()));
} catch (AssertionError e) {
// Mark failed test
flag--;
// Test the reverse of the backwards equals the forwards
if (IsIterableContainingInOrder.contains(forwardValues.toArray()).matches(backwardValues)) {
return;
}
}
}
......@@ -322,8 +311,8 @@ public class NodeChecker {
ex.printStackTrace();
fail();
}
// A flag of zero indicates success as it passed twice
assertEquals(0, flag, "Forwards and backwards lists of values do not agree");
// Exiting the loop indicates failure
fail("Forwards and backwards lists of values do not agree");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment