1
2
3
4
5
6
7
8public
9class LinkedListTest {
10
11
12
13
14 static
15 int failed = 0;
16
17
18
19 static
20 void test( Object result,
21 Object expected,
22 String testCase )
23 {
24 if ( ! result.equals(expected) ) {
25 ++ failed;
26 System.out.println
27 ("Test Case " + testCase + " failed\n" +
28 "result : " + result.toString() + "\n" +
29 "expected : " + expected.toString() );
30 }
31 }
32
33
34
35 static
36 void test( boolean result,
37 boolean expected,
38 String testCase )
39 {
40 test( new Boolean(result),
41 new Boolean(expected),
42 testCase );
43 }
44
45
46
47 static
48 void test( int result,
49 int expected,
50 String testCase )
51 {
52 test( new Integer(result),
53 new Integer(expected),
54 testCase );
55 }
56
57
58
59 public static
60 void main(String[] argv) {
61
62
63
64
65 test(LinkedList.mkEmptyList().toString(),
66 "[]",
67 "toString 1.1");
68 test(LinkedList.mkOne(new Integer(42)).toString(),
69 "[42]",
70 "toString 1.2");
71 test(LinkedList.mkOne("abc").cons("def").toString(),
72 "[def,abc]",
73 "toString 1.3");
74 test(LinkedList.mkOne("abc").append(new Integer(13)).toString(),
75 "[abc,13]",
76 "toString 1.4");
77
78
79
80
81 test(LinkedList.mkEmptyList().isEmpty(),
82 true,
83 "isEmpty 1.1");
84 test(LinkedList.mkOne(new Integer(42)).isEmpty(),
85 false,
86 "isEmpty 1.2");
87 test(LinkedList.mkEmptyList().cons("abc").isEmpty(),
88 false,
89 "isEmpty 1.3");
90 test(LinkedList.mkEmptyList().concat(LinkedList.mkEmptyList()).isEmpty(),
91 true,
92 "isEmpty 1.4");
93 test(LinkedList.mkOne("xyz").concat(LinkedList.mkEmptyList()).isEmpty(),
94 false,
95 "isEmpty 1.5");
96 test(LinkedList.mkEmptyList().concat(LinkedList.mkOne("abc")).isEmpty(),
97 false,
98 "isEmpty 1.6");
99 test(LinkedList.mkOne("abc").concat(LinkedList.mkOne("abc")).isEmpty(),
100 false,
101 "isEmpty 1.7");
102
103
104
105
106 test(LinkedList.mkEmptyList().isIn("abc"),
107 false,
108 "isIn 1.1");
109 test(LinkedList.mkOne("xyz").isIn("abc"),
110 false,
111 "isIn 1.2");
112 test(LinkedList.mkOne("abc").isIn("abc"),
113 true,
114 "isIn 1.3");
115 test(LinkedList.mkOne("abc").isIn(new Integer(42)),
116 false,
117 "isIn 1.4");
118
119 {
120 Integer
121 i = new Integer(2001);
122 LinkedList
123 l = LinkedList.mkEmptyList().insert(i);
124
125 test(l.isIn(i),
126 true,
127 "isIn 1.5.1");
128
129 l = l.remove(i);
130 test(l.isIn(i),
131 false,
132 "isIn 1.5.2");
133 }
134
135
136
137
138 test(LinkedList.mkEmptyList().len(),
139 0,
140 "len 1.1");
141 test(LinkedList.mkOne("abc").len(),
142 1,
143 "len 1.2");
144
145
146
147 {
148 Integer e = new Integer(42);
149 LinkedList l = LinkedList.mkEmptyList();
150
151
152
153
154 for (int i = 0; i < 5; ++i) {
155 l = l.cons(e);
156 test(l.len(),
157 i + 1,
158 "len 1.3." + (i + 1));
159 }
160
161
162
163 for (int i = 0; i < 5; ++i) {
164 l = l.remove(e);
165 test(l.len(),
166 5 - (i + 1),
167 "len 1.4." + (i + 1));
168 }
169
170 test(l.isEmpty(),
171 true,
172 "len 1.5");
173
174
175
176 for (int i = 0; i < 3; ++i) {
177 l = l.insert(e);
178 test(l.len(),
179 1,
180 "len 1.6." + (i + 1));
181 }
182
183 }
184
185
186
187
188 {
189 LinkedList
190 l = LinkedList.mkEmptyList();
191
192
193 {
194 Object result =
195 l.forall(new NoOfElements());
196
197 int len =
198 ((Integer)result).intValue();
199
200 test(len, 0, "NoOfElements 1.1");
201 }
202
203
204 {
205 Object result =
206 l.forall(new IntegerSum());
207
208 int sum =
209 ((Integer)result).intValue();
210
211 test(sum, 0, "IntegerSum 1.1");
212 }
213
214
215 test(l.forall(new ToString("[", ",", "]")),
216 "[]",
217 "ToString 1.1");
218 }
219
220
221
222
223 {
224 LinkedList
225 l = LinkedList.mkEmptyList();
226
227 for (int i = 1; i < 6; ++i) {
228 l = l.cons( new Integer(i) );
229 }
230
231
232 {
233 Object result =
234 l.forall(new NoOfElements());
235
236 int len =
237 ((Integer)result).intValue();
238
239 test(len, 5, "NoOfElements 1.2");
240 }
241
242
243 {
244 Object result =
245 l.forall(new IntegerSum());
246
247 int sum =
248 ((Integer)result).intValue();
249
250 test(sum, 15, "IntegerSum 1.2");
251 }
252
253
254 test(l.forall(new ToString("[", ",", "]")),
255 "[5,4,3,2,1]",
256 "ToString 1.1");
257 }
258
259
260
261
262 {
263 LinkedList
264 l = LinkedList.mkEmptyList();
265
266 test(l.len1(), l.len(), "len1 1.1");
267
268 l.cons("abc");
269 test(l.len1(), l.len(), "len1 1.2");
270
271 l.cons("xyz");
272 test(l.len1(), l.len(), "len1 1.3");
273 }
274
275
276
277
278 {
279 LinkedList
280 l = LinkedList.mkEmptyList();
281
282 test(l.toString1(), l.toString(), "toString1 1.1");
283
284 l.cons("abc");
285 test(l.toString1(), l.toString(), "toString1 1.2");
286
287 l.cons("xyz");
288 test(l.toString1(), l.toString(), "toString1 1.3");
289 }
290
291
292
293
294 System.out.println
295 ( (failed == 0)
296 ? "all tests passed"
297 : (failed + " Test(s) failed") );
298 }
299}
300