Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/Buffer/ByteBufferViews.java
41149 views
1
/*
2
* Copyright (c) 2016, 2021, 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
/* @test
25
* @summary Binary data and view tests for byte buffers
26
* @bug 8159257 8258955
27
* @run testng ByteBufferViews
28
*/
29
30
import org.testng.annotations.DataProvider;
31
import org.testng.annotations.Test;
32
33
import java.nio.Buffer;
34
import java.nio.ByteBuffer;
35
import java.nio.ByteOrder;
36
import java.nio.CharBuffer;
37
import java.nio.DoubleBuffer;
38
import java.nio.FloatBuffer;
39
import java.nio.IntBuffer;
40
import java.nio.LongBuffer;
41
import java.nio.ShortBuffer;
42
import java.util.List;
43
import java.util.Map;
44
import java.util.function.Function;
45
import java.util.function.IntFunction;
46
import java.util.function.IntUnaryOperator;
47
import java.util.function.UnaryOperator;
48
import java.util.stream.Collectors;
49
50
import static org.testng.Assert.*;
51
52
public class ByteBufferViews {
53
static final int SIZE = 32;
54
55
// List of buffer allocator functions
56
static final List<Map.Entry<String, IntFunction<ByteBuffer>>> BYTE_BUFFER_ALLOCATE_FUNCTIONS = List.of(
57
// Heap
58
Map.entry("ByteBuffer.allocate(ba)",
59
size -> ByteBuffer.allocate(size)),
60
// Aligned
61
Map.entry("ByteBuffer.allocate(size).position(8)",
62
size -> ByteBuffer.allocate(size).position(8)),
63
Map.entry("ByteBuffer.allocate(size).position(8).slice()",
64
size -> ByteBuffer.allocate(size).position(8).slice()),
65
Map.entry("ByteBuffer.allocate(size).position(8).slice().duplicate()",
66
size -> ByteBuffer.allocate(size).position(8).slice().duplicate()),
67
Map.entry("ByteBuffer.allocate(size).slice(8,size-8)",
68
size -> ByteBuffer.allocate(size).slice(8,size-8)),
69
// Unaligned
70
Map.entry("ByteBuffer.allocate(size).position(1)",
71
size -> ByteBuffer.allocate(size).position(1)),
72
Map.entry("ByteBuffer.allocate(size).position(1).slice()",
73
size -> ByteBuffer.allocate(size).position(1).slice()),
74
Map.entry("ByteBuffer.allocate(size).position(1).slice().duplicate()",
75
size -> ByteBuffer.allocate(size).position(1).slice().duplicate()),
76
Map.entry("ByteBuffer.allocate(size).slice(1,size-1)",
77
size -> ByteBuffer.allocate(size).slice(1,size-1)),
78
79
// Off-heap
80
Map.entry("ByteBuffer.allocateDirect(size)",
81
size -> ByteBuffer.allocateDirect(size)),
82
// Aligned
83
Map.entry("ByteBuffer.allocateDirect(size).position(8)",
84
size -> ByteBuffer.allocateDirect(size).position(8)),
85
Map.entry("ByteBuffer.allocateDirect(size).position(8).slice()",
86
size -> ByteBuffer.allocateDirect(size).position(8).slice()),
87
Map.entry("ByteBuffer.allocateDirect(size).position(8).slice().duplicate()",
88
size -> ByteBuffer.allocateDirect(size).position(8).slice().duplicate()),
89
Map.entry("ByteBuffer.allocateDirect(size).slice(8,size-8)",
90
size -> ByteBuffer.allocateDirect(size).slice(8,size-8)),
91
// Unaligned
92
Map.entry("ByteBuffer.allocateDirect(size).position(1)",
93
size -> ByteBuffer.allocateDirect(size).position(1)),
94
Map.entry("ByteBuffer.allocateDirect(size).position(1).slice()",
95
size -> ByteBuffer.allocateDirect(size).position(1).slice()),
96
Map.entry("ByteBuffer.allocateDirect(size).position(1).slice().duplicate()",
97
size -> ByteBuffer.allocateDirect(size).position(1).slice().duplicate()),
98
Map.entry("ByteBuffer.allocateDirect(size).slice(1,size-1)",
99
size -> ByteBuffer.allocateDirect(size).slice(1,size-1))
100
);
101
102
// List of buffer byte order functions
103
static final List<Map.Entry<String, UnaryOperator<ByteBuffer>>> BYTE_BUFFER_ORDER_FUNCTIONS = List.of(
104
Map.entry("order(ByteOrder.BIG_ENDIAN)",
105
(ByteBuffer bb) -> bb.order(ByteOrder.BIG_ENDIAN)),
106
Map.entry("order(ByteOrder.LITTLE_ENDIAN)",
107
(ByteBuffer bb) -> bb.order(ByteOrder.LITTLE_ENDIAN))
108
);
109
110
// Produce a composition of allocation and byte order buffer functions
111
static List<Map.Entry<String, IntFunction<ByteBuffer>>> composeBufferFunctions(
112
List<Map.Entry<String, IntFunction<ByteBuffer>>> af,
113
List<Map.Entry<String, UnaryOperator<ByteBuffer>>> of) {
114
return af.stream().flatMap(afe -> of.stream().
115
map(ofe -> {
116
String s = afe.getKey() + "." + ofe.getKey();
117
IntFunction<ByteBuffer> f = size -> ofe.getValue().
118
apply(afe.getValue().apply(size));
119
return Map.entry(s, f);
120
})
121
).collect(Collectors.toList());
122
}
123
124
// List of buffer allocator functions to test
125
static final List<Map.Entry<String, IntFunction<ByteBuffer>>> BYTE_BUFFER_FUNCTIONS =
126
composeBufferFunctions(BYTE_BUFFER_ALLOCATE_FUNCTIONS, BYTE_BUFFER_ORDER_FUNCTIONS);
127
128
// Creates a cross product of test arguments for
129
// buffer allocator functions and buffer view functions
130
static Object[][] product(List<? extends Map.Entry<String, ?>> la,
131
List<? extends Map.Entry<String, ?>> lb) {
132
return la.stream().flatMap(lae -> lb.stream().
133
map(lbe -> List.of(
134
lae.getKey() + " -> " + lbe.getKey(),
135
lae.getValue(),
136
lbe.getValue()).toArray()
137
)).toArray(Object[][]::new);
138
}
139
140
static void assertValues(int i, Object bValue, Object bbValue, ByteBuffer bb) {
141
if (!bValue.equals(bbValue)) {
142
fail(String.format("Values %s and %s differ at index %d for %s",
143
bValue, bbValue, i, bb));
144
}
145
}
146
147
static void assertValues(int i, Object bbValue, Object bvValue, ByteBuffer bb, Buffer bv) {
148
if (!bbValue.equals(bvValue)) {
149
fail(String.format("Values %s and %s differ at index %d for %s and %s",
150
bbValue, bvValue, i, bb, bv));
151
}
152
}
153
154
static ByteBuffer allocate(IntFunction<ByteBuffer> f) {
155
return allocate(f, i -> i);
156
}
157
158
static ByteBuffer allocate(IntFunction<ByteBuffer> f, IntUnaryOperator o) {
159
return fill(f.apply(SIZE), o);
160
}
161
162
static ByteBuffer fill(ByteBuffer bb, IntUnaryOperator o) {
163
for (int i = 0; i < bb.limit(); i++) {
164
bb.put(i, (byte) o.applyAsInt(i));
165
}
166
return bb;
167
}
168
169
170
@DataProvider
171
public static Object[][] shortViewProvider() {
172
List<Map.Entry<String, Function<ByteBuffer, ShortBuffer>>> bfs = List.of(
173
Map.entry("bb.asShortBuffer()",
174
bb -> bb.asShortBuffer()),
175
Map.entry("bb.asShortBuffer().slice()",
176
bb -> bb.asShortBuffer().slice()),
177
Map.entry("bb.asShortBuffer().slice(index,length)",
178
bb -> { var sb = bb.asShortBuffer();
179
sb = sb.slice(1, sb.limit() - 1);
180
bb.position(bb.position() + 2);
181
return sb; }),
182
Map.entry("bb.asShortBuffer().slice().duplicate()",
183
bb -> bb.asShortBuffer().slice().duplicate())
184
);
185
186
return product(BYTE_BUFFER_FUNCTIONS, bfs);
187
}
188
189
@Test(dataProvider = "shortViewProvider")
190
public void testShortGet(String desc, IntFunction<ByteBuffer> fbb,
191
Function<ByteBuffer, ShortBuffer> fbi) {
192
ByteBuffer bb = allocate(fbb);
193
ShortBuffer vb = fbi.apply(bb);
194
int o = bb.position();
195
196
for (int i = 0; i < vb.limit(); i++) {
197
short fromBytes = getShortFromBytes(bb, o + i * 2);
198
short fromMethodView = bb.getShort(o + i * 2);
199
assertValues(i, fromBytes, fromMethodView, bb);
200
201
short fromBufferView = vb.get(i);
202
assertValues(i, fromMethodView, fromBufferView, bb, vb);
203
}
204
205
for (int i = 0; i < vb.limit(); i++) {
206
short v = getShortFromBytes(bb, o + i * 2);
207
short a = bb.getShort();
208
assertValues(i, v, a, bb);
209
210
short b = vb.get();
211
assertValues(i, a, b, bb, vb);
212
}
213
214
}
215
216
@Test(dataProvider = "shortViewProvider")
217
public void testShortPut(String desc, IntFunction<ByteBuffer> fbb,
218
Function<ByteBuffer, ShortBuffer> fbi) {
219
ByteBuffer bbfilled = allocate(fbb);
220
ByteBuffer bb = allocate(fbb, i -> 0);
221
ShortBuffer vb = fbi.apply(bb);
222
int o = bb.position();
223
224
for (int i = 0; i < vb.limit(); i++) {
225
short fromFilled = bbfilled.getShort(o + i * 2);
226
227
vb.put(i, fromFilled);
228
short fromMethodView = bb.getShort(o + i * 2);
229
assertValues(i, fromFilled, fromMethodView, bb, vb);
230
}
231
232
for (int i = 0; i < vb.limit(); i++) {
233
short fromFilled = bbfilled.getShort(o + i * 2);
234
235
vb.put(fromFilled);
236
short fromMethodView = bb.getShort();
237
assertValues(i, fromFilled, fromMethodView, bb, vb);
238
}
239
240
241
fill(bb, i -> 0);
242
bb.clear().position(o);
243
vb.clear();
244
245
for (int i = 0; i < vb.limit(); i++) {
246
short fromFilled = bbfilled.getShort(o + i * 2);
247
248
bb.putShort(o + i * 2, fromFilled);
249
short fromBufferView = vb.get(i);
250
assertValues(i, fromFilled, fromBufferView, bb, vb);
251
}
252
253
for (int i = 0; i < vb.limit(); i++) {
254
short fromFilled = bbfilled.getShort(o + i * 2);
255
256
bb.putShort(fromFilled);
257
short fromBufferView = vb.get();
258
assertValues(i, fromFilled, fromBufferView, bb, vb);
259
}
260
}
261
262
static short getShortFromBytes(ByteBuffer bb, int i) {
263
int a = bb.get(i) & 0xFF;
264
int b = bb.get(i + 1) & 0xFF;
265
266
if (bb.order() == ByteOrder.BIG_ENDIAN) {
267
return (short) ((a << 8) | b);
268
}
269
else {
270
return (short) ((b << 8) | a);
271
}
272
}
273
274
@DataProvider
275
public static Object[][] charViewProvider() {
276
List<Map.Entry<String, Function<ByteBuffer, CharBuffer>>> bfs = List.of(
277
Map.entry("bb.asCharBuffer()",
278
bb -> bb.asCharBuffer()),
279
Map.entry("bb.asCharBuffer().slice()",
280
bb -> bb.asCharBuffer().slice()),
281
Map.entry("bb.asCharBuffer().slice(index,length)",
282
bb -> { var cb = bb.asCharBuffer();
283
cb = cb.slice(1, cb.limit() - 1);
284
bb.position(bb.position() + 2);
285
return cb; }),
286
Map.entry("bb.asCharBuffer().slice().duplicate()",
287
bb -> bb.asCharBuffer().slice().duplicate())
288
);
289
290
return product(BYTE_BUFFER_FUNCTIONS, bfs);
291
}
292
293
@Test(dataProvider = "charViewProvider")
294
public void testCharGet(String desc, IntFunction<ByteBuffer> fbb,
295
Function<ByteBuffer, CharBuffer> fbi) {
296
ByteBuffer bb = allocate(fbb);
297
CharBuffer vb = fbi.apply(bb);
298
int o = bb.position();
299
300
for (int i = 0; i < vb.limit(); i++) {
301
char fromBytes = getCharFromBytes(bb, o + i * 2);
302
char fromMethodView = bb.getChar(o + i * 2);
303
assertValues(i, fromBytes, fromMethodView, bb);
304
305
char fromBufferView = vb.get(i);
306
assertValues(i, fromMethodView, fromBufferView, bb, vb);
307
}
308
309
for (int i = 0; i < vb.limit(); i++) {
310
char fromBytes = getCharFromBytes(bb, o + i * 2);
311
char fromMethodView = bb.getChar();
312
assertValues(i, fromBytes, fromMethodView, bb);
313
314
char fromBufferView = vb.get();
315
assertValues(i, fromMethodView, fromBufferView, bb, vb);
316
}
317
318
}
319
320
@Test(dataProvider = "charViewProvider")
321
public void testCharPut(String desc, IntFunction<ByteBuffer> fbb,
322
Function<ByteBuffer, CharBuffer> fbi) {
323
ByteBuffer bbfilled = allocate(fbb);
324
ByteBuffer bb = allocate(fbb, i -> 0);
325
CharBuffer vb = fbi.apply(bb);
326
int o = bb.position();
327
328
for (int i = 0; i < vb.limit(); i++) {
329
char fromFilled = bbfilled.getChar(o + i * 2);
330
331
vb.put(i, fromFilled);
332
char fromMethodView = bb.getChar(o + i * 2);
333
assertValues(i, fromFilled, fromMethodView, bb, vb);
334
}
335
336
for (int i = 0; i < vb.limit(); i++) {
337
char fromFilled = bbfilled.getChar(o + i * 2);
338
339
vb.put(fromFilled);
340
char fromMethodView = bb.getChar();
341
assertValues(i, fromFilled, fromMethodView, bb, vb);
342
}
343
344
345
fill(bb, i -> 0);
346
bb.clear().position(o);
347
vb.clear();
348
349
for (int i = 0; i < vb.limit(); i++) {
350
char fromFilled = bbfilled.getChar(o + i * 2);
351
352
bb.putChar(o + i * 2, fromFilled);
353
char fromBufferView = vb.get(i);
354
assertValues(i, fromFilled, fromBufferView, bb, vb);
355
}
356
357
for (int i = 0; i < vb.limit(); i++) {
358
char fromFilled = bbfilled.getChar(o + i * 2);
359
360
bb.putChar(fromFilled);
361
char fromBufferView = vb.get();
362
assertValues(i, fromFilled, fromBufferView, bb, vb);
363
}
364
}
365
366
static char getCharFromBytes(ByteBuffer bb, int i) {
367
return (char) getShortFromBytes(bb, i);
368
}
369
370
371
@DataProvider
372
public static Object[][] intViewProvider() {
373
List<Map.Entry<String, Function<ByteBuffer, IntBuffer>>> bfs = List.of(
374
Map.entry("bb.asIntBuffer()",
375
bb -> bb.asIntBuffer()),
376
Map.entry("bb.asIntBuffer().slice()",
377
bb -> bb.asIntBuffer().slice()),
378
Map.entry("bb.asIntBuffer().slice(index,length)",
379
bb -> { var ib = bb.asIntBuffer();
380
ib = ib.slice(1, ib.limit() - 1);
381
bb.position(bb.position() + 4);
382
return ib; }),
383
Map.entry("bb.asIntBuffer().slice().duplicate()",
384
bb -> bb.asIntBuffer().slice().duplicate())
385
);
386
387
return product(BYTE_BUFFER_FUNCTIONS, bfs);
388
}
389
390
@Test(dataProvider = "intViewProvider")
391
public void testIntGet(String desc, IntFunction<ByteBuffer> fbb,
392
Function<ByteBuffer, IntBuffer> fbi) {
393
ByteBuffer bb = allocate(fbb);
394
IntBuffer vb = fbi.apply(bb);
395
int o = bb.position();
396
397
for (int i = 0; i < vb.limit(); i++) {
398
int fromBytes = getIntFromBytes(bb, o + i * 4);
399
int fromMethodView = bb.getInt(o + i * 4);
400
assertValues(i, fromBytes, fromMethodView, bb);
401
402
int fromBufferView = vb.get(i);
403
assertValues(i, fromMethodView, fromBufferView, bb, vb);
404
}
405
406
for (int i = 0; i < vb.limit(); i++) {
407
int v = getIntFromBytes(bb, o + i * 4);
408
int a = bb.getInt();
409
assertValues(i, v, a, bb);
410
411
int b = vb.get();
412
assertValues(i, a, b, bb, vb);
413
}
414
415
}
416
417
@Test(dataProvider = "intViewProvider")
418
public void testIntPut(String desc, IntFunction<ByteBuffer> fbb,
419
Function<ByteBuffer, IntBuffer> fbi) {
420
ByteBuffer bbfilled = allocate(fbb);
421
ByteBuffer bb = allocate(fbb, i -> 0);
422
IntBuffer vb = fbi.apply(bb);
423
int o = bb.position();
424
425
for (int i = 0; i < vb.limit(); i++) {
426
int fromFilled = bbfilled.getInt(o + i * 4);
427
428
vb.put(i, fromFilled);
429
int fromMethodView = bb.getInt(o + i * 4);
430
assertValues(i, fromFilled, fromMethodView, bb, vb);
431
}
432
433
for (int i = 0; i < vb.limit(); i++) {
434
int fromFilled = bbfilled.getInt(o + i * 4);
435
436
vb.put(fromFilled);
437
int fromMethodView = bb.getInt();
438
assertValues(i, fromFilled, fromMethodView, bb, vb);
439
}
440
441
442
fill(bb, i -> 0);
443
bb.clear().position(o);
444
vb.clear();
445
446
for (int i = 0; i < vb.limit(); i++) {
447
int fromFilled = bbfilled.getInt(o + i * 4);
448
449
bb.putInt(o + i * 4, fromFilled);
450
int fromBufferView = vb.get(i);
451
assertValues(i, fromFilled, fromBufferView, bb, vb);
452
}
453
454
for (int i = 0; i < vb.limit(); i++) {
455
int fromFilled = bbfilled.getInt(o + i * 4);
456
457
bb.putInt(fromFilled);
458
int fromBufferView = vb.get();
459
assertValues(i, fromFilled, fromBufferView, bb, vb);
460
}
461
}
462
463
static int getIntFromBytes(ByteBuffer bb, int i) {
464
int a = bb.get(i) & 0xFF;
465
int b = bb.get(i + 1) & 0xFF;
466
int c = bb.get(i + 2) & 0xFF;
467
int d = bb.get(i + 3) & 0xFF;
468
469
if (bb.order() == ByteOrder.BIG_ENDIAN) {
470
return ((a << 24) | (b << 16) | (c << 8) | d);
471
}
472
else {
473
return ((d << 24) | (c << 16) | (b << 8) | a);
474
}
475
}
476
477
478
@DataProvider
479
public static Object[][] longViewProvider() {
480
List<Map.Entry<String, Function<ByteBuffer, LongBuffer>>> bfs = List.of(
481
Map.entry("bb.asLongBuffer()",
482
bb -> bb.asLongBuffer()),
483
Map.entry("bb.asLongBuffer().slice()",
484
bb -> bb.asLongBuffer().slice()),
485
Map.entry("bb.asLongBuffer().slice(index,length)",
486
bb -> { var lb = bb.asLongBuffer();
487
lb = lb.slice(1, lb.limit() - 1);
488
bb.position(bb.position() + 8);
489
return lb; }),
490
Map.entry("bb.asLongBuffer().slice().duplicate()",
491
bb -> bb.asLongBuffer().slice().duplicate())
492
);
493
494
return product(BYTE_BUFFER_FUNCTIONS, bfs);
495
}
496
497
@Test(dataProvider = "longViewProvider")
498
public void testLongGet(String desc, IntFunction<ByteBuffer> fbb,
499
Function<ByteBuffer, LongBuffer> fbi) {
500
ByteBuffer bb = allocate(fbb);
501
LongBuffer vb = fbi.apply(bb);
502
int o = bb.position();
503
504
for (int i = 0; i < vb.limit(); i++) {
505
long fromBytes = getLongFromBytes(bb, o + i * 8);
506
long fromMethodView = bb.getLong(o + i * 8);
507
assertValues(i, fromBytes, fromMethodView, bb);
508
509
long fromBufferView = vb.get(i);
510
assertValues(i, fromMethodView, fromBufferView, bb, vb);
511
}
512
513
for (int i = 0; i < vb.limit(); i++) {
514
long v = getLongFromBytes(bb, o + i * 8);
515
long a = bb.getLong();
516
assertValues(i, v, a, bb);
517
518
long b = vb.get();
519
assertValues(i, a, b, bb, vb);
520
}
521
522
}
523
524
@Test(dataProvider = "longViewProvider")
525
public void testLongPut(String desc, IntFunction<ByteBuffer> fbb,
526
Function<ByteBuffer, LongBuffer> fbi) {
527
ByteBuffer bbfilled = allocate(fbb);
528
ByteBuffer bb = allocate(fbb, i -> 0);
529
LongBuffer vb = fbi.apply(bb);
530
int o = bb.position();
531
532
for (int i = 0; i < vb.limit(); i++) {
533
long fromFilled = bbfilled.getLong(o + i * 8);
534
535
vb.put(i, fromFilled);
536
long fromMethodView = bb.getLong(o + i * 8);
537
assertValues(i, fromFilled, fromMethodView, bb, vb);
538
}
539
540
for (int i = 0; i < vb.limit(); i++) {
541
long fromFilled = bbfilled.getLong(o + i * 8);
542
543
vb.put(fromFilled);
544
long fromMethodView = bb.getLong();
545
assertValues(i, fromFilled, fromMethodView, bb, vb);
546
}
547
548
549
fill(bb, i -> 0);
550
bb.clear().position(o);
551
vb.clear();
552
553
for (int i = 0; i < vb.limit(); i++) {
554
long fromFilled = bbfilled.getLong(o + i * 8);
555
556
bb.putLong(o + i * 8, fromFilled);
557
long fromBufferView = vb.get(i);
558
assertValues(i, fromFilled, fromBufferView, bb, vb);
559
}
560
561
for (int i = 0; i < vb.limit(); i++) {
562
long fromFilled = bbfilled.getLong(o + i * 8);
563
564
bb.putLong(fromFilled);
565
long fromBufferView = vb.get();
566
assertValues(i, fromFilled, fromBufferView, bb, vb);
567
}
568
}
569
570
static long getLongFromBytes(ByteBuffer bb, int i) {
571
long a = bb.get(i) & 0xFF;
572
long b = bb.get(i + 1) & 0xFF;
573
long c = bb.get(i + 2) & 0xFF;
574
long d = bb.get(i + 3) & 0xFF;
575
long e = bb.get(i + 4) & 0xFF;
576
long f = bb.get(i + 5) & 0xFF;
577
long g = bb.get(i + 6) & 0xFF;
578
long h = bb.get(i + 7) & 0xFF;
579
580
if (bb.order() == ByteOrder.BIG_ENDIAN) {
581
return ((a << 56) | (b << 48) | (c << 40) | (d << 32) |
582
(e << 24) | (f << 16) | (g << 8) | h);
583
}
584
else {
585
return ((h << 56) | (g << 48) | (f << 40) | (e << 32) |
586
(d << 24) | (c << 16) | (b << 8) | a);
587
}
588
}
589
590
591
@DataProvider
592
public static Object[][] floatViewProvider() {
593
List<Map.Entry<String, Function<ByteBuffer, FloatBuffer>>> bfs = List.of(
594
Map.entry("bb.asFloatBuffer()",
595
bb -> bb.asFloatBuffer()),
596
Map.entry("bb.asFloatBuffer().slice()",
597
bb -> bb.asFloatBuffer().slice()),
598
Map.entry("bb.asFloatBuffer().slice(index,length)",
599
bb -> { var fb = bb.asFloatBuffer();
600
fb = fb.slice(1, fb.limit() - 1);
601
bb.position(bb.position() + 4);
602
return fb; }),
603
Map.entry("bb.asFloatBuffer().slice().duplicate()",
604
bb -> bb.asFloatBuffer().slice().duplicate())
605
);
606
607
return product(BYTE_BUFFER_FUNCTIONS, bfs);
608
}
609
610
@Test(dataProvider = "floatViewProvider")
611
public void testFloatGet(String desc, IntFunction<ByteBuffer> fbb,
612
Function<ByteBuffer, FloatBuffer> fbi) {
613
ByteBuffer bb = allocate(fbb);
614
FloatBuffer vb = fbi.apply(bb);
615
int o = bb.position();
616
617
for (int i = 0; i < vb.limit(); i++) {
618
float fromBytes = getFloatFromBytes(bb, o + i * 4);
619
float fromMethodView = bb.getFloat(o + i * 4);
620
assertValues(i, fromBytes, fromMethodView, bb);
621
622
float fromBufferView = vb.get(i);
623
assertValues(i, fromMethodView, fromBufferView, bb, vb);
624
}
625
626
for (int i = 0; i < vb.limit(); i++) {
627
float v = getFloatFromBytes(bb, o + i * 4);
628
float a = bb.getFloat();
629
assertValues(i, v, a, bb);
630
631
float b = vb.get();
632
assertValues(i, a, b, bb, vb);
633
}
634
635
}
636
637
@Test(dataProvider = "floatViewProvider")
638
public void testFloatPut(String desc, IntFunction<ByteBuffer> fbb,
639
Function<ByteBuffer, FloatBuffer> fbi) {
640
ByteBuffer bbfilled = allocate(fbb);
641
ByteBuffer bb = allocate(fbb, i -> 0);
642
FloatBuffer vb = fbi.apply(bb);
643
int o = bb.position();
644
645
for (int i = 0; i < vb.limit(); i++) {
646
float fromFilled = bbfilled.getFloat(o + i * 4);
647
648
vb.put(i, fromFilled);
649
float fromMethodView = bb.getFloat(o + i * 4);
650
assertValues(i, fromFilled, fromMethodView, bb, vb);
651
}
652
653
for (int i = 0; i < vb.limit(); i++) {
654
float fromFilled = bbfilled.getFloat(o + i * 4);
655
656
vb.put(fromFilled);
657
float fromMethodView = bb.getFloat();
658
assertValues(i, fromFilled, fromMethodView, bb, vb);
659
}
660
661
662
fill(bb, i -> 0);
663
bb.clear().position(o);
664
vb.clear();
665
666
for (int i = 0; i < vb.limit(); i++) {
667
float fromFilled = bbfilled.getFloat(o + i * 4);
668
669
bb.putFloat(o + i * 4, fromFilled);
670
float fromBufferView = vb.get(i);
671
assertValues(i, fromFilled, fromBufferView, bb, vb);
672
}
673
674
for (int i = 0; i < vb.limit(); i++) {
675
float fromFilled = bbfilled.getFloat(o + i * 4);
676
677
bb.putFloat(fromFilled);
678
float fromBufferView = vb.get();
679
assertValues(i, fromFilled, fromBufferView, bb, vb);
680
}
681
}
682
683
static float getFloatFromBytes(ByteBuffer bb, int i) {
684
return Float.intBitsToFloat(getIntFromBytes(bb, i));
685
}
686
687
688
689
@DataProvider
690
public static Object[][] doubleViewProvider() {
691
List<Map.Entry<String, Function<ByteBuffer, DoubleBuffer>>> bfs = List.of(
692
Map.entry("bb.asDoubleBuffer()",
693
bb -> bb.asDoubleBuffer()),
694
Map.entry("bb.asDoubleBuffer().slice()",
695
bb -> bb.asDoubleBuffer().slice()),
696
Map.entry("bb.asDoubleBuffer().slice(index,length)",
697
bb -> { var db = bb.asDoubleBuffer();
698
db = db.slice(1, db.limit() - 1);
699
bb.position(bb.position() + 8);
700
return db; }),
701
Map.entry("bb.asDoubleBuffer().slice().duplicate()",
702
bb -> bb.asDoubleBuffer().slice().duplicate())
703
);
704
705
return product(BYTE_BUFFER_FUNCTIONS, bfs);
706
}
707
708
@Test(dataProvider = "doubleViewProvider")
709
public void testDoubleGet(String desc, IntFunction<ByteBuffer> fbb,
710
Function<ByteBuffer, DoubleBuffer> fbi) {
711
ByteBuffer bb = allocate(fbb);
712
DoubleBuffer vb = fbi.apply(bb);
713
int o = bb.position();
714
715
for (int i = 0; i < vb.limit(); i++) {
716
double fromBytes = getDoubleFromBytes(bb, o + i * 8);
717
double fromMethodView = bb.getDouble(o + i * 8);
718
assertValues(i, fromBytes, fromMethodView, bb);
719
720
double fromBufferView = vb.get(i);
721
assertValues(i, fromMethodView, fromBufferView, bb, vb);
722
}
723
724
for (int i = 0; i < vb.limit(); i++) {
725
double v = getDoubleFromBytes(bb, o + i * 8);
726
double a = bb.getDouble();
727
assertValues(i, v, a, bb);
728
729
double b = vb.get();
730
assertValues(i, a, b, bb, vb);
731
}
732
733
}
734
735
@Test(dataProvider = "doubleViewProvider")
736
public void testDoublePut(String desc, IntFunction<ByteBuffer> fbb,
737
Function<ByteBuffer, DoubleBuffer> fbi) {
738
ByteBuffer bbfilled = allocate(fbb);
739
ByteBuffer bb = allocate(fbb, i -> 0);
740
DoubleBuffer vb = fbi.apply(bb);
741
int o = bb.position();
742
743
for (int i = 0; i < vb.limit(); i++) {
744
double fromFilled = bbfilled.getDouble(o + i * 8);
745
746
vb.put(i, fromFilled);
747
double fromMethodView = bb.getDouble(o + i * 8);
748
assertValues(i, fromFilled, fromMethodView, bb, vb);
749
}
750
751
for (int i = 0; i < vb.limit(); i++) {
752
double fromFilled = bbfilled.getDouble(o + i * 8);
753
754
vb.put(fromFilled);
755
double fromMethodView = bb.getDouble();
756
assertValues(i, fromFilled, fromMethodView, bb, vb);
757
}
758
759
760
fill(bb, i -> 0);
761
bb.clear().position(o);
762
vb.clear();
763
764
for (int i = 0; i < vb.limit(); i++) {
765
double fromFilled = bbfilled.getDouble(o + i * 8);
766
767
bb.putDouble(o + i * 8, fromFilled);
768
double fromBufferView = vb.get(i);
769
assertValues(i, fromFilled, fromBufferView, bb, vb);
770
}
771
772
for (int i = 0; i < vb.limit(); i++) {
773
double fromFilled = bbfilled.getDouble(o + i * 8);
774
775
bb.putDouble(fromFilled);
776
double fromBufferView = vb.get();
777
assertValues(i, fromFilled, fromBufferView, bb, vb);
778
}
779
}
780
781
static double getDoubleFromBytes(ByteBuffer bb, int i) {
782
return Double.longBitsToDouble(getLongFromBytes(bb, i));
783
}
784
}
785
786