• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static org.junit.Assert.assertThrows;
25 
26 import junit.framework.TestCase;
27 
28 /**
29  * Tests for {@link LinearTransformation}.
30  *
31  * @author Pete Gillin
32  */
33 public class LinearTransformationTest extends TestCase {
34 
35   private static final double ALLOWED_ERROR = 1e-10;
36 
testMappingAnd_regular()37   public void testMappingAnd_regular() {
38     double x1 = 1.2;
39     double y1 = 3.4;
40     double xDelta = 5.6;
41     double yDelta = 7.8;
42     LinearTransformation transformation =
43         LinearTransformation.mapping(x1, y1).and(x1 + xDelta, y1 + yDelta);
44     assertDiagonalLinearTransformation(transformation, x1, y1, xDelta, yDelta);
45   }
46 
testMappingAnd_horizontal()47   public void testMappingAnd_horizontal() {
48     double x1 = 1.2;
49     double xDelta = 3.4;
50     double y = 5.6;
51     LinearTransformation transformation = LinearTransformation.mapping(x1, y).and(x1 + xDelta, y);
52     assertHorizontalLinearTransformation(transformation, y);
53   }
54 
testMappingAnd_vertical()55   public void testMappingAnd_vertical() {
56     double x = 1.2;
57     double y1 = 3.4;
58     double yDelta = 5.6;
59     LinearTransformation transformation = LinearTransformation.mapping(x, y1).and(x, y1 + yDelta);
60     assertVerticalLinearTransformation(transformation, x);
61   }
62 
testMapping_infiniteX1()63   public void testMapping_infiniteX1() {
64     assertThrows(
65         IllegalArgumentException.class,
66         () -> LinearTransformation.mapping(Double.POSITIVE_INFINITY, 3.4));
67   }
68 
testMapping_infiniteY1()69   public void testMapping_infiniteY1() {
70     assertThrows(
71         IllegalArgumentException.class,
72         () -> LinearTransformation.mapping(1.2, Double.NEGATIVE_INFINITY));
73   }
74 
testMappingAnd_infiniteX2()75   public void testMappingAnd_infiniteX2() {
76     assertThrows(
77         IllegalArgumentException.class,
78         () -> LinearTransformation.mapping(1.2, 3.4).and(Double.NEGATIVE_INFINITY, 7.8));
79   }
80 
testMappingAnd_infiniteY2()81   public void testMappingAnd_infiniteY2() {
82     assertThrows(
83         IllegalArgumentException.class,
84         () -> LinearTransformation.mapping(1.2, 3.4).and(5.6, Double.POSITIVE_INFINITY));
85   }
86 
testMapping_nanX1()87   public void testMapping_nanX1() {
88     assertThrows(
89         IllegalArgumentException.class, () -> LinearTransformation.mapping(Double.NaN, 3.4));
90   }
91 
testMapping_nanY1()92   public void testMapping_nanY1() {
93     assertThrows(
94         IllegalArgumentException.class, () -> LinearTransformation.mapping(1.2, Double.NaN));
95   }
96 
testMappingAnd_nanX2()97   public void testMappingAnd_nanX2() {
98     assertThrows(
99         IllegalArgumentException.class,
100         () -> LinearTransformation.mapping(1.2, 3.4).and(Double.NaN, 7.8));
101   }
102 
testMappingAnd_nanY2()103   public void testMappingAnd_nanY2() {
104     assertThrows(
105         IllegalArgumentException.class,
106         () -> LinearTransformation.mapping(1.2, 3.4).and(5.6, Double.NaN));
107   }
108 
testMappingAnd_samePointTwice()109   public void testMappingAnd_samePointTwice() {
110     double x = 1.2;
111     double y = 3.4;
112     assertThrows(
113         IllegalArgumentException.class,
114         () -> {
115           LinearTransformation.mapping(x, y).and(x, y);
116         });
117   }
118 
testMappingWithSlope_regular()119   public void testMappingWithSlope_regular() {
120     double x1 = 1.2;
121     double y1 = 3.4;
122     double xDelta = -5.6;
123     double slope = -7.8;
124     LinearTransformation transformation = LinearTransformation.mapping(x1, y1).withSlope(slope);
125     assertDiagonalLinearTransformation(transformation, x1, y1, xDelta, xDelta * slope);
126   }
127 
testMappingWithSlope_horizontal()128   public void testMappingWithSlope_horizontal() {
129     double x1 = 1.2;
130     double y = 5.6;
131     LinearTransformation transformation = LinearTransformation.mapping(x1, y).withSlope(0.0);
132     assertHorizontalLinearTransformation(transformation, y);
133   }
134 
testMappingWithSlope_vertical()135   public void testMappingWithSlope_vertical() {
136     double x = 1.2;
137     double y1 = 3.4;
138     LinearTransformation transformation =
139         LinearTransformation.mapping(x, y1).withSlope(Double.POSITIVE_INFINITY);
140     assertVerticalLinearTransformation(transformation, x);
141   }
142 
testMappingWithSlope_minimalSlope()143   public void testMappingWithSlope_minimalSlope() {
144     double x1 = 1.2;
145     double y1 = 3.4;
146     double slope = Double.MIN_VALUE;
147     LinearTransformation transformation = LinearTransformation.mapping(x1, y1).withSlope(slope);
148     assertThat(transformation.isVertical()).isFalse();
149     assertThat(transformation.isHorizontal()).isFalse();
150     assertThat(transformation.slope()).isWithin(ALLOWED_ERROR).of(slope);
151     // Note that we cannot test the actual mapping of points, as the results will be unreliable due
152     // to loss of precision with this value of the slope.
153   }
154 
testMappingWithSlope_maximalSlope()155   public void testMappingWithSlope_maximalSlope() {
156     double x1 = 1.2;
157     double y1 = 3.4;
158     double slope = Double.MAX_VALUE;
159     LinearTransformation transformation = LinearTransformation.mapping(x1, y1).withSlope(slope);
160     assertThat(transformation.isVertical()).isFalse();
161     assertThat(transformation.isHorizontal()).isFalse();
162     assertThat(transformation.slope()).isWithin(ALLOWED_ERROR).of(slope);
163     // Note that we cannot test the actual mapping of points, as the results will be unreliable due
164     // to loss of precision with this value of the slope.
165   }
166 
testMappingWithSlope_nanSlope()167   public void testMappingWithSlope_nanSlope() {
168     assertThrows(
169         IllegalArgumentException.class,
170         () -> LinearTransformation.mapping(1.2, 3.4).withSlope(Double.NaN));
171   }
172 
testVertical_regular()173   public void testVertical_regular() {
174     double x = 1.2;
175     LinearTransformation transformation = LinearTransformation.vertical(x);
176     assertVerticalLinearTransformation(transformation, x);
177   }
178 
testVertical_infiniteX()179   public void testVertical_infiniteX() {
180     assertThrows(
181         IllegalArgumentException.class,
182         () -> LinearTransformation.vertical(Double.NEGATIVE_INFINITY));
183   }
184 
testVertical_nanX()185   public void testVertical_nanX() {
186     assertThrows(IllegalArgumentException.class, () -> LinearTransformation.vertical(Double.NaN));
187   }
188 
testHorizontal_regular()189   public void testHorizontal_regular() {
190     double y = 1.2;
191     LinearTransformation transformation = LinearTransformation.horizontal(y);
192     assertHorizontalLinearTransformation(transformation, y);
193   }
194 
testHorizontal_infiniteY()195   public void testHorizontal_infiniteY() {
196     assertThrows(
197         IllegalArgumentException.class,
198         () -> LinearTransformation.horizontal(Double.POSITIVE_INFINITY));
199   }
200 
testHorizontal_nanY()201   public void testHorizontal_nanY() {
202     assertThrows(IllegalArgumentException.class, () -> LinearTransformation.horizontal(Double.NaN));
203   }
204 
testForNaN()205   public void testForNaN() {
206     LinearTransformation transformation = LinearTransformation.forNaN();
207     assertLinearTransformationNaN(transformation);
208   }
209 }
210