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