Path: blob/master/test/jdk/java/util/Collections/FindSubList.java
41149 views
/*1* Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 432307426* @summary Basic test for Collections.indexOfSubList/lastIndexOfSubList27*/2829import java.util.ArrayList;30import java.util.Arrays;31import java.util.Collections;32import java.util.LinkedList;33import java.util.List;34import java.util.Vector;3536public class FindSubList {37public static void main(String[] args) throws Exception {38int N = 500;39List source = new ArrayList(3 * N);40List[] target = new List[N+1];41int[] index = new int[N+1];42for (int i=0; i<=N; i++) {43List t = new ArrayList();44String s = Integer.toString(i, 2);45for (int j=0, len = s.length(); j<len; j++)46t.add(s.charAt(j)=='1' ? "1" : "0");47target[i] = t;48if (i == N) {49index[i] = -1;50} else {51index[i] = source.size();52source.addAll(t);53source.add("*");54}55}5657List[] src = {58source,59new LinkedList(source),60new Vector(source),61Arrays.asList(source.toArray())62};63for (int j=0; j<src.length; j++) {64List s = src[j];6566for (int i=0; i<=N; i++) {67int idx = Collections.indexOfSubList(s, target[i]);68if (idx != index[i])69throw new Exception(s.getClass()+" indexOfSubList: " + i +70"is " + idx + ", should be "+index[i]);71}7273if (Collections.indexOfSubList(s,Collections.nCopies(2*s.size(),74"0")) != -1)75throw new Exception(s.getClass()+" indexOfSubList: big target");7677}7879Collections.reverse(source);80int srcSize = source.size();81for (int i=0; i<=N; i++) {82Collections.reverse(target[i]);83if (i != N)84index[i] = srcSize - index[i] - target[i].size();85}86List[] src2 = {87source,88new LinkedList(source),89new Vector(source),90Arrays.asList(source.toArray())91};92for (int j=0; j<src2.length; j++) {93List s = src2[j];9495for (int i=0; i<=N; i++) {96int idx = Collections.lastIndexOfSubList(s, target[i]);97if (idx != index[i])98throw new Exception(s.getClass()+" lastIdexOfSubList: "+i +99"is " + idx + ", should be "+index[i]);100}101if (Collections.indexOfSubList(s,Collections.nCopies(2*s.size(),102"0")) != -1)103throw new Exception(s.getClass()+" lastIndexOfSubList: big tgt");104}105}106}107108109