author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
17922
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
1 |
/* |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
4 |
* |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
8 |
* |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
13 |
* accompanied this code). |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
14 |
* |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
18 |
* |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
21 |
* questions. |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
22 |
*/ |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
23 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
24 |
/** |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
25 |
* @test |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
26 |
* @bug 8014854 |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
27 |
* @summary Exercises CharBuffer#chars on each of the CharBuffer types |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
28 |
* @run testng Chars |
30046 | 29 |
* @key randomness |
17922
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
30 |
*/ |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
31 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
32 |
import java.nio.ByteBuffer; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
33 |
import java.nio.ByteOrder; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
34 |
import java.nio.CharBuffer; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
35 |
import java.util.ArrayList; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
36 |
import java.util.List; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
37 |
import java.util.Random; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
38 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
39 |
import org.testng.annotations.DataProvider; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
40 |
import org.testng.annotations.Test; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
41 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
42 |
import static org.testng.Assert.assertEquals; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
43 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
44 |
public class Chars { |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
45 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
46 |
static final Random RAND = new Random(); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
47 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
48 |
static final int SIZE = 128 + RAND.nextInt(1024); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
49 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
50 |
/** |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
51 |
* Randomize the char buffer's position and limit. |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
52 |
*/ |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
53 |
static CharBuffer randomizeRange(CharBuffer cb) { |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
54 |
int mid = cb.capacity() >>> 1; |
22077
29f58b0d4f78
8030690: TEST_BUG java/nio/Buffer/Chars.java fails intermittently
alanb
parents:
17922
diff
changeset
|
55 |
int start = RAND.nextInt(mid + 1); // from 0 to mid |
29f58b0d4f78
8030690: TEST_BUG java/nio/Buffer/Chars.java fails intermittently
alanb
parents:
17922
diff
changeset
|
56 |
int end = mid + RAND.nextInt(cb.capacity() - mid + 1); // from mid to capacity |
17922
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
57 |
cb.position(start); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
58 |
cb.limit(end); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
59 |
return cb; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
60 |
} |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
61 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
62 |
/** |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
63 |
* Randomize the char buffer's contents, position and limit. |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
64 |
*/ |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
65 |
static CharBuffer randomize(CharBuffer cb) { |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
66 |
while (cb.hasRemaining()) { |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
67 |
cb.put((char)RAND.nextInt()); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
68 |
} |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
69 |
return randomizeRange(cb); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
70 |
} |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
71 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
72 |
/** |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
73 |
* Sums the remaining chars in the char buffer. |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
74 |
*/ |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
75 |
static int intSum(CharBuffer cb) { |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
76 |
int sum = 0; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
77 |
cb.mark(); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
78 |
while (cb.hasRemaining()) { |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
79 |
sum += cb.get(); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
80 |
} |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
81 |
cb.reset(); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
82 |
return sum; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
83 |
} |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
84 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
85 |
/** |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
86 |
* Creates char buffers to test, adding them to the given list. |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
87 |
*/ |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
88 |
static void addCases(CharBuffer cb, List<CharBuffer> buffers) { |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
89 |
randomize(cb); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
90 |
buffers.add(cb); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
91 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
92 |
buffers.add(cb.slice()); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
93 |
buffers.add(cb.duplicate()); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
94 |
buffers.add(cb.asReadOnlyBuffer()); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
95 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
96 |
buffers.add(randomizeRange(cb.slice())); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
97 |
buffers.add(randomizeRange(cb.duplicate())); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
98 |
buffers.add(randomizeRange(cb.asReadOnlyBuffer())); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
99 |
} |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
100 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
101 |
@DataProvider(name = "charbuffers") |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
102 |
public Object[][] createCharBuffers() { |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
103 |
List<CharBuffer> buffers = new ArrayList<>(); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
104 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
105 |
// heap |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
106 |
addCases(CharBuffer.allocate(SIZE), buffers); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
107 |
addCases(CharBuffer.wrap(new char[SIZE]), buffers); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
108 |
addCases(ByteBuffer.allocate(SIZE*2).order(ByteOrder.BIG_ENDIAN).asCharBuffer(), |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
109 |
buffers); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
110 |
addCases(ByteBuffer.allocate(SIZE*2).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(), |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
111 |
buffers); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
112 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
113 |
// direct |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
114 |
addCases(ByteBuffer.allocateDirect(SIZE*2).order(ByteOrder.BIG_ENDIAN).asCharBuffer(), |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
115 |
buffers); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
116 |
addCases(ByteBuffer.allocateDirect(SIZE*2).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(), |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
117 |
buffers); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
118 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
119 |
// read-only buffer backed by a CharSequence |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
120 |
buffers.add(CharBuffer.wrap(randomize(CharBuffer.allocate(SIZE)))); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
121 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
122 |
Object[][] params = new Object[buffers.size()][]; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
123 |
for (int i = 0; i < buffers.size(); i++) { |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
124 |
CharBuffer cb = buffers.get(i); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
125 |
params[i] = new Object[] { cb.getClass().getName(), cb }; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
126 |
} |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
127 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
128 |
return params; |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
129 |
} |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
130 |
|
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
131 |
@Test(dataProvider = "charbuffers") |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
132 |
public void testChars(String type, CharBuffer cb) { |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
133 |
System.out.format("%s position=%d, limit=%d%n", type, cb.position(), cb.limit()); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
134 |
int expected = intSum(cb); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
135 |
assertEquals(cb.chars().sum(), expected); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
136 |
assertEquals(cb.chars().parallel().sum(), expected); |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
137 |
} |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
diff
changeset
|
138 |
} |