22 */
23
24 /* Type-specific source code for unit test
25 *
26 * Regenerate the BasicX classes via genBasic.sh whenever this file changes.
27 * We check in the generated source files so that the test tree can be used
28 * independently of the rest of the source tree.
29 */
30
31 #warn This file is preprocessed before being compiled
32
33 #if[byte]
34 import java.io.IOException;
35 import java.io.UncheckedIOException;
36 #end[byte]
37 import java.nio.*;
38 #if[byte]
39 import java.nio.channels.FileChannel;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 #end[byte]
43
44
45 public class Basic$Type$
46 extends Basic
47 {
48
49 private static final $type$[] VALUES = {
50 $Fulltype$.MIN_VALUE,
51 ($type$) -1,
52 ($type$) 0,
53 ($type$) 1,
54 $Fulltype$.MAX_VALUE,
55 #if[float]
56 $Fulltype$.NEGATIVE_INFINITY,
57 $Fulltype$.POSITIVE_INFINITY,
58 $Fulltype$.NaN,
59 ($type$) -0.0,
60 #end[float]
61 #if[double]
475 int ec = l - p;
476 if (as.limit() != ec) {
477 fail("Buffer capacity incorrect, expected: " + ec, as);
478 }
479 }
480 }
481
482 // mapped buffers
483 try {
484 for (MappedByteBuffer bb : mappedBuffers()) {
485 try {
486 int offset = bb.alignmentOffset(1, 4);
487 ck(bb, offset >= 0);
488 } catch (UnsupportedOperationException e) {
489 System.out.println("Not applicable, UOE thrown: ");
490 }
491 }
492 } catch (IOException e) {
493 throw new UncheckedIOException(e);
494 }
495 }
496
497 private static MappedByteBuffer[] mappedBuffers() throws IOException {
498 return new MappedByteBuffer[]{
499 createMappedBuffer(new byte[]{0, 1, 2, 3}),
500 createMappedBuffer(new byte[]{0, 1, 2, -3,
501 45, 6, 7, 78, 3, -7, 6, 7, -128, 127}),
502 };
503 }
504
505 private static MappedByteBuffer createMappedBuffer(byte[] contents)
506 throws IOException {
507 Path tempFile = Files.createTempFile("mbb", null);
508 tempFile.toFile().deleteOnExit();
509 Files.write(tempFile, contents);
510 try (FileChannel fc = FileChannel.open(tempFile)) {
511 MappedByteBuffer map =
512 fc.map(FileChannel.MapMode.READ_ONLY, 0, contents.length);
513 map.load();
514 return map;
|
22 */
23
24 /* Type-specific source code for unit test
25 *
26 * Regenerate the BasicX classes via genBasic.sh whenever this file changes.
27 * We check in the generated source files so that the test tree can be used
28 * independently of the rest of the source tree.
29 */
30
31 #warn This file is preprocessed before being compiled
32
33 #if[byte]
34 import java.io.IOException;
35 import java.io.UncheckedIOException;
36 #end[byte]
37 import java.nio.*;
38 #if[byte]
39 import java.nio.channels.FileChannel;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.util.Random;
43 #end[byte]
44
45
46 public class Basic$Type$
47 extends Basic
48 {
49
50 private static final $type$[] VALUES = {
51 $Fulltype$.MIN_VALUE,
52 ($type$) -1,
53 ($type$) 0,
54 ($type$) 1,
55 $Fulltype$.MAX_VALUE,
56 #if[float]
57 $Fulltype$.NEGATIVE_INFINITY,
58 $Fulltype$.POSITIVE_INFINITY,
59 $Fulltype$.NaN,
60 ($type$) -0.0,
61 #end[float]
62 #if[double]
476 int ec = l - p;
477 if (as.limit() != ec) {
478 fail("Buffer capacity incorrect, expected: " + ec, as);
479 }
480 }
481 }
482
483 // mapped buffers
484 try {
485 for (MappedByteBuffer bb : mappedBuffers()) {
486 try {
487 int offset = bb.alignmentOffset(1, 4);
488 ck(bb, offset >= 0);
489 } catch (UnsupportedOperationException e) {
490 System.out.println("Not applicable, UOE thrown: ");
491 }
492 }
493 } catch (IOException e) {
494 throw new UncheckedIOException(e);
495 }
496
497 // alignment identities
498 final int maxPow2 = 12;
499 ByteBuffer bb = ByteBuffer.allocateDirect(1 << maxPow2); // cap 4096
500
501 Random rnd = new Random();
502 long seed = rnd.nextLong();
503 rnd = new Random(seed);
504
505 for (int i = 0; i < 100; i++) {
506 // 1 == 2^0 <= unitSize == 2^k <= bb.capacity()/2
507 int unitSize = 1 << rnd.nextInt(maxPow2);
508 // 0 <= index < 2*unitSize
509 int index = rnd.nextInt(unitSize << 1);
510 int value = bb.alignmentOffset(index, unitSize);
511 try {
512 if (value < 0 || value >= unitSize) {
513 throw new RuntimeException(value + " < 0 || " +
514 value + " >= " + unitSize);
515 }
516 if (value <= index &&
517 bb.alignmentOffset(index - value, unitSize) != 0)
518 throw new RuntimeException("Identity 1");
519 if (bb.alignmentOffset(index + (unitSize - value),
520 unitSize) != 0)
521 throw new RuntimeException("Identity 2");
522 } catch (RuntimeException re) {
523 System.err.format("seed %d, index %d, unitSize %d, value %d%n",
524 seed, index, unitSize, value);
525 throw re;
526 }
527 }
528 }
529
530 private static MappedByteBuffer[] mappedBuffers() throws IOException {
531 return new MappedByteBuffer[]{
532 createMappedBuffer(new byte[]{0, 1, 2, 3}),
533 createMappedBuffer(new byte[]{0, 1, 2, -3,
534 45, 6, 7, 78, 3, -7, 6, 7, -128, 127}),
535 };
536 }
537
538 private static MappedByteBuffer createMappedBuffer(byte[] contents)
539 throws IOException {
540 Path tempFile = Files.createTempFile("mbb", null);
541 tempFile.toFile().deleteOnExit();
542 Files.write(tempFile, contents);
543 try (FileChannel fc = FileChannel.open(tempFile)) {
544 MappedByteBuffer map =
545 fc.map(FileChannel.MapMode.READ_ONLY, 0, contents.length);
546 map.load();
547 return map;
|