-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyLinkedListTest.java
More file actions
118 lines (101 loc) · 3.87 KB
/
MyLinkedListTest.java
File metadata and controls
118 lines (101 loc) · 3.87 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
***** Important! Please Read! *****
*
* - Do NOT remove any of the existing import statements
* - Do NOT import additional junit packages
* - You MAY add in other non-junit packages as needed
*
* - Do NOT remove any of the existing test methods or change their name
* - You MAY add additional test methods. If you do, they should all pass
*
* - ALL of your assert test cases within each test method MUST pass, otherwise the
* autograder will fail that test method
* - You MUST write the require number of assert test cases in each test method,
* otherwise the autograder will fail that test method
* - You MAY write more than the required number of assert test cases as long as they all pass
*
* - All of your assert test cases within a method must be related to the method they are meant to test
* - All of your assert test cases within a method must be distinct and non-trivial
* - Your test cases should reflect the method requirements in the homework instruction specification
*
* - Your assert test cases will be reviewed by the course instructors and they may take off
* points if your assert test cases to do not meet the requirements
*/
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class MyLinkedListTest {
private MyLinkedList linkedList;
@BeforeEach
void setUp() {
linkedList = new MyLinkedList();
linkedList.addFirst("1");
linkedList.addLast("2");
linkedList.addLast("3");
linkedList.addLast("4");
linkedList.addLast("5");
}
@Test
void testContains() {
assertTrue(linkedList.contains("3"));
assertFalse(linkedList.contains("6"));
}
@Test
void testReverse() {
linkedList.reverse();
assertEquals("5", linkedList.get(0));
assertEquals("4", linkedList.get(1));
assertEquals("3", linkedList.get(2));
assertEquals("2", linkedList.get(3));
assertEquals("1", linkedList.get(4));
linkedList.reverse(); // Reverse back to original
assertEquals("1", linkedList.get(0));
assertEquals("2", linkedList.get(1));
assertEquals("3", linkedList.get(2));
assertEquals("4", linkedList.get(3));
assertEquals("5", linkedList.get(4));
}
@Test
void testRemoveMaximumValues() {
// Setup the list with a known series of values
MyLinkedList list = new MyLinkedList();
list.addLast("1");
list.addLast("3");
list.addLast("5");
list.addLast("4");
list.addLast("2");
// Remove the two maximum values ("5" and "4")
list.removeMaximumValues(2);
// Verify the list now contains the remaining values
assertTrue(list.contains("1"));
assertTrue(list.contains("2"));
assertTrue(list.contains("3"));
assertFalse(list.contains("4")); // Should be removed
assertFalse(list.contains("5")); // Should be removed
assertEquals(3, list.size); // Size should be reduced to 3
}
void testContainsSubsequence() {
// Main list setup
MyLinkedList mainList = new MyLinkedList();
mainList.addLast("1");
mainList.addLast("2");
mainList.addLast("3");
mainList.addLast("4");
mainList.addLast("5");
// Subsequence list setup
MyLinkedList subList = new MyLinkedList();
subList.addLast("2");
subList.addLast("3");
subList.addLast("4");
// Test for a valid subsequence
assertTrue(mainList.containsSubsequence(subList));
// Test for a non-existent subsequence
MyLinkedList nonSubList = new MyLinkedList();
nonSubList.addLast("3");
nonSubList.addLast("2"); // Incorrect order
assertFalse(mainList.containsSubsequence(nonSubList));
// Test with an empty subsequence (should always return true)
MyLinkedList emptyList = new MyLinkedList();
assertTrue(mainList.containsSubsequence(emptyList));
}
}