1 /* 2 * Copyright (C) 2012 The Guava Authors 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 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.math; 18 19 import static com.google.common.math.StatsTesting.assertDiagonalLinearTransformation; 20 import static com.google.common.math.StatsTesting.assertHorizontalLinearTransformation; 21 import static com.google.common.math.StatsTesting.assertLinearTransformationNaN; 22 import static com.google.common.math.StatsTesting.assertVerticalLinearTransformation; 23 import static com.google.common.truth.Truth.assertThat; 24 25 import junit.framework.TestCase; 26 27 /** 28 * Tests for {@link LinearTransformation}. 29 * 30 * @author Pete Gillin 31 */ 32 public class LinearTransformationTest extends TestCase { 33 34 private static final double ALLOWED_ERROR = 1e-10; 35 testMappingAnd_regular()36 public void testMappingAnd_regular() { 37 double x1 = 1.2; 38 double y1 = 3.4; 39 double xDelta = 5.6; 40 double yDelta = 7.8; 41 LinearTransformation transformation = 42 LinearTransformation.mapping(x1, y1).and(x1 + xDelta, y1 + yDelta); 43 assertDiagonalLinearTransformation(transformation, x1, y1, xDelta, yDelta); 44 } 45 testMappingAnd_horizontal()46 public void testMappingAnd_horizontal() { 47 double x1 = 1.2; 48 double xDelta = 3.4; 49 double y = 5.6; 50 LinearTransformation transformation = LinearTransformation.mapping(x1, y).and(x1 + xDelta, y); 51 assertHorizontalLinearTransformation(transformation, y); 52 } 53 testMappingAnd_vertical()54 public void testMappingAnd_vertical() { 55 double x = 1.2; 56 double y1 = 3.4; 57 double yDelta = 5.6; 58 LinearTransformation transformation = LinearTransformation.mapping(x, y1).and(x, y1 + yDelta); 59 assertVerticalLinearTransformation(transformation, x); 60 } 61 testMapping_infiniteX1()62 public void testMapping_infiniteX1() { 63 try { 64 LinearTransformation.mapping(Double.POSITIVE_INFINITY, 3.4); 65 fail("Expected IllegalArgumentException from mapping(x, y) with infinite x"); 66 } catch (IllegalArgumentException expected) { 67 } 68 } 69 testMapping_infiniteY1()70 public void testMapping_infiniteY1() { 71 try { 72 LinearTransformation.mapping(1.2, Double.NEGATIVE_INFINITY); 73 fail("Expected IllegalArgumentException from mapping(x, y) with infinite y"); 74 } catch (IllegalArgumentException expected) { 75 } 76 } 77 testMappingAnd_infiniteX2()78 public void testMappingAnd_infiniteX2() { 79 try { 80 LinearTransformation.mapping(1.2, 3.4).and(Double.NEGATIVE_INFINITY, 7.8); 81 fail("Expected IllegalArgumentException from and(x, y) with infinite x"); 82 } catch (IllegalArgumentException expected) { 83 } 84 } 85 testMappingAnd_infiniteY2()86 public void testMappingAnd_infiniteY2() { 87 try { 88 LinearTransformation.mapping(1.2, 3.4).and(5.6, Double.POSITIVE_INFINITY); 89 fail("Expected IllegalArgumentException from and(x, y) with infinite y"); 90 } catch (IllegalArgumentException expected) { 91 } 92 } 93 testMapping_nanX1()94 public void testMapping_nanX1() { 95 try { 96 LinearTransformation.mapping(Double.NaN, 3.4); 97 fail("Expected IllegalArgumentException from mapping(x, y) with NaN x"); 98 } catch (IllegalArgumentException expected) { 99 } 100 } 101 testMapping_nanY1()102 public void testMapping_nanY1() { 103 try { 104 LinearTransformation.mapping(1.2, Double.NaN); 105 fail("Expected IllegalArgumentException from mapping(x, y) with NaN y"); 106 } catch (IllegalArgumentException expected) { 107 } 108 } 109 testMappingAnd_nanX2()110 public void testMappingAnd_nanX2() { 111 try { 112 LinearTransformation.mapping(1.2, 3.4).and(Double.NaN, 7.8); 113 fail("Expected IllegalArgumentException from and(x, y) with NaN x"); 114 } catch (IllegalArgumentException expected) { 115 } 116 } 117 testMappingAnd_nanY2()118 public void testMappingAnd_nanY2() { 119 try { 120 LinearTransformation.mapping(1.2, 3.4).and(5.6, Double.NaN); 121 fail("Expected IllegalArgumentException from and(x, y) with NaN y"); 122 } catch (IllegalArgumentException expected) { 123 } 124 } 125 testMappingAnd_samePointTwice()126 public void testMappingAnd_samePointTwice() { 127 try { 128 double x = 1.2; 129 double y = 3.4; 130 LinearTransformation.mapping(x, y).and(x, y); 131 fail( 132 "Expected IllegalArgumentException from mapping(x1, y1).and(x2, y2) with" 133 + " (x1 == x2) && (y1 == y2)"); 134 } catch (IllegalArgumentException expected) { 135 } 136 } 137 testMappingWithSlope_regular()138 public void testMappingWithSlope_regular() { 139 double x1 = 1.2; 140 double y1 = 3.4; 141 double xDelta = -5.6; 142 double slope = -7.8; 143 LinearTransformation transformation = LinearTransformation.mapping(x1, y1).withSlope(slope); 144 assertDiagonalLinearTransformation(transformation, x1, y1, xDelta, xDelta * slope); 145 } 146 testMappingWithSlope_horizontal()147 public void testMappingWithSlope_horizontal() { 148 double x1 = 1.2; 149 double y = 5.6; 150 LinearTransformation transformation = LinearTransformation.mapping(x1, y).withSlope(0.0); 151 assertHorizontalLinearTransformation(transformation, y); 152 } 153 testMappingWithSlope_vertical()154 public void testMappingWithSlope_vertical() { 155 double x = 1.2; 156 double y1 = 3.4; 157 LinearTransformation transformation = 158 LinearTransformation.mapping(x, y1).withSlope(Double.POSITIVE_INFINITY); 159 assertVerticalLinearTransformation(transformation, x); 160 } 161 testMappingWithSlope_minimalSlope()162 public void testMappingWithSlope_minimalSlope() { 163 double x1 = 1.2; 164 double y1 = 3.4; 165 double slope = Double.MIN_VALUE; 166 LinearTransformation transformation = LinearTransformation.mapping(x1, y1).withSlope(slope); 167 assertThat(transformation.isVertical()).isFalse(); 168 assertThat(transformation.isHorizontal()).isFalse(); 169 assertThat(transformation.slope()).isWithin(ALLOWED_ERROR).of(slope); 170 // Note that we cannot test the actual mapping of points, as the results will be unreliable due 171 // to loss of precision with this value of the slope. 172 } 173 testMappingWithSlope_maximalSlope()174 public void testMappingWithSlope_maximalSlope() { 175 double x1 = 1.2; 176 double y1 = 3.4; 177 double slope = Double.MAX_VALUE; 178 LinearTransformation transformation = LinearTransformation.mapping(x1, y1).withSlope(slope); 179 assertThat(transformation.isVertical()).isFalse(); 180 assertThat(transformation.isHorizontal()).isFalse(); 181 assertThat(transformation.slope()).isWithin(ALLOWED_ERROR).of(slope); 182 // Note that we cannot test the actual mapping of points, as the results will be unreliable due 183 // to loss of precision with this value of the slope. 184 } 185 testMappingWithSlope_nanSlope()186 public void testMappingWithSlope_nanSlope() { 187 try { 188 LinearTransformation.mapping(1.2, 3.4).withSlope(Double.NaN); 189 fail("Expected IllegalArgumentException from withSlope(slope) with NaN slope"); 190 } catch (IllegalArgumentException expected) { 191 } 192 } 193 testVertical_regular()194 public void testVertical_regular() { 195 double x = 1.2; 196 LinearTransformation transformation = LinearTransformation.vertical(x); 197 assertVerticalLinearTransformation(transformation, x); 198 } 199 testVertical_infiniteX()200 public void testVertical_infiniteX() { 201 try { 202 LinearTransformation.vertical(Double.NEGATIVE_INFINITY); 203 fail("Expected IllegalArgumentException from vertical(x) with infinite x"); 204 } catch (IllegalArgumentException expected) { 205 } 206 } 207 testVertical_nanX()208 public void testVertical_nanX() { 209 try { 210 LinearTransformation.vertical(Double.NaN); 211 fail("Expected IllegalArgumentException from vertical(x) with NaN x"); 212 } catch (IllegalArgumentException expected) { 213 } 214 } 215 testHorizontal_regular()216 public void testHorizontal_regular() { 217 double y = 1.2; 218 LinearTransformation transformation = LinearTransformation.horizontal(y); 219 assertHorizontalLinearTransformation(transformation, y); 220 } 221 testHorizontal_infiniteY()222 public void testHorizontal_infiniteY() { 223 try { 224 LinearTransformation.horizontal(Double.POSITIVE_INFINITY); 225 fail("Expected IllegalArgumentException from horizontal(y) with infinite y"); 226 } catch (IllegalArgumentException expected) { 227 } 228 } 229 testHorizontal_nanY()230 public void testHorizontal_nanY() { 231 try { 232 LinearTransformation.horizontal(Double.NaN); 233 fail("Expected IllegalArgumentException from horizontal(y) with NaN y"); 234 } catch (IllegalArgumentException expected) { 235 } 236 } 237 testForNaN()238 public void testForNaN() { 239 LinearTransformation transformation = LinearTransformation.forNaN(); 240 assertLinearTransformationNaN(transformation); 241 } 242 } 243