|
| 1 | +package org.asdfformat.asdf.ndarray; |
| 2 | + |
| 3 | +import org.asdfformat.asdf.Asdf; |
| 4 | +import org.asdfformat.asdf.AsdfFile; |
| 5 | +import org.asdfformat.asdf.standard.AsdfStandardType; |
| 6 | +import org.asdfformat.asdf.testing.CoreReferenceFileType; |
| 7 | +import org.asdfformat.asdf.testing.ReferenceFileUtils; |
| 8 | +import org.asdfformat.asdf.util.Version; |
| 9 | +import org.junit.jupiter.api.Tag; |
| 10 | +import org.junit.jupiter.params.ParameterizedTest; |
| 11 | +import org.junit.jupiter.params.provider.Arguments; |
| 12 | +import org.junit.jupiter.params.provider.MethodSource; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.math.BigDecimal; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +import static org.asdfformat.asdf.testing.TestCategories.REFERENCE_TESTS; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +@Tag(REFERENCE_TESTS) |
| 25 | +public class NdArrayFloat16ReferenceTest { |
| 26 | + private static final Version FLOAT16_MIN_VERSION = new Version(1, 6, 0); |
| 27 | + |
| 28 | + private static final CoreReferenceFileType[] FILE_TYPES = { |
| 29 | + CoreReferenceFileType.NDARRAY_FLOAT16_1D_BLOCK_BIG, |
| 30 | + CoreReferenceFileType.NDARRAY_FLOAT16_1D_BLOCK_LITTLE, |
| 31 | + CoreReferenceFileType.NDARRAY_FLOAT16_1D_INLINE, |
| 32 | + }; |
| 33 | + |
| 34 | + private static Stream<Arguments> float16Args() { |
| 35 | + return Arrays.stream(FILE_TYPES) |
| 36 | + .flatMap(fileType -> Arrays.stream(AsdfStandardType.values()) |
| 37 | + .filter(std -> std.getVersion().compareTo(FLOAT16_MIN_VERSION) >= 0) |
| 38 | + .map(std -> Arguments.of(fileType, std))); |
| 39 | + } |
| 40 | + |
| 41 | + @ParameterizedTest |
| 42 | + @MethodSource("float16Args") |
| 43 | + public void testFloat1d(final CoreReferenceFileType coreTestFileType, final AsdfStandardType asdfStandardType) throws IOException { |
| 44 | + final Path path = ReferenceFileUtils.getPath(coreTestFileType, asdfStandardType.getVersion()); |
| 45 | + |
| 46 | + try (final AsdfFile asdfFile = Asdf.open(path)) { |
| 47 | + final FloatNdArray floatNdArray = asdfFile.getTree().get("arr").asNdArray().asFloatNdArray(); |
| 48 | + |
| 49 | + assertEquals(-65504.0f, floatNdArray.get(0)); |
| 50 | + assertEquals(65504.0f, floatNdArray.get(1)); |
| 51 | + assertEquals(5.9604645E-8f, floatNdArray.get(2)); |
| 52 | + assertEquals(0.0f, floatNdArray.get(3)); |
| 53 | + assertTrue(Float.isNaN(floatNdArray.get(4))); |
| 54 | + assertEquals(Float.POSITIVE_INFINITY, floatNdArray.get(5)); |
| 55 | + assertEquals(Float.NEGATIVE_INFINITY, floatNdArray.get(6)); |
| 56 | + assertEquals(3.140625f, floatNdArray.get(7)); |
| 57 | + assertEquals(-3.140625f, floatNdArray.get(8)); |
| 58 | + |
| 59 | + final float[] arr = floatNdArray.toArray(new float[9]); |
| 60 | + assertEquals(-65504.0f, arr[0]); |
| 61 | + assertEquals(65504.0f, arr[1]); |
| 62 | + assertEquals(5.9604645E-8f, arr[2]); |
| 63 | + assertEquals(0.0f, arr[3]); |
| 64 | + assertTrue(Float.isNaN(arr[4])); |
| 65 | + assertEquals(Float.POSITIVE_INFINITY, arr[5]); |
| 66 | + assertEquals(Float.NEGATIVE_INFINITY, arr[6]); |
| 67 | + assertEquals(3.140625f, arr[7]); |
| 68 | + assertEquals(-3.140625f, arr[8]); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @ParameterizedTest |
| 73 | + @MethodSource("float16Args") |
| 74 | + public void testDouble1d(final CoreReferenceFileType coreTestFileType, final AsdfStandardType asdfStandardType) throws IOException { |
| 75 | + final Path path = ReferenceFileUtils.getPath(coreTestFileType, asdfStandardType.getVersion()); |
| 76 | + |
| 77 | + try (final AsdfFile asdfFile = Asdf.open(path)) { |
| 78 | + final DoubleNdArray doubleNdArray = asdfFile.getTree().get("arr").asNdArray().asDoubleNdArray(); |
| 79 | + |
| 80 | + assertEquals(-65504.0, doubleNdArray.get(0)); |
| 81 | + assertEquals(65504.0, doubleNdArray.get(1)); |
| 82 | + assertEquals(5.960464477539063E-8, doubleNdArray.get(2)); |
| 83 | + assertEquals(0.0, doubleNdArray.get(3)); |
| 84 | + assertTrue(Double.isNaN(doubleNdArray.get(4))); |
| 85 | + assertEquals(Double.POSITIVE_INFINITY, doubleNdArray.get(5)); |
| 86 | + assertEquals(Double.NEGATIVE_INFINITY, doubleNdArray.get(6)); |
| 87 | + assertEquals(3.140625, doubleNdArray.get(7)); |
| 88 | + assertEquals(-3.140625, doubleNdArray.get(8)); |
| 89 | + |
| 90 | + final double[] arr = doubleNdArray.toArray(new double[9]); |
| 91 | + assertEquals(-65504.0, arr[0]); |
| 92 | + assertEquals(65504.0, arr[1]); |
| 93 | + assertEquals(5.960464477539063E-8, arr[2]); |
| 94 | + assertEquals(0.0, arr[3]); |
| 95 | + assertTrue(Double.isNaN(arr[4])); |
| 96 | + assertEquals(Double.POSITIVE_INFINITY, arr[5]); |
| 97 | + assertEquals(Double.NEGATIVE_INFINITY, arr[6]); |
| 98 | + assertEquals(3.140625, arr[7]); |
| 99 | + assertEquals(-3.140625, arr[8]); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @ParameterizedTest |
| 104 | + @MethodSource("float16Args") |
| 105 | + public void testBigDecimal1d(final CoreReferenceFileType coreTestFileType, final AsdfStandardType asdfStandardType) throws IOException { |
| 106 | + final Path path = ReferenceFileUtils.getPath(coreTestFileType, asdfStandardType.getVersion()); |
| 107 | + |
| 108 | + try (final AsdfFile asdfFile = Asdf.open(path)) { |
| 109 | + final BigDecimalNdArray bigDecimalNdArray = asdfFile.getTree().get("arr").asNdArray().asBigDecimalNdArray(); |
| 110 | + |
| 111 | + assertEquals(BigDecimal.valueOf(-65504.0), bigDecimalNdArray.get(0)); |
| 112 | + assertEquals(BigDecimal.valueOf(65504.0), bigDecimalNdArray.get(1)); |
| 113 | + assertEquals(BigDecimal.valueOf(5.960464477539063E-8), bigDecimalNdArray.get(2)); |
| 114 | + assertEquals(BigDecimal.valueOf(0.0), bigDecimalNdArray.get(3)); |
| 115 | + assertEquals(BigDecimal.valueOf(3.140625), bigDecimalNdArray.get(7)); |
| 116 | + assertEquals(BigDecimal.valueOf(-3.140625), bigDecimalNdArray.get(8)); |
| 117 | + |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments