Skip to content

Commit 1229321

Browse files
committed
refactor: Enhance docs, add more tests in AffineConverter
1 parent e499d3b commit 1229321

File tree

2 files changed

+91
-13
lines changed

2 files changed

+91
-13
lines changed
Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
11
package com.thealgorithms.conversions;
22

3+
/**
4+
* A utility class to perform affine transformations of the form:
5+
* y = slope * x + intercept.
6+
*
7+
* This class supports inversion and composition of affine transformations.
8+
* It is immutable, meaning each instance represents a fixed transformation.
9+
*/
310
public final class AffineConverter {
411
private final double slope;
512
private final double intercept;
13+
14+
/**
15+
* Constructs an AffineConverter with the given slope and intercept.
16+
*
17+
* @param inSlope The slope of the affine transformation.
18+
* @param inIntercept The intercept (constant term) of the affine transformation.
19+
* @throws IllegalArgumentException if either parameter is NaN.
20+
*/
621
public AffineConverter(final double inSlope, final double inIntercept) {
22+
if (Double.isNaN(inSlope) || Double.isNaN(inIntercept)) {
23+
throw new IllegalArgumentException("Slope and intercept must be valid numbers.");
24+
}
725
slope = inSlope;
826
intercept = inIntercept;
927
}
1028

29+
/**
30+
* Converts the given input value using the affine transformation:
31+
* result = slope * inValue + intercept.
32+
*
33+
* @param inValue The input value to convert.
34+
* @return The transformed value.
35+
*/
1136
public double convert(final double inValue) {
1237
return slope * inValue + intercept;
1338
}
1439

40+
/**
41+
* Returns a new AffineConverter representing the inverse of the current transformation.
42+
* The inverse of y = slope * x + intercept is x = (y - intercept) / slope.
43+
*
44+
* @return A new AffineConverter representing the inverse transformation.
45+
* @throws AssertionError if the slope is zero, as the inverse would be undefined.
46+
*/
1547
public AffineConverter invert() {
16-
assert slope != 0.0;
48+
assert slope != 0.0 : "Slope cannot be zero for inversion.";
1749
return new AffineConverter(1.0 / slope, -intercept / slope);
1850
}
1951

52+
/**
53+
* Composes this affine transformation with another, returning a new AffineConverter.
54+
* If this transformation is f(x) and the other is g(x), the result is f(g(x)).
55+
*
56+
* @param other Another AffineConverter to compose with.
57+
* @return A new AffineConverter representing the composition of the two transformations.
58+
*/
2059
public AffineConverter compose(final AffineConverter other) {
21-
return new AffineConverter(slope * other.slope, slope * other.intercept + intercept);
60+
double newSlope = slope * other.slope;
61+
double newIntercept = slope * other.intercept + intercept;
62+
return new AffineConverter(newSlope, newIntercept);
2263
}
2364
}

src/test/java/com/thealgorithms/conversions/AffineConverterTest.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,43 @@ void setUp() {
1616
}
1717

1818
@Test
19-
void testConstructor() {
19+
void testConstructorWithValidValues() {
2020
assertEquals(3.0, converter.convert(0.0), "Expected value when input is 0.0");
2121
assertEquals(5.0, converter.convert(1.0), "Expected value when input is 1.0");
22-
assertEquals(7.0, converter.convert(2.0), "Expected value when input is 2.0");
2322
}
2423

2524
@Test
26-
void testConvert() {
27-
assertEquals(3.0, converter.convert(0.0), "Conversion at 0.0 should equal the intercept");
28-
assertEquals(7.0, converter.convert(2.0), "2.0 should convert to 7.0");
29-
assertEquals(11.0, converter.convert(4.0), "4.0 should convert to 11.0");
25+
void testConstructorWithInvalidValues() {
26+
assertThrows(IllegalArgumentException.class,
27+
() -> new AffineConverter(Double.NaN, 3.0),
28+
"Constructor should throw IllegalArgumentException for NaN slope");
29+
}
30+
31+
@Test
32+
void testConvertWithNegativeValues() {
33+
assertEquals(-1.0, converter.convert(-2.0), "Negative input should convert correctly");
34+
assertEquals(-3.0, new AffineConverter(-1.0, -1.0).convert(2.0),
35+
"Slope and intercept can be negative");
36+
}
37+
38+
@Test
39+
void testConvertWithFloatingPointPrecision() {
40+
double result = new AffineConverter(1.3333, 0.6667).convert(3.0);
41+
assertEquals(4.6666, result, 1e-4, "Conversion should maintain floating-point precision");
3042
}
3143

3244
@Test
3345
void testInvert() {
3446
AffineConverter inverted = converter.invert();
35-
assertEquals(0.0, inverted.convert(3.0), "Inverted converter should return 0.0 for input 3.0");
36-
assertEquals(1.0, inverted.convert(5.0), "Inverted converter should return 1.0 for input 5.0");
37-
assertEquals(2.0, inverted.convert(7.0), "Inverted converter should return 2.0 for input 7.0");
47+
assertEquals(0.0, inverted.convert(3.0), "Inverted should return 0.0 for input 3.0");
48+
assertEquals(1.0, inverted.convert(5.0), "Inverted should return 1.0 for input 5.0");
3849
}
3950

4051
@Test
4152
void testInvertWithZeroSlope() {
4253
AffineConverter zeroSlopeConverter = new AffineConverter(0.0, 3.0);
43-
assertThrows(AssertionError.class, zeroSlopeConverter::invert, "Invert should throw assertion error when slope is zero");
54+
assertThrows(AssertionError.class, zeroSlopeConverter::invert,
55+
"Invert should throw AssertionError when slope is zero");
4456
}
4557

4658
@Test
@@ -50,6 +62,31 @@ void testCompose() {
5062

5163
assertEquals(7.0, composed.convert(0.0), "Expected composed conversion at 0.0");
5264
assertEquals(9.0, composed.convert(1.0), "Expected composed conversion at 1.0");
53-
assertEquals(11.0, composed.convert(2.0), "Expected composed conversion at 2.0");
65+
}
66+
67+
@Test
68+
void testMultipleCompositions() {
69+
AffineConverter c1 = new AffineConverter(2.0, 1.0);
70+
AffineConverter c2 = new AffineConverter(3.0, -2.0);
71+
AffineConverter c3 = c1.compose(c2); // (2x + 1) ∘ (3x - 2) => 6x - 1
72+
73+
assertEquals(-3.0, c3.convert(0.0), "Composed transformation should return -3.0 at 0.0");
74+
assertEquals(3.0, c3.convert(1.0), "Composed transformation should return 3.0 at 1.0");
75+
}
76+
77+
@Test
78+
void testIdentityComposition() {
79+
AffineConverter identity = new AffineConverter(1.0, 0.0);
80+
AffineConverter composed = converter.compose(identity);
81+
82+
assertEquals(3.0, composed.convert(0.0), "Identity composition should not change the transformation");
83+
assertEquals(7.0, composed.convert(2.0), "Identity composition should behave like the original");
84+
}
85+
86+
@Test
87+
void testLargeInputs() {
88+
double largeValue = 1e6;
89+
assertEquals(2.0 * largeValue + 3.0, converter.convert(largeValue),
90+
"Should handle large input values without overflow");
5491
}
5592
}

0 commit comments

Comments
 (0)