-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedListDequeTest.java
92 lines (74 loc) · 2.63 KB
/
LinkedListDequeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/** Performs some basic linked list tests. */
public class LinkedListDequeTest {
/* Utility method for printing out empty checks. */
public static boolean checkEmpty(boolean expected, boolean actual) {
if (expected != actual) {
System.out.println("isEmpty() returned " + actual + ", but expected: " + expected);
return false;
}
return true;
}
/* Utility method for printing out empty checks. */
public static boolean checkSize(int expected, int actual) {
if (expected != actual) {
System.out.println("size() returned " + actual + ", but expected: " + expected);
return false;
}
return true;
}
/* Prints a nice message based on whether a test passed.
* The \n means newline. */
public static void printTestStatus(boolean passed) {
if (passed) {
System.out.println("Test passed!\n");
} else {
System.out.println("Test failed!\n");
}
}
/** Adds a few things to the list, checking isEmpty() and size() are correct,
* finally printing the results.
*
* && is the "and" operation. */
public static void addIsEmptySizeTest() {
System.out.println("Running add/isEmpty/Size test.");
System.out.println("Make sure to uncomment the lines below (and delete this print statement).");
/*
LinkedListDeque<String> lld1 = new LinkedListDeque<String>();
boolean passed = checkEmpty(true, lld1.isEmpty());
lld1.addFirst("front");
// The && operator is the same as "and" in Python.
// It's a binary operator that returns true if both arguments true, and false otherwise.
passed = checkSize(1, lld1.size()) && passed;
passed = checkEmpty(false, lld1.isEmpty()) && passed;
lld1.addLast("middle");
passed = checkSize(2, lld1.size()) && passed;
lld1.addLast("back");
passed = checkSize(3, lld1.size()) && passed;
System.out.println("Printing out deque: ");
lld1.printDeque();
printTestStatus(passed);
*/
}
/** Adds an item, then removes an item, and ensures that dll is empty afterwards. */
public static void addRemoveTest() {
System.out.println("Running add/remove test.");
System.out.println("Make sure to uncomment the lines below (and delete this print statement).");
/*
LinkedListDeque<Integer> lld1 = new LinkedListDeque<Integer>();
// should be empty
boolean passed = checkEmpty(true, lld1.isEmpty());
lld1.addFirst(10);
// should not be empty
passed = checkEmpty(false, lld1.isEmpty()) && passed;
lld1.removeFirst();
// should be empty
passed = checkEmpty(true, lld1.isEmpty()) && passed;
printTestStatus(passed);
*/
}
public static void main(String[] args) {
System.out.println("Running tests.\n");
addIsEmptySizeTest();
addRemoveTest();
}
}