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("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.mkOne("abc")).isEmpty(),
94 false,
95 "isEmpty 1.5");
96 { LinkedList <String> l = LinkedList.mkEmptyList();
97 test(l.concat(LinkedList.mkOne("abc")).isEmpty(),
98 false,
99 "isEmpty 1.6");
100 }
101 test(LinkedList.mkOne("abc").concat(LinkedList.mkOne("abc")).isEmpty(),
102 false,
103 "isEmpty 1.7");
104
105
106
107
108 test(LinkedList.mkEmptyList().isIn("abc"),
109 false,
110 "isIn 1.1");
111 test(LinkedList.mkOne("xyz").isIn("abc"),
112 false,
113 "isIn 1.2");
114 test(LinkedList.mkOne("abc").isIn("abc"),
115 true,
116 "isIn 1.3");
117 test(LinkedList.mkOne("abc").isIn(new Integer(42).toString()),
118 false,
119 "isIn 1.4");
120
121 {
122 Integer
123 i = 2005;
124
125 LinkedList <Integer>
126 l = LinkedList.mkEmptyList();
127 l = l.insert(i);
128
129 test(l.isIn(i),
130 true,
131 "isIn 1.5.1");
132
133 l = l.remove(i);
134 test(l.isIn(i),
135 false,
136 "isIn 1.5.2");
137 }
138
139
140
141
142 test(LinkedList.mkEmptyList().len(),
143 0,
144 "len 1.1");
145 test(LinkedList.mkOne("abc").len(),
146 1,
147 "len 1.2");
148
149
150
151 {
152 Integer e = 42;
153 LinkedList <Integer> l = LinkedList.mkEmptyList();
154
155
156
157
158 for (int i = 0; i < 5; ++i) {
159 l = l.cons(e);
160 test(l.len(),
161 i + 1,
162 "len 1.3." + (i + 1));
163 }
164
165
166
167 for (int i = 0; i < 5; ++i) {
168 l = l.remove(e);
169 test(l.len(),
170 5 - (i + 1),
171 "len 1.4." + (i + 1));
172 }
173
174 test(l.isEmpty(),
175 true,
176 "len 1.5");
177
178
179
180 for (int i = 0; i < 3; ++i) {
181 l = l.insert(e);
182 test(l.len(),
183 1,
184 "len 1.6." + (i + 1));
185 }
186
187 }
188
189
190
191
192 {
193 LinkedList<Integer>
194 l = LinkedList.mkEmptyList();
195
196
197 {
198 int len =
199 l.forall(new NoOfElements <Integer>());
200
201 test(len, 0, "NoOfElements 1.1");
202 }
203
204
205 {
206 int sum =
207 l.forall(new IntegerSum());
208
209 test(sum, 0, "IntegerSum 1.1");
210 }
211
212
213 test(l.forall(new ToString<Integer>("[", ",", "]")),
214 "[]",
215 "ToString 1.1");
216 }
217
218
219
220
221 {
222 LinkedList <Integer>
223 l = LinkedList.mkEmptyList();
224
225 for (int i = 1; i < 6; ++i) {
226 l = l.cons(i);
227 }
228
229
230 {
231 int len =
232 l.forall(new NoOfElements<Integer>());
233
234 test(len, 5, "NoOfElements 1.2");
235 }
236
237
238 {
239 Object result =
240 l.forall(new IntegerSum());
241
242 int sum =
243 ((Integer)result).intValue();
244
245 test(sum, 15, "IntegerSum 1.2");
246 }
247
248
249 test(l.forall(new ToString <Integer>("[", ",", "]")),
250 "[5,4,3,2,1]",
251 "ToString 1.1");
252 }
253
254
255
256
257 {
258 LinkedList <String>
259 l = LinkedList.mkEmptyList();
260
261 test(l.len1(), l.len(), "len1 1.1");
262
263 l.cons("abc");
264 test(l.len1(), l.len(), "len1 1.2");
265
266 l.cons("xyz");
267 test(l.len1(), l.len(), "len1 1.3");
268 }
269
270
271
272
273 {
274 LinkedList <String>
275 l = LinkedList.mkEmptyList();
276
277 test(l.toString1(), l.toString(), "toString1 1.1");
278
279 l.cons("abc");
280 test(l.toString1(), l.toString(), "toString1 1.2");
281
282 l.cons("xyz");
283 test(l.toString1(), l.toString(), "toString1 1.3");
284 }
285
286
287
288
289 System.out.println
290 ( (failed == 0)
291 ? "all tests passed"
292 : (failed + " Test(s) failed") );
293 }
294}
295