Mon, 26 Oct 2015 13:23:30 -0700
Added tag jdk8u76-b00 for changeset 10ffafaf5340
1 /*
2 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /*
25 * @test
26 * @bug 6267067 6351336 6389198
27 * @summary unit test for javac List
28 */
30 import java.util.*;
31 import com.sun.tools.javac.util.List;
33 public class TList {
34 public static void main(String[] args) {
35 new TList().run();
36 }
38 String[][] data = {
39 { },
40 { "1" },
41 { "1", "2" },
42 { "1", "2" }, // different but equal
43 { "1", "2", "3", "4", "X", "X", "X", "8", "9", "10" } // contains duplicates
44 };
46 Map<java.util.List<String>,List<String>> examples;
48 void run() {
49 examples = new LinkedHashMap<java.util.List<String>,List<String>>();
50 for (String[] values: data)
51 examples.put(Arrays.asList(values), createList(values));
53 // 6351336: com.sun.tools.javac.util.List shouldn't extend java.util.AbstractList
54 test_AbstractList();
56 // general unit tests for java.util.List methods, including...
57 // 6389198: com.sun.tools.javac.util.List.equals() violates java.util.List.equals() contract
58 test_add_E();
59 test_add_int_E();
60 test_addAll_Collection();
61 test_addAll_int_Collection();
62 test_clear();
63 test_contains_Object();
64 test_contains_All();
65 test_equals_Object();
66 test_get_int();
67 test_hashCode();
68 test_indexOf_Object();
69 test_isEmpty();
70 test_iterator();
71 test_lastIndexOf_Object();
72 test_listIterator();
73 test_listIterator_int();
74 test_remove_int();
75 test_remove_Object();
76 test_removeAll_Collection();
77 test_retainAll_Collection();
78 test_set_int_E();
79 test_size();
80 test_subList_int_int();
81 test_toArray();
82 test_toArray_TArray();
84 // tests for additional methods
85 test_prependList_List();
86 test_reverse();
87 }
89 // 6351336
90 void test_AbstractList() {
91 System.err.println("test AbstractList");
92 if (AbstractList.class.isAssignableFrom(List.class))
93 throw new AssertionError();
94 }
96 void test_add_E() {
97 System.err.println("test add(E)");
98 for (List<String> l: examples.values()) {
99 try {
100 l.add("test");
101 throw new AssertionError();
102 } catch (UnsupportedOperationException ex) {
103 }
104 }
105 }
107 void test_add_int_E() {
108 System.err.println("test add(int,E)");
109 for (List<String> l: examples.values()) {
110 try {
111 l.add(0, "test");
112 throw new AssertionError();
113 } catch (UnsupportedOperationException ex) {
114 }
115 }
116 }
118 void test_addAll_Collection() {
119 System.err.println("test addAll(Collection)");
120 for (List<String> l: examples.values()) {
121 int l_size = l.size();
122 for (java.util.List<String> arg: examples.keySet()) {
123 try {
124 boolean modified = l.addAll(arg);
125 if (modified)
126 throw new AssertionError();
127 } catch (UnsupportedOperationException e) {
128 }
129 if (l.size() != l_size)
130 throw new AssertionError();
131 }
132 }
133 }
135 void test_addAll_int_Collection() {
136 System.err.println("test addAll(int,Collection)");
137 for (List<String> l: examples.values()) {
138 int l_size = l.size();
139 for (java.util.List<String> arg: examples.keySet()) {
140 try {
141 boolean modified = l.addAll(0, arg);
142 if (modified)
143 throw new AssertionError();
144 } catch (UnsupportedOperationException e) {
145 }
146 if (l.size() != l_size)
147 throw new AssertionError();
148 }
149 }
150 }
152 void test_clear() {
153 System.err.println("test clear()");
154 for (List<String> l: examples.values()) {
155 int l_size = l.size();
156 try {
157 l.clear();
158 if (l_size > 0)
159 throw new AssertionError();
160 } catch (UnsupportedOperationException e) {
161 }
162 if (l.size() != l_size)
163 throw new AssertionError();
164 }
165 }
167 void test_contains_Object() {
168 System.err.println("test contains(Object)");
169 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
170 java.util.List<String> ref = e.getKey();
171 List<String> l = e.getValue();
172 boolean expect = ref.contains("1");
173 boolean found = l.contains("1");
174 if (expect != found)
175 throw new AssertionError();
176 }
177 }
179 void test_contains_All() {
180 System.err.println("test containsAll()");
181 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
182 java.util.List<String> ref = e.getKey();
183 List<String> l = e.getValue();
184 for (java.util.List<String> arg: examples.keySet()) {
185 boolean expect = ref.containsAll(arg);
186 boolean found = l.containsAll(arg);
187 if (expect != found)
188 throw new AssertionError();
189 }
190 }
191 }
193 // 6389198
194 void test_equals_Object() {
195 System.err.println("test equals(Object)");
196 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
197 java.util.List<String> ref = e.getKey();
198 List<String> l = e.getValue();
199 for (java.util.List<String> arg: examples.keySet()) {
200 boolean expect = ref.equals(arg);
201 boolean found = l.equals(arg);
202 if (expect != found) {
203 System.err.println("ref: " + ref);
204 System.err.println("l: " + l);
205 System.err.println("arg: " + arg);
206 System.err.println("expect: " + expect + ", found: " + found);
207 throw new AssertionError();
208 }
209 }
210 }
211 }
213 void test_get_int() {
214 System.err.println("test get(int)");
215 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
216 java.util.List<String> ref = e.getKey();
217 List<String> l = e.getValue();
218 for (int i = -1; i <= ref.size(); i++) {
219 boolean expectException = i < 0 || i >= ref.size();
220 String expectValue = (expectException ? null : ref.get(i));
221 try {
222 String foundValue = l.get(i);
223 if (expectException || !equal(expectValue, foundValue))
224 throw new AssertionError();
225 } catch (IndexOutOfBoundsException ex) {
226 if (!expectException)
227 throw new AssertionError();
228 }
229 }
230 }
231 }
233 void test_hashCode() {
234 System.err.println("test hashCode()");
235 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
236 java.util.List<String> ref = e.getKey();
237 List<String> l = e.getValue();
238 long expect = ref.hashCode();
239 long found = l.hashCode();
240 if (expect != found) {
241 System.err.println("ref: " + ref);
242 System.err.println("l: " + l);
243 System.err.println("expect: " + expect);
244 System.err.println("found: " + found);
245 throw new AssertionError();
246 }
247 }
248 }
250 void test_indexOf_Object() {
251 System.err.println("test indexOf(Object)");
252 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
253 java.util.List<String> ref = e.getKey();
254 List<String> l = e.getValue();
255 for (int i = -1; i < ref.size(); i++) {
256 String arg = (i == -1 ? "NOT IN LIST" : ref.get(i));
257 int expect = ref.indexOf(arg);
258 int found = l.indexOf(arg);
259 if (expect != found)
260 throw new AssertionError();
261 }
262 }
263 }
265 void test_isEmpty() {
266 System.err.println("test isEmpty()");
267 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
268 java.util.List<String> ref = e.getKey();
269 List<String> l = e.getValue();
270 boolean expect = ref.isEmpty();
271 boolean found = l.isEmpty();
272 if (expect != found)
273 throw new AssertionError();
274 }
275 }
277 void test_iterator() {
278 System.err.println("test iterator()");
279 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
280 java.util.List<String> ref = e.getKey();
281 List<String> l = e.getValue();
282 if (!equal(l.iterator(), ref.iterator()))
283 throw new AssertionError();
284 }
285 }
287 void test_lastIndexOf_Object() {
288 System.err.println("test lastIndexOf(Object)");
289 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
290 java.util.List<String> ref = e.getKey();
291 List<String> l = e.getValue();
292 for (int i = -1; i < ref.size(); i++) {
293 String arg = (i == -1 ? "NOT IN LIST" : ref.get(i));
294 int expect = ref.lastIndexOf(arg);
295 int found = l.lastIndexOf(arg);
296 if (expect != found)
297 throw new AssertionError();
298 }
299 }
300 }
302 void test_listIterator() {
303 System.err.println("test listIterator()");
304 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
305 java.util.List<String> ref = e.getKey();
306 List<String> l = e.getValue();
307 if (!equal(l.listIterator(), ref.listIterator()))
308 throw new AssertionError();
309 }
310 }
312 void test_listIterator_int() {
313 System.err.println("test listIterator(int)");
314 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
315 java.util.List<String> ref = e.getKey();
316 List<String> l = e.getValue();
317 for (int i = 0; i < ref.size(); i++) {
318 if (!equal(l.listIterator(i), ref.listIterator(i)))
319 throw new AssertionError();
320 }
321 }
322 }
324 void test_remove_int() {
325 System.err.println("test remove(int)");
326 for (List<String> l: examples.values()) {
327 try {
328 l.remove(0);
329 throw new AssertionError();
330 } catch (UnsupportedOperationException ex) {
331 }
332 }
333 }
335 void test_remove_Object() {
336 System.err.println("test remove(Object)");
337 for (List<String> l: examples.values()) {
338 boolean hasX = l.contains("X");
339 try {
340 l.remove("X");
341 if (hasX)
342 throw new AssertionError();
343 } catch (UnsupportedOperationException ex) {
344 }
345 }
346 }
348 void test_removeAll_Collection() {
349 System.err.println("test removeAll(Collection)");
350 for (List<String> l: examples.values()) {
351 int l_size = l.size();
352 for (java.util.List<String> arg: examples.keySet()) {
353 try {
354 boolean modified = l.removeAll(arg);
355 if (modified)
356 throw new AssertionError();
357 } catch (UnsupportedOperationException e) {
358 }
359 if (l.size() != l_size)
360 throw new AssertionError();
361 }
362 }
363 }
365 void test_retainAll_Collection() {
366 System.err.println("test retainAll(Collection)");
367 for (List<String> l: examples.values()) {
368 int l_size = l.size();
369 for (java.util.List<String> arg: examples.keySet()) {
370 try {
371 boolean modified = l.retainAll(arg);
372 if (modified)
373 throw new AssertionError();
374 } catch (UnsupportedOperationException e) {
375 }
376 if (l.size() != l_size)
377 throw new AssertionError();
378 }
379 }
380 }
382 void test_set_int_E() {
383 System.err.println("test set(int,E)");
384 for (List<String> l: examples.values()) {
385 try {
386 l.set(0, "X");
387 throw new AssertionError();
388 } catch (UnsupportedOperationException ex) {
389 }
390 }
391 }
393 void test_size() {
394 System.err.println("test size()");
395 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
396 java.util.List<String> ref = e.getKey();
397 List<String> l = e.getValue();
398 int expect = ref.size();
399 int found = l.size();
400 if (expect != found)
401 throw new AssertionError();
402 }
403 }
405 void test_subList_int_int() {
406 System.err.println("test subList(int,int)");
407 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
408 java.util.List<String> ref = e.getKey();
409 List<String> l = e.getValue();
410 for (int lwb = 0; lwb < ref.size(); lwb++) {
411 for (int upb = lwb; upb <= ref.size(); upb++) {
412 if (!equal(l.subList(lwb, upb), ref.subList(lwb,upb)))
413 throw new AssertionError();
414 }
415 }
416 }
417 }
419 void test_toArray() {
420 System.err.println("test toArray()");
421 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
422 java.util.List<String> ref = e.getKey();
423 List<String> l = e.getValue();
424 if (!equal(l.toArray(), ref.toArray()))
425 throw new AssertionError();
426 }
427 }
429 void test_toArray_TArray() {
430 System.err.println("test toArray(E[])");
431 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
432 java.util.List<String> ref = e.getKey();
433 List<String> l = e.getValue();
434 if (!equal(l.toArray(new String[0]), ref.toArray(new String[0])))
435 throw new AssertionError();
436 }
437 }
439 void test_prependList_List() {
440 System.err.println("test prependList(List<E>)");
441 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
442 java.util.List<String> ref = e.getKey();
443 List<String> l = e.getValue();
444 for (Map.Entry<java.util.List<String>, List<String>> arg_e: examples.entrySet()) {
445 java.util.List<String> arg_ref = arg_e.getKey();
446 List<String> arg = arg_e.getValue();
447 java.util.List<String> expect = join(arg, ref);
448 List<String> found = l.prependList(arg);
449 // verify results, and that original and arg lists are unchanged
450 if (!equal(expect, found)) {
451 System.err.println("ref: " + ref);
452 System.err.println("l: " + l);
453 System.err.println("arg: " + arg);
454 System.err.println("expect: " + expect);
455 System.err.println("found: " + found);
456 throw new AssertionError();
457 }
458 if (!equal(l, ref))
459 throw new AssertionError();
460 if (!equal(arg, arg_ref))
461 throw new AssertionError();
462 }
463 }
464 }
466 void test_reverse() {
467 System.err.println("test reverse()");
468 for (Map.Entry<java.util.List<String>,List<String>> e: examples.entrySet()) {
469 java.util.List<String> ref = e.getKey();
470 List<String> l = e.getValue();
471 java.util.List<String> expect = reverse(ref);
472 List<String> found = l.reverse();
473 if (l.size() < 2 && found != l) // reverse of empty or singleton list is itself
474 throw new AssertionError();
475 if (!equal(l, ref)) // orginal should be unchanged
476 throw new AssertionError();
477 if (!equal(expect, found))
478 throw new AssertionError();
479 }
480 }
482 static <T> com.sun.tools.javac.util.List<T> createList(List<T> d) {
483 com.sun.tools.javac.util.List<T> l = com.sun.tools.javac.util.List.nil();
484 for (ListIterator<T> iter = d.listIterator(d.size()); iter.hasPrevious(); )
485 l = l.prepend(iter.previous());
486 return l;
487 }
489 static <T> com.sun.tools.javac.util.List<T> createList(T... d) {
490 com.sun.tools.javac.util.List<T> l = com.sun.tools.javac.util.List.nil();
491 for (int i = d.length - 1; i >= 0; i--)
492 l = l.prepend(d[i]);
493 return l;
494 }
496 static <T> boolean equal(T t1, T t2) {
497 return (t1 == null ? t2 == null : t1.equals(t2));
498 }
500 static <T> boolean equal(Iterator<T> iter1, Iterator<T> iter2) {
501 if (iter1 == null || iter2 == null)
502 return (iter1 == iter2);
504 while (iter1.hasNext() && iter2.hasNext()) {
505 if (!equal(iter1.next(), iter2.next()))
506 return false;
507 }
509 return (!iter1.hasNext() && !iter2.hasNext());
510 }
512 static <T> boolean equal(ListIterator<T> iter1, ListIterator<T> iter2) {
513 if (iter1 == null || iter2 == null)
514 return (iter1 == iter2);
516 if (iter1.previousIndex() != iter2.previousIndex())
517 return false;
519 while (iter1.hasPrevious() && iter2.hasPrevious()) {
520 iter1.previous();
521 iter2.previous();
522 }
524 return equal((Iterator<T>) iter1, (Iterator<T>) iter2);
525 }
527 static <T> boolean equal(T[] a1, T[] a2) {
528 if (a1 == null || a2 == null)
529 return (a1 == a2);
531 if (a1.length != a2.length)
532 return false;
534 for (int i = 0; i < a1.length; i++) {
535 if (!equal(a1[i], a2[i]))
536 return false;
537 }
539 return true;
540 }
542 static <T> java.util.List<T> join(java.util.List<T>... lists) {
543 java.util.List<T> r = new ArrayList<T>();
544 for (java.util.List<T> l: lists)
545 r.addAll(l);
546 return r;
547 }
549 static <T> java.util.List<T> reverse(java.util.List<T> l) {
550 java.util.List<T> r = new ArrayList<T>(l.size());
551 for (T t: l)
552 r.add(0, t);
553 return r;
554 }
555 }