jdk/test/java/util/ArrayList/ArrayManagement.java
author martin
Mon, 25 Jan 2016 16:07:00 -0800
changeset 35383 61820dd2360e
parent 35312 4fbe776d28c5
child 42338 a60f280f803c
permissions -rw-r--r--
8148174: NegativeArraySizeException in Vector.grow(int) Summary: improve management of internal array Reviewed-by: smarks, forax
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35312
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
     1
/*
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
     2
 * Copyright 2016 Google, Inc.  All Rights Reserved.
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
     4
 *
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
     8
 *
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    13
 * accompanied this code).
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    14
 *
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    18
 *
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    21
 * questions.
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    22
 */
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    23
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    24
/*
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    25
 * @test
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    26
 * @bug 8146568
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    27
 * @summary brittle white box test of internal array management
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    28
 * @run testng ArrayManagement
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    29
 */
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    30
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    31
import java.lang.reflect.Field;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    32
import java.util.AbstractList;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    33
import java.util.ArrayList;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    34
import java.util.Collections;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    35
import java.util.List;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    36
import java.util.SplittableRandom;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    37
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    38
import org.testng.annotations.Test;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    39
import static org.testng.Assert.*;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    40
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    41
public class ArrayManagement {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    42
    static final int DEFAULT_CAPACITY = 10;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    43
    static final Field ELEMENT_DATA;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    44
    static final Field MODCOUNT;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    45
    static final SplittableRandom rnd = new SplittableRandom();
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    46
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    47
    static {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    48
        try {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    49
            ELEMENT_DATA = ArrayList.class.getDeclaredField("elementData");
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    50
            ELEMENT_DATA.setAccessible(true);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    51
            MODCOUNT = AbstractList.class.getDeclaredField("modCount");
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    52
            MODCOUNT.setAccessible(true);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    53
        } catch (ReflectiveOperationException huh) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    54
            throw new AssertionError(huh);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    55
        }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    56
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    57
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    58
    static Object[] elementData(ArrayList<?> list) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    59
        try {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    60
            return (Object[]) ELEMENT_DATA.get(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    61
        } catch (ReflectiveOperationException huh) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    62
            throw new AssertionError(huh);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    63
        }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    64
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    65
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    66
    static int modCount(ArrayList<?> list) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    67
        try {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    68
            return MODCOUNT.getInt(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    69
        } catch (ReflectiveOperationException huh) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    70
            throw new AssertionError(huh);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    71
        }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    72
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    73
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    74
    static int capacity(ArrayList<?> list) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    75
        return elementData(list).length;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    76
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    77
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    78
    static int newCapacity(int oldCapacity) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    79
        return oldCapacity + (oldCapacity >> 1);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    80
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    81
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    82
    static void ensureCapacity(ArrayList<Object> list, int capacity) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    83
        int oldCapacity = capacity(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    84
        int oldModCount = modCount(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    85
        list.ensureCapacity(capacity);
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 35312
diff changeset
    86
        assertTrue(capacity(list) >= capacity || capacity(list) == 0);
35312
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    87
        assertEquals(modCount(list),
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    88
                     (capacity(list) == oldCapacity)
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    89
                     ? oldModCount
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    90
                     : oldModCount + 1);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    91
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    92
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    93
    static List<Object> singletonList() {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    94
        return Collections.singletonList(Boolean.TRUE);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    95
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    96
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    97
    /** Opportunistically randomly test various add operations. */
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    98
    static void addOneElement(ArrayList<Object> list) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
    99
        int size = list.size();
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   100
        int modCount = modCount(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   101
        switch (rnd.nextInt(4)) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   102
        case 0: assertTrue(list.add(Boolean.TRUE)); break;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   103
        case 1: list.add(size, Boolean.TRUE); break;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   104
        case 2: assertTrue(list.addAll(singletonList())); break;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   105
        case 3: assertTrue(list.addAll(size, singletonList())); break;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   106
        default: throw new AssertionError();
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   107
        }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   108
        assertEquals(modCount(list), modCount + 1);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   109
        assertEquals(list.size(), size + 1);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   110
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   111
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   112
    @Test public void defaultCapacity() {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   113
        ArrayList<Object> list = new ArrayList<>();
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   114
        assertEquals(capacity(new ArrayList<Object>()), 0);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   115
        for (int i = 0; i < DEFAULT_CAPACITY; i++) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   116
            addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   117
            assertEquals(capacity(list), DEFAULT_CAPACITY);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   118
        }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   119
        addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   120
        assertEquals(capacity(list), newCapacity(DEFAULT_CAPACITY));
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   121
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   122
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   123
    @Test public void defaultCapacityEnsureCapacity() {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   124
        ArrayList<Object> list = new ArrayList<>();
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   125
        for (int i = 0; i <= DEFAULT_CAPACITY; i++) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   126
            ensureCapacity(list, i);     // no-op!
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   127
            assertEquals(capacity(list), 0);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   128
        }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   129
        for (int i = 0; i < DEFAULT_CAPACITY; i++) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   130
            addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   131
            assertEquals(capacity(list), DEFAULT_CAPACITY);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   132
        }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   133
        addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   134
        assertEquals(capacity(list), newCapacity(DEFAULT_CAPACITY));
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   135
        {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   136
            int capacity = capacity(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   137
            ensureCapacity(list, capacity + 1);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   138
            assertEquals(capacity(list), newCapacity(capacity));
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   139
        }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   140
        {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   141
            int capacity = capacity(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   142
            ensureCapacity(list, 3 * capacity);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   143
            assertEquals(capacity(list), 3 * capacity);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   144
        }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   145
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   146
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   147
    @Test public void ensureCapacityBeyondDefaultCapacity() {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   148
        ArrayList<Object> list = new ArrayList<>();
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   149
        list.ensureCapacity(DEFAULT_CAPACITY + 1);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   150
        assertEquals(capacity(list), DEFAULT_CAPACITY + 1);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   151
        for (int i = 0; i < DEFAULT_CAPACITY + 1; i++) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   152
            addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   153
            assertEquals(capacity(list), DEFAULT_CAPACITY + 1);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   154
        }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   155
        addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   156
        assertEquals(capacity(list), newCapacity(DEFAULT_CAPACITY + 1));
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   157
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   158
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   159
    @Test public void explicitZeroCapacity() {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   160
        ArrayList<Object> list = new ArrayList<>(0);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   161
        assertEquals(capacity(list), 0);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   162
        addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   163
        assertEquals(capacity(list), 1);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   164
        addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   165
        assertEquals(capacity(list), 2);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   166
        addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   167
        assertEquals(capacity(list), 3);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   168
        addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   169
        assertEquals(capacity(list), 4);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   170
        addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   171
        assertEquals(capacity(list), 6);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   172
        addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   173
        assertEquals(capacity(list), 6);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   174
        addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   175
        assertEquals(capacity(list), 9);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   176
        list.clear();
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   177
        assertEquals(capacity(list), 9);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   178
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   179
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   180
    @Test public void explicitLargeCapacity() {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   181
        int n = DEFAULT_CAPACITY * 3;
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   182
        ArrayList<Object> list = new ArrayList<>(n);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   183
        assertEquals(capacity(list), n);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   184
        ensureCapacity(list, 0);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   185
        ensureCapacity(list, n);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   186
        for (int i = 0; i < n; i++) addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   187
        assertEquals(capacity(list), n);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   188
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   189
        addOneElement(list);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   190
        assertEquals(capacity(list), newCapacity(n));
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   191
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   192
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   193
    @Test public void emptyArraysAreShared() {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   194
        assertSame(elementData(new ArrayList<Object>()),
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   195
                   elementData(new ArrayList<Object>()));
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   196
        assertSame(elementData(new ArrayList<Object>(0)),
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   197
                   elementData(new ArrayList<Object>(0)));
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   198
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   199
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   200
    @Test public void emptyArraysDifferBetweenDefaultAndExplicit() {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   201
        assertNotSame(elementData(new ArrayList<Object>()),
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   202
                      elementData(new ArrayList<Object>(0)));
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   203
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   204
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   205
    @Test public void negativeCapacity() {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   206
        for (int capacity : new int[] { -1, Integer.MIN_VALUE }) {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   207
            try {
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   208
                new ArrayList<Object>(capacity);
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   209
                fail("should throw");
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   210
            } catch (IllegalArgumentException success) {}
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   211
        }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   212
    }
4fbe776d28c5 8146568: NegativeArraySizeException in ArrayList.grow(int)
martin
parents:
diff changeset
   213
}