-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListTest.java
More file actions
166 lines (149 loc) · 5.59 KB
/
LinkedListTest.java
File metadata and controls
166 lines (149 loc) · 5.59 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author Brianna Penkala
* This is the testing class for LinkedList
*/
class LinkedListTest {
/** These fields initialize LinkedLists for testing */
LinkedList list;
LinkedList mathList;
LinkedList subTest;
LinkedList emptyList;
LinkedList subTestTwo;
/** A method that sets up the lists to be used in the testing methods* */
@BeforeEach
public void setUp() {
list = new LinkedList();
mathList = new LinkedList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
mathList.add(6);
mathList.add(8);
mathList.add(8);
mathList.add(8);
mathList.add(8);
mathList.add(9);
mathList.add(9);
subTest = mathList.sublist(6, 8);
emptyList = new LinkedList();
emptyList.add(0);
}
/** This test checks the getFirstNode method* */
@Test
void getFirstNode() {
assertEquals(1, list.getFirstElement()); //showing the element in the first node since it is more readable
assertEquals(6, mathList.getFirstElement());
}
/** This test checks the add method with positive, 0, and negative numbers*/
@Test
void add() {
assertEquals("1, 2, 3, 4", list.toString());
assertEquals("6, 8, 8, 8, 8, 9, 9", mathList.toString());
mathList.add(-2);
assertEquals("6, 8, 8, 8, 8, 9, 9, -2", mathList.toString());
list.add(0);
assertEquals("1, 2, 3, 4, 0", list.toString());
}
/** This test checks the add at an index method at first, middle, and end indices, and when there is a negative index*/
@Test
void otherAdd() {
list.add(6, 1);
assertEquals("1, 6, 2, 3, 4", list.toString());
list.add(7, 5);
assertEquals("1, 6, 2, 3, 4, 7", list.toString());
list.add(8, 0);
assertThrows(IllegalArgumentException.class, () -> { //when a negative index in given
list.add(1, -1);
});
}
/** This test checks the indexOf method with integers at the front, middle, and end of the list, and non-existent integers*/
@Test
void indexOf() {
assertEquals(1, list.indexOf(2));
assertEquals(0, list.indexOf(1));
assertEquals(3, list.indexOf(4));
assertEquals(-1, list.indexOf(10));
}
/** This test checks the remove method with integers at the front, middle, and end of the list*/
@Test
void remove() {
assertEquals("1, 2, 3, 4", list.toString());
assertEquals(3, list.remove(2));
assertEquals("1, 2, 4", list.toString()); //proof of removal
assertEquals(1, list.remove(0));
assertEquals("2, 4", list.toString());
assertEquals(4, list.remove(1));
assertEquals("2", list.toString());
}
/** This test checks the removeValue method with integers at the front, middle, and end of the list*/
@Test
void removeValue() {
list.removeValue(2);
assertEquals("1, 3, 4", list.toString());
list.removeValue(4);
assertEquals("1, 3", list.toString());
list.removeValue(1);
assertEquals("3", list.toString());
}
/** This test checks the removeAll method with integers at the front, middle, and end of the list*/
@Test
void removeAll() {
mathList.removeAll(100);
assertEquals("6, 8, 8, 8, 8, 9, 9", mathList.toString());
mathList.removeAll(8);
assertEquals("6, 9, 9", mathList.toString());
mathList.removeAll(9);
assertEquals("6", mathList.toString());
mathList.removeAll(6);
assertEquals("", mathList.toString());
}
/** This test checks the mean method with positive and 0 values*/
@Test
void mean() {
assertEquals(8, mathList.mean());
assertEquals(0, emptyList.mean());
assertEquals(2.5, list.mean());
}
/** This test checks the variance method with positive and 0 values*/
@Test
void variance() {
assertEquals(1, mathList.variance());
assertEquals(-0.0, emptyList.variance()); //should be 0, same thing
assertEquals(1.375, list.variance());
}
/** This test checks the sublist method with boundaries at the front, middle, and ends of the list, with varying sizes*/
@Test
void sublist() {
subTest = mathList.sublist(6, 8);
assertEquals("6, 8, 8, 8, 8", subTest.toString());
subTestTwo = list.sublist(2, 4);
assertEquals("2, 3, 4", subTestTwo.toString());
}
/** This test checks the removeNoise method with lists of varying sizes*/
@Test
void removeNoise() { //did not know a dataset that had omitted values, should work regardless
assertEquals("6, 8, 8, 8, 8, 9", mathList.removeNoise().toString());
mathList.add(10);
mathList.add(100);
mathList.add(200);
assertEquals("6, 8, 8, 8, 8, 9, 9, 10, 100", mathList.removeNoise().toString());
}
/** This test checks the toString method lists of varying sizes, this method is also tested throughout the rest of the tests*/
@Test
void testToString() {
assertEquals("1, 2, 3, 4", list.toString());
assertEquals("6, 8, 8, 8, 8, 9, 9", mathList.toString());
}
//The other toString method (with the node parameter) is tested in the swapNodesInPairs method. It works.
/** This test checks the length method*/
@Test
void length() {
assertEquals(4, list.length());
assertEquals(7, mathList.length());
assertEquals(1, emptyList.length());
}
}