File: TList.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 65,320 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (556 lines) | stat: -rw-r--r-- 19,786 bytes parent folder | download | duplicates (18)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 6267067 6351336 6389198
 * @summary unit test for javac List
 * @modules jdk.compiler/com.sun.tools.javac.util
 */

import java.util.*;
import com.sun.tools.javac.util.List;

public class TList {
    public static void main(String[] args) {
        new TList().run();
    }

    String[][] data = {
        { },
        { "1" },
        { "1", "2" },
        { "1", "2" }, // different but equal
        { "1", "2", "3", "4", "X", "X", "X", "8", "9", "10" } // contains duplicates
    };

    Map<java.util.List<String>,List<String>> examples;

    void run() {
        examples = new LinkedHashMap<java.util.List<String>,List<String>>();
        for (String[] values: data)
            examples.put(Arrays.asList(values), createList(values));

        // 6351336: com.sun.tools.javac.util.List shouldn't extend java.util.AbstractList
        test_AbstractList();

        // general unit tests for java.util.List methods, including...
        // 6389198: com.sun.tools.javac.util.List.equals() violates java.util.List.equals() contract
        test_add_E();
        test_add_int_E();
        test_addAll_Collection();
        test_addAll_int_Collection();
        test_clear();
        test_contains_Object();
        test_contains_All();
        test_equals_Object();
        test_get_int();
        test_hashCode();
        test_indexOf_Object();
        test_isEmpty();
        test_iterator();
        test_lastIndexOf_Object();
        test_listIterator();
        test_listIterator_int();
        test_remove_int();
        test_remove_Object();
        test_removeAll_Collection();
        test_retainAll_Collection();
        test_set_int_E();
        test_size();
        test_subList_int_int();
        test_toArray();
        test_toArray_TArray();

        // tests for additional methods
        test_prependList_List();
        test_reverse();
    }

    // 6351336
    void test_AbstractList() {
        System.err.println("test AbstractList");
        if (AbstractList.class.isAssignableFrom(List.class))
            throw new AssertionError();
    }

    void test_add_E() {
        System.err.println("test add(E)");
        for (List<String> l: examples.values()) {
            try {
                l.add("test");
                throw new AssertionError();
            } catch (UnsupportedOperationException ex) {
            }
        }
    }

    void test_add_int_E() {
        System.err.println("test add(int,E)");
        for (List<String> l: examples.values()) {
            try {
                l.add(0, "test");
                throw new AssertionError();
            } catch (UnsupportedOperationException ex) {
            }
        }
    }

    void test_addAll_Collection() {
        System.err.println("test addAll(Collection)");
        for (List<String> l: examples.values()) {
            int l_size = l.size();
            for (java.util.List<String> arg: examples.keySet()) {
                try {
                    boolean modified = l.addAll(arg);
                    if (modified)
                        throw new AssertionError();
                } catch (UnsupportedOperationException e) {
                }
                if (l.size() != l_size)
                    throw new AssertionError();
            }
        }
    }

    void test_addAll_int_Collection() {
        System.err.println("test addAll(int,Collection)");
        for (List<String> l: examples.values()) {
            int l_size = l.size();
            for (java.util.List<String> arg: examples.keySet()) {
                try {
                    boolean modified = l.addAll(0, arg);
                    if (modified)
                        throw new AssertionError();
                } catch (UnsupportedOperationException e) {
                }
                if (l.size() != l_size)
                    throw new AssertionError();
            }
        }
    }

    void test_clear() {
        System.err.println("test clear()");
        for (List<String> l: examples.values()) {
            int l_size = l.size();
            try {
                l.clear();
                if (l_size > 0)
                    throw new AssertionError();
            } catch (UnsupportedOperationException e) {
            }
            if (l.size() != l_size)
                throw new AssertionError();
        }
    }

    void test_contains_Object() {
        System.err.println("test contains(Object)");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            boolean expect = ref.contains("1");
            boolean found = l.contains("1");
            if (expect != found)
                throw new AssertionError();
        }
    }

    void test_contains_All() {
        System.err.println("test containsAll()");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            for (java.util.List<String> arg: examples.keySet()) {
                boolean expect = ref.containsAll(arg);
                boolean found = l.containsAll(arg);
                if (expect != found)
                    throw new AssertionError();
            }
        }
    }

    // 6389198
    void test_equals_Object() {
        System.err.println("test equals(Object)");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            for (java.util.List<String> arg: examples.keySet()) {
                boolean expect = ref.equals(arg);
                boolean found = l.equals(arg);
                if (expect != found) {
                    System.err.println("ref: " + ref);
                    System.err.println("l: " + l);
                    System.err.println("arg: " + arg);
                    System.err.println("expect: " + expect + ", found: " + found);
                    throw new AssertionError();
                }
            }
        }
    }

    void test_get_int() {
        System.err.println("test get(int)");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            for (int i = -1; i <= ref.size(); i++) {
                boolean expectException = i < 0 || i >= ref.size();
                String expectValue = (expectException ? null : ref.get(i));
                try {
                    String foundValue = l.get(i);
                    if (expectException || !equal(expectValue, foundValue))
                        throw new AssertionError();
                } catch (IndexOutOfBoundsException ex) {
                    if (!expectException)
                        throw new AssertionError();
                }
            }
        }
    }

    void test_hashCode() {
        System.err.println("test hashCode()");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            long expect = ref.hashCode();
            long found = l.hashCode();
            if (expect != found) {
                System.err.println("ref: " + ref);
                System.err.println("l: " + l);
                System.err.println("expect: " + expect);
                System.err.println("found: " + found);
                throw new AssertionError();
            }
        }
    }

    void test_indexOf_Object() {
        System.err.println("test indexOf(Object)");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            for (int i = -1; i < ref.size(); i++) {
                String arg = (i == -1 ? "NOT IN LIST" : ref.get(i));
                int expect = ref.indexOf(arg);
                int found = l.indexOf(arg);
                if (expect != found)
                    throw new AssertionError();
            }
        }
    }

    void test_isEmpty() {
        System.err.println("test isEmpty()");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            boolean expect = ref.isEmpty();
            boolean found = l.isEmpty();
            if (expect != found)
                throw new AssertionError();
        }
    }

    void test_iterator() {
        System.err.println("test iterator()");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            if (!equal(l.iterator(), ref.iterator()))
                throw new AssertionError();
        }
    }

    void test_lastIndexOf_Object() {
        System.err.println("test lastIndexOf(Object)");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            for (int i = -1; i < ref.size(); i++) {
                String arg = (i == -1 ? "NOT IN LIST" : ref.get(i));
                int expect = ref.lastIndexOf(arg);
                int found = l.lastIndexOf(arg);
                if (expect != found)
                    throw new AssertionError();
            }
        }
    }

    void test_listIterator() {
        System.err.println("test listIterator()");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            if (!equal(l.listIterator(), ref.listIterator()))
                throw new AssertionError();
        }
    }

    void test_listIterator_int() {
        System.err.println("test listIterator(int)");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            for (int i = 0; i < ref.size(); i++) {
                if (!equal(l.listIterator(i), ref.listIterator(i)))
                    throw new AssertionError();
            }
        }
    }

    void test_remove_int() {
        System.err.println("test remove(int)");
        for (List<String> l: examples.values()) {
            try {
                l.remove(0);
                throw new AssertionError();
            } catch (UnsupportedOperationException ex) {
            }
        }
    }

    void test_remove_Object() {
        System.err.println("test remove(Object)");
        for (List<String> l: examples.values()) {
            boolean hasX = l.contains("X");
            try {
                l.remove("X");
                if (hasX)
                    throw new AssertionError();
            } catch (UnsupportedOperationException ex) {
            }
        }
    }

    void test_removeAll_Collection() {
        System.err.println("test removeAll(Collection)");
        for (List<String> l: examples.values()) {
            int l_size = l.size();
            for (java.util.List<String> arg: examples.keySet()) {
                try {
                    boolean modified = l.removeAll(arg);
                    if (modified)
                        throw new AssertionError();
                } catch (UnsupportedOperationException e) {
                }
                if (l.size() != l_size)
                    throw new AssertionError();
            }
        }
    }

    void test_retainAll_Collection() {
        System.err.println("test retainAll(Collection)");
        for (List<String> l: examples.values()) {
            int l_size = l.size();
            for (java.util.List<String> arg: examples.keySet()) {
                try {
                    boolean modified = l.retainAll(arg);
                    if (modified)
                        throw new AssertionError();
                } catch (UnsupportedOperationException e) {
                }
                if (l.size() != l_size)
                    throw new AssertionError();
            }
        }
    }

    void test_set_int_E() {
        System.err.println("test set(int,E)");
        for (List<String> l: examples.values()) {
            try {
                l.set(0, "X");
                throw new AssertionError();
            } catch (UnsupportedOperationException ex) {
            }
        }
    }

    void test_size() {
        System.err.println("test size()");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            int  expect = ref.size();
            int found = l.size();
            if (expect != found)
                throw new AssertionError();
        }
    }

    void test_subList_int_int() {
        System.err.println("test subList(int,int)");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            for (int lwb = 0; lwb < ref.size(); lwb++) {
                for (int upb = lwb; upb <= ref.size(); upb++) {
                    if (!equal(l.subList(lwb, upb), ref.subList(lwb,upb)))
                    throw new AssertionError();
                }
            }
        }
    }

    void test_toArray() {
        System.err.println("test toArray()");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            if (!equal(l.toArray(), ref.toArray()))
                throw new AssertionError();
        }
    }

    void test_toArray_TArray() {
        System.err.println("test toArray(E[])");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            if (!equal(l.toArray(new String[0]), ref.toArray(new String[0])))
                throw new AssertionError();
        }
    }

    void test_prependList_List() {
        System.err.println("test prependList(List<E>)");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            for (Map.Entry<java.util.List<String>, List<String>> arg_e: examples.entrySet()) {
                java.util.List<String> arg_ref = arg_e.getKey();
                List<String> arg = arg_e.getValue();
                java.util.List<String> expect = join(arg, ref);
                List<String> found = l.prependList(arg);
                // verify results, and that original and arg lists are unchanged
                if (!equal(expect, found)) {
                    System.err.println("ref: " + ref);
                    System.err.println("l: " + l);
                    System.err.println("arg: " + arg);
                    System.err.println("expect: " + expect);
                    System.err.println("found: " + found);
                    throw new AssertionError();
                }
                if (!equal(l, ref))
                    throw new AssertionError();
                if (!equal(arg, arg_ref))
                    throw new AssertionError();
            }
        }
    }

    void test_reverse() {
        System.err.println("test reverse()");
        for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
            java.util.List<String> ref = e.getKey();
            List<String> l = e.getValue();
            java.util.List<String> expect = reverse(ref);
            List<String> found = l.reverse();
            if (l.size() < 2 && found != l)  // reverse of empty or singleton list is itself
                throw new AssertionError();
            if (!equal(l, ref)) // orginal should be unchanged
                throw new AssertionError();
            if (!equal(expect, found))
                throw new AssertionError();
        }
    }

    static <T> com.sun.tools.javac.util.List<T> createList(List<T> d) {
        com.sun.tools.javac.util.List<T> l = com.sun.tools.javac.util.List.nil();
        for (ListIterator<T> iter = d.listIterator(d.size()); iter.hasPrevious(); )
            l = l.prepend(iter.previous());
        return l;
    }

    static <T> com.sun.tools.javac.util.List<T> createList(T... d) {
        com.sun.tools.javac.util.List<T> l = com.sun.tools.javac.util.List.nil();
        for (int i = d.length - 1; i >= 0; i--)
            l = l.prepend(d[i]);
        return l;
    }

    static <T> boolean equal(T t1, T t2) {
        return (t1 == null ? t2 == null : t1.equals(t2));
    }

    static <T> boolean equal(Iterator<T> iter1, Iterator<T> iter2) {
        if (iter1 == null || iter2 == null)
            return (iter1 == iter2);

        while (iter1.hasNext() && iter2.hasNext()) {
            if (!equal(iter1.next(), iter2.next()))
                return false;
        }

        return (!iter1.hasNext() && !iter2.hasNext());
    }

    static <T> boolean equal(ListIterator<T> iter1, ListIterator<T> iter2) {
        if (iter1 == null || iter2 == null)
            return (iter1 == iter2);

        if (iter1.previousIndex() != iter2.previousIndex())
            return false;

        while (iter1.hasPrevious() && iter2.hasPrevious()) {
            iter1.previous();
            iter2.previous();
        }

        return equal((Iterator<T>) iter1, (Iterator<T>) iter2);
    }

    static <T> boolean equal(T[] a1, T[] a2) {
        if (a1 == null || a2 == null)
            return (a1 == a2);

        if (a1.length != a2.length)
            return false;

        for (int i = 0; i < a1.length; i++) {
            if (!equal(a1[i], a2[i]))
                return false;
        }

        return true;
    }

    static <T> java.util.List<T> join(java.util.List<T>... lists) {
        java.util.List<T> r = new ArrayList<T>();
        for (java.util.List<T> l: lists)
            r.addAll(l);
        return r;
    }

    static <T> java.util.List<T> reverse(java.util.List<T> l) {
        java.util.List<T> r = new ArrayList<T>(l.size());
        for (T t: l)
            r.add(0, t);
        return r;
    }
}