< prev index next >

test/jdk/java/util/Collections/FindSubList.java

Print this page

  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.ArrayList;
 31 import java.util.Arrays;
 32 import java.util.Collections;
 33 import java.util.LinkedList;
 34 import java.util.List;
 35 import java.util.Vector;
 36 
 37 public class FindSubList {

 38     public static void main(String[] args) throws Exception {
 39         int N = 500;
 40         List source = new ArrayList(3 * N);
 41         List[] target = new List[N+1];
 42         int[] index = new int[N+1];
 43         for (int i=0; i<=N; i++) {
 44             List t = new ArrayList();
 45             String s = Integer.toString(i, 2);
 46             for (int j=0, len = s.length(); j<len; j++)
 47                 t.add(s.charAt(j)=='1' ? "1" : "0");
 48             target[i] = t;
 49             if (i == N) {
 50                 index[i] = -1;
 51             } else {
 52                 index[i] = source.size();
 53                 source.addAll(t);
 54                 source.add("*");
 55             }
 56         }
 57 

 86         }
 87         List[] src2 = {
 88             source,
 89             new LinkedList(source),
 90             new Vector(source),
 91             Arrays.asList(source.toArray())
 92         };
 93         for (int j=0; j<src2.length; j++) {
 94             List s = src2[j];
 95 
 96             for (int i=0; i<=N; i++) {
 97                 int idx = Collections.lastIndexOfSubList(s, target[i]);
 98                 if (idx != index[i])
 99                     throw new Exception(s.getClass()+" lastIdexOfSubList: "+i +
100                                         "is " + idx + ", should be "+index[i]);
101             }
102             if (Collections.indexOfSubList(s,Collections.nCopies(2*s.size(),
103                                                                  "0")) != -1)
104               throw new Exception(s.getClass()+" lastIndexOfSubList: big tgt");
105         }







106     }
107 }

  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 

 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 }
< prev index next >