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

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/util/list/ListBufferTest.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,112 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8004504
    1.30 + * @summary Ensure that ListBuffer is working properly
    1.31 + */
    1.32 +
    1.33 +import com.sun.tools.javac.util.List;
    1.34 +import com.sun.tools.javac.util.ListBuffer;
    1.35 +import java.util.Iterator;
    1.36 +import java.util.Objects;
    1.37 +
    1.38 +public class ListBufferTest {
    1.39 +    public static void main(String... args) {
    1.40 +        testRemove();
    1.41 +        testCopiedEndsWithList_nil();
    1.42 +    }
    1.43 +
    1.44 +    private static void testCopiedEndsWithList_nil() {
    1.45 +        ListBuffer<String> lb = new ListBuffer<>();
    1.46 +
    1.47 +        lb.add("a");
    1.48 +        lb.add("b");
    1.49 +        lb.add("c");
    1.50 +
    1.51 +        List<String> l1 = lb.toList();
    1.52 +
    1.53 +        assertListEquals(l1, "a", "b", "c");
    1.54 +        assertEndsWithNil(l1);
    1.55 +
    1.56 +        lb.add("d");
    1.57 +
    1.58 +        List<String> l2 = lb.toList();
    1.59 +        assertListEquals(l2, "a", "b", "c", "d");
    1.60 +        assertEndsWithNil(l2);
    1.61 +        assertListEquals(l1, "a", "b", "c");
    1.62 +    }
    1.63 +
    1.64 +    private static void testRemove() {
    1.65 +        ListBuffer<String> lb1 = new ListBuffer<>();
    1.66 +
    1.67 +        lb1.add("a");
    1.68 +        lb1.add("b");
    1.69 +        lb1.add("c");
    1.70 +
    1.71 +        assertListEquals(lb1.toList(), "a", "b", "c");
    1.72 +        assertEquals(lb1.next(), "a");
    1.73 +        assertListEquals(lb1.toList(), "b", "c");
    1.74 +        assertEquals(lb1.next(), "b");
    1.75 +        assertListEquals(lb1.toList(), "c");
    1.76 +        assertEquals(lb1.next(), "c");
    1.77 +        assertListEquals(lb1.toList());
    1.78 +        assertEquals(lb1.next(), null);
    1.79 +
    1.80 +        lb1.add("d");
    1.81 +
    1.82 +        assertEquals(lb1.next(), "d");
    1.83 +    }
    1.84 +
    1.85 +    private static void assertEndsWithNil(List<?> list) {
    1.86 +        while (!list.isEmpty()) {
    1.87 +            list = list.tail;
    1.88 +        }
    1.89 +
    1.90 +        if (list != List.nil()) throw new IllegalStateException("Not ending with List.nil()");
    1.91 +    }
    1.92 +
    1.93 +    private static <T> void assertListEquals(Iterable<T> list, T... data) {
    1.94 +        int i = 0;
    1.95 +        Iterator<T> it = list.iterator();
    1.96 +
    1.97 +        while (it.hasNext() && i < data.length) {
    1.98 +            assertEquals(it.next(), data[i++]);
    1.99 +        }
   1.100 +
   1.101 +        if (it.hasNext()) {
   1.102 +            throw new IllegalStateException("Too many elements in the list");
   1.103 +        }
   1.104 +
   1.105 +        if (i < data.length) {
   1.106 +            throw new IllegalStateException("Too few elements in the list");
   1.107 +        }
   1.108 +    }
   1.109 +
   1.110 +    private static void assertEquals(Object expected, Object actual) {
   1.111 +        if (!Objects.equals(expected, actual)) {
   1.112 +            throw new IllegalStateException("Incorrect content. Expected: " + expected + ", actual: " + actual);
   1.113 +        }
   1.114 +    }
   1.115 +}

mercurial