Skip to content

Commit d422bf5

Browse files
authored
Add tests for AffineConverter.java (#5602)
1 parent 25dc55e commit d422bf5

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@
651651
* [VigenereTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/VigenereTest.java)
652652
* [XORCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/XORCipherTest.java)
653653
* conversions
654+
* [AffineConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java)
654655
* [AnyBaseToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AnyBaseToDecimalTest.java)
655656
* [BinaryToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java)
656657
* [BinaryToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToHexadecimalTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class AffineConverterTest {
10+
11+
private AffineConverter converter;
12+
13+
@BeforeEach
14+
void setUp() {
15+
converter = new AffineConverter(2.0, 3.0);
16+
}
17+
18+
@Test
19+
void testConstructor() {
20+
assertEquals(3.0, converter.convert(0.0), "Expected value when input is 0.0");
21+
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");
23+
}
24+
25+
@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");
30+
}
31+
32+
@Test
33+
void testInvert() {
34+
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");
38+
}
39+
40+
@Test
41+
void testInvertWithZeroSlope() {
42+
AffineConverter zeroSlopeConverter = new AffineConverter(0.0, 3.0);
43+
assertThrows(AssertionError.class, zeroSlopeConverter::invert, "Invert should throw assertion error when slope is zero");
44+
}
45+
46+
@Test
47+
void testCompose() {
48+
AffineConverter otherConverter = new AffineConverter(1.0, 2.0);
49+
AffineConverter composed = converter.compose(otherConverter);
50+
51+
assertEquals(7.0, composed.convert(0.0), "Expected composed conversion at 0.0");
52+
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");
54+
}
55+
}

0 commit comments

Comments
 (0)