1 /* 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 it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 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 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23 /* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea and Martin Buchholz with assistance from 30 * members of JCP JSR-166 Expert Group and released to the public 31 * domain, as explained at 32 * http://creativecommons.org/publicdomain/zero/1.0/ 33 */ 34 35 package test.java.util.concurrent.tck; 36 import static org.junit.Assert.assertEquals; 37 import static org.junit.Assert.assertFalse; 38 import static org.junit.Assert.assertNull; 39 import static org.junit.Assert.assertNotNull; 40 import static org.junit.Assert.assertSame; 41 import static org.junit.Assert.assertNotSame; 42 import static org.junit.Assert.assertTrue; 43 import static org.junit.Assert.fail; 44 45 import java.util.ArrayDeque; 46 import java.util.Collections; 47 import java.util.Spliterator; 48 49 import org.junit.Test; 50 import org.junit.runner.RunWith; 51 import org.junit.runners.JUnit4; 52 53 // Android-changed: Use JUnit4. 54 @RunWith(JUnit4.class) 55 public class ArrayDeque8Test extends JSR166TestCase { 56 // Android-changed: Use JUnitCore.main. main(String[] args)57 public static void main(String[] args) { 58 // main(suite(), args); 59 org.junit.runner.JUnitCore.main("test.java.util.concurrent.tck.ArrayDeque8Test"); 60 } 61 62 // public static Test suite() { 63 // return new TestSuite(ArrayDeque8Test.class); 64 // } 65 66 /** 67 * Spliterator.getComparator always throws IllegalStateException 68 */ 69 @Test testSpliterator_getComparator()70 public void testSpliterator_getComparator() { 71 assertThrows(IllegalStateException.class, 72 () -> new ArrayDeque().spliterator().getComparator()); 73 } 74 75 /** 76 * Spliterator characteristics are as advertised 77 */ 78 @Test testSpliterator_characteristics()79 public void testSpliterator_characteristics() { 80 ArrayDeque q = new ArrayDeque(); 81 Spliterator s = q.spliterator(); 82 int characteristics = s.characteristics(); 83 int required = Spliterator.NONNULL 84 | Spliterator.ORDERED 85 | Spliterator.SIZED 86 | Spliterator.SUBSIZED; 87 assertEquals(required, characteristics & required); 88 assertTrue(s.hasCharacteristics(required)); 89 assertEquals(0, characteristics 90 & (Spliterator.CONCURRENT 91 | Spliterator.DISTINCT 92 | Spliterator.IMMUTABLE 93 | Spliterator.SORTED)); 94 } 95 96 /** 97 * Handle capacities near Integer.MAX_VALUE. 98 * ant -Dvmoptions='-Xms28g -Xmx28g' -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ArrayDeque8Test -Djsr166.methodFilter=testHugeCapacity tck 99 */ 100 @Test testHugeCapacity()101 public void testHugeCapacity() { 102 if (! (testImplementationDetails 103 && expensiveTests 104 && Runtime.getRuntime().maxMemory() > 24L * (1 << 30))) 105 return; 106 107 final Integer e = 42; 108 final int maxArraySize = Integer.MAX_VALUE - 8; 109 110 assertThrows(OutOfMemoryError.class, 111 () -> new ArrayDeque(Integer.MAX_VALUE)); 112 113 { 114 ArrayDeque q = new ArrayDeque(maxArraySize - 1); 115 assertEquals(0, q.size()); 116 assertTrue(q.isEmpty()); 117 q = null; 118 } 119 120 { 121 ArrayDeque q = new ArrayDeque(); 122 assertTrue(q.addAll(Collections.nCopies(maxArraySize - 3, e))); 123 assertEquals(e, q.peekFirst()); 124 assertEquals(e, q.peekLast()); 125 assertEquals(maxArraySize - 3, q.size()); 126 q.addFirst((Integer) 0); 127 q.addLast((Integer) 1); 128 assertEquals((Integer) 0, q.peekFirst()); 129 assertEquals((Integer) 1, q.peekLast()); 130 assertEquals(maxArraySize - 1, q.size()); 131 132 ArrayDeque qq = q; 133 ArrayDeque smallish = new ArrayDeque( 134 Collections.nCopies(Integer.MAX_VALUE - q.size() + 1, e)); 135 assertThrows( 136 IllegalStateException.class, 137 () -> qq.addAll(qq), 138 () -> qq.addAll(smallish), 139 () -> smallish.addAll(qq)); 140 } 141 } 142 143 } 144