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