author | pli |
Tue, 16 Jul 2019 00:57:00 +0000 | |
changeset 55689 | 8c5c9d86e1d6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
37685
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
1 |
/* |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
4 |
* |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
8 |
* |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
13 |
* accompanied this code). |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
14 |
* |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
18 |
* |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
21 |
* questions. |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
22 |
*/ |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
23 |
|
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
24 |
/** |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
25 |
* @test |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
26 |
* @bug 8155600 |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
27 |
* @summary Tests for Arrays.asList() |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
28 |
* @run testng AsList |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
29 |
*/ |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
30 |
|
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
31 |
import java.util.Arrays; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
32 |
import java.util.Iterator; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
33 |
import java.util.NoSuchElementException; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
34 |
import java.util.stream.IntStream; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
35 |
|
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
36 |
import org.testng.annotations.Test; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
37 |
import org.testng.annotations.DataProvider; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
38 |
import static org.testng.Assert.assertSame; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
39 |
import static org.testng.Assert.assertTrue; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
40 |
import static org.testng.Assert.assertFalse; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
41 |
import static org.testng.Assert.fail; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
42 |
|
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
43 |
public class AsList { |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
44 |
/* |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
45 |
* Iterator contract test |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
46 |
*/ |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
47 |
@Test(dataProvider = "Arrays") |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
48 |
public void testIterator(Object[] array) { |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
49 |
Iterator<Object> itr = Arrays.asList(array).iterator(); |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
50 |
for (int i = 0; i < array.length; i++) { |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
51 |
assertTrue(itr.hasNext()); |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
52 |
assertTrue(itr.hasNext()); // must be idempotent |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
53 |
assertSame(array[i], itr.next()); |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
54 |
try { |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
55 |
itr.remove(); |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
56 |
fail("Remove must throw"); |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
57 |
} catch (UnsupportedOperationException ex) { |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
58 |
// expected |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
59 |
} |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
60 |
} |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
61 |
assertFalse(itr.hasNext()); |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
62 |
for (int i = 0; i < 3; i++) { |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
63 |
assertFalse(itr.hasNext()); |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
64 |
try { |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
65 |
itr.next(); |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
66 |
fail("Next succeed when there's no data left"); |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
67 |
} catch (NoSuchElementException ex) { |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
68 |
// expected |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
69 |
} |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
70 |
} |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
71 |
} |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
72 |
|
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
73 |
@DataProvider(name = "Arrays") |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
74 |
public static Object[][] arrays() { |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
75 |
Object[][] arrays = { |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
76 |
{ new Object[] { } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
77 |
{ new Object[] { 1 } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
78 |
{ new Object[] { null } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
79 |
{ new Object[] { null, 1 } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
80 |
{ new Object[] { 1, null } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
81 |
{ new Object[] { null, null } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
82 |
{ new Object[] { null, 1, 2 } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
83 |
{ new Object[] { 1, null, 2 } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
84 |
{ new Object[] { 1, 2, null } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
85 |
{ new Object[] { null, null, null } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
86 |
{ new Object[] { 1, 2, 3, null, 4 } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
87 |
{ new Object[] { "a", "a", "a", "a" } }, |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
88 |
{ IntStream.range(0, 100).boxed().toArray() } |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
89 |
}; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
90 |
|
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
91 |
return arrays; |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
92 |
} |
6045bfdd65a5
8155600: Performance optimization of Arrays.asList().iterator()
tvaleev
parents:
diff
changeset
|
93 |
} |