From 37326b3271751062b91c4f862e71c74312817fcc Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 12:12:45 +0530 Subject: [PATCH 1/3] Add tests for AffineConverter.java --- .../conversions/AffineConverterTest.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/test/java/com/thealgorithms/conversions/AffineConverterTest.java diff --git a/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java b/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java new file mode 100644 index 000000000000..26ce8b71fc6e --- /dev/null +++ b/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java @@ -0,0 +1,55 @@ +package com.thealgorithms.conversions; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class AffineConverterTest { + + private AffineConverter converter; + + @BeforeEach + void setUp() { + converter = new AffineConverter(2.0, 3.0); + } + + @Test + void testConstructor() { + 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"); + assertEquals(7.0, converter.convert(2.0), "Expected value when input is 2.0"); + } + + @Test + void testConvert() { + assertEquals(3.0, converter.convert(0.0), "Conversion at 0.0 should equal the intercept"); + assertEquals(7.0, converter.convert(2.0), "2.0 should convert to 7.0"); + assertEquals(11.0, converter.convert(4.0), "4.0 should convert to 11.0"); + } + + @Test + void testInvert() { + AffineConverter inverted = converter.invert(); + assertEquals(0.0, inverted.convert(3.0), "Inverted converter should return 0.0 for input 3.0"); + assertEquals(1.0, inverted.convert(5.0), "Inverted converter should return 1.0 for input 5.0"); + assertEquals(2.0, inverted.convert(7.0), "Inverted converter should return 2.0 for input 7.0"); + } + + @Test + void testInvertWithZeroSlope() { + AffineConverter zeroSlopeConverter = new AffineConverter(0.0, 3.0); + assertThrows(AssertionError.class, zeroSlopeConverter::invert, "Invert should throw assertion error when slope is zero"); + } + + @Test + void testCompose() { + AffineConverter otherConverter = new AffineConverter(1.0, 2.0); // slope = 1.0, intercept = 2.0 + AffineConverter composed = 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"); + assertEquals(11.0, composed.convert(2.0), "Expected composed conversion at 2.0"); + } +} From a94d8ad1c432a0ec0279b2f4554c46f27f659385 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 6 Oct 2024 06:43:06 +0000 Subject: [PATCH 2/3] Update directory --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1bad5d3b98a3..dc043959550a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -629,6 +629,7 @@ * [VigenereTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/VigenereTest.java) * [XORCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/XORCipherTest.java) * conversions + * [AffineConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java) * [AnyBaseToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AnyBaseToDecimalTest.java) * [BinaryToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java) * [BinaryToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToHexadecimalTest.java) From 729e461d94ed122be8b5a5044cdec7de6ebab460 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 12:39:25 +0530 Subject: [PATCH 3/3] Fix --- .../java/com/thealgorithms/conversions/AffineConverterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java b/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java index 26ce8b71fc6e..2705955f68f6 100644 --- a/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java +++ b/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java @@ -45,7 +45,7 @@ void testInvertWithZeroSlope() { @Test void testCompose() { - AffineConverter otherConverter = new AffineConverter(1.0, 2.0); // slope = 1.0, intercept = 2.0 + AffineConverter otherConverter = new AffineConverter(1.0, 2.0); AffineConverter composed = converter.compose(otherConverter); assertEquals(7.0, composed.convert(0.0), "Expected composed conversion at 0.0");