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