|
| 1 | +/* |
| 2 | + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amazon.randomcutforest.returntypes; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +public class RangeVectorTest { |
| 26 | + |
| 27 | + int dimensions; |
| 28 | + private RangeVector vector; |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + public void setUp() { |
| 32 | + dimensions = 3; |
| 33 | + vector = new RangeVector(dimensions); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testNew() { |
| 38 | + assertThrows(IllegalArgumentException.class, () -> new RangeVector(0)); |
| 39 | + assertThrows(IllegalArgumentException.class, () -> new RangeVector(new float[0])); |
| 40 | + float[] expected = new float[dimensions]; |
| 41 | + assertArrayEquals(expected, vector.values); |
| 42 | + assertArrayEquals(expected, vector.upper); |
| 43 | + assertArrayEquals(expected, vector.lower); |
| 44 | + |
| 45 | + float[] another = new float[0]; |
| 46 | + assertThrows(IllegalArgumentException.class, () -> new RangeVector(another, another, another)); |
| 47 | + assertThrows(IllegalArgumentException.class, |
| 48 | + () -> new RangeVector(expected, expected, new float[dimensions + 1])); |
| 49 | + assertThrows(IllegalArgumentException.class, |
| 50 | + () -> new RangeVector(expected, new float[dimensions + 1], expected)); |
| 51 | + assertThrows(IllegalArgumentException.class, |
| 52 | + () -> new RangeVector(new float[dimensions + 1], expected, expected)); |
| 53 | + assertDoesNotThrow(() -> new RangeVector(expected, expected, expected)); |
| 54 | + |
| 55 | + assertThrows(IllegalArgumentException.class, |
| 56 | + () -> new RangeVector(expected, new float[] { -1f, 0f, 0f }, expected)); |
| 57 | + assertDoesNotThrow(() -> new RangeVector(expected, expected, new float[] { -1f, 0f, 0f })); |
| 58 | + |
| 59 | + assertThrows(IllegalArgumentException.class, |
| 60 | + () -> new RangeVector(expected, new float[] { 1f, 0f, 0f }, new float[] { 1f, 0f, 0f })); |
| 61 | + assertDoesNotThrow(() -> new RangeVector(expected, new float[] { 1f, 0f, 0f }, new float[] { -1f, 0f, 0f })); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testScale() { |
| 66 | + vector.upper[0] = 1.1f; |
| 67 | + vector.upper[2] = 3.1f; |
| 68 | + vector.upper[1] = 3.1f; |
| 69 | + vector.lower[1] = -2.2f; |
| 70 | + |
| 71 | + float z = 9.9f; |
| 72 | + assertThrows(IllegalArgumentException.class, () -> vector.scale(0, -1.0f)); |
| 73 | + assertThrows(IllegalArgumentException.class, () -> vector.scale(-1, 1.0f)); |
| 74 | + assertThrows(IllegalArgumentException.class, () -> vector.scale(dimensions + 1, 1.0f)); |
| 75 | + vector.scale(0, z); |
| 76 | + |
| 77 | + float[] expected = new float[] { 1.1f * 9.9f, 3.1f, 3.1f }; |
| 78 | + assertArrayEquals(expected, vector.upper, 1e-6f); |
| 79 | + |
| 80 | + expected = new float[] { 0.0f, -2.2f, 0.0f }; |
| 81 | + assertArrayEquals(expected, vector.lower); |
| 82 | + |
| 83 | + vector.scale(1, 2 * z); |
| 84 | + assertArrayEquals(new float[] { 1.1f * 9.9f, 3.1f * 2 * z, 3.1f }, vector.upper, 1e-6f); |
| 85 | + assertArrayEquals(new float[] { 0f, -2.2f * 2 * z, 0f }, vector.lower, 1e-6f); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testShift() { |
| 90 | + vector.upper[0] = 1.1f; |
| 91 | + vector.upper[2] = 3.1f; |
| 92 | + vector.lower[1] = -2.2f; |
| 93 | + |
| 94 | + float z = -9.9f; |
| 95 | + assertThrows(IllegalArgumentException.class, () -> vector.shift(-1, z)); |
| 96 | + assertThrows(IllegalArgumentException.class, () -> vector.shift(dimensions + 1, z)); |
| 97 | + vector.shift(0, z); |
| 98 | + |
| 99 | + float[] expected = new float[] { 1.1f - 9.9f, 0.0f, 3.1f }; |
| 100 | + assertArrayEquals(expected, vector.upper, 1e-6f); |
| 101 | + |
| 102 | + expected = new float[] { z, -2.2f, 0.0f }; |
| 103 | + assertArrayEquals(expected, vector.lower); |
| 104 | + |
| 105 | + assertArrayEquals(new float[] { z, 0, 0 }, vector.values, 1e-6f); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments