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