1 /*
2  * Copyright (c) 2013, Oracle and/or its affiliates. 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 8014854
27  * @summary Exercises CharBuffer#chars on each of the CharBuffer types
28  * @run testng Chars
29  * @key randomness
30  */
31 package test.java.nio.Buffer;
32 
33 import java.nio.ByteBuffer;
34 import java.nio.ByteOrder;
35 import java.nio.CharBuffer;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Random;
39 
40 import org.testng.annotations.DataProvider;
41 import org.testng.annotations.Test;
42 
43 import static org.testng.Assert.assertEquals;
44 
45 public class Chars {
46 
47     static final Random RAND = new Random();
48 
49     static final int SIZE = 128 + RAND.nextInt(1024);
50 
51     /**
52      * Randomize the char buffer's position and limit.
53      */
randomizeRange(CharBuffer cb)54     static CharBuffer randomizeRange(CharBuffer cb) {
55         int mid = cb.capacity() >>> 1;
56         int start = RAND.nextInt(mid + 1); // from 0 to mid
57         int end = mid + RAND.nextInt(cb.capacity() - mid + 1); // from mid to capacity
58         cb.position(start);
59         cb.limit(end);
60         return cb;
61     }
62 
63     /**
64      * Randomize the char buffer's contents, position and limit.
65      */
randomize(CharBuffer cb)66     static CharBuffer randomize(CharBuffer cb) {
67         while (cb.hasRemaining()) {
68             cb.put((char)RAND.nextInt());
69         }
70         return randomizeRange(cb);
71     }
72 
73     /**
74      * Sums the remaining chars in the char buffer.
75      */
intSum(CharBuffer cb)76     static int intSum(CharBuffer cb) {
77         int sum = 0;
78         cb.mark();
79         while (cb.hasRemaining()) {
80             sum += cb.get();
81         }
82         cb.reset();
83         return sum;
84     }
85 
86     /**
87      * Creates char buffers to test, adding them to the given list.
88      */
addCases(CharBuffer cb, List<CharBuffer> buffers)89     static void addCases(CharBuffer cb, List<CharBuffer> buffers) {
90         randomize(cb);
91         buffers.add(cb);
92 
93         buffers.add(cb.slice());
94         buffers.add(cb.duplicate());
95         buffers.add(cb.asReadOnlyBuffer());
96 
97         buffers.add(randomizeRange(cb.slice()));
98         buffers.add(randomizeRange(cb.duplicate()));
99         buffers.add(randomizeRange(cb.asReadOnlyBuffer()));
100     }
101 
102     @DataProvider(name = "charbuffers")
createCharBuffers()103     public Object[][] createCharBuffers() {
104         List<CharBuffer> buffers = new ArrayList<>();
105 
106         // heap
107         addCases(CharBuffer.allocate(SIZE), buffers);
108         addCases(CharBuffer.wrap(new char[SIZE]), buffers);
109         addCases(ByteBuffer.allocate(SIZE*2).order(ByteOrder.BIG_ENDIAN).asCharBuffer(),
110                  buffers);
111         addCases(ByteBuffer.allocate(SIZE*2).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(),
112                  buffers);
113 
114         // direct
115         addCases(ByteBuffer.allocateDirect(SIZE*2).order(ByteOrder.BIG_ENDIAN).asCharBuffer(),
116                  buffers);
117         addCases(ByteBuffer.allocateDirect(SIZE*2).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(),
118                  buffers);
119 
120         // read-only buffer backed by a CharSequence
121         buffers.add(CharBuffer.wrap(randomize(CharBuffer.allocate(SIZE))));
122 
123         Object[][] params = new Object[buffers.size()][];
124         for (int i = 0; i < buffers.size(); i++) {
125             CharBuffer cb = buffers.get(i);
126             params[i] = new Object[] { cb.getClass().getName(), cb };
127         }
128 
129         return params;
130     }
131 
132     @Test(dataProvider = "charbuffers")
testChars(String type, CharBuffer cb)133     public void testChars(String type, CharBuffer cb) {
134         // Android-changed: Avoid spamming the logcat.
135         // System.out.format("%s position=%d, limit=%d%n", type, cb.position(), cb.limit());
136         int expected = intSum(cb);
137         assertEquals(cb.chars().sum(), expected);
138         assertEquals(cb.chars().parallel().sum(), expected);
139     }
140 }