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