- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathAffineConverterTest.java
87 lines (69 loc) · 3.27 KB
/
AffineConverterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
packagecom.thealgorithms.conversions;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importstaticorg.junit.jupiter.api.Assertions.assertThrows;
importorg.junit.jupiter.api.BeforeEach;
importorg.junit.jupiter.api.Test;
publicclassAffineConverterTest {
privateAffineConverterconverter;
@BeforeEach
voidsetUp() {
converter = newAffineConverter(2.0, 3.0);
}
@Test
voidtestConstructorWithValidValues() {
assertEquals(3.0, converter.convert(0.0), "Expected value when input is 0.0");
assertEquals(5.0, converter.convert(1.0), "Expected value when input is 1.0");
}
@Test
voidtestConstructorWithInvalidValues() {
assertThrows(IllegalArgumentException.class, () -> newAffineConverter(Double.NaN, 3.0), "Constructor should throw IllegalArgumentException for NaN slope");
}
@Test
voidtestConvertWithNegativeValues() {
assertEquals(-1.0, converter.convert(-2.0), "Negative input should convert correctly");
assertEquals(-3.0, newAffineConverter(-1.0, -1.0).convert(2.0), "Slope and intercept can be negative");
}
@Test
voidtestConvertWithFloatingPointPrecision() {
doubleresult = newAffineConverter(1.3333, 0.6667).convert(3.0);
assertEquals(4.6666, result, 1e-4, "Conversion should maintain floating-point precision");
}
@Test
voidtestInvert() {
AffineConverterinverted = converter.invert();
assertEquals(0.0, inverted.convert(3.0), "Inverted should return 0.0 for input 3.0");
assertEquals(1.0, inverted.convert(5.0), "Inverted should return 1.0 for input 5.0");
}
@Test
voidtestInvertWithZeroSlope() {
AffineConverterzeroSlopeConverter = newAffineConverter(0.0, 3.0);
assertThrows(AssertionError.class, zeroSlopeConverter::invert, "Invert should throw AssertionError when slope is zero");
}
@Test
voidtestCompose() {
AffineConverterotherConverter = newAffineConverter(1.0, 2.0);
AffineConvertercomposed = converter.compose(otherConverter);
assertEquals(7.0, composed.convert(0.0), "Expected composed conversion at 0.0");
assertEquals(9.0, composed.convert(1.0), "Expected composed conversion at 1.0");
}
@Test
voidtestMultipleCompositions() {
AffineConverterc1 = newAffineConverter(2.0, 1.0);
AffineConverterc2 = newAffineConverter(3.0, -2.0);
AffineConverterc3 = c1.compose(c2); // (2x + 1) ∘ (3x - 2) => 6x - 1
assertEquals(-3.0, c3.convert(0.0), "Composed transformation should return -3.0 at 0.0");
assertEquals(3.0, c3.convert(1.0), "Composed transformation should return 3.0 at 1.0");
}
@Test
voidtestIdentityComposition() {
AffineConverteridentity = newAffineConverter(1.0, 0.0);
AffineConvertercomposed = converter.compose(identity);
assertEquals(3.0, composed.convert(0.0), "Identity composition should not change the transformation");
assertEquals(7.0, composed.convert(2.0), "Identity composition should behave like the original");
}
@Test
voidtestLargeInputs() {
doublelargeValue = 1e6;
assertEquals(2.0 * largeValue + 3.0, converter.convert(largeValue), "Should handle large input values without overflow");
}
}