|
@@ -0,0 +1,178 @@
|
|
1
|
+import org.junit.Assert;
|
|
2
|
+import org.junit.Test;
|
|
3
|
+
|
|
4
|
+import java.util.ArrayList;
|
|
5
|
+
|
|
6
|
+public class MySetTest {
|
|
7
|
+
|
|
8
|
+ MySet<Integer> testSet = new MySet<>();
|
|
9
|
+
|
|
10
|
+ public void populateSet() {
|
|
11
|
+ testSet.add(10);
|
|
12
|
+ testSet.add(3);
|
|
13
|
+ testSet.add(9);
|
|
14
|
+ testSet.add(2);
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ @Test
|
|
18
|
+ public void testAdd() {
|
|
19
|
+ testSet.add(10);
|
|
20
|
+
|
|
21
|
+ Integer expected = 10;
|
|
22
|
+ Integer actual = testSet.get(0);
|
|
23
|
+
|
|
24
|
+ Assert.assertEquals(expected, actual);
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ @Test
|
|
28
|
+ public void testAdd_Multiple() {
|
|
29
|
+ populateSet();
|
|
30
|
+
|
|
31
|
+ Integer expected = 2;
|
|
32
|
+ Integer actual = testSet.get(3);
|
|
33
|
+
|
|
34
|
+ Assert.assertEquals(expected, actual);
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ @Test(expected = IndexOutOfBoundsException.class)
|
|
38
|
+ public void testAdd_IfDuplicate() {
|
|
39
|
+ testSet.add(10);
|
|
40
|
+ testSet.add(10);
|
|
41
|
+
|
|
42
|
+ testSet.get(2);
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ @Test
|
|
46
|
+ public void testAdd_IfDuplicate_Size() {
|
|
47
|
+ populateSet();
|
|
48
|
+ testSet.add(9);
|
|
49
|
+
|
|
50
|
+ int expected = 4;
|
|
51
|
+ int actual = testSet.size();
|
|
52
|
+
|
|
53
|
+ Assert.assertEquals(expected, actual);
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ @Test
|
|
57
|
+ public void testAddAll_NoDuplicates() {
|
|
58
|
+ populateSet();
|
|
59
|
+ ArrayList<Integer> addArray = new ArrayList<>();
|
|
60
|
+ addArray.add(5);
|
|
61
|
+ addArray.add(7);
|
|
62
|
+ addArray.add(8);
|
|
63
|
+
|
|
64
|
+ testSet.addAll(addArray);
|
|
65
|
+
|
|
66
|
+ int expected = 7;
|
|
67
|
+ int actual = testSet.size();
|
|
68
|
+
|
|
69
|
+ Assert.assertEquals(expected, actual);
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ @Test
|
|
73
|
+ public void testAddAll_OneDuplicate() {
|
|
74
|
+ populateSet();
|
|
75
|
+ ArrayList<Integer> addArray = new ArrayList<>();
|
|
76
|
+ addArray.add(5);
|
|
77
|
+ addArray.add(7);
|
|
78
|
+ addArray.add(10);
|
|
79
|
+
|
|
80
|
+ testSet.addAll(addArray);
|
|
81
|
+
|
|
82
|
+ int expected = 6;
|
|
83
|
+ int actual = testSet.size();
|
|
84
|
+
|
|
85
|
+ Assert.assertEquals(expected, actual);
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ @Test
|
|
89
|
+ public void testAddAll_AllDuplicates() {
|
|
90
|
+ populateSet();
|
|
91
|
+ ArrayList<Integer> addArray = new ArrayList<>();
|
|
92
|
+ addArray.add(9);
|
|
93
|
+ addArray.add(3);
|
|
94
|
+ addArray.add(10);
|
|
95
|
+
|
|
96
|
+ testSet.addAll(addArray);
|
|
97
|
+
|
|
98
|
+ int expected = 4;
|
|
99
|
+ int actual = testSet.size();
|
|
100
|
+
|
|
101
|
+ Assert.assertEquals(expected, actual);
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ @Test
|
|
105
|
+ public void testClear(){
|
|
106
|
+ populateSet();
|
|
107
|
+ int expected = 4;
|
|
108
|
+ int actual = testSet.size();
|
|
109
|
+
|
|
110
|
+ Assert.assertEquals(expected, actual);
|
|
111
|
+
|
|
112
|
+ testSet.clear();
|
|
113
|
+ int newExpected = 0;
|
|
114
|
+ int newActual = testSet.size();
|
|
115
|
+
|
|
116
|
+ Assert.assertEquals(newExpected, newActual);
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ @Test
|
|
120
|
+ public void testContains_EmptySet(){
|
|
121
|
+ Assert.assertFalse(testSet.contains(10));
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ @Test
|
|
125
|
+ public void testContains_True(){
|
|
126
|
+ populateSet();
|
|
127
|
+
|
|
128
|
+ Assert.assertTrue(testSet.contains(10));
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ @Test
|
|
132
|
+ public void testContains_False(){
|
|
133
|
+ populateSet();
|
|
134
|
+
|
|
135
|
+ Assert.assertFalse(testSet.contains(7));
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ @Test
|
|
139
|
+ public void testContainsAll_False() {
|
|
140
|
+ populateSet();
|
|
141
|
+ ArrayList<Integer> newArray = new ArrayList<>();
|
|
142
|
+ newArray.add(2);
|
|
143
|
+ newArray.add(3);
|
|
144
|
+ newArray.add(7);
|
|
145
|
+
|
|
146
|
+ Assert.assertFalse(testSet.containsAll(newArray));
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ @Test
|
|
150
|
+ public void testContainsAll_True() {
|
|
151
|
+ populateSet();
|
|
152
|
+ ArrayList<Integer> newArray = new ArrayList<>();
|
|
153
|
+ newArray.add(9);
|
|
154
|
+ newArray.add(3);
|
|
155
|
+ newArray.add(10);
|
|
156
|
+
|
|
157
|
+ Assert.assertTrue(testSet.containsAll(newArray));
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+ @Test
|
|
161
|
+ public void testIsEmpty_True(){
|
|
162
|
+ Assert.assertTrue(testSet.isEmpty());
|
|
163
|
+ }
|
|
164
|
+
|
|
165
|
+ @Test
|
|
166
|
+ public void testIsEmpty_False(){
|
|
167
|
+ populateSet();
|
|
168
|
+
|
|
169
|
+ Assert.assertFalse(testSet.isEmpty());
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ @Test
|
|
173
|
+ public void testEquals(){
|
|
174
|
+ MySet<Object> object = new MySet<>();
|
|
175
|
+ testSet.equals(object);
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+}
|