Path: blob/master/test/jdk/java/nio/Buffer/Chars.java
41149 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 801485426* @summary Exercises CharBuffer#chars on each of the CharBuffer types27* @run testng Chars28* @key randomness29*/3031import java.nio.ByteBuffer;32import java.nio.ByteOrder;33import java.nio.CharBuffer;34import java.util.ArrayList;35import java.util.List;36import java.util.Random;3738import org.testng.annotations.DataProvider;39import org.testng.annotations.Test;4041import static org.testng.Assert.assertEquals;4243public class Chars {4445static final Random RAND = new Random();4647static final int SIZE = 128 + RAND.nextInt(1024);4849/**50* Randomize the char buffer's position and limit.51*/52static CharBuffer randomizeRange(CharBuffer cb) {53int mid = cb.capacity() >>> 1;54int start = RAND.nextInt(mid + 1); // from 0 to mid55int end = mid + RAND.nextInt(cb.capacity() - mid + 1); // from mid to capacity56cb.position(start);57cb.limit(end);58return cb;59}6061/**62* Randomize the char buffer's contents, position and limit.63*/64static CharBuffer randomize(CharBuffer cb) {65while (cb.hasRemaining()) {66cb.put((char)RAND.nextInt());67}68return randomizeRange(cb);69}7071/**72* Sums the remaining chars in the char buffer.73*/74static int intSum(CharBuffer cb) {75int sum = 0;76cb.mark();77while (cb.hasRemaining()) {78sum += cb.get();79}80cb.reset();81return sum;82}8384/**85* Creates char buffers to test, adding them to the given list.86*/87static void addCases(CharBuffer cb, List<CharBuffer> buffers) {88randomize(cb);89buffers.add(cb);9091buffers.add(cb.slice());92buffers.add(cb.duplicate());93buffers.add(cb.asReadOnlyBuffer());9495buffers.add(randomizeRange(cb.slice()));96buffers.add(randomizeRange(cb.duplicate()));97buffers.add(randomizeRange(cb.asReadOnlyBuffer()));98}99100@DataProvider(name = "charbuffers")101public Object[][] createCharBuffers() {102List<CharBuffer> buffers = new ArrayList<>();103104// heap105addCases(CharBuffer.allocate(SIZE), buffers);106addCases(CharBuffer.wrap(new char[SIZE]), buffers);107addCases(ByteBuffer.allocate(SIZE*2).order(ByteOrder.BIG_ENDIAN).asCharBuffer(),108buffers);109addCases(ByteBuffer.allocate(SIZE*2).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(),110buffers);111112// direct113addCases(ByteBuffer.allocateDirect(SIZE*2).order(ByteOrder.BIG_ENDIAN).asCharBuffer(),114buffers);115addCases(ByteBuffer.allocateDirect(SIZE*2).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(),116buffers);117118// read-only buffer backed by a CharSequence119buffers.add(CharBuffer.wrap(randomize(CharBuffer.allocate(SIZE))));120121Object[][] params = new Object[buffers.size()][];122for (int i = 0; i < buffers.size(); i++) {123CharBuffer cb = buffers.get(i);124params[i] = new Object[] { cb.getClass().getName(), cb };125}126127return params;128}129130@Test(dataProvider = "charbuffers")131public void testChars(String type, CharBuffer cb) {132System.out.format("%s position=%d, limit=%d%n", type, cb.position(), cb.limit());133int expected = intSum(cb);134assertEquals(cb.chars().sum(), expected);135assertEquals(cb.chars().parallel().sum(), expected);136}137}138139140