1 /*
   2  * Copyright (c) 2014, 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 /* @test
  25  * @bug 8014066
  26  * @summary Make sure that ArrayList#removeRange() method throws IOB when necessary
  27  */
  28 
  29 import java.util.ArrayList;
  30 
  31 public class RemoveRange extends ArrayList<Integer> {
  32     public static void main(String[] args) throws Throwable {
  33         RemoveRange list = new RemoveRange();
  34         for (int i = 0; i != 11; ++i)
  35             list.add(i);
  36 
  37         final int START_INDEX = -2;
  38         final int END_INDEX = list.size() + 2;
  39 
  40         for (int fromIndex = START_INDEX; fromIndex <= END_INDEX; ++fromIndex) {
  41             for (int toIndex = START_INDEX; toIndex <= END_INDEX; ++toIndex) {
  42                 if (fromIndex < 0
  43                         || toIndex > list.size()
  44                         || toIndex < fromIndex) {
  45                     // make sure the expected exception is thrown
  46                     test(list, fromIndex, toIndex);
  47                 } else {
  48                     // should not throw
  49                     // this include the case  list.removeRange(list.size(), list.size())
  50                     RemoveRange list1 = (RemoveRange)list.clone();
  51                     list1.removeRange(fromIndex, toIndex);
  52                 }
  53             }
  54         }
  55     }
  56 
  57     static void test(RemoveRange list, int fromIndex, int toIndex)
  58             throws Throwable {
  59         try {
  60             list.removeRange(fromIndex, toIndex);
  61             throw new AssertionError(
  62                     "ArrayList.removeRange(" + fromIndex + ", " + toIndex + "): "
  63                             + "no expected IndexOutOfBoundsException thrown");
  64         } catch (IndexOutOfBoundsException ok) {
  65         }
  66     }
  67 }