1 /* 2 * Copyright 2016 Google, Inc. 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 8148174 27 * @summary brittle white box test of internal array management 28 * @run testng ArrayManagement 29 */ 30 31 package test.java.util.Vector; 32 33 import java.lang.reflect.Field; 34 import java.util.AbstractList; 35 import java.util.Vector; 36 import java.util.Collections; 37 import java.util.List; 38 import java.util.SplittableRandom; 39 40 import org.testng.annotations.Test; 41 import static org.testng.Assert.*; 42 43 public class ArrayManagement { 44 45 /** 46 * A Vector that exposes all protected elements, and checks class 47 * invariants. 48 */ 49 static class PublicVector<E> extends Vector<E> { PublicVector()50 public PublicVector() { super(); } PublicVector(int capacity)51 public PublicVector(int capacity) { super(capacity); } PublicVector(int capacity, int capacityIncrement)52 public PublicVector(int capacity, int capacityIncrement) { 53 super(capacity, capacityIncrement); 54 } elementData()55 public Object[] elementData() { return elementData; } modCount()56 public int modCount() { return modCount; } capacityIncrement()57 public int capacityIncrement() { return capacityIncrement; } capacity()58 public int capacity() { return elementData.length; } 59 ensureCapacity(int minCapacity)60 public void ensureCapacity(int minCapacity) { 61 int oldCapacity = capacity(); 62 int oldModCount = modCount(); 63 super.ensureCapacity(minCapacity); 64 assertTrue(capacity() >= minCapacity); 65 if (minCapacity <= oldCapacity) 66 assertEquals(capacity(), oldCapacity); 67 if (minCapacity > 0) 68 assertEquals(modCount(), oldModCount + 1); 69 } 70 } 71 72 static final int DEFAULT_CAPACITY = 10; 73 static final SplittableRandom rnd = new SplittableRandom(); 74 newCapacity(int oldCapacity)75 static int newCapacity(int oldCapacity) { 76 return 2 * oldCapacity; 77 } 78 singletonList()79 static List<Object> singletonList() { 80 return Collections.singletonList(Boolean.TRUE); 81 } 82 83 /** Opportunistically randomly test various add operations. */ addOneElement(PublicVector<Object> list)84 static void addOneElement(PublicVector<Object> list) { 85 int size = list.size(); 86 int modCount = list.modCount(); 87 switch (rnd.nextInt(4)) { 88 case 0: assertTrue(list.add(Boolean.TRUE)); break; 89 case 1: list.add(size, Boolean.TRUE); break; 90 case 2: assertTrue(list.addAll(singletonList())); break; 91 case 3: assertTrue(list.addAll(size, singletonList())); break; 92 default: throw new AssertionError(); 93 } 94 assertEquals(list.modCount(), modCount + 1); 95 assertEquals(list.size(), size + 1); 96 } 97 defaultCapacity()98 @Test public void defaultCapacity() { 99 PublicVector<Object> list = new PublicVector<>(); 100 assertEquals(new PublicVector<Object>().capacity(), DEFAULT_CAPACITY); 101 for (int i = 0; i < DEFAULT_CAPACITY; i++) { 102 addOneElement(list); 103 assertEquals(list.capacity(), DEFAULT_CAPACITY); 104 } 105 addOneElement(list); 106 assertEquals(list.capacity(), newCapacity(DEFAULT_CAPACITY)); 107 } 108 defaultCapacityEnsureCapacity()109 @Test public void defaultCapacityEnsureCapacity() { 110 PublicVector<Object> list = new PublicVector<>(); 111 for (int i = 0; i <= DEFAULT_CAPACITY; i++) { 112 list.ensureCapacity(i); // no-op! 113 assertEquals(list.capacity(), DEFAULT_CAPACITY); 114 } 115 for (int i = 0; i < DEFAULT_CAPACITY; i++) { 116 addOneElement(list); 117 assertEquals(list.capacity(), DEFAULT_CAPACITY); 118 } 119 addOneElement(list); 120 assertEquals(list.capacity(), newCapacity(DEFAULT_CAPACITY)); 121 { 122 int capacity = list.capacity(); 123 list.ensureCapacity(capacity + 1); 124 assertEquals(list.capacity(), newCapacity(capacity)); 125 } 126 { 127 int capacity = list.capacity(); 128 list.ensureCapacity(3 * capacity); 129 assertEquals(list.capacity(), 3 * capacity); 130 } 131 } 132 ensureCapacityBeyondDefaultCapacity()133 @Test public void ensureCapacityBeyondDefaultCapacity() { 134 PublicVector<Object> list = new PublicVector<>(); 135 list.ensureCapacity(DEFAULT_CAPACITY + 1); 136 assertEquals(list.capacity(), newCapacity(DEFAULT_CAPACITY)); 137 } 138 explicitZeroCapacity()139 @Test public void explicitZeroCapacity() { 140 PublicVector<Object> list = new PublicVector<>(0); 141 assertEquals(list.capacity(), 0); 142 addOneElement(list); 143 assertEquals(list.capacity(), 1); 144 addOneElement(list); 145 assertEquals(list.capacity(), 2); 146 addOneElement(list); 147 assertEquals(list.capacity(), 4); 148 addOneElement(list); 149 assertEquals(list.capacity(), 4); 150 addOneElement(list); 151 assertEquals(list.capacity(), 8); 152 addOneElement(list); 153 assertEquals(list.capacity(), 8); 154 addOneElement(list); 155 assertEquals(list.capacity(), 8); 156 list.clear(); 157 assertEquals(list.capacity(), 8); 158 } 159 explicitZeroCapacityWithCapacityIncrement()160 @Test public void explicitZeroCapacityWithCapacityIncrement() { 161 PublicVector<Object> list = new PublicVector<>(0, 2); 162 assertEquals(list.capacity(), 0); 163 addOneElement(list); 164 assertEquals(list.capacity(), 2); 165 addOneElement(list); 166 assertEquals(list.capacity(), 2); 167 addOneElement(list); 168 assertEquals(list.capacity(), 4); 169 addOneElement(list); 170 assertEquals(list.capacity(), 4); 171 addOneElement(list); 172 assertEquals(list.capacity(), 6); 173 addOneElement(list); 174 assertEquals(list.capacity(), 6); 175 addOneElement(list); 176 assertEquals(list.capacity(), 8); 177 list.clear(); 178 assertEquals(list.capacity(), 8); 179 } 180 explicitLargeCapacity()181 @Test public void explicitLargeCapacity() { 182 int n = DEFAULT_CAPACITY * 3; 183 PublicVector<Object> list = new PublicVector<>(n); 184 assertEquals(list.capacity(), n); 185 list.ensureCapacity(0); 186 list.ensureCapacity(n); 187 for (int i = 0; i < n; i++) addOneElement(list); 188 assertEquals(list.capacity(), n); 189 190 addOneElement(list); 191 assertEquals(list.capacity(), newCapacity(n)); 192 } 193 explicitLargeCapacityWithCapacityIncrement()194 @Test public void explicitLargeCapacityWithCapacityIncrement() { 195 int n = DEFAULT_CAPACITY * 3; 196 PublicVector<Object> list = new PublicVector<>(n, 2); 197 assertEquals(list.capacity(), n); 198 list.ensureCapacity(0); 199 list.ensureCapacity(n); 200 for (int i = 0; i < n; i++) addOneElement(list); 201 assertEquals(list.capacity(), n); 202 203 addOneElement(list); 204 assertEquals(list.capacity(), n + 2); 205 } 206 emptyArraysAreNotShared()207 @Test public void emptyArraysAreNotShared() { 208 assertNotSame(new PublicVector<Object>(0).elementData(), 209 new PublicVector<Object>(0).elementData()); 210 } 211 negativeCapacity()212 @Test public void negativeCapacity() { 213 for (int capacity : new int[] { -1, Integer.MIN_VALUE }) { 214 try { 215 new Vector<Object>(capacity); 216 fail("should throw"); 217 } catch (IllegalArgumentException success) {} 218 } 219 } 220 } 221