1 /*
2 * Copyright (c) 2000, 2026, 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 4323074
27 * @summary Basic test for Collections.indexOfSubList/lastIndexOfSubList
28 * @library /test/lib
29 */
30
31 import jdk.test.lib.valueclass.VClass;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.LinkedList;
36 import java.util.List;
37 import java.util.Vector;
38
39 public class FindSubList {
40
41 public static void main(String[] args) throws Exception {
42 int N = 500;
43 List source = new ArrayList(3 * N);
44 List[] target = new List[N+1];
45 int[] index = new int[N+1];
46 for (int i=0; i<=N; i++) {
47 List t = new ArrayList();
48 String s = Integer.toString(i, 2);
49 for (int j=0, len = s.length(); j<len; j++)
50 t.add(s.charAt(j)=='1' ? "1" : "0");
51 target[i] = t;
52 if (i == N) {
53 index[i] = -1;
54 } else {
55 index[i] = source.size();
56 source.addAll(t);
57 source.add("*");
58 }
59 }
60
61 List[] src = {
62 source,
63 new LinkedList(source),
64 new Vector(source),
65 Arrays.asList(source.toArray())
66 };
67 for (int j=0; j<src.length; j++) {
68 List s = src[j];
69
70 for (int i=0; i<=N; i++) {
71 int idx = Collections.indexOfSubList(s, target[i]);
72 if (idx != index[i])
73 throw new Exception(s.getClass()+" indexOfSubList: " + i +
74 "is " + idx + ", should be "+index[i]);
75 }
76
77 if (Collections.indexOfSubList(s,Collections.nCopies(2*s.size(),
78 "0")) != -1)
79 throw new Exception(s.getClass()+" indexOfSubList: big target");
80
81 }
82
83 Collections.reverse(source);
84 int srcSize = source.size();
85 for (int i=0; i<=N; i++) {
86 Collections.reverse(target[i]);
87 if (i != N)
88 index[i] = srcSize - index[i] - target[i].size();
89 }
90 List[] src2 = {
91 source,
92 new LinkedList(source),
93 new Vector(source),
94 Arrays.asList(source.toArray())
95 };
96 for (int j=0; j<src2.length; j++) {
97 List s = src2[j];
98
99 for (int i=0; i<=N; i++) {
100 int idx = Collections.lastIndexOfSubList(s, target[i]);
101 if (idx != index[i])
102 throw new Exception(s.getClass()+" lastIdexOfSubList: "+i +
103 "is " + idx + ", should be "+index[i]);
104 }
105 if (Collections.indexOfSubList(s,Collections.nCopies(2*s.size(),
106 "0")) != -1)
107 throw new Exception(s.getClass()+" lastIndexOfSubList: big tgt");
108 }
109
110 List<VClass> vsource = Arrays.asList(new VClass(1, new int[] { 1 }), new VClass(2, new int[] { 2 }), new VClass(3, new int[] { 3 }), new VClass(2, new int[] { 2 }), new VClass(3, new int[] { 3 }));
111 List<VClass> vtarget = Arrays.asList(new VClass(2, new int[] { 2 }), new VClass(3, new int[] { 3 }));
112 if (Collections.indexOfSubList(vsource, vtarget) != 1)
113 throw new Exception("value indexOfSubList failed");
114 if (Collections.lastIndexOfSubList(vsource, vtarget) != 3)
115 throw new Exception("value lastIndexOfSubList failed");
116 }
117 }