test/tools/javac/util/list/ListBufferTest.java

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
parent 0
959103a6100f
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

     1 /*
     2  * Copyright (c) 2012, 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 8004504
    27  * @summary Ensure that ListBuffer is working properly
    28  */
    30 import com.sun.tools.javac.util.List;
    31 import com.sun.tools.javac.util.ListBuffer;
    32 import java.util.Iterator;
    33 import java.util.Objects;
    35 public class ListBufferTest {
    36     public static void main(String... args) {
    37         testRemove();
    38         testCopiedEndsWithList_nil();
    39     }
    41     private static void testCopiedEndsWithList_nil() {
    42         ListBuffer<String> lb = new ListBuffer<>();
    44         lb.add("a");
    45         lb.add("b");
    46         lb.add("c");
    48         List<String> l1 = lb.toList();
    50         assertListEquals(l1, "a", "b", "c");
    51         assertEndsWithNil(l1);
    53         lb.add("d");
    55         List<String> l2 = lb.toList();
    56         assertListEquals(l2, "a", "b", "c", "d");
    57         assertEndsWithNil(l2);
    58         assertListEquals(l1, "a", "b", "c");
    59     }
    61     private static void testRemove() {
    62         ListBuffer<String> lb1 = new ListBuffer<>();
    64         lb1.add("a");
    65         lb1.add("b");
    66         lb1.add("c");
    68         assertListEquals(lb1.toList(), "a", "b", "c");
    69         assertEquals(lb1.next(), "a");
    70         assertListEquals(lb1.toList(), "b", "c");
    71         assertEquals(lb1.next(), "b");
    72         assertListEquals(lb1.toList(), "c");
    73         assertEquals(lb1.next(), "c");
    74         assertListEquals(lb1.toList());
    75         assertEquals(lb1.next(), null);
    77         lb1.add("d");
    79         assertEquals(lb1.next(), "d");
    80     }
    82     private static void assertEndsWithNil(List<?> list) {
    83         while (!list.isEmpty()) {
    84             list = list.tail;
    85         }
    87         if (list != List.nil()) throw new IllegalStateException("Not ending with List.nil()");
    88     }
    90     private static <T> void assertListEquals(Iterable<T> list, T... data) {
    91         int i = 0;
    92         Iterator<T> it = list.iterator();
    94         while (it.hasNext() && i < data.length) {
    95             assertEquals(it.next(), data[i++]);
    96         }
    98         if (it.hasNext()) {
    99             throw new IllegalStateException("Too many elements in the list");
   100         }
   102         if (i < data.length) {
   103             throw new IllegalStateException("Too few elements in the list");
   104         }
   105     }
   107     private static void assertEquals(Object expected, Object actual) {
   108         if (!Objects.equals(expected, actual)) {
   109             throw new IllegalStateException("Incorrect content. Expected: " + expected + ", actual: " + actual);
   110         }
   111     }
   112 }

mercurial