Path: blob/master/test/jdk/java/nio/Buffer/ByteBufferViews.java
41149 views
/*1* Copyright (c) 2016, 2021, 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/* @test24* @summary Binary data and view tests for byte buffers25* @bug 8159257 825895526* @run testng ByteBufferViews27*/2829import org.testng.annotations.DataProvider;30import org.testng.annotations.Test;3132import java.nio.Buffer;33import java.nio.ByteBuffer;34import java.nio.ByteOrder;35import java.nio.CharBuffer;36import java.nio.DoubleBuffer;37import java.nio.FloatBuffer;38import java.nio.IntBuffer;39import java.nio.LongBuffer;40import java.nio.ShortBuffer;41import java.util.List;42import java.util.Map;43import java.util.function.Function;44import java.util.function.IntFunction;45import java.util.function.IntUnaryOperator;46import java.util.function.UnaryOperator;47import java.util.stream.Collectors;4849import static org.testng.Assert.*;5051public class ByteBufferViews {52static final int SIZE = 32;5354// List of buffer allocator functions55static final List<Map.Entry<String, IntFunction<ByteBuffer>>> BYTE_BUFFER_ALLOCATE_FUNCTIONS = List.of(56// Heap57Map.entry("ByteBuffer.allocate(ba)",58size -> ByteBuffer.allocate(size)),59// Aligned60Map.entry("ByteBuffer.allocate(size).position(8)",61size -> ByteBuffer.allocate(size).position(8)),62Map.entry("ByteBuffer.allocate(size).position(8).slice()",63size -> ByteBuffer.allocate(size).position(8).slice()),64Map.entry("ByteBuffer.allocate(size).position(8).slice().duplicate()",65size -> ByteBuffer.allocate(size).position(8).slice().duplicate()),66Map.entry("ByteBuffer.allocate(size).slice(8,size-8)",67size -> ByteBuffer.allocate(size).slice(8,size-8)),68// Unaligned69Map.entry("ByteBuffer.allocate(size).position(1)",70size -> ByteBuffer.allocate(size).position(1)),71Map.entry("ByteBuffer.allocate(size).position(1).slice()",72size -> ByteBuffer.allocate(size).position(1).slice()),73Map.entry("ByteBuffer.allocate(size).position(1).slice().duplicate()",74size -> ByteBuffer.allocate(size).position(1).slice().duplicate()),75Map.entry("ByteBuffer.allocate(size).slice(1,size-1)",76size -> ByteBuffer.allocate(size).slice(1,size-1)),7778// Off-heap79Map.entry("ByteBuffer.allocateDirect(size)",80size -> ByteBuffer.allocateDirect(size)),81// Aligned82Map.entry("ByteBuffer.allocateDirect(size).position(8)",83size -> ByteBuffer.allocateDirect(size).position(8)),84Map.entry("ByteBuffer.allocateDirect(size).position(8).slice()",85size -> ByteBuffer.allocateDirect(size).position(8).slice()),86Map.entry("ByteBuffer.allocateDirect(size).position(8).slice().duplicate()",87size -> ByteBuffer.allocateDirect(size).position(8).slice().duplicate()),88Map.entry("ByteBuffer.allocateDirect(size).slice(8,size-8)",89size -> ByteBuffer.allocateDirect(size).slice(8,size-8)),90// Unaligned91Map.entry("ByteBuffer.allocateDirect(size).position(1)",92size -> ByteBuffer.allocateDirect(size).position(1)),93Map.entry("ByteBuffer.allocateDirect(size).position(1).slice()",94size -> ByteBuffer.allocateDirect(size).position(1).slice()),95Map.entry("ByteBuffer.allocateDirect(size).position(1).slice().duplicate()",96size -> ByteBuffer.allocateDirect(size).position(1).slice().duplicate()),97Map.entry("ByteBuffer.allocateDirect(size).slice(1,size-1)",98size -> ByteBuffer.allocateDirect(size).slice(1,size-1))99);100101// List of buffer byte order functions102static final List<Map.Entry<String, UnaryOperator<ByteBuffer>>> BYTE_BUFFER_ORDER_FUNCTIONS = List.of(103Map.entry("order(ByteOrder.BIG_ENDIAN)",104(ByteBuffer bb) -> bb.order(ByteOrder.BIG_ENDIAN)),105Map.entry("order(ByteOrder.LITTLE_ENDIAN)",106(ByteBuffer bb) -> bb.order(ByteOrder.LITTLE_ENDIAN))107);108109// Produce a composition of allocation and byte order buffer functions110static List<Map.Entry<String, IntFunction<ByteBuffer>>> composeBufferFunctions(111List<Map.Entry<String, IntFunction<ByteBuffer>>> af,112List<Map.Entry<String, UnaryOperator<ByteBuffer>>> of) {113return af.stream().flatMap(afe -> of.stream().114map(ofe -> {115String s = afe.getKey() + "." + ofe.getKey();116IntFunction<ByteBuffer> f = size -> ofe.getValue().117apply(afe.getValue().apply(size));118return Map.entry(s, f);119})120).collect(Collectors.toList());121}122123// List of buffer allocator functions to test124static final List<Map.Entry<String, IntFunction<ByteBuffer>>> BYTE_BUFFER_FUNCTIONS =125composeBufferFunctions(BYTE_BUFFER_ALLOCATE_FUNCTIONS, BYTE_BUFFER_ORDER_FUNCTIONS);126127// Creates a cross product of test arguments for128// buffer allocator functions and buffer view functions129static Object[][] product(List<? extends Map.Entry<String, ?>> la,130List<? extends Map.Entry<String, ?>> lb) {131return la.stream().flatMap(lae -> lb.stream().132map(lbe -> List.of(133lae.getKey() + " -> " + lbe.getKey(),134lae.getValue(),135lbe.getValue()).toArray()136)).toArray(Object[][]::new);137}138139static void assertValues(int i, Object bValue, Object bbValue, ByteBuffer bb) {140if (!bValue.equals(bbValue)) {141fail(String.format("Values %s and %s differ at index %d for %s",142bValue, bbValue, i, bb));143}144}145146static void assertValues(int i, Object bbValue, Object bvValue, ByteBuffer bb, Buffer bv) {147if (!bbValue.equals(bvValue)) {148fail(String.format("Values %s and %s differ at index %d for %s and %s",149bbValue, bvValue, i, bb, bv));150}151}152153static ByteBuffer allocate(IntFunction<ByteBuffer> f) {154return allocate(f, i -> i);155}156157static ByteBuffer allocate(IntFunction<ByteBuffer> f, IntUnaryOperator o) {158return fill(f.apply(SIZE), o);159}160161static ByteBuffer fill(ByteBuffer bb, IntUnaryOperator o) {162for (int i = 0; i < bb.limit(); i++) {163bb.put(i, (byte) o.applyAsInt(i));164}165return bb;166}167168169@DataProvider170public static Object[][] shortViewProvider() {171List<Map.Entry<String, Function<ByteBuffer, ShortBuffer>>> bfs = List.of(172Map.entry("bb.asShortBuffer()",173bb -> bb.asShortBuffer()),174Map.entry("bb.asShortBuffer().slice()",175bb -> bb.asShortBuffer().slice()),176Map.entry("bb.asShortBuffer().slice(index,length)",177bb -> { var sb = bb.asShortBuffer();178sb = sb.slice(1, sb.limit() - 1);179bb.position(bb.position() + 2);180return sb; }),181Map.entry("bb.asShortBuffer().slice().duplicate()",182bb -> bb.asShortBuffer().slice().duplicate())183);184185return product(BYTE_BUFFER_FUNCTIONS, bfs);186}187188@Test(dataProvider = "shortViewProvider")189public void testShortGet(String desc, IntFunction<ByteBuffer> fbb,190Function<ByteBuffer, ShortBuffer> fbi) {191ByteBuffer bb = allocate(fbb);192ShortBuffer vb = fbi.apply(bb);193int o = bb.position();194195for (int i = 0; i < vb.limit(); i++) {196short fromBytes = getShortFromBytes(bb, o + i * 2);197short fromMethodView = bb.getShort(o + i * 2);198assertValues(i, fromBytes, fromMethodView, bb);199200short fromBufferView = vb.get(i);201assertValues(i, fromMethodView, fromBufferView, bb, vb);202}203204for (int i = 0; i < vb.limit(); i++) {205short v = getShortFromBytes(bb, o + i * 2);206short a = bb.getShort();207assertValues(i, v, a, bb);208209short b = vb.get();210assertValues(i, a, b, bb, vb);211}212213}214215@Test(dataProvider = "shortViewProvider")216public void testShortPut(String desc, IntFunction<ByteBuffer> fbb,217Function<ByteBuffer, ShortBuffer> fbi) {218ByteBuffer bbfilled = allocate(fbb);219ByteBuffer bb = allocate(fbb, i -> 0);220ShortBuffer vb = fbi.apply(bb);221int o = bb.position();222223for (int i = 0; i < vb.limit(); i++) {224short fromFilled = bbfilled.getShort(o + i * 2);225226vb.put(i, fromFilled);227short fromMethodView = bb.getShort(o + i * 2);228assertValues(i, fromFilled, fromMethodView, bb, vb);229}230231for (int i = 0; i < vb.limit(); i++) {232short fromFilled = bbfilled.getShort(o + i * 2);233234vb.put(fromFilled);235short fromMethodView = bb.getShort();236assertValues(i, fromFilled, fromMethodView, bb, vb);237}238239240fill(bb, i -> 0);241bb.clear().position(o);242vb.clear();243244for (int i = 0; i < vb.limit(); i++) {245short fromFilled = bbfilled.getShort(o + i * 2);246247bb.putShort(o + i * 2, fromFilled);248short fromBufferView = vb.get(i);249assertValues(i, fromFilled, fromBufferView, bb, vb);250}251252for (int i = 0; i < vb.limit(); i++) {253short fromFilled = bbfilled.getShort(o + i * 2);254255bb.putShort(fromFilled);256short fromBufferView = vb.get();257assertValues(i, fromFilled, fromBufferView, bb, vb);258}259}260261static short getShortFromBytes(ByteBuffer bb, int i) {262int a = bb.get(i) & 0xFF;263int b = bb.get(i + 1) & 0xFF;264265if (bb.order() == ByteOrder.BIG_ENDIAN) {266return (short) ((a << 8) | b);267}268else {269return (short) ((b << 8) | a);270}271}272273@DataProvider274public static Object[][] charViewProvider() {275List<Map.Entry<String, Function<ByteBuffer, CharBuffer>>> bfs = List.of(276Map.entry("bb.asCharBuffer()",277bb -> bb.asCharBuffer()),278Map.entry("bb.asCharBuffer().slice()",279bb -> bb.asCharBuffer().slice()),280Map.entry("bb.asCharBuffer().slice(index,length)",281bb -> { var cb = bb.asCharBuffer();282cb = cb.slice(1, cb.limit() - 1);283bb.position(bb.position() + 2);284return cb; }),285Map.entry("bb.asCharBuffer().slice().duplicate()",286bb -> bb.asCharBuffer().slice().duplicate())287);288289return product(BYTE_BUFFER_FUNCTIONS, bfs);290}291292@Test(dataProvider = "charViewProvider")293public void testCharGet(String desc, IntFunction<ByteBuffer> fbb,294Function<ByteBuffer, CharBuffer> fbi) {295ByteBuffer bb = allocate(fbb);296CharBuffer vb = fbi.apply(bb);297int o = bb.position();298299for (int i = 0; i < vb.limit(); i++) {300char fromBytes = getCharFromBytes(bb, o + i * 2);301char fromMethodView = bb.getChar(o + i * 2);302assertValues(i, fromBytes, fromMethodView, bb);303304char fromBufferView = vb.get(i);305assertValues(i, fromMethodView, fromBufferView, bb, vb);306}307308for (int i = 0; i < vb.limit(); i++) {309char fromBytes = getCharFromBytes(bb, o + i * 2);310char fromMethodView = bb.getChar();311assertValues(i, fromBytes, fromMethodView, bb);312313char fromBufferView = vb.get();314assertValues(i, fromMethodView, fromBufferView, bb, vb);315}316317}318319@Test(dataProvider = "charViewProvider")320public void testCharPut(String desc, IntFunction<ByteBuffer> fbb,321Function<ByteBuffer, CharBuffer> fbi) {322ByteBuffer bbfilled = allocate(fbb);323ByteBuffer bb = allocate(fbb, i -> 0);324CharBuffer vb = fbi.apply(bb);325int o = bb.position();326327for (int i = 0; i < vb.limit(); i++) {328char fromFilled = bbfilled.getChar(o + i * 2);329330vb.put(i, fromFilled);331char fromMethodView = bb.getChar(o + i * 2);332assertValues(i, fromFilled, fromMethodView, bb, vb);333}334335for (int i = 0; i < vb.limit(); i++) {336char fromFilled = bbfilled.getChar(o + i * 2);337338vb.put(fromFilled);339char fromMethodView = bb.getChar();340assertValues(i, fromFilled, fromMethodView, bb, vb);341}342343344fill(bb, i -> 0);345bb.clear().position(o);346vb.clear();347348for (int i = 0; i < vb.limit(); i++) {349char fromFilled = bbfilled.getChar(o + i * 2);350351bb.putChar(o + i * 2, fromFilled);352char fromBufferView = vb.get(i);353assertValues(i, fromFilled, fromBufferView, bb, vb);354}355356for (int i = 0; i < vb.limit(); i++) {357char fromFilled = bbfilled.getChar(o + i * 2);358359bb.putChar(fromFilled);360char fromBufferView = vb.get();361assertValues(i, fromFilled, fromBufferView, bb, vb);362}363}364365static char getCharFromBytes(ByteBuffer bb, int i) {366return (char) getShortFromBytes(bb, i);367}368369370@DataProvider371public static Object[][] intViewProvider() {372List<Map.Entry<String, Function<ByteBuffer, IntBuffer>>> bfs = List.of(373Map.entry("bb.asIntBuffer()",374bb -> bb.asIntBuffer()),375Map.entry("bb.asIntBuffer().slice()",376bb -> bb.asIntBuffer().slice()),377Map.entry("bb.asIntBuffer().slice(index,length)",378bb -> { var ib = bb.asIntBuffer();379ib = ib.slice(1, ib.limit() - 1);380bb.position(bb.position() + 4);381return ib; }),382Map.entry("bb.asIntBuffer().slice().duplicate()",383bb -> bb.asIntBuffer().slice().duplicate())384);385386return product(BYTE_BUFFER_FUNCTIONS, bfs);387}388389@Test(dataProvider = "intViewProvider")390public void testIntGet(String desc, IntFunction<ByteBuffer> fbb,391Function<ByteBuffer, IntBuffer> fbi) {392ByteBuffer bb = allocate(fbb);393IntBuffer vb = fbi.apply(bb);394int o = bb.position();395396for (int i = 0; i < vb.limit(); i++) {397int fromBytes = getIntFromBytes(bb, o + i * 4);398int fromMethodView = bb.getInt(o + i * 4);399assertValues(i, fromBytes, fromMethodView, bb);400401int fromBufferView = vb.get(i);402assertValues(i, fromMethodView, fromBufferView, bb, vb);403}404405for (int i = 0; i < vb.limit(); i++) {406int v = getIntFromBytes(bb, o + i * 4);407int a = bb.getInt();408assertValues(i, v, a, bb);409410int b = vb.get();411assertValues(i, a, b, bb, vb);412}413414}415416@Test(dataProvider = "intViewProvider")417public void testIntPut(String desc, IntFunction<ByteBuffer> fbb,418Function<ByteBuffer, IntBuffer> fbi) {419ByteBuffer bbfilled = allocate(fbb);420ByteBuffer bb = allocate(fbb, i -> 0);421IntBuffer vb = fbi.apply(bb);422int o = bb.position();423424for (int i = 0; i < vb.limit(); i++) {425int fromFilled = bbfilled.getInt(o + i * 4);426427vb.put(i, fromFilled);428int fromMethodView = bb.getInt(o + i * 4);429assertValues(i, fromFilled, fromMethodView, bb, vb);430}431432for (int i = 0; i < vb.limit(); i++) {433int fromFilled = bbfilled.getInt(o + i * 4);434435vb.put(fromFilled);436int fromMethodView = bb.getInt();437assertValues(i, fromFilled, fromMethodView, bb, vb);438}439440441fill(bb, i -> 0);442bb.clear().position(o);443vb.clear();444445for (int i = 0; i < vb.limit(); i++) {446int fromFilled = bbfilled.getInt(o + i * 4);447448bb.putInt(o + i * 4, fromFilled);449int fromBufferView = vb.get(i);450assertValues(i, fromFilled, fromBufferView, bb, vb);451}452453for (int i = 0; i < vb.limit(); i++) {454int fromFilled = bbfilled.getInt(o + i * 4);455456bb.putInt(fromFilled);457int fromBufferView = vb.get();458assertValues(i, fromFilled, fromBufferView, bb, vb);459}460}461462static int getIntFromBytes(ByteBuffer bb, int i) {463int a = bb.get(i) & 0xFF;464int b = bb.get(i + 1) & 0xFF;465int c = bb.get(i + 2) & 0xFF;466int d = bb.get(i + 3) & 0xFF;467468if (bb.order() == ByteOrder.BIG_ENDIAN) {469return ((a << 24) | (b << 16) | (c << 8) | d);470}471else {472return ((d << 24) | (c << 16) | (b << 8) | a);473}474}475476477@DataProvider478public static Object[][] longViewProvider() {479List<Map.Entry<String, Function<ByteBuffer, LongBuffer>>> bfs = List.of(480Map.entry("bb.asLongBuffer()",481bb -> bb.asLongBuffer()),482Map.entry("bb.asLongBuffer().slice()",483bb -> bb.asLongBuffer().slice()),484Map.entry("bb.asLongBuffer().slice(index,length)",485bb -> { var lb = bb.asLongBuffer();486lb = lb.slice(1, lb.limit() - 1);487bb.position(bb.position() + 8);488return lb; }),489Map.entry("bb.asLongBuffer().slice().duplicate()",490bb -> bb.asLongBuffer().slice().duplicate())491);492493return product(BYTE_BUFFER_FUNCTIONS, bfs);494}495496@Test(dataProvider = "longViewProvider")497public void testLongGet(String desc, IntFunction<ByteBuffer> fbb,498Function<ByteBuffer, LongBuffer> fbi) {499ByteBuffer bb = allocate(fbb);500LongBuffer vb = fbi.apply(bb);501int o = bb.position();502503for (int i = 0; i < vb.limit(); i++) {504long fromBytes = getLongFromBytes(bb, o + i * 8);505long fromMethodView = bb.getLong(o + i * 8);506assertValues(i, fromBytes, fromMethodView, bb);507508long fromBufferView = vb.get(i);509assertValues(i, fromMethodView, fromBufferView, bb, vb);510}511512for (int i = 0; i < vb.limit(); i++) {513long v = getLongFromBytes(bb, o + i * 8);514long a = bb.getLong();515assertValues(i, v, a, bb);516517long b = vb.get();518assertValues(i, a, b, bb, vb);519}520521}522523@Test(dataProvider = "longViewProvider")524public void testLongPut(String desc, IntFunction<ByteBuffer> fbb,525Function<ByteBuffer, LongBuffer> fbi) {526ByteBuffer bbfilled = allocate(fbb);527ByteBuffer bb = allocate(fbb, i -> 0);528LongBuffer vb = fbi.apply(bb);529int o = bb.position();530531for (int i = 0; i < vb.limit(); i++) {532long fromFilled = bbfilled.getLong(o + i * 8);533534vb.put(i, fromFilled);535long fromMethodView = bb.getLong(o + i * 8);536assertValues(i, fromFilled, fromMethodView, bb, vb);537}538539for (int i = 0; i < vb.limit(); i++) {540long fromFilled = bbfilled.getLong(o + i * 8);541542vb.put(fromFilled);543long fromMethodView = bb.getLong();544assertValues(i, fromFilled, fromMethodView, bb, vb);545}546547548fill(bb, i -> 0);549bb.clear().position(o);550vb.clear();551552for (int i = 0; i < vb.limit(); i++) {553long fromFilled = bbfilled.getLong(o + i * 8);554555bb.putLong(o + i * 8, fromFilled);556long fromBufferView = vb.get(i);557assertValues(i, fromFilled, fromBufferView, bb, vb);558}559560for (int i = 0; i < vb.limit(); i++) {561long fromFilled = bbfilled.getLong(o + i * 8);562563bb.putLong(fromFilled);564long fromBufferView = vb.get();565assertValues(i, fromFilled, fromBufferView, bb, vb);566}567}568569static long getLongFromBytes(ByteBuffer bb, int i) {570long a = bb.get(i) & 0xFF;571long b = bb.get(i + 1) & 0xFF;572long c = bb.get(i + 2) & 0xFF;573long d = bb.get(i + 3) & 0xFF;574long e = bb.get(i + 4) & 0xFF;575long f = bb.get(i + 5) & 0xFF;576long g = bb.get(i + 6) & 0xFF;577long h = bb.get(i + 7) & 0xFF;578579if (bb.order() == ByteOrder.BIG_ENDIAN) {580return ((a << 56) | (b << 48) | (c << 40) | (d << 32) |581(e << 24) | (f << 16) | (g << 8) | h);582}583else {584return ((h << 56) | (g << 48) | (f << 40) | (e << 32) |585(d << 24) | (c << 16) | (b << 8) | a);586}587}588589590@DataProvider591public static Object[][] floatViewProvider() {592List<Map.Entry<String, Function<ByteBuffer, FloatBuffer>>> bfs = List.of(593Map.entry("bb.asFloatBuffer()",594bb -> bb.asFloatBuffer()),595Map.entry("bb.asFloatBuffer().slice()",596bb -> bb.asFloatBuffer().slice()),597Map.entry("bb.asFloatBuffer().slice(index,length)",598bb -> { var fb = bb.asFloatBuffer();599fb = fb.slice(1, fb.limit() - 1);600bb.position(bb.position() + 4);601return fb; }),602Map.entry("bb.asFloatBuffer().slice().duplicate()",603bb -> bb.asFloatBuffer().slice().duplicate())604);605606return product(BYTE_BUFFER_FUNCTIONS, bfs);607}608609@Test(dataProvider = "floatViewProvider")610public void testFloatGet(String desc, IntFunction<ByteBuffer> fbb,611Function<ByteBuffer, FloatBuffer> fbi) {612ByteBuffer bb = allocate(fbb);613FloatBuffer vb = fbi.apply(bb);614int o = bb.position();615616for (int i = 0; i < vb.limit(); i++) {617float fromBytes = getFloatFromBytes(bb, o + i * 4);618float fromMethodView = bb.getFloat(o + i * 4);619assertValues(i, fromBytes, fromMethodView, bb);620621float fromBufferView = vb.get(i);622assertValues(i, fromMethodView, fromBufferView, bb, vb);623}624625for (int i = 0; i < vb.limit(); i++) {626float v = getFloatFromBytes(bb, o + i * 4);627float a = bb.getFloat();628assertValues(i, v, a, bb);629630float b = vb.get();631assertValues(i, a, b, bb, vb);632}633634}635636@Test(dataProvider = "floatViewProvider")637public void testFloatPut(String desc, IntFunction<ByteBuffer> fbb,638Function<ByteBuffer, FloatBuffer> fbi) {639ByteBuffer bbfilled = allocate(fbb);640ByteBuffer bb = allocate(fbb, i -> 0);641FloatBuffer vb = fbi.apply(bb);642int o = bb.position();643644for (int i = 0; i < vb.limit(); i++) {645float fromFilled = bbfilled.getFloat(o + i * 4);646647vb.put(i, fromFilled);648float fromMethodView = bb.getFloat(o + i * 4);649assertValues(i, fromFilled, fromMethodView, bb, vb);650}651652for (int i = 0; i < vb.limit(); i++) {653float fromFilled = bbfilled.getFloat(o + i * 4);654655vb.put(fromFilled);656float fromMethodView = bb.getFloat();657assertValues(i, fromFilled, fromMethodView, bb, vb);658}659660661fill(bb, i -> 0);662bb.clear().position(o);663vb.clear();664665for (int i = 0; i < vb.limit(); i++) {666float fromFilled = bbfilled.getFloat(o + i * 4);667668bb.putFloat(o + i * 4, fromFilled);669float fromBufferView = vb.get(i);670assertValues(i, fromFilled, fromBufferView, bb, vb);671}672673for (int i = 0; i < vb.limit(); i++) {674float fromFilled = bbfilled.getFloat(o + i * 4);675676bb.putFloat(fromFilled);677float fromBufferView = vb.get();678assertValues(i, fromFilled, fromBufferView, bb, vb);679}680}681682static float getFloatFromBytes(ByteBuffer bb, int i) {683return Float.intBitsToFloat(getIntFromBytes(bb, i));684}685686687688@DataProvider689public static Object[][] doubleViewProvider() {690List<Map.Entry<String, Function<ByteBuffer, DoubleBuffer>>> bfs = List.of(691Map.entry("bb.asDoubleBuffer()",692bb -> bb.asDoubleBuffer()),693Map.entry("bb.asDoubleBuffer().slice()",694bb -> bb.asDoubleBuffer().slice()),695Map.entry("bb.asDoubleBuffer().slice(index,length)",696bb -> { var db = bb.asDoubleBuffer();697db = db.slice(1, db.limit() - 1);698bb.position(bb.position() + 8);699return db; }),700Map.entry("bb.asDoubleBuffer().slice().duplicate()",701bb -> bb.asDoubleBuffer().slice().duplicate())702);703704return product(BYTE_BUFFER_FUNCTIONS, bfs);705}706707@Test(dataProvider = "doubleViewProvider")708public void testDoubleGet(String desc, IntFunction<ByteBuffer> fbb,709Function<ByteBuffer, DoubleBuffer> fbi) {710ByteBuffer bb = allocate(fbb);711DoubleBuffer vb = fbi.apply(bb);712int o = bb.position();713714for (int i = 0; i < vb.limit(); i++) {715double fromBytes = getDoubleFromBytes(bb, o + i * 8);716double fromMethodView = bb.getDouble(o + i * 8);717assertValues(i, fromBytes, fromMethodView, bb);718719double fromBufferView = vb.get(i);720assertValues(i, fromMethodView, fromBufferView, bb, vb);721}722723for (int i = 0; i < vb.limit(); i++) {724double v = getDoubleFromBytes(bb, o + i * 8);725double a = bb.getDouble();726assertValues(i, v, a, bb);727728double b = vb.get();729assertValues(i, a, b, bb, vb);730}731732}733734@Test(dataProvider = "doubleViewProvider")735public void testDoublePut(String desc, IntFunction<ByteBuffer> fbb,736Function<ByteBuffer, DoubleBuffer> fbi) {737ByteBuffer bbfilled = allocate(fbb);738ByteBuffer bb = allocate(fbb, i -> 0);739DoubleBuffer vb = fbi.apply(bb);740int o = bb.position();741742for (int i = 0; i < vb.limit(); i++) {743double fromFilled = bbfilled.getDouble(o + i * 8);744745vb.put(i, fromFilled);746double fromMethodView = bb.getDouble(o + i * 8);747assertValues(i, fromFilled, fromMethodView, bb, vb);748}749750for (int i = 0; i < vb.limit(); i++) {751double fromFilled = bbfilled.getDouble(o + i * 8);752753vb.put(fromFilled);754double fromMethodView = bb.getDouble();755assertValues(i, fromFilled, fromMethodView, bb, vb);756}757758759fill(bb, i -> 0);760bb.clear().position(o);761vb.clear();762763for (int i = 0; i < vb.limit(); i++) {764double fromFilled = bbfilled.getDouble(o + i * 8);765766bb.putDouble(o + i * 8, fromFilled);767double fromBufferView = vb.get(i);768assertValues(i, fromFilled, fromBufferView, bb, vb);769}770771for (int i = 0; i < vb.limit(); i++) {772double fromFilled = bbfilled.getDouble(o + i * 8);773774bb.putDouble(fromFilled);775double fromBufferView = vb.get();776assertValues(i, fromFilled, fromBufferView, bb, vb);777}778}779780static double getDoubleFromBytes(ByteBuffer bb, int i) {781return Double.longBitsToDouble(getLongFromBytes(bb, i));782}783}784785786