< prev index next >

test/jdk/java/util/Collections/CheckedListBash.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2003, 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.
--- 1,7 ---
  /*
!  * Copyright (c) 2003, 2026, 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.

*** 21,16 ***
   * questions.
   */
  
  /*
   * @test
!  * @bug     4904067
   * @summary Unit test for Collections.checkedList
   * @author  Josh Bloch
   * @key randomness
   */
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.Iterator;
  import java.util.List;
--- 21,18 ---
   * questions.
   */
  
  /*
   * @test
!  * @bug     4904067 8336669
   * @summary Unit test for Collections.checkedList
   * @author  Josh Bloch
   * @key randomness
+  * @library /test/lib
   */
  
+ import jdk.test.lib.valueclass.VClass;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.Iterator;
  import java.util.List;

*** 107,11 ***
                  fail("set(int, Object) doesn't work");
          }
  
          List s = newList();
          for (int i=0; i<listSize; i++)
!             s.add(new Integer(i));
          if (s.size() != listSize)
              fail("Size of [0..n-1] != n");
  
          List even = clone(s);
          Iterator it = even.iterator();
--- 109,11 ---
                  fail("set(int, Object) doesn't work");
          }
  
          List s = newList();
          for (int i=0; i<listSize; i++)
!             s.add(i);
          if (s.size() != listSize)
              fail("Size of [0..n-1] != n");
  
          List even = clone(s);
          Iterator it = even.iterator();

*** 145,17 ***
              itAll.previous(); // ???
          }
          itAll = all.listIterator();
          while (itAll.hasNext()) {
              Integer i = (Integer)itAll.next();
!             itAll.set(new Integer(i.intValue()));
          }
-         itAll = all.listIterator();
-         it = s.iterator();
-         while (it.hasNext())
-             if (it.next()==itAll.next())
-                 fail("Iterator.set failed to change value.");
          if (!all.equals(s))
              fail("Failed to reconstruct ints with ListIterator.");
  
          it = all.listIterator();
          int i=0;
--- 147,12 ---
              itAll.previous(); // ???
          }
          itAll = all.listIterator();
          while (itAll.hasNext()) {
              Integer i = (Integer)itAll.next();
!             itAll.set(i);
          }
          if (!all.equals(s))
              fail("Failed to reconstruct ints with ListIterator.");
  
          it = all.listIterator();
          int i=0;

*** 179,16 ***
          ia = new Integer[listSize];
          Integer[] ib = (Integer[]) l.toArray(ia);
          if (ia != ib || !l.equals(Arrays.asList(ia)))
              fail("toArray(Object[]) is hosed (2)");
          ia = new Integer[listSize+1];
!         ia[listSize] = new Integer(69);
          ib = (Integer[]) l.toArray(ia);
          if (ia != ib || ia[listSize] != null
              || !l.equals(Arrays.asList(ia).subList(0, listSize)))
              fail("toArray(Object[]) is hosed (3)");
  
      }
  
      // Done inefficiently so as to exercise toArray
      static List clone(List s) {
          List a = Arrays.asList(s.toArray());
--- 176,17 ---
          ia = new Integer[listSize];
          Integer[] ib = (Integer[]) l.toArray(ia);
          if (ia != ib || !l.equals(Arrays.asList(ia)))
              fail("toArray(Object[]) is hosed (2)");
          ia = new Integer[listSize+1];
!         ia[listSize] = 69;
          ib = (Integer[]) l.toArray(ia);
          if (ia != ib || ia[listSize] != null
              || !l.equals(Arrays.asList(ia).subList(0, listSize)))
              fail("toArray(Object[]) is hosed (3)");
  
+         testValueCheckedList();
      }
  
      // Done inefficiently so as to exercise toArray
      static List clone(List s) {
          List a = Arrays.asList(s.toArray());

*** 228,6 ***
--- 226,21 ---
      }
  
      static void fail(String s) {
          throw new RuntimeException(s);
      }
+ 
+     static void testValueCheckedList() {
+         List<VClass> list = Collections.checkedList(new ArrayList<>(), VClass.class);
+         list.add(new VClass(1, new int[] { 1 }));
+         list.add(new VClass(2, new int[] { 2 }));
+         if (!list.contains(new VClass(1, new int[] { 1 })) || list.indexOf(new VClass(2, new int[] { 2 })) != 1)
+             fail("value checkedList lookup failed");
+         VClass[] a = list.toArray(new VClass[0]);
+         if (!Arrays.asList(a).equals(list))
+             fail("value checkedList toArray failed");
+         try {
+             ((List) list).add("not a Tuple");
+             fail("value checkedList accepted wrong type");
+         } catch (ClassCastException expected) { }
+     }
  }
< prev index next >