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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8004504
aoqi@0 27 * @summary Ensure that ListBuffer is working properly
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import com.sun.tools.javac.util.List;
aoqi@0 31 import com.sun.tools.javac.util.ListBuffer;
aoqi@0 32 import java.util.Iterator;
aoqi@0 33 import java.util.Objects;
aoqi@0 34
aoqi@0 35 public class ListBufferTest {
aoqi@0 36 public static void main(String... args) {
aoqi@0 37 testRemove();
aoqi@0 38 testCopiedEndsWithList_nil();
aoqi@0 39 }
aoqi@0 40
aoqi@0 41 private static void testCopiedEndsWithList_nil() {
aoqi@0 42 ListBuffer<String> lb = new ListBuffer<>();
aoqi@0 43
aoqi@0 44 lb.add("a");
aoqi@0 45 lb.add("b");
aoqi@0 46 lb.add("c");
aoqi@0 47
aoqi@0 48 List<String> l1 = lb.toList();
aoqi@0 49
aoqi@0 50 assertListEquals(l1, "a", "b", "c");
aoqi@0 51 assertEndsWithNil(l1);
aoqi@0 52
aoqi@0 53 lb.add("d");
aoqi@0 54
aoqi@0 55 List<String> l2 = lb.toList();
aoqi@0 56 assertListEquals(l2, "a", "b", "c", "d");
aoqi@0 57 assertEndsWithNil(l2);
aoqi@0 58 assertListEquals(l1, "a", "b", "c");
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 private static void testRemove() {
aoqi@0 62 ListBuffer<String> lb1 = new ListBuffer<>();
aoqi@0 63
aoqi@0 64 lb1.add("a");
aoqi@0 65 lb1.add("b");
aoqi@0 66 lb1.add("c");
aoqi@0 67
aoqi@0 68 assertListEquals(lb1.toList(), "a", "b", "c");
aoqi@0 69 assertEquals(lb1.next(), "a");
aoqi@0 70 assertListEquals(lb1.toList(), "b", "c");
aoqi@0 71 assertEquals(lb1.next(), "b");
aoqi@0 72 assertListEquals(lb1.toList(), "c");
aoqi@0 73 assertEquals(lb1.next(), "c");
aoqi@0 74 assertListEquals(lb1.toList());
aoqi@0 75 assertEquals(lb1.next(), null);
aoqi@0 76
aoqi@0 77 lb1.add("d");
aoqi@0 78
aoqi@0 79 assertEquals(lb1.next(), "d");
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 private static void assertEndsWithNil(List<?> list) {
aoqi@0 83 while (!list.isEmpty()) {
aoqi@0 84 list = list.tail;
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 if (list != List.nil()) throw new IllegalStateException("Not ending with List.nil()");
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 private static <T> void assertListEquals(Iterable<T> list, T... data) {
aoqi@0 91 int i = 0;
aoqi@0 92 Iterator<T> it = list.iterator();
aoqi@0 93
aoqi@0 94 while (it.hasNext() && i < data.length) {
aoqi@0 95 assertEquals(it.next(), data[i++]);
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 if (it.hasNext()) {
aoqi@0 99 throw new IllegalStateException("Too many elements in the list");
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 if (i < data.length) {
aoqi@0 103 throw new IllegalStateException("Too few elements in the list");
aoqi@0 104 }
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 private static void assertEquals(Object expected, Object actual) {
aoqi@0 108 if (!Objects.equals(expected, actual)) {
aoqi@0 109 throw new IllegalStateException("Incorrect content. Expected: " + expected + ", actual: " + actual);
aoqi@0 110 }
aoqi@0 111 }
aoqi@0 112 }

mercurial