|
| 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