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