From 2e0dea1c30cf43062f6696c1e97fd4de61c919a9 Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Thu, 2 Oct 2025 13:56:36 +0300 Subject: [PATCH 01/56] First commit --- pom.xml | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/pom.xml b/pom.xml index b7ca85e1407c..07ea84531152 100644 --- a/pom.xml +++ b/pom.xml @@ -153,6 +153,53 @@ pmd-exclude.properties + + + org.pitest + pitest-maven + 1.20.2 + + + org.pitest + pitest-junit5-plugin + 1.2.3 + + + + + com.thealgorithms.* + + + com.thealgorithms.*Test + + + com.thealgorithms.*Test + + + DEFAULTS + + 4 + + HTML + XML + + true + 4000 + + -Xmx1024m + + true + + + + pitest + test + + mutationCoverage + + + + From 3ac709ec15396b16b8b45fd2b9edeffdbf2c6575 Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Thu, 9 Oct 2025 21:15:15 +0200 Subject: [PATCH 02/56] deleted the dependabot file that kept issuing pr's --- .github/dependabot.yml | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index 2e5622f7b51d..000000000000 --- a/.github/dependabot.yml +++ /dev/null @@ -1,18 +0,0 @@ ---- -version: 2 -updates: - - package-ecosystem: "docker" - directory: "/" - schedule: - interval: "weekly" - - - package-ecosystem: "github-actions" - directory: "/.github/workflows/" - schedule: - interval: "daily" - - - package-ecosystem: "maven" - directory: "/" - schedule: - interval: "daily" -... From cd8864a7dbe2f73f710e004ab8ffbfb263b16153 Mon Sep 17 00:00:00 2001 From: labe2301 Date: Fri, 10 Oct 2025 21:50:15 +0200 Subject: [PATCH 03/56] [feature] Remove automatic PIT when executing *mvn test* --- pom.xml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pom.xml b/pom.xml index 07ea84531152..4a73e5cbc525 100644 --- a/pom.xml +++ b/pom.xml @@ -190,15 +190,6 @@ true - - - pitest - test - - mutationCoverage - - - From ffcd39bb082fec56d6c1f2ebfe295897f877d7ab Mon Sep 17 00:00:00 2001 From: labe2301 Date: Thu, 16 Oct 2025 21:54:00 +0200 Subject: [PATCH 04/56] [feature] Add tests to AES --- .../com/thealgorithms/ciphers/AESTest.java | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 src/test/java/com/thealgorithms/ciphers/AESTest.java diff --git a/src/test/java/com/thealgorithms/ciphers/AESTest.java b/src/test/java/com/thealgorithms/ciphers/AESTest.java new file mode 100644 index 000000000000..773208f35f17 --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/AESTest.java @@ -0,0 +1,168 @@ +package com.thealgorithms.ciphers; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.math.BigInteger; + +import org.junit.jupiter.api.Test; + +public class AESTest { + @Test + void testScheduleCore() { + BigInteger input = new BigInteger("1a2b3c4d", 16); + int rconCounter = 1; + + BigInteger expected = new BigInteger("f0ebe3a2", 16); + + BigInteger result = AES.scheduleCore(input, rconCounter); + assertEquals(expected, result, "Should return " + expected); + } + + @Test + void testKeyExpansion() { + BigInteger initialKey = new BigInteger("000102030405060708090a0b0c0d0e0f", 16); + + BigInteger[] roundKeys = AES.keyExpansion(initialKey); + + assertEquals(initialKey, roundKeys[0], "First round key should match initial key"); + + for (int i = 1; i < roundKeys.length; i++) { + assertNotEquals(BigInteger.ZERO, roundKeys[i], "Round key " + i + " should not be zero"); + } + } + + @Test + void testSplitBlockIntoCells() { + StringBuilder binary = new StringBuilder(); + + for (int i = 1; i <= 16; i++) { + String byteStr = String.format("%8s", Integer.toBinaryString(i)).replace(" ", "0"); + binary.append(byteStr); + } + + BigInteger block = new BigInteger(binary.toString(), 2); + + int[] expected = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; + + int[] result = AES.splitBlockIntoCells(block); + + assertArrayEquals(expected, result); + } + + @Test + void testMergeCellsIntoBlocks() { + int[] cells = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; + + StringBuilder expectedBinary = new StringBuilder(); + + for (int cell : cells) { + expectedBinary.append(String.format("%8s", Integer.toBinaryString(cell)).replace(" ", "0")); + } + + BigInteger expected = new BigInteger(expectedBinary.toString(), 2); + + BigInteger result = AES.mergeCellsIntoBlock(cells); + + assertEquals(expected, result, "Merged block from cells should match expected BigInteger"); + } + + @Test + void testAddRoundKey() { + BigInteger ciphertext = new BigInteger("a1b2c3d5e5f60718", 16); + BigInteger key = new BigInteger("0f0e0d0c0b0a0909", 16); + + BigInteger expected = new BigInteger("12591166093633785361"); + + BigInteger result = AES.addRoundKey(ciphertext, key); + + assertEquals(expected, result, "Should return XOR of ciphertext and key"); + } + + @Test + void testSubBytes() { + BigInteger input = new BigInteger("000102030405060708090a0b0c0d0e0f", 16); + + BigInteger expected = new BigInteger("132239839819997069106320266673331350390"); + + BigInteger result = AES.subBytes(input); + + assertEquals(expected, result, "Should match correct S-Box substituted block"); + } + + @Test + void testSubBytesDec() { + BigInteger originalBlock = new BigInteger("3243f6a8885a308d313198a2e0370734", 16); + + BigInteger afterSubBytes = AES.subBytes(originalBlock); + BigInteger afterInverse = AES.subBytesDec(afterSubBytes); + + assertEquals(originalBlock, afterInverse, "subBytesDec should reverse subBytes"); + } + + @Test + void testShiftRows() { + BigInteger input = new BigInteger("00010203101112132021222330313233", 16); + + BigInteger expected = new BigInteger("00112233102132032031021330011223", 16); + + BigInteger result = AES.shiftRows(input); + + assertEquals(expected, result, "Should shift rows correctly"); + } + + @Test + void testShiftRowsDec() { + BigInteger originalBlock = new BigInteger("00010203101112132021222330313233", 16); + + BigInteger shifted = AES.shiftRows(originalBlock); + BigInteger unshifted = AES.shiftRowsDec(shifted); + + assertEquals(originalBlock, unshifted, "shiftRowsDec should reverse shiftRows"); + } + + @Test + void testMixColumns() { + BigInteger input = new BigInteger("d4bf5d30e0b452aeb84111f11e2798e5", 16); + + BigInteger expected = new BigInteger("046681e5e0cb199a48f8d37a2806264c", 16); + + BigInteger result = AES.mixColumns(input); + + assertEquals(expected, result, "MixColumns output did not match expected value"); + } + + @Test + void testMixColumnsDec() { + BigInteger input = new BigInteger("000102030405060708090a0b0c0d0e0f", 16); + + BigInteger mixed = AES.mixColumns(input); + BigInteger unmixed = AES.mixColumnsDec(mixed); + + assertEquals(input, unmixed, "mixColumnsDec should reverse mixColumns"); + } + + @Test + void testEncrypt() { + BigInteger plainText = new BigInteger("00112233445566778899aabbccddeeff", 16); + BigInteger key = new BigInteger("000102030405060708090a0b0c0d0e0f", 16); + + BigInteger expectedCipherText = new BigInteger("69c4e0d86a7b0430d8cdb78070b4c55a", 16); + + BigInteger actualCipherText = AES.encrypt(plainText, key); + + assertEquals(expectedCipherText, actualCipherText, "Encrypt output should match known ciphertext"); + } + + @Test + void testDecrypt() { + BigInteger plaintext = new BigInteger("00112233445566778899aabbccddeeff", 16); + BigInteger key = new BigInteger("000102030405060708090a0b0c0d0e0f", 16); + + BigInteger ciphertext = AES.encrypt(plaintext, key); + BigInteger decrypted = AES.decrypt(ciphertext, key); + + assertEquals(plaintext, decrypted, "Decrypt should reverse Encrypt"); + } +} From 25e9f88dd89fd16407ab6da573ca91ea1099810e Mon Sep 17 00:00:00 2001 From: labe2301 Date: Sat, 18 Oct 2025 08:34:46 +0200 Subject: [PATCH 05/56] [refactor] Rename AESTest --- .../com/thealgorithms/ciphers/{AESTest.java => MyAESTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/test/java/com/thealgorithms/ciphers/{AESTest.java => MyAESTest.java} (99%) diff --git a/src/test/java/com/thealgorithms/ciphers/AESTest.java b/src/test/java/com/thealgorithms/ciphers/MyAESTest.java similarity index 99% rename from src/test/java/com/thealgorithms/ciphers/AESTest.java rename to src/test/java/com/thealgorithms/ciphers/MyAESTest.java index 773208f35f17..f67362c810cf 100644 --- a/src/test/java/com/thealgorithms/ciphers/AESTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MyAESTest.java @@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test; -public class AESTest { +public class MyAESTest { @Test void testScheduleCore() { BigInteger input = new BigInteger("1a2b3c4d", 16); From 43a6de033e9f8c0dca4b9ea6d0800f1f65668258 Mon Sep 17 00:00:00 2001 From: labe2301 Date: Sat, 18 Oct 2025 10:02:51 +0200 Subject: [PATCH 06/56] [feature] Kill mutations in ColumnarTranspositionCipher --- .../MyColumnarTranspositionCipherTest.java | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java diff --git a/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java b/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java new file mode 100644 index 000000000000..484318bfdeff --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java @@ -0,0 +1,132 @@ +package com.thealgorithms.ciphers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class MyColumnarTranspositionCipherTest { + private String keyword; + private String plaintext; + + @BeforeEach + public void setUp() { + keyword = "keyword"; + plaintext = "This is a test message for Columnar Transposition Cipher"; + } + + @Test + void shouldNotProduceNullOrEmptyEncryptedText() { + String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword); + + assertNotNull(encrypted, "Encrypted text should not be null"); + assertFalse(encrypted.isEmpty(), "Encrypted text should not be empty"); + } + + @Test + void shouldChangePlaintextToEncrypted() { + String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword); + + assertNotEquals(plaintext, encrypted, "Encrypted text should differ from plaintext"); + } + + @Test + void shouldEncryptDetermenistically() { + String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); + String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); + + assertEquals(encrypted1, encrypted2, "Encryptions should be equal"); + } + + @Test + void shouldProduceDifferentEncryptionsWithDifferentKeywoards() { + String keyword2 = keyword + "a"; + + String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); + String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword2); + + assertNotEquals(encrypted1, encrypted2, "Should produce different encryptions"); + } + + @Test + void shouldMatchWithUnsortedKeyword() { + String myPlaintext = "123456789"; + String myKeyword = "unsorted"; + + String encrypted = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); + String expected = "8≈7≈2≈4≈5≈3≈6≈19"; + + assertEquals(expected, encrypted, "Should match"); + } + + @Test + void shouldMatchEncryptionAndDecryptionWithNoSpacesOrPadding() { + String myPlaintext = "NoSpacesOrPadding"; + + String encrypted = ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); + String decrypted = ColumnarTranspositionCipher.decrypt(); + + assertEquals(myPlaintext, decrypted, "Decrypted text should match original plaintext"); + } + + @Test + void shouldNotContainPaddingInDecryption() { + String myPlaintext = "text"; + + String encrypted = ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); + String decrypted = ColumnarTranspositionCipher.decrypt(); + + assertFalse(decrypted.contains("≈"), "Should not contain padding characters"); + } + + @Test + void shouldEncryptWithKeywordLongerThanPlaintext() { + String myPlaintext = "text"; + String myKeyword = "averylongkeyword"; + + String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); + + assertNotNull(encryption, "Should encrypt where plaintext.length() < keyword.length()"); + } + + @Test + void shouldEncryptWithKeywordWithSameLengthAsPlaintext() { + String myPlaintext = "textaslongaskeyword"; + String myKeyword = "keywordaslongastext"; + + String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); + + assertNotNull(encryption, "Should encrypt where plaintext.length() == keyword.length()"); + } + + @Test + void shouldProduceDifferentEncryptionForSameKeywordButSortedDifferently() { + String unsertedKeyword1 = "EFGHABCD"; + String unsertedKeyword2 = "AEBFCGDH"; + + String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword1); + String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword2); + + assertNotEquals(encrypted1, encrypted2, "Should differ with different keywords"); + } + + @Test + void shouldEncryptWithCustomAbecedarium() { + String myAbecedarium = "abcdefghijklmnopqrstuvwxyz"; + + String encryption = ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium); + + assertNotNull(encryption, "Should encrypt with custom abecedarium"); + } + + @Test + void shouldNotEncryptWithInvalidAbecedarium() { + String myAbecedarium = "abcde"; + + assertThrows(NullPointerException.class, () -> ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium), "Should throw error when keyword contains characters not present in abecedarium"); + } +} From faae7b2b55dba57283a12235d51f29cc96418ea0 Mon Sep 17 00:00:00 2001 From: labe2301 Date: Sat, 18 Oct 2025 15:49:39 +0200 Subject: [PATCH 07/56] [feature] Kill mutations and strengthen code for Caesar --- .../thealgorithms/ciphers/MyCaesarTest.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java diff --git a/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java b/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java new file mode 100644 index 000000000000..0a35fe426ebe --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java @@ -0,0 +1,85 @@ +package com.thealgorithms.ciphers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +public class MyCaesarTest { + private final Caesar caesar = new Caesar(); + + @Test + void shouldReturnSameAsInputWhenShiftingZeroOr26() { + String message = "message"; + + String encoded1 = caesar.encode(message, 0); + String encoded2 = caesar.encode(message, 26); + + assertEquals(message, encoded1, "Encoded should be same as original"); + assertEquals(message, encoded2, "Encoded should be same as original"); + } + + @Test + void shouldReturnsameAsInputWhenUsingCharactersOutsideLatinAlphabet() { + String message = "!#¤%&/()=?`^½§@£$€{[]}´`¨~'*-.,_:;<>|"; + + String encoded = caesar.encode(message, 10); + + assertEquals(message, encoded); + } + + @Test + void shouldWrapZToAWhenEncodingSingleShift() { + String message = "zZ"; + + String encoded = caesar.encode(message, 1); + String exptected = "aA"; + + assertEquals(exptected, encoded, "zZ should wrap to aA"); + } + + @Test + void shouldWrapAToZWhenDecodingSingleShift() { + String message = "aA"; + + String decoded = caesar.decode(message, 1); + String expected = "zZ"; + + assertEquals(expected, decoded); + } + + @Test + void shouldNotWrapWhenEncodingFromYToZ() { + String message = "yY"; + + String encoded = caesar.encode(message, 1); + String expected = "zZ"; + + assertEquals(expected, encoded); + } + + @Test + void shouldNotWrapWhenDecodingFromBToA() { + String message = "bB"; + + String decoded = caesar.decode(message, 1); + String expected = "aA"; + + assertEquals(expected, decoded); + } + + @Test + void shouldContain27CombinationsFromBruteForce() { + String message = "message"; + + String encoded = caesar.encode(message, 10); + String[] combinations = caesar.bruteforce(encoded); + System.out.println(encoded); + String expected = "wocckqo"; + + assertEquals(27, combinations.length, "Should contain 27 possible decoded combinations"); + assertEquals(expected, combinations[0], "First combination should contain encoded message"); + assertEquals(message, combinations[10], "10:th entry should contain original message"); + assertEquals(expected, combinations[26], "26:th entry should be the same as the 0:th entry, the encoded message"); + } +} From fa892200ea7cad6c55e5217e4aafcd3813f14a3e Mon Sep 17 00:00:00 2001 From: labe2301 Date: Sun, 19 Oct 2025 10:20:57 +0200 Subject: [PATCH 08/56] [feature] Add tests to PlayfairCipher --- .../ciphers/MyPlayfairCipherTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java diff --git a/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java b/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java new file mode 100644 index 000000000000..5b1e35dbe128 --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java @@ -0,0 +1,40 @@ +package com.thealgorithms.ciphers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.lang.reflect.Method; +import java.util.Arrays; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class MyPlayfairCipherTest { + private PlayfairCipher playfair; + private final String keyword = "KEYWORD"; + + @BeforeEach + public void setup() { + playfair = new PlayfairCipher(keyword); + } + + @Test + void shouldEncryptAndDecryptDuringSameRowDigraph() { + String plaintext = keyword.substring(0, 2); + + String encrypted = playfair.encrypt(plaintext); + String decrypted = playfair.decrypt(encrypted); + + assertEquals(plaintext, decrypted, "Should not decrypt to the same letters"); + } + + @Test + void shouldPadOddLengthplaintext() { + String plaintext = "cat"; + + String encrypted = playfair.encrypt(plaintext); + + assertTrue(encrypted.length() % 2 == 0, "Should be even length"); + } +} From 54ed85580b179ac750989c12e9d1d2ec27ad60c6 Mon Sep 17 00:00:00 2001 From: labe2301 Date: Mon, 20 Oct 2025 21:34:29 +0200 Subject: [PATCH 09/56] Remove debug print statement from MyCaesarTest --- src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java b/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java index 0a35fe426ebe..28c90a4ca50a 100644 --- a/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java @@ -74,7 +74,6 @@ void shouldContain27CombinationsFromBruteForce() { String encoded = caesar.encode(message, 10); String[] combinations = caesar.bruteforce(encoded); - System.out.println(encoded); String expected = "wocckqo"; assertEquals(27, combinations.length, "Should contain 27 possible decoded combinations"); From 6ac376101aaecfb43dc86fe075343ba4acfa2809 Mon Sep 17 00:00:00 2001 From: labe2301 Date: Mon, 20 Oct 2025 21:37:34 +0200 Subject: [PATCH 10/56] Restore PIT mutation coverage execution to pom.xml --- pom.xml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pom.xml b/pom.xml index 4a73e5cbc525..07ea84531152 100644 --- a/pom.xml +++ b/pom.xml @@ -190,6 +190,15 @@ true + + + pitest + test + + mutationCoverage + + + From 0a7b4c85b8517019cd2290c8663238dce000838a Mon Sep 17 00:00:00 2001 From: hedint <2022.195@student.setur.fo> Date: Sat, 11 Oct 2025 16:57:29 +0100 Subject: [PATCH 11/56] aded test for floor --- .../com/thealgorithms/maths/MathBuilderTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index dc381bfca5d3..7989722311bb 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.doubleThat; import org.junit.jupiter.api.Test; @@ -49,4 +50,19 @@ void areaOfCircle() { double area = new MathBuilder.Builder().pi().openParenthesis(4).multiply(4).closeParenthesisAndMultiply().build().get(); assertEquals(Math.PI * 4 * 4, area); } + @Test + void floorTest(){ + // floor(10.5 + (20+2.1)) + double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndPlus().floor().build().get(); + double expected = Math.floor(10.5+20+2.1); + + // 10.5 + floor((20+2.1)) + double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).floor().closeParenthesisAndPlus().build().get(); + double expected2 = 10.5+Math.floor(20+2.1); + + + assertEquals(expected,actual); + assertEquals(expected2,actual2); + } + } From ba86402e174c05eda5303d4a713f02a4b055255a Mon Sep 17 00:00:00 2001 From: hedint <2022.195@student.setur.fo> Date: Sat, 11 Oct 2025 17:03:10 +0100 Subject: [PATCH 12/56] aded test for closeParenthesisAnd --- .../com/thealgorithms/maths/MathBuilderTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 7989722311bb..12c1c804c052 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -61,6 +61,21 @@ void floorTest(){ double expected2 = 10.5+Math.floor(20+2.1); + assertEquals(expected,actual); + assertEquals(expected2,actual2); + } + @Test + void closeParenthesisAndOther(){ + // 10.5 - (20+2.1) + double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndMinus().build().get(); + double expected = 10.5-(20+2.1); + + + // 10.5 / (20+2.1) + double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndDivide().build().get(); + double expected2 = 10.5/(20+2.1); + + assertEquals(expected,actual); assertEquals(expected2,actual2); } From ffad9a6e45858122316ffe3eff750e2cd2b44dae Mon Sep 17 00:00:00 2001 From: hedint <2022.195@student.setur.fo> Date: Sat, 11 Oct 2025 17:19:44 +0100 Subject: [PATCH 13/56] not run pitest when calling test --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 07ea84531152..fbd9baada8c1 100644 --- a/pom.xml +++ b/pom.xml @@ -193,7 +193,7 @@ pitest - test + pitest mutationCoverage From 9b9f86bef9b74c960dcc11decd9019385de3d904 Mon Sep 17 00:00:00 2001 From: hedint <2022.195@student.setur.fo> Date: Tue, 14 Oct 2025 11:16:17 +0100 Subject: [PATCH 14/56] test for some random and divide by 0 --- .../thealgorithms/maths/MathBuilderTest.java | 219 ++++++++++++------ 1 file changed, 148 insertions(+), 71 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 12c1c804c052..9e6d015151ca 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -2,82 +2,159 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.doubleThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import java.util.Random; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.DisplayName; class MathBuilderTest { - @Test - void simpleMath() { - double result = new MathBuilder.Builder(100).add(200).multiply(10).print().divideIf(20, (a, b) -> a % 2 == 0).sqrt().print().ceil().build().get(); - assertEquals(13, result); - } - - @Test - void memoryTest() { - long result = new MathBuilder.Builder().set(100).sqrt().remember().add(21).recallIf(a -> a < 50, true).mod(2).build().toLong(); - assertEquals(0, result); - } - - @Test - void freeFallDistance() { - long distance = new MathBuilder.Builder(9.81).multiply(0.5).multiply(5 * 5).round().build().toLong(); - assertEquals(123, distance); // Expected result: 0.5 * 9.81 * 25 = 122.625 ≈ 123 - } - - @Test - void batchSalaryProcessing() { - double[] salaries = {2000, 3000, 4000, 5000}; - long[] processedSalaries = new long[salaries.length]; - for (int i = 0; i < salaries.length; i++) { - processedSalaries[i] = new MathBuilder.Builder(salaries[i]).addIf(salaries[i] * 0.1, (sal, bonus) -> sal > 2500).multiply(0.92).round().build().toLong(); - } - long[] expectedSalaries = {1840, 3036, 4048, 5060}; - assertArrayEquals(expectedSalaries, processedSalaries); - } - - @Test - void parenthesis() { - // 10 + (20*5) - 40 + (100 / 10) = 80 - double result = new MathBuilder.Builder(10).openParenthesis(20).multiply(5).closeParenthesisAndPlus().minus(40).openParenthesis(100).divide(10).closeParenthesisAndPlus().build().get(); - assertEquals(80, result); - } - - @Test - void areaOfCircle() { - // Radius is 4 - double area = new MathBuilder.Builder().pi().openParenthesis(4).multiply(4).closeParenthesisAndMultiply().build().get(); - assertEquals(Math.PI * 4 * 4, area); - } - @Test - void floorTest(){ - // floor(10.5 + (20+2.1)) - double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndPlus().floor().build().get(); - double expected = Math.floor(10.5+20+2.1); - - // 10.5 + floor((20+2.1)) - double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).floor().closeParenthesisAndPlus().build().get(); - double expected2 = 10.5+Math.floor(20+2.1); - - - assertEquals(expected,actual); - assertEquals(expected2,actual2); - } - @Test - void closeParenthesisAndOther(){ - // 10.5 - (20+2.1) - double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndMinus().build().get(); - double expected = 10.5-(20+2.1); - - - // 10.5 / (20+2.1) - double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndDivide().build().get(); - double expected2 = 10.5/(20+2.1); - - - assertEquals(expected,actual); - assertEquals(expected2,actual2); - } + @Test + void simpleMath() { + double result = new MathBuilder.Builder(100).add(200).multiply(10).print() + .divideIf(20, (a, b) -> a % 2 == 0).sqrt().print().ceil().build().get(); + assertEquals(13, result); + } + @Test + void memoryTest() { + long result = new MathBuilder.Builder().set(100).sqrt().remember().add(21).recallIf(a -> a < 50, true) + .mod(2).build().toLong(); + assertEquals(0, result); + } + + @Test + void freeFallDistance() { + long distance = new MathBuilder.Builder(9.81).multiply(0.5).multiply(5 * 5).round().build().toLong(); + assertEquals(123, distance); // Expected result: 0.5 * 9.81 * 25 = 122.625 ≈ 123 + } + + @Test + void batchSalaryProcessing() { + double[] salaries = { 2000, 3000, 4000, 5000 }; + long[] processedSalaries = new long[salaries.length]; + for (int i = 0; i < salaries.length; i++) { + processedSalaries[i] = new MathBuilder.Builder(salaries[i]) + .addIf(salaries[i] * 0.1, (sal, bonus) -> sal > 2500).multiply(0.92).round() + .build().toLong(); + } + long[] expectedSalaries = { 1840, 3036, 4048, 5060 }; + assertArrayEquals(expectedSalaries, processedSalaries); + } + + @Test + void parenthesis() { + // 10 + (20*5) - 40 + (100 / 10) = 80 + double result = new MathBuilder.Builder(10).openParenthesis(20).multiply(5).closeParenthesisAndPlus() + .minus(40).openParenthesis(100).divide(10).closeParenthesisAndPlus().build().get(); + assertEquals(80, result); + } + + @Test + void areaOfCircle() { + // Radius is 4 + double area = new MathBuilder.Builder().pi().openParenthesis(4).multiply(4) + .closeParenthesisAndMultiply().build().get(); + assertEquals(Math.PI * 4 * 4, area); + } + + @Test + @DisplayName("Floor Test") + void floorTest() { + // floor(10.5 + (20+2.1)) + double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndPlus() + .floor().build().get(); + double expected = Math.floor(10.5 + 20 + 2.1); + + // 10.5 + floor((20+2.1)) + double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).floor() + .closeParenthesisAndPlus().build().get(); + double expected2 = 10.5 + Math.floor(20 + 2.1); + + assertEquals(expected, actual); + assertEquals(expected2, actual2); + } + + @Test + @DisplayName("Close parntasis Test") + void closeParenthesisAndOtherTest() { + // 10.5 - (20+2.1) + double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndMinus() + .build().get(); + double expected = 10.5 - (20 + 2.1); + + // 10.5 / (20+2.1) + double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndDivide() + .build().get(); + double expected2 = 10.5 / (20 + 2.1); + + assertEquals(expected, actual); + assertEquals(expected2, actual2); + } + + @Test + @DisplayName("Runtime Errors Tests") + void runtimeErrorTest() { + // 10.5 - (20+2.1) + MathBuilder.Builder actual = new MathBuilder.Builder(10.5); + + // 10.5 / (20+2.1) + + assertThrows(RuntimeException.class, () -> actual.rand(1)); + assertThrows(RuntimeException.class, () -> actual.randomInRange(1, 10)); + assertThrows(RuntimeException.class, () -> actual.pi()); + assertThrows(RuntimeException.class, () -> actual.e()); + assertThrows(RuntimeException.class, () -> actual.set(1)); + } + + @Test + @DisplayName("Should divide 10 by 2") + void divideByNum() { + double actual = new MathBuilder.Builder(10).divide(2).build().get(); + + double expected = 10 / 2; + + assertEquals(expected, actual); + } + + @Test + @DisplayName("This Should throw but does not") + void divideByZero() { + MathBuilder.Builder actual2 = new MathBuilder.Builder(10.5).openParenthesis(0) + .closeParenthesisAndDivide(); + + assertDoesNotThrow(() -> actual2.build().get()); + assertDoesNotThrow(() -> actual2.divide(0).build().get()); + + } + + @Test + void randomFunctions() { + + double minValue = 0.0; + double maxValue = 2.1; + double actual = new MathBuilder.Builder().rand(2L).build().get(); + double actual2 = new MathBuilder.Builder().randomInRange(minValue,maxValue).build().get(); + + assertTrue(actual < maxValue); + assertTrue(actual2 >= minValue); + assertTrue(actual2 <= maxValue); + + } + @Test + void toRadiansTest() { + + double expected = Math.toRadians(10); + double expected2 = 2+Math.toRadians(10); + + double actual = new MathBuilder.Builder(10).toRadians().build().get(); + double actual2 = new MathBuilder.Builder(2).openParenthesis(10).toRadians().closeParenthesisAndPlus().build().get(); + + assertEquals(expected, actual); + assertEquals(expected2, actual2); + } } From bcbc15dfcae2532a19c11e33164a5c30f0df8c85 Mon Sep 17 00:00:00 2001 From: hedint <2022.195@student.setur.fo> Date: Thu, 16 Oct 2025 11:49:57 +0100 Subject: [PATCH 15/56] done some romannumerals --- .../thealgorithms/maths/MathBuilderTest.java | 12 +++--- .../maths/RomanNumeralUtilTest.java | 40 +++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 9e6d015151ca..84e183bcd34d 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -6,8 +6,8 @@ import static org.mockito.ArgumentMatchers.doubleThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - import java.util.Random; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.DisplayName; @@ -138,21 +138,23 @@ void randomFunctions() { double minValue = 0.0; double maxValue = 2.1; double actual = new MathBuilder.Builder().rand(2L).build().get(); - double actual2 = new MathBuilder.Builder().randomInRange(minValue,maxValue).build().get(); + double actual2 = new MathBuilder.Builder().randomInRange(minValue, maxValue).build().get(); assertTrue(actual < maxValue); assertTrue(actual2 >= minValue); assertTrue(actual2 <= maxValue); } + @Test void toRadiansTest() { - + double expected = Math.toRadians(10); - double expected2 = 2+Math.toRadians(10); + double expected2 = 2 + Math.toRadians(10); double actual = new MathBuilder.Builder(10).toRadians().build().get(); - double actual2 = new MathBuilder.Builder(2).openParenthesis(10).toRadians().closeParenthesisAndPlus().build().get(); + double actual2 = new MathBuilder.Builder(2).openParenthesis(10).toRadians().closeParenthesisAndPlus() + .build().get(); assertEquals(expected, actual); assertEquals(expected2, actual2); diff --git a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java new file mode 100644 index 000000000000..3e6290a3b3d4 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java @@ -0,0 +1,40 @@ +package com.thealgorithms.maths; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + + +class RomanNumeralUtilTest{ + + + @ParameterizedTest() + @CsvSource( + { + "2000,MM", + "1,I", + "2,II", + "1003,MIII", + "1004,MIV" + + }) + void minimumMaximumTest(int testint,String word){ + + + System.out.println(word + ": " + testint); + assertEquals(RomanNumeralUtil.generate(testint),word); + + } + @Test + void calcSomeNumerals(){ + + + assertThrows(IllegalArgumentException.class, ()->RomanNumeralUtil.generate(0)); + assertThrows(IllegalArgumentException.class, ()->RomanNumeralUtil.generate(6000)); + + } +} From 792f748ecb86690033919d8937e2dac6c66d5031 Mon Sep 17 00:00:00 2001 From: labe2301 Date: Sat, 18 Oct 2025 22:48:59 +0200 Subject: [PATCH 16/56] [feature] Create 100% line coverage, mutation coverage and test strength for RomanNumeralUtil --- .../RomanNumeralUtilConstructorTest.java | 19 +++++++++++++++++++ .../maths/RomanNumeralUtilTest.java | 10 ++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java diff --git a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java new file mode 100644 index 000000000000..2b583d14093d --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java @@ -0,0 +1,19 @@ +package com.thealgorithms.maths; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.lang.reflect.Constructor; + +import org.junit.jupiter.api.Test; + +// Covers the private constructor for code coverage tools +public class RomanNumeralUtilConstructorTest { + @Test + void shouldInvokePrivateConstructor() throws Exception { + Constructor constructor = RomanNumeralUtil.class.getDeclaredConstructor(); + constructor.setAccessible(true); + RomanNumeralUtil instance = constructor.newInstance(); + + assertTrue(instance instanceof RomanNumeralUtil); + } +} diff --git a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java index 3e6290a3b3d4..158c9ca8435d 100644 --- a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java +++ b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -37,4 +38,13 @@ void calcSomeNumerals(){ assertThrows(IllegalArgumentException.class, ()->RomanNumeralUtil.generate(6000)); } + + @Test + void shouldNotThrowOnBounds() { + int min = 1; + int max = 5999; + + assertDoesNotThrow(() -> RomanNumeralUtil.generate(min), "Should not throw exception on " + min); + assertDoesNotThrow(() -> RomanNumeralUtil.generate(max), "Should not throw exception on " + max); + } } From 38e811f88eaf5f05224e4b88e8cd83d03f2c7a3a Mon Sep 17 00:00:00 2001 From: labe2301 Date: Sun, 19 Oct 2025 00:51:07 +0200 Subject: [PATCH 17/56] [feature] Increase line and mutation coverage for SimpsonIntegration --- .../maths/SimpsonIntegrationTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java diff --git a/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java b/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java new file mode 100644 index 000000000000..bb2ca65eb8a6 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java @@ -0,0 +1,61 @@ +package com.thealgorithms.maths; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +public class SimpsonIntegrationTest { + private final SimpsonIntegration simpson = new SimpsonIntegration(); + + @Test + void shouldCalculateCorrectFunction() { + assertAll( + () -> assertEquals(-0.24893534183931973, simpson.f(3)), + () -> assertEquals(0.0, simpson.f(2)), + () -> assertEquals(4.0, simpson.f(0)), + () -> assertEquals(8.154845485377136, simpson.f(-1)) + ); + } + + @Test + void shouldCalculateCorrectMethod() { + int n = 4; + double a = -1.0; + double b = 1.0; + double h = (b - a)/n; + + double result = simpson.simpsonsMethod(n, h, a); + double expected = 8.51454379418048; + + assertEquals(expected, result); + } + + @Test + void shouldIncreaseAccuracy() { + int n1 = 10; + int n2 = 20; + double a = 1.0; + double b = 3.0; + double h1 = (b - a) / n1; + double h2 = (b - a) / n2; + + double result1 = simpson.simpsonsMethod(n1, h1, a); + double result2 = simpson.simpsonsMethod(n2, h2, a); + + assertTrue(Math.abs(result2 - result1) < Math.abs(result1)); + } + + @Test + void shouldThrow() { + int n = 3; + double a = -1.0; + double b = 1.0; + double h = (b - a) / n; + + double result = simpson.simpsonsMethod(n, h, a); + + assertTrue(Double.isFinite(result)); + } +} From 6730e2e07ec7e7f17f01d628a461aaaf8c2d479d Mon Sep 17 00:00:00 2001 From: hedint <2022.195@student.setur.fo> Date: Sun, 19 Oct 2025 23:25:04 +0100 Subject: [PATCH 18/56] added test for min,max.tolong --- .../thealgorithms/maths/MathBuilderTest.java | 101 +++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 84e183bcd34d..21765e2cace5 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -80,7 +80,7 @@ void floorTest() { } @Test - @DisplayName("Close parntasis Test") + @DisplayName("Close parenthesis Test") void closeParenthesisAndOtherTest() { // 10.5 - (20+2.1) double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndMinus() @@ -96,6 +96,22 @@ void closeParenthesisAndOtherTest() { assertEquals(expected2, actual2); } + @Test + @DisplayName("open parenthesis Test") + void openParenthesisAndOtherTest() { + // 10.5 - (20+2.1) + double actual = new MathBuilder.Builder(10.5).openParenthesis(20).minus(2.1).closeParenthesisAndMinus() + .build().get(); + double expected = 10.5 - (20 - 2.1); + + // 10.5 / (20+2.1) + double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.2).abs().closeParenthesisAndPlus().abs() + .build().get(); + double expected2 = 10.5 + (20 + 2.2); + + assertEquals(expected, actual); + assertEquals(expected2, actual2); + } @Test @DisplayName("Runtime Errors Tests") void runtimeErrorTest() { @@ -124,9 +140,16 @@ void divideByNum() { @Test @DisplayName("This Should throw but does not") void divideByZero() { - MathBuilder.Builder actual2 = new MathBuilder.Builder(10.5).openParenthesis(0) + + + + MathBuilder actual = new MathBuilder.Builder(10.5).openParenthesis(0) + .closeParenthesisAndDivide().divide(0).build(); + + MathBuilder.Builder actual2 = new MathBuilder.Builder(10.5).openParenthesis(0) .closeParenthesisAndDivide(); + assertTrue(Double.isInfinite(actual.get())); assertDoesNotThrow(() -> actual2.build().get()); assertDoesNotThrow(() -> actual2.divide(0).build().get()); @@ -159,4 +182,78 @@ void toRadiansTest() { assertEquals(expected, actual); assertEquals(expected2, actual2); } + + @Test + void roundCielABS() { + + double actual = new MathBuilder.Builder(10).openParenthesis(10.5).round().closeParenthesisAndPlus().build().get(); + double expected = 10+ (Math.round(10.5)); + + double expected2 = 10 + Math.ceil(10.5); + double actual2 = new MathBuilder.Builder(10).openParenthesis(10.5).ceil().closeParenthesisAndPlus().build().get(); + + double expected3 = 10 + Math.abs(10.5); + double actual3 = new MathBuilder.Builder(10).openParenthesis(10.5).abs().closeParenthesisAndPlus().build().get(); + + double expected4 = Math.abs(10 + 10.5); + double actual4 = new MathBuilder.Builder(10).openParenthesis(10.5).closeParenthesisAndPlus().abs().build().get(); + + + assertEquals(expected, actual); + assertEquals(expected2, actual2); + assertEquals(expected3, actual3); + assertEquals(expected4, actual4); + } + + + @Test + void toLongTest() { + + + MathBuilder actual = new MathBuilder.Builder(10.5).openParenthesis(0) + .closeParenthesisAndDivide().divide(0).build(); + + MathBuilder actual2 = new MathBuilder.Builder(10.5).openParenthesis(0) + .closeParenthesisAndDivide().divide(0).multiply(-1).build(); + + MathBuilder actual3 = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(3).build(); + MathBuilder actual4 = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(-3).build(); + + assertEquals(Long.MAX_VALUE,actual.toLong()); + assertEquals(Long.MIN_VALUE,actual2.toLong()); + assertEquals(Long.MAX_VALUE,actual3.toLong()); + assertEquals(Long.MIN_VALUE,actual4.toLong()); + + } + + @Test + void maxTest() { + MathBuilder actual = new MathBuilder.Builder(10.5).max(20).build(); + MathBuilder actual2 = new MathBuilder.Builder(13.5).max(10).build(); + + MathBuilder actual3 = new MathBuilder.Builder(10.5).openParenthesis(10).max(20).closeParenthesisAndPlus().build(); + MathBuilder actual4 = new MathBuilder.Builder(12.5).openParenthesis(10).closeParenthesisAndPlus().max(20).build(); + + assertEquals(20,actual.get()); + assertEquals(13.5,actual2.get()); + assertEquals(30.5,actual3.get()); + assertEquals(22.5,actual4.get()); + + } + + @Test + void minTest() { + MathBuilder actual = new MathBuilder.Builder(10.5).min(20).build(); + MathBuilder actual2 = new MathBuilder.Builder(8.5).min(10).build(); + + MathBuilder actual3 = new MathBuilder.Builder(10.5).openParenthesis(10).min(20).closeParenthesisAndPlus().build(); + MathBuilder actual4 = new MathBuilder.Builder(12.5).openParenthesis(10).closeParenthesisAndPlus().min(20).build(); + + assertEquals(10.5,actual.get()); + assertEquals(8.5,actual2.get()); + assertEquals(20.5,actual3.get()); + assertEquals(20,actual4.get()); + + } + } From 09c91f3820e8f91bd5cfbaa7405a80b265343295 Mon Sep 17 00:00:00 2001 From: hedint <2022.195@student.setur.fo> Date: Tue, 21 Oct 2025 12:32:05 +0100 Subject: [PATCH 19/56] tried to follow reviews --- .../thealgorithms/maths/MathBuilderTest.java | 138 ++++++++++-------- 1 file changed, 79 insertions(+), 59 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 21765e2cace5..1ec6efd126df 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -1,15 +1,16 @@ package com.thealgorithms.maths; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.doubleThat; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import java.util.List; import java.util.Random; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; class MathBuilderTest { @@ -81,7 +82,7 @@ void floorTest() { @Test @DisplayName("Close parenthesis Test") - void closeParenthesisAndOtherTest() { + void closeParenthesisTest() { // 10.5 - (20+2.1) double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndMinus() .build().get(); @@ -98,7 +99,7 @@ void closeParenthesisAndOtherTest() { @Test @DisplayName("open parenthesis Test") - void openParenthesisAndOtherTest() { + void openParenthesisAndABSTest() { // 10.5 - (20+2.1) double actual = new MathBuilder.Builder(10.5).openParenthesis(20).minus(2.1).closeParenthesisAndMinus() .build().get(); @@ -115,16 +116,16 @@ void openParenthesisAndOtherTest() { @Test @DisplayName("Runtime Errors Tests") void runtimeErrorTest() { - // 10.5 - (20+2.1) MathBuilder.Builder actual = new MathBuilder.Builder(10.5); - // 10.5 / (20+2.1) - assertThrows(RuntimeException.class, () -> actual.rand(1)); - assertThrows(RuntimeException.class, () -> actual.randomInRange(1, 10)); - assertThrows(RuntimeException.class, () -> actual.pi()); - assertThrows(RuntimeException.class, () -> actual.e()); - assertThrows(RuntimeException.class, () -> actual.set(1)); + assertAll( + () -> assertThrows(RuntimeException.class, () -> actual.rand(1)), + () -> assertThrows(RuntimeException.class, () -> actual.randomInRange(1, 10)), + () -> assertThrows(RuntimeException.class, () -> actual.pi()), + () -> assertThrows(RuntimeException.class, () -> actual.e()), + () -> assertThrows(RuntimeException.class, () -> actual.set(1)) + ); } @Test @@ -133,8 +134,10 @@ void divideByNum() { double actual = new MathBuilder.Builder(10).divide(2).build().get(); double expected = 10 / 2; + double expected2 = 10 / 4; assertEquals(expected, actual); + assertNotEquals(expected2,actual); } @Test @@ -148,10 +151,12 @@ void divideByZero() { MathBuilder.Builder actual2 = new MathBuilder.Builder(10.5).openParenthesis(0) .closeParenthesisAndDivide(); + assertAll( + () -> assertTrue(Double.isInfinite(actual.get())), + () -> assertDoesNotThrow(() -> actual2.build().get()), + () -> assertDoesNotThrow(() -> actual2.divide(0).build().get()) + ); - assertTrue(Double.isInfinite(actual.get())); - assertDoesNotThrow(() -> actual2.build().get()); - assertDoesNotThrow(() -> actual2.divide(0).build().get()); } @@ -162,29 +167,37 @@ void randomFunctions() { double maxValue = 2.1; double actual = new MathBuilder.Builder().rand(2L).build().get(); double actual2 = new MathBuilder.Builder().randomInRange(minValue, maxValue).build().get(); + assertAll( + () ->assertTrue(actual < maxValue), + () ->assertTrue(actual2 >= minValue), + () ->assertTrue(actual2 <= maxValue) - assertTrue(actual < maxValue); - assertTrue(actual2 >= minValue); - assertTrue(actual2 <= maxValue); - - } - - @Test - void toRadiansTest() { + ); - double expected = Math.toRadians(10); - double expected2 = 2 + Math.toRadians(10); - double actual = new MathBuilder.Builder(10).toRadians().build().get(); - double actual2 = new MathBuilder.Builder(2).openParenthesis(10).toRadians().closeParenthesisAndPlus() - .build().get(); - - assertEquals(expected, actual); - assertEquals(expected2, actual2); } + @ParameterizedTest + @MethodSource("radiansHelper") + void toRadiansTests(double expectedAngle, double actualAngle) { + assertEquals(expectedAngle, actualAngle); + } + + private static List radiansHelper() { + return List.of( + Arguments.of( + Math.toRadians(10), + new MathBuilder.Builder(10).toRadians().build().get() + ), + Arguments.of( + 2 + Math.toRadians(10), + new MathBuilder.Builder(2).openParenthesis(10).toRadians().closeParenthesisAndPlus() + .build().get() + ) + ); + } @Test - void roundCielABS() { + void roundCielABSTest() { double actual = new MathBuilder.Builder(10).openParenthesis(10.5).round().closeParenthesisAndPlus().build().get(); double expected = 10+ (Math.round(10.5)); @@ -198,11 +211,14 @@ void roundCielABS() { double expected4 = Math.abs(10 + 10.5); double actual4 = new MathBuilder.Builder(10).openParenthesis(10.5).closeParenthesisAndPlus().abs().build().get(); - - assertEquals(expected, actual); - assertEquals(expected2, actual2); - assertEquals(expected3, actual3); - assertEquals(expected4, actual4); + assertAll( + () -> assertNotEquals(0,actual), + () -> assertNotEquals(1,actual2), + () -> assertEquals(expected, actual), + () -> assertEquals(expected2, actual2), + () -> assertEquals(expected3, actual3), + () -> assertEquals(expected4, actual4) + ); } @@ -218,12 +234,14 @@ void toLongTest() { MathBuilder actual3 = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(3).build(); MathBuilder actual4 = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(-3).build(); - - assertEquals(Long.MAX_VALUE,actual.toLong()); - assertEquals(Long.MIN_VALUE,actual2.toLong()); - assertEquals(Long.MAX_VALUE,actual3.toLong()); - assertEquals(Long.MIN_VALUE,actual4.toLong()); - + assertAll( + () -> assertEquals(Long.MAX_VALUE,actual.toLong()), + () -> assertEquals(Long.MIN_VALUE,actual2.toLong()), + () -> assertEquals(Long.MAX_VALUE,actual3.toLong()), + () -> assertEquals(Long.MIN_VALUE,actual4.toLong()), + () -> assertNotEquals(0,actual.toLong()), + () -> assertNotEquals(1,actual2.toLong()) + ); } @Test @@ -233,14 +251,15 @@ void maxTest() { MathBuilder actual3 = new MathBuilder.Builder(10.5).openParenthesis(10).max(20).closeParenthesisAndPlus().build(); MathBuilder actual4 = new MathBuilder.Builder(12.5).openParenthesis(10).closeParenthesisAndPlus().max(20).build(); - - assertEquals(20,actual.get()); - assertEquals(13.5,actual2.get()); - assertEquals(30.5,actual3.get()); - assertEquals(22.5,actual4.get()); - + assertAll( + () -> assertEquals(20,actual.get()), + () -> assertEquals(13.5,actual2.get()), + () -> assertEquals(30.5,actual3.get()), + () -> assertEquals(22.5,actual4.get()), + () -> assertNotEquals(30,actual4.get()), + () -> assertNotEquals(5,actual4.get()) + ); } - @Test void minTest() { MathBuilder actual = new MathBuilder.Builder(10.5).min(20).build(); @@ -248,12 +267,13 @@ void minTest() { MathBuilder actual3 = new MathBuilder.Builder(10.5).openParenthesis(10).min(20).closeParenthesisAndPlus().build(); MathBuilder actual4 = new MathBuilder.Builder(12.5).openParenthesis(10).closeParenthesisAndPlus().min(20).build(); - - assertEquals(10.5,actual.get()); - assertEquals(8.5,actual2.get()); - assertEquals(20.5,actual3.get()); - assertEquals(20,actual4.get()); - +assertAll( + () -> assertEquals(10.5,actual.get()), + () -> assertEquals(8.5,actual2.get()), + () -> assertEquals(20.5,actual3.get()), + () -> assertEquals(20,actual4.get()), + () -> assertNotEquals(5, actual.get()), + () -> assertNotEquals(-1000, actual3.get()) +); } - } From e66760ee803b4545cb0c1352e0e48226914319e4 Mon Sep 17 00:00:00 2001 From: labe2301 Date: Tue, 21 Oct 2025 12:25:27 +0200 Subject: [PATCH 20/56] [bugfix] Fix minor bugs and misses and adds missing tests --- .../maths/SimpsonIntegrationTest.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java b/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java index bb2ca65eb8a6..ea4fae2b651a 100644 --- a/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java +++ b/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java @@ -8,6 +8,7 @@ public class SimpsonIntegrationTest { private final SimpsonIntegration simpson = new SimpsonIntegration(); + private static final double DELTA = 1e-9; @Test void shouldCalculateCorrectFunction() { @@ -29,7 +30,7 @@ void shouldCalculateCorrectMethod() { double result = simpson.simpsonsMethod(n, h, a); double expected = 8.51454379418048; - assertEquals(expected, result); + assertEquals(expected, result, DELTA); } @Test @@ -44,11 +45,11 @@ void shouldIncreaseAccuracy() { double result1 = simpson.simpsonsMethod(n1, h1, a); double result2 = simpson.simpsonsMethod(n2, h2, a); - assertTrue(Math.abs(result2 - result1) < Math.abs(result1)); + assertTrue(Math.abs(result2 - result1) < Math.abs(result1), "Accuracy should improve with more intervals."); } @Test - void shouldThrow() { + void shouldNotFailOnOddNButTestResultSanity() { int n = 3; double a = -1.0; double b = 1.0; @@ -56,6 +57,31 @@ void shouldThrow() { double result = simpson.simpsonsMethod(n, h, a); - assertTrue(Double.isFinite(result)); + assertTrue(Double.isFinite(result), "Result should be finite even with odd n (though it may be inaccurate)."); + } + + @Test + void shouldReturnZeroWhenAEqualsB() { + int n = 2; + double a = 2.0; + double b = 2.0; + double h = (b - a) / n; + + double expected = 0.0; + double result = simpson.simpsonsMethod(n, h, a); + + assertEquals(expected, result, DELTA, "Integral over zero-width interval should be zero."); + } + + @Test + void shouldHandleMinimalEvenN() { + int n = 2; + double a = 0.0; + double b = 1.0; + double h = (b - a) / n; + + double result = simpson.simpsonsMethod(n, h, a); + + assertTrue(Double.isFinite(result), "Result should be finite with minimal even n=2."); } } From d4ed468990ce51f864bbc79099d9897e9ee2d7b4 Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Sat, 18 Oct 2025 15:31:36 +0300 Subject: [PATCH 21/56] First commit on new tree branch --- pom.xml | 58 +++++++++++++++++++ .../trees/LowestCommonAncestorTest.java | 5 ++ 2 files changed, 63 insertions(+) create mode 100644 src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java diff --git a/pom.xml b/pom.xml index fbd9baada8c1..0ec383d771df 100644 --- a/pom.xml +++ b/pom.xml @@ -202,4 +202,62 @@ + + + + jolla-dev + + + + + maven-surefire-plugin + + + com/thealgorithms/datastructures/trees/**/*.java + + + + + + + org.pitest + pitest-maven + + com.thealgorithms.datastructures.trees.** + com.thealgorithms.datastructures.trees.** + + + + pitest + verify + + mutationCoverage + + + + + + + + org.jacoco + jacoco-maven-plugin + + + + prepare-agent + + + + generate-code-coverage-report + verify + + report + + + + + + + + diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java new file mode 100644 index 000000000000..044a17c3f2b2 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java @@ -0,0 +1,5 @@ +package com.thealgorithms.datastructures.trees; + +public class LowestCommonAncestorTest { + +} From 44a7df4cd3acf47753f18b886836dfcc96ff4e5e Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Sat, 18 Oct 2025 20:17:56 +0300 Subject: [PATCH 22/56] Complete LCA test --- pom.xml | 4 +- .../datastructures/trees/AVLSimpleTest.java | 3 + .../trees/LowestCommonAncestorTest.java | 60 +++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java diff --git a/pom.xml b/pom.xml index 0ec383d771df..62d970a0e497 100644 --- a/pom.xml +++ b/pom.xml @@ -223,8 +223,8 @@ org.pitest pitest-maven - com.thealgorithms.datastructures.trees.** - com.thealgorithms.datastructures.trees.** + com.thealgorithms.datastructures.trees.LCA + com.thealgorithms.datastructures.trees.LowestCommonAncestorTest diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java new file mode 100644 index 000000000000..74fca8b4abce --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java @@ -0,0 +1,3 @@ +package com.thealgorithms.datastructures.trees; + +public class AVLSimpleTest {} diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java index 044a17c3f2b2..b81ab59bc2af 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java @@ -1,5 +1,65 @@ package com.thealgorithms.datastructures.trees; +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.*; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; + + public class LowestCommonAncestorTest { + /** + * This input creates the following tree: + *
+     *     0
+     *   /   \
+     *  1     2
+     *  |    / \
+     *  5   4   3
+     *  |       |
+     *  6       7
+     *         / \
+     *        9   8
+     * 
+     */
+    private final int[] treeInput = new int[] {0,1,0,2,1,5,5,6,2,4,2,3,3,7,7,9,7,8};
+
+    @ParameterizedTest
+    @CsvSource({"9,4,2", "5,6,5", "5,4,0", "3,8,3", "6,3,0", "3,3,3"})
+    void shouldReturnCorrectLCA(int v1, int v2, int expectedParent) {
+        ArrayList> adj = new ArrayList<>();
+        int actualParent;
+        int arrayLength = 10;
+
+        for (int i = 0; i < arrayLength; i++) adj.add(new ArrayList<>());
+
+        for (int i = 0; i < treeInput.length - 1; i +=2) {
+            int to = treeInput[i];
+            int from = treeInput[i+1];
+
+            adj.get(to).add(from);
+            adj.get(from).add(to);
+        }
+
+        int[] parent = new int[arrayLength];
+        int[] depth = new int[arrayLength];
+
+        try {
+            Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class);
+            Method getLCA = LCA.class.getDeclaredMethod("getLCA", int.class, int.class, int[].class, int[].class);
+            dfs.setAccessible(true);
+            getLCA.setAccessible(true);
+
+            dfs.invoke(null, adj, 0, -1, parent, depth);
+
+            actualParent = (int)getLCA.invoke(null, v1, v2, depth, parent);
+        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
+            throw new RuntimeException(e);
+        }
 
+        assertEquals(expectedParent, actualParent);
+    }
 }

From e911422046a5bd2d84b7e3b299fb212811750bb0 Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Sat, 18 Oct 2025 20:43:32 +0300
Subject: [PATCH 23/56] Added test to LSA for guarding against the parent and
 depth creation

---
 .../trees/LowestCommonAncestorTest.java       | 61 ++++++++++++++-----
 1 file changed, 46 insertions(+), 15 deletions(-)

diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
index b81ab59bc2af..0f4a1e64ac55 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
@@ -1,7 +1,8 @@
 package com.thealgorithms.datastructures.trees;
 
-import static org.junit.jupiter.api.Assertions.*;
-import org.junit.jupiter.api.*;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.CsvSource;
 
@@ -9,6 +10,8 @@
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
 public class LowestCommonAncestorTest {
     /**
@@ -25,28 +28,30 @@ public class LowestCommonAncestorTest {
      *        9   8
      * 
      */
-    private final int[] treeInput = new int[] {0,1,0,2,1,5,5,6,2,4,2,3,3,7,7,9,7,8};
+    private final int[] treeInput = new int[] {0, 1, 0, 2, 1, 5, 5, 6, 2, 4, 2, 3, 3, 7, 7, 9, 7, 8};
+    private final int arrayLength = 10;
+    private final int[] parent = new int[arrayLength];
+    private final int[] depth = new int[arrayLength];
+    private final ArrayList> adj = new ArrayList<>();
 
-    @ParameterizedTest
-    @CsvSource({"9,4,2", "5,6,5", "5,4,0", "3,8,3", "6,3,0", "3,3,3"})
-    void shouldReturnCorrectLCA(int v1, int v2, int expectedParent) {
-        ArrayList> adj = new ArrayList<>();
-        int actualParent;
-        int arrayLength = 10;
 
+    @BeforeEach
+    void setup(){
         for (int i = 0; i < arrayLength; i++) adj.add(new ArrayList<>());
 
-        for (int i = 0; i < treeInput.length - 1; i +=2) {
+        for (int i = 0; i < treeInput.length - 1; i += 2) {
             int to = treeInput[i];
             int from = treeInput[i+1];
 
             adj.get(to).add(from);
             adj.get(from).add(to);
         }
+    }
 
-        int[] parent = new int[arrayLength];
-        int[] depth = new int[arrayLength];
-
+    @ParameterizedTest
+    @CsvSource({"9,4,2", "5,6,5", "5,4,0", "3,8,3", "6,3,0", "3,3,3"})
+    @DisplayName("Should return correct common ancestor for any two nodes in the tree")
+    void shouldReturnCorrectLCA(int v1, int v2, int expectedParent) {
         try {
             Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class);
             Method getLCA = LCA.class.getDeclaredMethod("getLCA", int.class, int.class, int[].class, int[].class);
@@ -55,11 +60,37 @@ void shouldReturnCorrectLCA(int v1, int v2, int expectedParent) {
 
             dfs.invoke(null, adj, 0, -1, parent, depth);
 
-            actualParent = (int)getLCA.invoke(null, v1, v2, depth, parent);
+            assertEquals(expectedParent, getLCA.invoke(null, v1, v2, depth, parent));
+        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Test
+    void shouldReturnCorrectDepthsForArray() {
+        try {
+            Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class);
+            dfs.setAccessible(true);
+
+            dfs.invoke(null, adj, 0, -1, parent, depth);
+        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
+            throw new RuntimeException(e);
+        }
+
+        assertArrayEquals(new int[]{0, 1, 1, 2, 2, 2, 3, 3, 4, 4}, depth);
+    }
+
+    @Test
+    void shouldReturnCorrectParentsForArray() {
+        try {
+            Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class);
+            dfs.setAccessible(true);
+
+            dfs.invoke(null, adj, 0, -1, parent, depth);
         } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
             throw new RuntimeException(e);
         }
 
-        assertEquals(expectedParent, actualParent);
+        assertArrayEquals(new int[]{0, 0, 0, 2, 2, 1, 5, 3, 7, 7}, parent);
     }
 }

From 1f7d6088be32872a758d720ed126074c9f10d44f Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Sat, 18 Oct 2025 20:59:48 +0300
Subject: [PATCH 24/56] Started AVLSimpleTest

---
 pom.xml                                       |  4 +--
 .../datastructures/trees/AVLSimpleTest.java   | 26 ++++++++++++++++++-
 .../trees/LowestCommonAncestorTest.java       |  8 +++---
 3 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/pom.xml b/pom.xml
index 62d970a0e497..1a3a04619722 100644
--- a/pom.xml
+++ b/pom.xml
@@ -223,8 +223,8 @@
                         org.pitest
                         pitest-maven
                         
-                            com.thealgorithms.datastructures.trees.LCA
-                            com.thealgorithms.datastructures.trees.LowestCommonAncestorTest
+                            com.thealgorithms.datastructures.trees.AVLSimple
+                            com.thealgorithms.datastructures.trees.AVLSimpleTest
                         
                         
                             
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index 74fca8b4abce..ca11ba68593f 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -1,3 +1,27 @@
 package com.thealgorithms.datastructures.trees;
 
-public class AVLSimpleTest {}
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class AVLSimpleTest {
+    AVLSimple tree = new AVLSimple();
+
+    @BeforeEach
+    void setup() {
+        tree.insert(20);
+        tree.insert(25);
+        tree.insert(30);
+        tree.insert(10);
+        tree.insert(5);
+        tree.insert(15);
+        tree.insert(27);
+        tree.insert(19);
+        tree.insert(16);
+    }
+
+    @Test
+    void test() {
+        tree.display();
+        System.out.println();
+    }
+}
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
index 0f4a1e64ac55..6b31a938564b 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
@@ -36,12 +36,12 @@ public class LowestCommonAncestorTest {
 
 
     @BeforeEach
-    void setup(){
+    void setup() {
         for (int i = 0; i < arrayLength; i++) adj.add(new ArrayList<>());
 
         for (int i = 0; i < treeInput.length - 1; i += 2) {
             int to = treeInput[i];
-            int from = treeInput[i+1];
+            int from = treeInput[i + 1];
 
             adj.get(to).add(from);
             adj.get(from).add(to);
@@ -77,7 +77,7 @@ void shouldReturnCorrectDepthsForArray() {
             throw new RuntimeException(e);
         }
 
-        assertArrayEquals(new int[]{0, 1, 1, 2, 2, 2, 3, 3, 4, 4}, depth);
+        assertArrayEquals(new int[] {0, 1, 1, 2, 2, 2, 3, 3, 4, 4}, depth);
     }
 
     @Test
@@ -91,6 +91,6 @@ void shouldReturnCorrectParentsForArray() {
             throw new RuntimeException(e);
         }
 
-        assertArrayEquals(new int[]{0, 0, 0, 2, 2, 1, 5, 3, 7, 7}, parent);
+        assertArrayEquals(new int[] {0, 0, 0, 2, 2, 1, 5, 3, 7, 7}, parent);
     }
 }

From 5cf2b67b1b0c1a4331826b815dcf96a15407f35a Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Sun, 19 Oct 2025 12:30:21 +0300
Subject: [PATCH 25/56] Rethought LCA

---
 .../trees/LowestCommonAncestorTest.java       | 40 ++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
index 6b31a938564b..ae4c63977409 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
@@ -1,14 +1,21 @@
 package com.thealgorithms.datastructures.trees;
 
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.MethodSource;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.stream.Stream;
 
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -34,7 +41,6 @@ public class LowestCommonAncestorTest {
     private final int[] depth = new int[arrayLength];
     private final ArrayList> adj = new ArrayList<>();
 
-
     @BeforeEach
     void setup() {
         for (int i = 0; i < arrayLength; i++) adj.add(new ArrayList<>());
@@ -48,6 +54,38 @@ void setup() {
         }
     }
 
+    @Disabled("This would be the best way to test LCA but since the scanner is a\n" +
+      "static global variable it doesn't work, it should be moved into the main method.")
+    @ParameterizedTest
+    @MethodSource("getInput")
+    @DisplayName("Should return correct common ancestor for any two nodes in the tree")
+    void shouldReturnCorrectLCA2(String simulatedInput, String expectedParent) {
+        System.setIn(new ByteArrayInputStream(simulatedInput.getBytes()));
+
+        ByteArrayOutputStream outContent = new ByteArrayOutputStream();
+        PrintStream originalOut = System.out;
+        System.setOut(new PrintStream(outContent));
+
+        LCA.main(new String[0]);
+
+        System.setOut(originalOut);
+        System.setIn(System.in);
+
+        String output = outContent.toString().trim();
+        assertEquals(expectedParent, output);
+    }
+
+    public static Stream getInput() {
+        return Stream.of(
+          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n9\n4\n", "2"),
+          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n5\n6\n", "5"),
+          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n5\n4\n", "0"),
+          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n3\n8\n", "3"),
+          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n6\n3\n", "0"),
+          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n3\n3\n", "3")
+        );
+    }
+
     @ParameterizedTest
     @CsvSource({"9,4,2", "5,6,5", "5,4,0", "3,8,3", "6,3,0", "3,3,3"})
     @DisplayName("Should return correct common ancestor for any two nodes in the tree")

From a0d3586f898651c4093f89ba7fa895db980e9f00 Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Sun, 19 Oct 2025 14:29:33 +0300
Subject: [PATCH 26/56] Initial tests in AVLSimpleTest

Line coverage finished but need to
check out mutation coverage a little
more
---
 .../datastructures/trees/AVLSimpleTest.java   | 91 ++++++++++++++++++-
 .../trees/LowestCommonAncestorTest.java       |  4 +-
 2 files changed, 88 insertions(+), 7 deletions(-)

diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index ca11ba68593f..e687135b7f28 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -1,27 +1,108 @@
 package com.thealgorithms.datastructures.trees;
 
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
 public class AVLSimpleTest {
-    AVLSimple tree = new AVLSimple();
+    ByteArrayOutputStream outContent;
+    PrintStream originalOut;
 
     @BeforeEach
-    void setup() {
-        tree.insert(20);
+    void setupOutputStream() {
+        outContent = new ByteArrayOutputStream();
+        originalOut = System.out;
+        System.setOut(new PrintStream(outContent));
+    }
+
+    @AfterEach
+    void tearDown() {
+        System.setOut(originalOut);
+    }
+
+    @Test
+    void testTreeCreation() {
+        AVLSimple tree = new AVLSimple();
+        String expectedTree = ("""
+          15=>25<=30
+          10=>15<=19
+          5=>10<=END
+          END=>5<=END
+          16=>19<=20
+          END=>16<=END
+          END=>20<=END
+          27=>30<=END
+          END=>27<=END
+          4""");
+
         tree.insert(25);
         tree.insert(30);
         tree.insert(10);
         tree.insert(5);
         tree.insert(15);
         tree.insert(27);
+        tree.insert(20);
         tree.insert(19);
         tree.insert(16);
+
+        tree.display();
+
+        String actualTree = outContent.toString().trim();
+
+        assertArrayEquals(expectedTree.toCharArray(), actualTree.toCharArray());
+    }
+
+    @Test
+    void testLRRotate() {
+        AVLSimple tree = new AVLSimple();
+        String expectedTree = ("""
+          10=>20<=30
+          END=>10<=END
+          END=>30<=END
+          2""")
+          .replace("\n", "");
+
+        tree.insert(30);
+        tree.insert(10);
+        tree.insert(20);
+
+        tree.display();
+
+        String actualTree = outContent
+          .toString()
+          .trim()
+          .replace("\n", "");
+
+        assertEquals(expectedTree, actualTree);
     }
 
     @Test
-    void test() {
+    void testRLRotate() {
+        AVLSimple tree = new AVLSimple();
+        String expectedTree = ("""
+          10=>20<=30
+          END=>10<=END
+          END=>30<=END
+          2""")
+          .replace("\n", "");
+
+        tree.insert(10);
+        tree.insert(30);
+        tree.insert(20);
+
         tree.display();
-        System.out.println();
+
+        String actualTree = outContent
+          .toString()
+          .trim()
+          .replace("\n", "");
+
+        assertEquals(expectedTree, actualTree);
     }
 }
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
index ae4c63977409..76b9f458c4c8 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
@@ -71,8 +71,8 @@ void shouldReturnCorrectLCA2(String simulatedInput, String expectedParent) {
         System.setOut(originalOut);
         System.setIn(System.in);
 
-        String output = outContent.toString().trim();
-        assertEquals(expectedParent, output);
+        String actualParent = outContent.toString().trim();
+        assertEquals(expectedParent, actualParent);
     }
 
     public static Stream getInput() {

From bd7bceedb220669e61c8f8d798607ff6912dbd68 Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Sun, 19 Oct 2025 18:03:24 +0300
Subject: [PATCH 27/56] Coverage tests done

Might check for RL and LR rotations
as well
---
 .../datastructures/trees/AVLSimpleTest.java   | 146 +++++++++++++-----
 .../trees/LowestCommonAncestorTest.java       |   8 +-
 2 files changed, 114 insertions(+), 40 deletions(-)

diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index e687135b7f28..ae7a3c0ae3b5 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -2,20 +2,29 @@
 
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
 
 import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
+import java.util.stream.Stream;
 
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 public class AVLSimpleTest {
+    AVLSimple tree = new AVLSimple();
     ByteArrayOutputStream outContent;
     PrintStream originalOut;
 
+    /* ========================
+        Setup/TearDown
+    ======================== */
+
     @BeforeEach
-    void setupOutputStream() {
+    void setup() {
         outContent = new ByteArrayOutputStream();
         originalOut = System.out;
         System.setOut(new PrintStream(outContent));
@@ -26,9 +35,45 @@ void tearDown() {
         System.setOut(originalOut);
     }
 
+    /* ========================
+        Helper methods
+    ======================== */
+
+    String getExpectedTree() {
+        return ("""
+          10=>20<=30
+          END=>10<=END
+          END=>30<=END
+          2""")
+          .replace("\n", "");
+    }
+
+    String getActualTree() {
+        return outContent
+          .toString()
+          .trim()
+          .replace("\n", "");
+    }
+
+    /* ========================
+        Tests
+    ======================== */
+
     @Test
+    @DisplayName("A longer generic tree creation that should pass")
     void testTreeCreation() {
-        AVLSimple tree = new AVLSimple();
+        tree.insert(25);
+        tree.insert(30);
+        tree.insert(10);
+        tree.insert(5);
+        tree.insert(15);
+        tree.insert(27);
+        tree.insert(20);
+        tree.insert(19);
+        tree.insert(16);
+
+        tree.display();
+
         String expectedTree = ("""
           15=>25<=30
           10=>15<=19
@@ -39,69 +84,96 @@ void testTreeCreation() {
           END=>20<=END
           27=>30<=END
           END=>27<=END
-          4""");
+          4""")
+          .replace("\n", "");
+        String actualTree = getActualTree();
 
-        tree.insert(25);
-        tree.insert(30);
-        tree.insert(10);
-        tree.insert(5);
-        tree.insert(15);
-        tree.insert(27);
-        tree.insert(20);
-        tree.insert(19);
-        tree.insert(16);
+        assertEquals(expectedTree, actualTree);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getTreeNodesInput")
+    @DisplayName("Test  to ensure all rotation paths are covered")
+    void testAllRotations(int node1, int node2, int node3) {
+        tree.insert(node1);
+        tree.insert(node2);
+        tree.insert(node3);
 
         tree.display();
 
-        String actualTree = outContent.toString().trim();
+        String expectedTree = getExpectedTree();
+        String actualTree = getActualTree();
 
-        assertArrayEquals(expectedTree.toCharArray(), actualTree.toCharArray());
+        assertEquals(expectedTree, actualTree);
     }
 
-    @Test
-    void testLRRotate() {
-        AVLSimple tree = new AVLSimple();
-        String expectedTree = ("""
-          10=>20<=30
-          END=>10<=END
-          END=>30<=END
-          2""")
-          .replace("\n", "");
+    public static Stream getTreeNodesInput() {
+        return Stream.of(
+          Arguments.of(30, 20, 10),
+          Arguments.of(30, 10, 20),
+          Arguments.of(10, 20, 30),
+          Arguments.of(10, 30, 20)
+        );
+    }
 
+    @Test
+    @DisplayName("Should return true for a tree that don't account for duplicates")
+    void testDuplicatesInTreeCreationDoNotStick() {
+        int duplicate = 20;
         tree.insert(30);
-        tree.insert(10);
+        tree.insert(duplicate);
         tree.insert(20);
+        tree.insert(duplicate);
+        tree.insert(10);
 
         tree.display();
 
-        String actualTree = outContent
-          .toString()
-          .trim()
-          .replace("\n", "");
+        String expectedTree = getExpectedTree();
+        String actualTree = getActualTree();
 
         assertEquals(expectedTree, actualTree);
     }
 
     @Test
-    void testRLRotate() {
-        AVLSimple tree = new AVLSimple();
+    @DisplayName("Should return true for a tree that don't account for duplicates")
+    void testRightRotateNotTriggeredWhenBFEqualsOne() {
+        tree.insert(30);
+        tree.insert(20);
+        tree.insert(10);
+        tree.insert(5);
+
+        tree.display();
+
         String expectedTree = ("""
           10=>20<=30
-          END=>10<=END
+          5=>10<=END
+          END=>5<=END
           END=>30<=END
-          2""")
+          3""")
           .replace("\n", "");
+        String actualTree = getActualTree();
 
-        tree.insert(10);
+        assertEquals(expectedTree, actualTree);
+    }
+
+    @Test
+    @DisplayName("Should return true for a tree that don't account for duplicates")
+    void testLeftRotateNotTriggeredWhenBFEqualsOne() {
         tree.insert(30);
         tree.insert(20);
+        tree.insert(10);
+        tree.insert(35);
 
         tree.display();
 
-        String actualTree = outContent
-          .toString()
-          .trim()
+        String expectedTree = ("""
+          10=>20<=30
+          END=>10<=END
+          END=>30<=35
+          END=>35<=END
+          3""")
           .replace("\n", "");
+        String actualTree = getActualTree();
 
         assertEquals(expectedTree, actualTree);
     }
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
index 76b9f458c4c8..44cc0fcb27d2 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java
@@ -43,7 +43,9 @@ public class LowestCommonAncestorTest {
 
     @BeforeEach
     void setup() {
-        for (int i = 0; i < arrayLength; i++) adj.add(new ArrayList<>());
+        for (int i = 0; i < arrayLength; i++) {
+            adj.add(new ArrayList<>());
+        }
 
         for (int i = 0; i < treeInput.length - 1; i += 2) {
             int to = treeInput[i];
@@ -54,8 +56,8 @@ void setup() {
         }
     }
 
-    @Disabled("This would be the best way to test LCA but since the scanner is a\n" +
-      "static global variable it doesn't work, it should be moved into the main method.")
+    @Disabled("This would be the best way to test LCA but since the scanner is a\n"
+      + "static global variable it doesn't work, it should be moved into the main method.")
     @ParameterizedTest
     @MethodSource("getInput")
     @DisplayName("Should return correct common ancestor for any two nodes in the tree")

From 2124c7f55f14fa652db9d9e81fd66345b1a86e5b Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Sun, 19 Oct 2025 20:56:31 +0300
Subject: [PATCH 28/56] Test written for GenericTree

Pleasant class to test for once :P
---
 pom.xml                                       |   4 +-
 .../datastructures/trees/AVLSimpleTest.java   |  69 +++++------
 .../datastructures/trees/GenericTreeTest.java | 108 ++++++++++++++++++
 3 files changed, 138 insertions(+), 43 deletions(-)
 create mode 100644 src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java

diff --git a/pom.xml b/pom.xml
index 1a3a04619722..0e44bca18eb3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -223,8 +223,8 @@
                         org.pitest
                         pitest-maven
                         
-                            com.thealgorithms.datastructures.trees.AVLSimple
-                            com.thealgorithms.datastructures.trees.AVLSimpleTest
+                            com.thealgorithms.datastructures.trees.GenericTree
+                            com.thealgorithms.datastructures.trees.GenericTreeTest
                         
                         
                             
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index ae7a3c0ae3b5..f8e387899544 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -60,7 +60,7 @@ String getActualTree() {
     ======================== */
 
     @Test
-    @DisplayName("A longer generic tree creation that should pass")
+    @DisplayName("A longer generic AVL tree creation that should pass")
     void testTreeCreation() {
         tree.insert(25);
         tree.insert(30);
@@ -86,9 +86,8 @@ void testTreeCreation() {
           END=>27<=END
           4""")
           .replace("\n", "");
-        String actualTree = getActualTree();
 
-        assertEquals(expectedTree, actualTree);
+        assertEquals(expectedTree, getActualTree());
     }
 
     @ParameterizedTest
@@ -101,10 +100,7 @@ void testAllRotations(int node1, int node2, int node3) {
 
         tree.display();
 
-        String expectedTree = getExpectedTree();
-        String actualTree = getActualTree();
-
-        assertEquals(expectedTree, actualTree);
+        assertEquals(getExpectedTree(), getActualTree());
     }
 
     public static Stream getTreeNodesInput() {
@@ -116,63 +112,54 @@ public static Stream getTreeNodesInput() {
         );
     }
 
-    @Test
-    @DisplayName("Should return true for a tree that don't account for duplicates")
-    void testDuplicatesInTreeCreationDoNotStick() {
-        int duplicate = 20;
+    @ParameterizedTest
+    @MethodSource("getTreeNodesInputForBFEqualsOneRotations")
+    @DisplayName("Rotation not triggered when balance factor equals threshold")
+    void testRotatesNotTriggeredWhenBFEqualsOne(int node, String expectedTree) {
         tree.insert(30);
-        tree.insert(duplicate);
         tree.insert(20);
-        tree.insert(duplicate);
         tree.insert(10);
+        tree.insert(node);
 
         tree.display();
 
-        String expectedTree = getExpectedTree();
-        String actualTree = getActualTree();
-
-        assertEquals(expectedTree, actualTree);
+        assertEquals(expectedTree, getActualTree());
     }
 
-    @Test
-    @DisplayName("Should return true for a tree that don't account for duplicates")
-    void testRightRotateNotTriggeredWhenBFEqualsOne() {
-        tree.insert(30);
-        tree.insert(20);
-        tree.insert(10);
-        tree.insert(5);
-
-        tree.display();
-
-        String expectedTree = ("""
+    public static Stream getTreeNodesInputForBFEqualsOneRotations() {
+        return Stream.of(
+          Arguments.of(5, ("""
           10=>20<=30
           5=>10<=END
           END=>5<=END
           END=>30<=END
           3""")
-          .replace("\n", "");
-        String actualTree = getActualTree();
-
-        assertEquals(expectedTree, actualTree);
+            .replace("\n", "")),
+          Arguments.of(35, ("""
+          10=>20<=30
+          END=>10<=END
+          END=>30<=35
+          END=>35<=END
+          3""")
+            .replace("\n", ""))
+        );
     }
 
+    // TODO: Add rotation tests for RL and LR when bf = 1
+
     @Test
     @DisplayName("Should return true for a tree that don't account for duplicates")
-    void testLeftRotateNotTriggeredWhenBFEqualsOne() {
+    void testDuplicatesInTreeCreationDoNotStick() {
+        int duplicate = 20;
         tree.insert(30);
+        tree.insert(duplicate);
         tree.insert(20);
+        tree.insert(duplicate);
         tree.insert(10);
-        tree.insert(35);
 
         tree.display();
 
-        String expectedTree = ("""
-          10=>20<=30
-          END=>10<=END
-          END=>30<=35
-          END=>35<=END
-          3""")
-          .replace("\n", "");
+        String expectedTree = getExpectedTree();
         String actualTree = getActualTree();
 
         assertEquals(expectedTree, actualTree);
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java
new file mode 100644
index 000000000000..37e0c1c79eb6
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java
@@ -0,0 +1,108 @@
+package com.thealgorithms.datastructures.trees;
+
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class GenericTreeTest {
+    GenericTree tree;
+    ByteArrayOutputStream outContent;
+    PrintStream originalOut;
+
+    /* ========================
+        Setup/TearDown
+    ======================== */
+
+    @BeforeEach
+    void setup() {
+        String simulatedScannerInput = "40\n4\n34\n0\n1\n0\n32\n1\n123\n0\n342\n2\n98\n0\n35\n0\n";
+        System.setIn(new ByteArrayInputStream(simulatedScannerInput.getBytes()));
+
+        tree = new GenericTree();
+
+        outContent = new ByteArrayOutputStream();
+        originalOut = System.out;
+        System.setOut(new PrintStream(outContent));
+    }
+
+    @AfterEach
+    void tearDown() {
+        System.setOut(originalOut);
+        System.setIn(System.in);
+    }
+
+    /* ========================
+        Helper methods
+    ======================== */
+
+    String getActualTree() {
+        return outContent
+          .toString()
+          .trim()
+          .replace("\n", "");
+    }
+
+    /**
+     * Creates a generic tree that looks something like this:
+     *
+     * 
+     *                   40
+     *                / / \  \
+     *              /  /   \  \
+     *             34 1    32 324
+     *                     |  / \
+     *                   123 98 35
+     * 
+ * @return The tree in a string format mimicking what the display() method gives. + */ + String getExpectedTree() { + return "40=>34 1 32 342 .34=>.1=>.32=>123 .123=>.342=>98 35 .98=>.35=>."; + } + + /* ======================== + Tests + ======================== */ + + @Test + void testCreateTree() { + tree.display(); + + assertEquals(getExpectedTree(), getActualTree()); + } + + @Test + void testGettingCorrectSizeOfTree() { + assertEquals(8, tree.size2call()); + } + + @Test + void testGettingTheMaximalValueOfTreesNodes() { + assertEquals(342, tree.maxcall()); + } + + @Test + void testGettingTheHeightOfTree() { + assertEquals(2, tree.heightcall()); + } + + @ParameterizedTest + @ValueSource(ints = {40, 34, 1, 32, 342, 123, 98, 35}) + void testFindingAllPresentValuesInTree(int value) { + assertTrue(tree.findcall(value)); + } + + @ParameterizedTest + @ValueSource(ints = {41, 35, 2, 52, 542, 223, 92, 38}) + void testFindingAbsentValuesInTree() { + assertFalse(tree.findcall(666)); + } +} From 80d99a95d776a19a3cc9b0ebbc900295dd6b70e4 Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Mon, 20 Oct 2025 08:47:02 +0300 Subject: [PATCH 29/56] Test push for GitHub Actions Have solved spotbugs issues and minimized checkstyle errors --- .../datastructures/trees/GenericTreeTest.java | 5 +-- .../trees/LowestCommonAncestorTest.java | 36 +++++++------------ 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java index 37e0c1c79eb6..829b78659e4e 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java @@ -1,6 +1,5 @@ package com.thealgorithms.datastructures.trees; - import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -11,7 +10,9 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class GenericTreeTest { GenericTree tree; diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java index 44cc0fcb27d2..1a4bc31f394c 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java @@ -12,7 +12,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.PrintStream; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.stream.Stream; @@ -61,7 +60,7 @@ void setup() { @ParameterizedTest @MethodSource("getInput") @DisplayName("Should return correct common ancestor for any two nodes in the tree") - void shouldReturnCorrectLCA2(String simulatedInput, String expectedParent) { + void shouldReturnCorrectLCAThroughMain(String simulatedInput, String expectedParent) { System.setIn(new ByteArrayInputStream(simulatedInput.getBytes())); ByteArrayOutputStream outContent = new ByteArrayOutputStream(); @@ -88,11 +87,11 @@ public static Stream getInput() { ); } + @SuppressWarnings("RFI_SET_ACCESSIBLE") @ParameterizedTest @CsvSource({"9,4,2", "5,6,5", "5,4,0", "3,8,3", "6,3,0", "3,3,3"}) @DisplayName("Should return correct common ancestor for any two nodes in the tree") - void shouldReturnCorrectLCA(int v1, int v2, int expectedParent) { - try { + void shouldReturnCorrectLCA(int v1, int v2, int expectedParent) throws Exception { Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class); Method getLCA = LCA.class.getDeclaredMethod("getLCA", int.class, int.class, int[].class, int[].class); dfs.setAccessible(true); @@ -101,35 +100,26 @@ void shouldReturnCorrectLCA(int v1, int v2, int expectedParent) { dfs.invoke(null, adj, 0, -1, parent, depth); assertEquals(expectedParent, getLCA.invoke(null, v1, v2, depth, parent)); - } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { - throw new RuntimeException(e); - } } + @SuppressWarnings("RFI_SET_ACCESSIBLE") @Test - void shouldReturnCorrectDepthsForArray() { - try { - Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class); - dfs.setAccessible(true); + void shouldReturnCorrectDepthsForArray() throws Exception { + Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class); + dfs.setAccessible(true); - dfs.invoke(null, adj, 0, -1, parent, depth); - } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { - throw new RuntimeException(e); - } + dfs.invoke(null, adj, 0, -1, parent, depth); assertArrayEquals(new int[] {0, 1, 1, 2, 2, 2, 3, 3, 4, 4}, depth); } + @SuppressWarnings("RFI_SET_ACCESSIBLE") @Test - void shouldReturnCorrectParentsForArray() { - try { - Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class); - dfs.setAccessible(true); + void shouldReturnCorrectParentsForArray() throws Exception { + Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class); + dfs.setAccessible(true); - dfs.invoke(null, adj, 0, -1, parent, depth); - } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { - throw new RuntimeException(e); - } + dfs.invoke(null, adj, 0, -1, parent, depth); assertArrayEquals(new int[] {0, 0, 0, 2, 2, 1, 5, 3, 7, 7}, parent); } From 2296d5eed4e31aaed94e40bbaccfae7210426ebd Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Mon, 20 Oct 2025 13:12:02 +0300 Subject: [PATCH 30/56] Generic tree done --- .../datastructures/trees/AVLSimpleTest.java | 24 ++--- .../datastructures/trees/GenericTreeTest.java | 102 +++++++++++------- ...stCommonAncestorTest.java => LCATest.java} | 18 ++-- .../utils/ConsoleInterceptor.java | 54 ++++++++++ 4 files changed, 133 insertions(+), 65 deletions(-) rename src/test/java/com/thealgorithms/datastructures/trees/{LowestCommonAncestorTest.java => LCATest.java} (89%) create mode 100644 src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java index f8e387899544..150699a81d36 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java @@ -1,5 +1,6 @@ package com.thealgorithms.datastructures.trees; +import com.thealgorithms.utils.ConsoleInterceptor; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -8,16 +9,13 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; public class AVLSimpleTest { AVLSimple tree = new AVLSimple(); - ByteArrayOutputStream outContent; - PrintStream originalOut; + ConsoleInterceptor systemOut = new ConsoleInterceptor(); /* ======================== Setup/TearDown @@ -25,14 +23,12 @@ public class AVLSimpleTest { @BeforeEach void setup() { - outContent = new ByteArrayOutputStream(); - originalOut = System.out; - System.setOut(new PrintStream(outContent)); + systemOut.captureOutput(); } @AfterEach void tearDown() { - System.setOut(originalOut); + systemOut.restoreOutput(); } /* ======================== @@ -49,10 +45,7 @@ String getExpectedTree() { } String getActualTree() { - return outContent - .toString() - .trim() - .replace("\n", ""); + return systemOut.getConsoleOutput(); } /* ======================== @@ -92,7 +85,7 @@ void testTreeCreation() { @ParameterizedTest @MethodSource("getTreeNodesInput") - @DisplayName("Test to ensure all rotation paths are covered") + @DisplayName("Test to ensure all rotation paths are covered") void testAllRotations(int node1, int node2, int node3) { tree.insert(node1); tree.insert(node2); @@ -159,9 +152,6 @@ void testDuplicatesInTreeCreationDoNotStick() { tree.display(); - String expectedTree = getExpectedTree(); - String actualTree = getActualTree(); - - assertEquals(expectedTree, actualTree); + assertEquals(getExpectedTree(), getActualTree()); } } diff --git a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java index 829b78659e4e..929683fd2419 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java @@ -1,23 +1,22 @@ package com.thealgorithms.datastructures.trees; +import com.thealgorithms.utils.ConsoleInterceptor; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; +import java.util.InputMismatchException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; public class GenericTreeTest { GenericTree tree; - ByteArrayOutputStream outContent; - PrintStream originalOut; + ConsoleInterceptor systemInOut = new ConsoleInterceptor(); /* ======================== Setup/TearDown @@ -25,48 +24,34 @@ public class GenericTreeTest { @BeforeEach void setup() { - String simulatedScannerInput = "40\n4\n34\n0\n1\n0\n32\n1\n123\n0\n342\n2\n98\n0\n35\n0\n"; - System.setIn(new ByteArrayInputStream(simulatedScannerInput.getBytes())); + String treeValues = "40\n5\n34\n0\n1\n0\n32\n1\n123\n0\n342\n2\n98\n0\n35\n0\n12\n0"; + systemInOut.mockInput(treeValues); tree = new GenericTree(); - outContent = new ByteArrayOutputStream(); - originalOut = System.out; - System.setOut(new PrintStream(outContent)); + systemInOut.captureOutput(); } @AfterEach void tearDown() { - System.setOut(originalOut); - System.setIn(System.in); - } - - /* ======================== - Helper methods - ======================== */ - - String getActualTree() { - return outContent - .toString() - .trim() - .replace("\n", ""); + systemInOut.restoreInAndOutput(); } /** * Creates a generic tree that looks something like this: * *
-     *                   40
-     *                / / \  \
-     *              /  /   \  \
-     *             34 1    32 324
-     *                     |  / \
-     *                   123 98 35
+     *                    40
+     *                / / | \  \
+     *              /  /  |  \  \
+     *             34 1   32 324 12
+     *                    |  | \
+     *                  123 98 35
      * 
* @return The tree in a string format mimicking what the display() method gives. */ String getExpectedTree() { - return "40=>34 1 32 342 .34=>.1=>.32=>123 .123=>.342=>98 35 .98=>.35=>."; + return "40=>34 1 32 342 12 .34=>.1=>.32=>123 .123=>.342=>98 35 .98=>.35=>.12=>."; } /* ======================== @@ -74,15 +59,23 @@ String getExpectedTree() { ======================== */ @Test - void testCreateTree() { + void testCreateValidTree() { tree.display(); - assertEquals(getExpectedTree(), getActualTree()); + assertEquals(getExpectedTree(), systemInOut.getConsoleOutput()); + } + + @Test + void testCreateInValidTree() { + String invalidTreeValues = "a\nb\nc\n"; + systemInOut.mockInput(invalidTreeValues); + + assertThrows(InputMismatchException.class, GenericTree::new); } @Test void testGettingCorrectSizeOfTree() { - assertEquals(8, tree.size2call()); + assertEquals(9, tree.size2call()); } @Test @@ -102,8 +95,45 @@ void testFindingAllPresentValuesInTree(int value) { } @ParameterizedTest - @ValueSource(ints = {41, 35, 2, 52, 542, 223, 92, 38}) - void testFindingAbsentValuesInTree() { - assertFalse(tree.findcall(666)); + @ValueSource(ints = {41, 31, 2, 52, 542, 223, 92, 38}) + void testFindingAbsentValuesInTree(int value) { + assertFalse(tree.findcall(value)); + } + + @Test + void testFindingAllNodesOfCertainDepthInTree() { + int height = tree.heightcall(); + + for (int i = 0; i <= height; i++) { + tree.depthcaller(i); + } + + assertEquals("4034132342121239835", systemInOut.getConsoleOutput()); + } + + @Test + void testPreOrderPrintsAsExpected() { + tree.preordercall(); + assertEquals("40 34 1 32 123 342 98 35 12 .", systemInOut.getConsoleOutput()); + } + + @Test + void testPostOrderPrintsAsExpected() { + tree.postordercall(); + assertEquals("34 1 123 32 98 35 342 12 40 .", systemInOut.getConsoleOutput()); + } + + @Test + void testLevelOrderPrintsAsExpected() { + tree.levelorder(); + assertEquals("40 34 1 32 342 12 123 98 35 .", systemInOut.getConsoleOutput()); + } + + @Test + void testLeavesAreRemoved() { + tree.removeleavescall(); + tree.display(); + + assertEquals("40=>32 342 .32=>.342=>.", systemInOut.getConsoleOutput()); } } diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java similarity index 89% rename from src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java rename to src/test/java/com/thealgorithms/datastructures/trees/LCATest.java index 1a4bc31f394c..d7d74cd92ed0 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/LowestCommonAncestorTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java @@ -1,5 +1,6 @@ package com.thealgorithms.datastructures.trees; +import com.thealgorithms.utils.ConsoleInterceptor; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; @@ -9,9 +10,6 @@ import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.MethodSource; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.stream.Stream; @@ -19,7 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; -public class LowestCommonAncestorTest { +public class LCATest { /** * This input creates the following tree: *
@@ -61,18 +59,14 @@ void setup() {
     @MethodSource("getInput")
     @DisplayName("Should return correct common ancestor for any two nodes in the tree")
     void shouldReturnCorrectLCAThroughMain(String simulatedInput, String expectedParent) {
-        System.setIn(new ByteArrayInputStream(simulatedInput.getBytes()));
-
-        ByteArrayOutputStream outContent = new ByteArrayOutputStream();
-        PrintStream originalOut = System.out;
-        System.setOut(new PrintStream(outContent));
+        ConsoleInterceptor systemInOut = new ConsoleInterceptor();
+        systemInOut.mockInputAndCaptureOutput(simulatedInput);
 
         LCA.main(new String[0]);
 
-        System.setOut(originalOut);
-        System.setIn(System.in);
+        systemInOut.restoreInAndOutput();
 
-        String actualParent = outContent.toString().trim();
+        String actualParent = systemInOut.getConsoleOutput();
         assertEquals(expectedParent, actualParent);
     }
 
diff --git a/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java b/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java
new file mode 100644
index 000000000000..e4fb5178f4a6
--- /dev/null
+++ b/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java
@@ -0,0 +1,54 @@
+package com.thealgorithms.utils;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+public class ConsoleInterceptor {
+    ByteArrayOutputStream outContent;
+    PrintStream originalOut;
+
+    /* ===========================
+        Input And Output
+    =========================== */
+
+    public void mockInputAndCaptureOutput(String mockedInput) {
+        mockInput(mockedInput);
+        captureOutput();
+    }
+
+    public void restoreInAndOutput() {
+        restoreInput();
+        restoreOutput();
+    }
+
+    /* ===========================
+        Input
+    =========================== */
+
+    public void mockInput(String mockedInput) {
+        System.setIn(new ByteArrayInputStream(mockedInput.getBytes()));
+    }
+
+    public void restoreInput() {
+        System.setIn(System.in);
+    }
+
+    /* ===========================
+        Output
+    =========================== */
+
+    public void captureOutput() {
+        outContent = new ByteArrayOutputStream();
+        originalOut = System.out;
+        System.setOut(new PrintStream(outContent));
+    }
+
+    public void restoreOutput() {
+        System.setOut(originalOut);
+    }
+
+    public String getConsoleOutput() {
+        return outContent.toString().trim().replace("\n", "");
+    }
+}

From 50d5ac6cacbe9f636f053b7b73b6ea6c7f13a785 Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Mon, 20 Oct 2025 22:03:36 +0300
Subject: [PATCH 31/56] Small changes regarding ConsoleInterceptor

---
 .../datastructures/trees/AVLSimpleTest.java   |  8 +-
 .../datastructures/trees/GenericTreeTest.java | 41 +++++++---
 .../datastructures/trees/LCATest.java         | 21 +++--
 .../utils/ConsoleInterceptor.java             | 78 ++++++++++++-------
 4 files changed, 99 insertions(+), 49 deletions(-)

diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index 150699a81d36..42f608088a1d 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -15,7 +15,7 @@
 
 public class AVLSimpleTest {
     AVLSimple tree = new AVLSimple();
-    ConsoleInterceptor systemOut = new ConsoleInterceptor();
+    ConsoleInterceptor interceptor = new ConsoleInterceptor();
 
     /* ========================
         Setup/TearDown
@@ -23,12 +23,12 @@ public class AVLSimpleTest {
 
     @BeforeEach
     void setup() {
-        systemOut.captureOutput();
+        interceptor.captureOutput();
     }
 
     @AfterEach
     void tearDown() {
-        systemOut.restoreOutput();
+        interceptor.close();
     }
 
     /* ========================
@@ -45,7 +45,7 @@ String getExpectedTree() {
     }
 
     String getActualTree() {
-        return systemOut.getConsoleOutput();
+        return interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "");
     }
 
     /* ========================
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java
index 929683fd2419..0c2902a7bb60 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java
@@ -16,7 +16,7 @@
 
 public class GenericTreeTest {
     GenericTree tree;
-    ConsoleInterceptor systemInOut = new ConsoleInterceptor();
+    ConsoleInterceptor interceptor = new ConsoleInterceptor();
 
     /* ========================
         Setup/TearDown
@@ -25,16 +25,17 @@ public class GenericTreeTest {
     @BeforeEach
     void setup() {
         String treeValues = "40\n5\n34\n0\n1\n0\n32\n1\n123\n0\n342\n2\n98\n0\n35\n0\n12\n0";
-        systemInOut.mockInput(treeValues);
+        interceptor.mockInput(treeValues);
+        interceptor.captureOutput();
 
         tree = new GenericTree();
 
-        systemInOut.captureOutput();
+        interceptor.clearConsoleOutput();
     }
 
     @AfterEach
     void tearDown() {
-        systemInOut.restoreInAndOutput();
+        interceptor.close();
     }
 
     /**
@@ -62,13 +63,16 @@ String getExpectedTree() {
     void testCreateValidTree() {
         tree.display();
 
-        assertEquals(getExpectedTree(), systemInOut.getConsoleOutput());
+        assertEquals(
+          getExpectedTree(),
+          interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "")
+        );
     }
 
     @Test
     void testCreateInValidTree() {
         String invalidTreeValues = "a\nb\nc\n";
-        systemInOut.mockInput(invalidTreeValues);
+        interceptor.mockInput(invalidTreeValues);
 
         assertThrows(InputMismatchException.class, GenericTree::new);
     }
@@ -108,25 +112,37 @@ void testFindingAllNodesOfCertainDepthInTree() {
             tree.depthcaller(i);
         }
 
-        assertEquals("4034132342121239835", systemInOut.getConsoleOutput());
+        assertEquals(
+          "4034132342121239835",
+          interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "")
+        );
     }
 
     @Test
     void testPreOrderPrintsAsExpected() {
         tree.preordercall();
-        assertEquals("40 34 1 32 123 342 98 35 12 .", systemInOut.getConsoleOutput());
+        assertEquals(
+          "40 34 1 32 123 342 98 35 12 .",
+          interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "")
+        );
     }
 
     @Test
     void testPostOrderPrintsAsExpected() {
         tree.postordercall();
-        assertEquals("34 1 123 32 98 35 342 12 40 .", systemInOut.getConsoleOutput());
+        assertEquals(
+          "34 1 123 32 98 35 342 12 40 .",
+          interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "")
+        );
     }
 
     @Test
     void testLevelOrderPrintsAsExpected() {
         tree.levelorder();
-        assertEquals("40 34 1 32 342 12 123 98 35 .", systemInOut.getConsoleOutput());
+        assertEquals(
+          "40 34 1 32 342 12 123 98 35 .",
+          interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "")
+        );
     }
 
     @Test
@@ -134,6 +150,9 @@ void testLeavesAreRemoved() {
         tree.removeleavescall();
         tree.display();
 
-        assertEquals("40=>32 342 .32=>.342=>.", systemInOut.getConsoleOutput());
+        assertEquals(
+          "40=>32 342 .32=>.342=>.",
+          interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "")
+        );
     }
 }
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java
index d7d74cd92ed0..ad059e774346 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java
@@ -38,6 +38,10 @@ public class LCATest {
     private final int[] depth = new int[arrayLength];
     private final ArrayList> adj = new ArrayList<>();
 
+    /* ========================
+        Setup
+    ======================== */
+
     @BeforeEach
     void setup() {
         for (int i = 0; i < arrayLength; i++) {
@@ -53,21 +57,26 @@ void setup() {
         }
     }
 
+    /* ========================
+        Tests
+    ======================== */
+
     @Disabled("This would be the best way to test LCA but since the scanner is a\n"
       + "static global variable it doesn't work, it should be moved into the main method.")
     @ParameterizedTest
     @MethodSource("getInput")
     @DisplayName("Should return correct common ancestor for any two nodes in the tree")
     void shouldReturnCorrectLCAThroughMain(String simulatedInput, String expectedParent) {
-        ConsoleInterceptor systemInOut = new ConsoleInterceptor();
-        systemInOut.mockInputAndCaptureOutput(simulatedInput);
+        try (ConsoleInterceptor interceptor = new ConsoleInterceptor()) {
+            interceptor.mockInput(simulatedInput);
+            interceptor.captureOutput();
 
-        LCA.main(new String[0]);
+            LCA.main(new String[0]);
 
-        systemInOut.restoreInAndOutput();
+            String actualParent = interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "");
 
-        String actualParent = systemInOut.getConsoleOutput();
-        assertEquals(expectedParent, actualParent);
+            assertEquals(expectedParent, actualParent);
+        }
     }
 
     public static Stream getInput() {
diff --git a/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java b/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java
index e4fb5178f4a6..e362ae5693d5 100644
--- a/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java
+++ b/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java
@@ -2,53 +2,75 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
 import java.io.PrintStream;
 
-public class ConsoleInterceptor {
-    ByteArrayOutputStream outContent;
-    PrintStream originalOut;
-
-    /* ===========================
-        Input And Output
-    =========================== */
-
-    public void mockInputAndCaptureOutput(String mockedInput) {
-        mockInput(mockedInput);
-        captureOutput();
-    }
-
-    public void restoreInAndOutput() {
-        restoreInput();
-        restoreOutput();
-    }
+/**
+ * Utility for mocking System.in and capturing System.out in a single-threaded environment.
+ * Designed for use with try-with-resources inside test methods to automatically restore them after usage.
+ * 

+ * Recommended usage: + *

    + *
  • Inside test methods with try-with-resources for automatic restoration of streams
  • + *
  • Or as a global instance in the test class, in which case you must call {@link #close()} in a teardown method
  • + *
+ */ +public class ConsoleInterceptor implements AutoCloseable { + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private final InputStream originalIn = System.in; + private boolean isCapturing; /* =========================== Input =========================== */ + /** Mock System.in with provided input */ public void mockInput(String mockedInput) { System.setIn(new ByteArrayInputStream(mockedInput.getBytes())); } - public void restoreInput() { - System.setIn(System.in); - } - /* =========================== Output =========================== */ + /** Start capturing System.out */ public void captureOutput() { - outContent = new ByteArrayOutputStream(); - originalOut = System.out; - System.setOut(new PrintStream(outContent)); + if (!isCapturing) { + System.setOut(new PrintStream(outContent)); + isCapturing = true; + } } - public void restoreOutput() { - System.setOut(originalOut); + /** + * Get current captured output and clear the buffer + * @return the console output as a string + * @throws IllegalStateException if output hasn't been captured yet + */ + public String getAndClearConsoleOutput() { + if (isCapturing && outContent.size() > 0) { + String output = outContent.toString(); + outContent.reset(); + return output; + } else { + throw new IllegalStateException("Output hasn't been captured yet."); + } } - public String getConsoleOutput() { - return outContent.toString().trim().replace("\n", ""); + /** Clears the output buffer */ + public void clearConsoleOutput() { + outContent.reset(); + } + + /* =========================== + Input And Output + =========================== */ + + @Override + public void close() { + System.setOut(originalOut); + System.setIn(originalIn); + outContent.reset(); + isCapturing = false; } } From a7f100f0042f14b0a215ba4883b42c18f1c49061 Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Mon, 20 Oct 2025 22:20:58 +0300 Subject: [PATCH 32/56] Restored the pom file for merge --- pom.xml | 58 --------------------------------------------------------- 1 file changed, 58 deletions(-) diff --git a/pom.xml b/pom.xml index 0e44bca18eb3..fbd9baada8c1 100644 --- a/pom.xml +++ b/pom.xml @@ -202,62 +202,4 @@ - - - - jolla-dev - - - - - maven-surefire-plugin - - - com/thealgorithms/datastructures/trees/**/*.java - - - - - - - org.pitest - pitest-maven - - com.thealgorithms.datastructures.trees.GenericTree - com.thealgorithms.datastructures.trees.GenericTreeTest - - - - pitest - verify - - mutationCoverage - - - - - - - - org.jacoco - jacoco-maven-plugin - - - - prepare-agent - - - - generate-code-coverage-report - verify - - report - - - - - - - - From d2f53d19ef210038e10ef21b5f0119f0b0e67312 Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Mon, 20 Oct 2025 23:20:06 +0300 Subject: [PATCH 33/56] Removed an effin TODO comment --- .../com/thealgorithms/datastructures/trees/AVLSimpleTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java index 42f608088a1d..540c2f4a814a 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java @@ -138,8 +138,6 @@ public static Stream getTreeNodesInputForBFEqualsOneRotations() { ); } - // TODO: Add rotation tests for RL and LR when bf = 1 - @Test @DisplayName("Should return true for a tree that don't account for duplicates") void testDuplicatesInTreeCreationDoNotStick() { From 575f4ae2e9052d488dc0127e3339813a096341be Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Tue, 21 Oct 2025 11:38:11 +0300 Subject: [PATCH 34/56] Added some empty/minimal tree tests --- .../datastructures/trees/AVLSimpleTest.java | 16 +++++++ .../datastructures/trees/GenericTreeTest.java | 45 +++++++++---------- .../datastructures/trees/LCATest.java | 34 +++++++++++--- .../utils/ConsoleInterceptor.java | 32 ++++++++++--- 4 files changed, 92 insertions(+), 35 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java index 540c2f4a814a..1bf90e6415be 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java @@ -3,6 +3,7 @@ import com.thealgorithms.utils.ConsoleInterceptor; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -83,6 +84,21 @@ void testTreeCreation() { assertEquals(expectedTree, getActualTree()); } + @Disabled( + "This test should pass for empty trees to protect against crashes " + + "when trying to access the display method before inserting anything" + ) + @Test + @DisplayName("A test where an empty tree should return the string \"Tree is empty\".") + void testEmptyTree() { + AVLSimple tree = new AVLSimple(); + + tree.display(); + + assertEquals("Tree is empty", getActualTree()); + } + + @ParameterizedTest @MethodSource("getTreeNodesInput") @DisplayName("Test to ensure all rotation paths are covered") diff --git a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java index 0c2902a7bb60..88196c808595 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java @@ -9,6 +9,7 @@ import java.util.InputMismatchException; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -38,6 +39,10 @@ void tearDown() { interceptor.close(); } + /* ======================== + Helper methods + ======================== */ + /** * Creates a generic tree that looks something like this: * @@ -55,6 +60,10 @@ String getExpectedTree() { return "40=>34 1 32 342 12 .34=>.1=>.32=>123 .123=>.342=>98 35 .98=>.35=>.12=>."; } + String getConsoleOutput() { + return interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", ""); + } + /* ======================== Tests ======================== */ @@ -64,9 +73,14 @@ void testCreateValidTree() { tree.display(); assertEquals( - getExpectedTree(), - interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "") - ); + getExpectedTree(), getConsoleOutput()); + } + + @Test + void testCreateValidTreeIsNotEmpty() { + tree.display(); + + assertDoesNotThrow(interceptor::getAndClearConsoleOutput); } @Test @@ -112,37 +126,25 @@ void testFindingAllNodesOfCertainDepthInTree() { tree.depthcaller(i); } - assertEquals( - "4034132342121239835", - interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "") - ); + assertEquals("4034132342121239835", getConsoleOutput()); } @Test void testPreOrderPrintsAsExpected() { tree.preordercall(); - assertEquals( - "40 34 1 32 123 342 98 35 12 .", - interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "") - ); + assertEquals("40 34 1 32 123 342 98 35 12 .", getConsoleOutput()); } @Test void testPostOrderPrintsAsExpected() { tree.postordercall(); - assertEquals( - "34 1 123 32 98 35 342 12 40 .", - interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "") - ); + assertEquals("34 1 123 32 98 35 342 12 40 .", getConsoleOutput()); } @Test void testLevelOrderPrintsAsExpected() { tree.levelorder(); - assertEquals( - "40 34 1 32 342 12 123 98 35 .", - interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "") - ); + assertEquals("40 34 1 32 342 12 123 98 35 .", getConsoleOutput()); } @Test @@ -150,9 +152,6 @@ void testLeavesAreRemoved() { tree.removeleavescall(); tree.display(); - assertEquals( - "40=>32 342 .32=>.342=>.", - interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "") - ); + assertEquals("40=>32 342 .32=>.342=>.", getConsoleOutput()); } } diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java index ad059e774346..e45457388a3d 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.stream.Stream; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -34,8 +35,10 @@ public class LCATest { */ private final int[] treeInput = new int[] {0, 1, 0, 2, 1, 5, 5, 6, 2, 4, 2, 3, 3, 7, 7, 9, 7, 8}; private final int arrayLength = 10; - private final int[] parent = new int[arrayLength]; - private final int[] depth = new int[arrayLength]; + private int[] parent = new int[arrayLength]; + private int[] depth = new int[arrayLength]; + private final int startingSourceVertex = 0; + private final int startParentOfSource = -1; private final ArrayList> adj = new ArrayList<>(); /* ======================== @@ -100,7 +103,7 @@ void shouldReturnCorrectLCA(int v1, int v2, int expectedParent) throws Exception dfs.setAccessible(true); getLCA.setAccessible(true); - dfs.invoke(null, adj, 0, -1, parent, depth); + dfs.invoke(null, adj, startingSourceVertex, startParentOfSource, parent, depth); assertEquals(expectedParent, getLCA.invoke(null, v1, v2, depth, parent)); } @@ -111,7 +114,7 @@ void shouldReturnCorrectDepthsForArray() throws Exception { Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class); dfs.setAccessible(true); - dfs.invoke(null, adj, 0, -1, parent, depth); + dfs.invoke(null, adj, startingSourceVertex, startParentOfSource, parent, depth); assertArrayEquals(new int[] {0, 1, 1, 2, 2, 2, 3, 3, 4, 4}, depth); } @@ -122,8 +125,29 @@ void shouldReturnCorrectParentsForArray() throws Exception { Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class); dfs.setAccessible(true); - dfs.invoke(null, adj, 0, -1, parent, depth); + dfs.invoke(null, adj, startingSourceVertex, startParentOfSource, parent, depth); assertArrayEquals(new int[] {0, 0, 0, 2, 2, 1, 5, 3, 7, 7}, parent); } + + @Test + void testMinimalTreeDepthAndParent() throws Exception { + int v = 1; + ArrayList> minimalAdj = new ArrayList<>(); + for (int i = 0; i < v; i++) { + minimalAdj.add(new ArrayList<>()); + } + parent = new int[v]; + depth = new int[v]; + + Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class); + dfs.setAccessible(true); + + dfs.invoke(null, minimalAdj, startingSourceVertex, startParentOfSource, parent, depth); + + assertAll( + () -> assertArrayEquals(new int[] {0}, depth), + () -> assertArrayEquals(new int[] {0}, parent) + ); + } } diff --git a/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java b/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java index e362ae5693d5..d773ce46aaad 100644 --- a/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java +++ b/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java @@ -12,12 +12,17 @@ * Recommended usage: *
    *
  • Inside test methods with try-with-resources for automatic restoration of streams
  • - *
  • Or as a global instance in the test class, in which case you must call {@link #close()} in a teardown method
  • + *
  • + * Or as a global instance in the test class, in which case you should call {@link #close()} + * for good measures in a teardown method + *
  • *
*/ public class ConsoleInterceptor implements AutoCloseable { private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + /** Saved reference to stdout */ private final PrintStream originalOut = System.out; + /** Saved reference to stdin */ private final InputStream originalIn = System.in; private boolean isCapturing; @@ -25,7 +30,10 @@ public class ConsoleInterceptor implements AutoCloseable { Input =========================== */ - /** Mock System.in with provided input */ + /** + * Mock System.in with the provided input. + * Used in test, mainly for simulating input from the keyboard for scanners. + * */ public void mockInput(String mockedInput) { System.setIn(new ByteArrayInputStream(mockedInput.getBytes())); } @@ -34,7 +42,10 @@ public void mockInput(String mockedInput) { Output =========================== */ - /** Start capturing System.out */ + /** + * Start capturing System.out by replacing stdout with a custom PrintStream. + * All printed data will be stored in outContent for later retrieval. + * */ public void captureOutput() { if (!isCapturing) { System.setOut(new PrintStream(outContent)); @@ -43,8 +54,8 @@ public void captureOutput() { } /** - * Get current captured output and clear the buffer - * @return the console output as a string + * Get current captured output and clears the buffer. + * @return the captured output as a string * @throws IllegalStateException if output hasn't been captured yet */ public String getAndClearConsoleOutput() { @@ -57,7 +68,7 @@ public String getAndClearConsoleOutput() { } } - /** Clears the output buffer */ + /** Clears the output buffer. */ public void clearConsoleOutput() { outContent.reset(); } @@ -65,7 +76,14 @@ public void clearConsoleOutput() { /* =========================== Input And Output =========================== */ - + /** + * {@inheritDoc} + *

+ * This override restores the original System.out and System.in streams, + * resets the captured output stored in the internal OutputStream, + * and sets the {@code isCapturing} flag to {@code false} to indicate + * that capturing has stopped and prevent further access to {@code outContent}. + */ @Override public void close() { System.setOut(originalOut); From 0de92cc585464d09f7b464aeac921b732f4661d9 Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Wed, 22 Oct 2025 12:00:22 +0300 Subject: [PATCH 35/56] Changed pitest phase in pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index fbd9baada8c1..07ea84531152 100644 --- a/pom.xml +++ b/pom.xml @@ -193,7 +193,7 @@ pitest - pitest + test mutationCoverage From 1f91e3433667e1321baa6568bf5d07b33751cca4 Mon Sep 17 00:00:00 2001 From: hedint <2022.195@student.setur.fo> Date: Thu, 23 Oct 2025 10:59:01 +0100 Subject: [PATCH 36/56] fixed comment and added extra test for IEEE 754 --- .../java/com/thealgorithms/maths/MathBuilderTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 1ec6efd126df..5d70f7b211d6 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -141,7 +141,7 @@ void divideByNum() { } @Test - @DisplayName("This Should throw but does not") + @DisplayName("follows IEEE 754") void divideByZero() { @@ -151,10 +151,16 @@ void divideByZero() { MathBuilder.Builder actual2 = new MathBuilder.Builder(10.5).openParenthesis(0) .closeParenthesisAndDivide(); + + MathBuilder actual3 = new MathBuilder.Builder(-10.5).openParenthesis(0) + .closeParenthesisAndDivide().divide(0).build(); + assertAll( () -> assertTrue(Double.isInfinite(actual.get())), () -> assertDoesNotThrow(() -> actual2.build().get()), - () -> assertDoesNotThrow(() -> actual2.divide(0).build().get()) + () -> assertDoesNotThrow(() -> actual2.divide(0).build().get()), + () -> assertEquals(Double.POSITIVE_INFINITY, actual2.divide(0).build().get()), + () -> assertEquals(Double.NEGATIVE_INFINITY, actual3.get()) ); From 59cf6f4d9128bf6349657c5b0f488c4868031b3c Mon Sep 17 00:00:00 2001 From: hedint <2022.195@student.setur.fo> Date: Fri, 24 Oct 2025 15:29:04 +0100 Subject: [PATCH 37/56] from J-manLans, a good solution --- .../thealgorithms/maths/MathBuilderTest.java | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 5d70f7b211d6..fb0aee2dac57 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -140,34 +140,29 @@ void divideByNum() { assertNotEquals(expected2,actual); } - @Test - @DisplayName("follows IEEE 754") - void divideByZero() { - - - - MathBuilder actual = new MathBuilder.Builder(10.5).openParenthesis(0) - .closeParenthesisAndDivide().divide(0).build(); - - MathBuilder.Builder actual2 = new MathBuilder.Builder(10.5).openParenthesis(0) - .closeParenthesisAndDivide(); - - MathBuilder actual3 = new MathBuilder.Builder(-10.5).openParenthesis(0) - .closeParenthesisAndDivide().divide(0).build(); - + @ParameterizedTest + @MethodSource("divideDoubleByZeroHelper") + @DisplayName("Test that ensures dividing a double by zero follows IEEE 754") + void divideDoubleByZero(double expected, MathBuilder.Builder actual, String error) { assertAll( - () -> assertTrue(Double.isInfinite(actual.get())), - () -> assertDoesNotThrow(() -> actual2.build().get()), - () -> assertDoesNotThrow(() -> actual2.divide(0).build().get()), - () -> assertEquals(Double.POSITIVE_INFINITY, actual2.divide(0).build().get()), - () -> assertEquals(Double.NEGATIVE_INFINITY, actual3.get()) + () -> assertDoesNotThrow(() -> actual.build().get(), "Dividing a double with zero should not throw"), + () -> assertDoesNotThrow(() -> actual.divide(0).build().get(), "Dividing infinity with 0 should not throw"), + () -> assertTrue(Double.isInfinite(actual.build().get()), "Dividing a double by zero should result in infinity"), + () -> assertEquals(expected, actual.build().get(), error) ); + } - - } + static List divideDoubleByZeroHelper() { + return List.of( + Arguments.of(Double.POSITIVE_INFINITY, new MathBuilder.Builder(10.5).openParenthesis(0) + .closeParenthesisAndDivide(), "10.5 / 0 should be +Infinity"), + Arguments.of(Double.NEGATIVE_INFINITY, new MathBuilder.Builder(-10.5).openParenthesis(0) + .closeParenthesisAndDivide(), "-10.5 / 0 should be -Infinity") + ); + } @Test - void randomFunctions() { + void randomFunctionsTest() { double minValue = 0.0; double maxValue = 2.1; From 6fd9c0feeb7620be910db0b082af2a37c3993291 Mon Sep 17 00:00:00 2001 From: hedint <2022.195@student.setur.fo> Date: Fri, 24 Oct 2025 15:32:01 +0100 Subject: [PATCH 38/56] ran clang-format --- .../thealgorithms/maths/MathBuilderTest.java | 304 +++++++----------- 1 file changed, 117 insertions(+), 187 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index fb0aee2dac57..c3711d063374 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -5,178 +5,148 @@ import java.util.List; import java.util.Random; - -import org.junit.jupiter.api.Test; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; class MathBuilderTest { - @Test - void simpleMath() { - double result = new MathBuilder.Builder(100).add(200).multiply(10).print() - .divideIf(20, (a, b) -> a % 2 == 0).sqrt().print().ceil().build().get(); - assertEquals(13, result); - } - - @Test - void memoryTest() { - long result = new MathBuilder.Builder().set(100).sqrt().remember().add(21).recallIf(a -> a < 50, true) - .mod(2).build().toLong(); - assertEquals(0, result); - } - - @Test - void freeFallDistance() { - long distance = new MathBuilder.Builder(9.81).multiply(0.5).multiply(5 * 5).round().build().toLong(); - assertEquals(123, distance); // Expected result: 0.5 * 9.81 * 25 = 122.625 ≈ 123 - } - - @Test - void batchSalaryProcessing() { - double[] salaries = { 2000, 3000, 4000, 5000 }; - long[] processedSalaries = new long[salaries.length]; - for (int i = 0; i < salaries.length; i++) { - processedSalaries[i] = new MathBuilder.Builder(salaries[i]) - .addIf(salaries[i] * 0.1, (sal, bonus) -> sal > 2500).multiply(0.92).round() - .build().toLong(); - } - long[] expectedSalaries = { 1840, 3036, 4048, 5060 }; - assertArrayEquals(expectedSalaries, processedSalaries); - } - - @Test - void parenthesis() { - // 10 + (20*5) - 40 + (100 / 10) = 80 - double result = new MathBuilder.Builder(10).openParenthesis(20).multiply(5).closeParenthesisAndPlus() - .minus(40).openParenthesis(100).divide(10).closeParenthesisAndPlus().build().get(); - assertEquals(80, result); - } - - @Test - void areaOfCircle() { - // Radius is 4 - double area = new MathBuilder.Builder().pi().openParenthesis(4).multiply(4) - .closeParenthesisAndMultiply().build().get(); - assertEquals(Math.PI * 4 * 4, area); - } - - @Test - @DisplayName("Floor Test") - void floorTest() { - // floor(10.5 + (20+2.1)) - double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndPlus() - .floor().build().get(); - double expected = Math.floor(10.5 + 20 + 2.1); - - // 10.5 + floor((20+2.1)) - double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).floor() - .closeParenthesisAndPlus().build().get(); - double expected2 = 10.5 + Math.floor(20 + 2.1); - - assertEquals(expected, actual); - assertEquals(expected2, actual2); - } - - @Test - @DisplayName("Close parenthesis Test") - void closeParenthesisTest() { - // 10.5 - (20+2.1) - double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndMinus() - .build().get(); - double expected = 10.5 - (20 + 2.1); - - // 10.5 / (20+2.1) - double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndDivide() - .build().get(); - double expected2 = 10.5 / (20 + 2.1); - - assertEquals(expected, actual); - assertEquals(expected2, actual2); - } + @Test + void simpleMath() { + double result = new MathBuilder.Builder(100).add(200).multiply(10).print().divideIf(20, (a, b) -> a % 2 == 0).sqrt().print().ceil().build().get(); + assertEquals(13, result); + } + + @Test + void memoryTest() { + long result = new MathBuilder.Builder().set(100).sqrt().remember().add(21).recallIf(a -> a < 50, true).mod(2).build().toLong(); + assertEquals(0, result); + } + + @Test + void freeFallDistance() { + long distance = new MathBuilder.Builder(9.81).multiply(0.5).multiply(5 * 5).round().build().toLong(); + assertEquals(123, distance); // Expected result: 0.5 * 9.81 * 25 = 122.625 ≈ 123 + } + + @Test + void batchSalaryProcessing() { + double[] salaries = {2000, 3000, 4000, 5000}; + long[] processedSalaries = new long[salaries.length]; + for (int i = 0; i < salaries.length; i++) { + processedSalaries[i] = new MathBuilder.Builder(salaries[i]).addIf(salaries[i] * 0.1, (sal, bonus) -> sal > 2500).multiply(0.92).round().build().toLong(); + } + long[] expectedSalaries = {1840, 3036, 4048, 5060}; + assertArrayEquals(expectedSalaries, processedSalaries); + } + + @Test + void parenthesis() { + // 10 + (20*5) - 40 + (100 / 10) = 80 + double result = new MathBuilder.Builder(10).openParenthesis(20).multiply(5).closeParenthesisAndPlus().minus(40).openParenthesis(100).divide(10).closeParenthesisAndPlus().build().get(); + assertEquals(80, result); + } + + @Test + void areaOfCircle() { + // Radius is 4 + double area = new MathBuilder.Builder().pi().openParenthesis(4).multiply(4).closeParenthesisAndMultiply().build().get(); + assertEquals(Math.PI * 4 * 4, area); + } + + @Test + @DisplayName("Floor Test") + void floorTest() { + // floor(10.5 + (20+2.1)) + double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndPlus().floor().build().get(); + double expected = Math.floor(10.5 + 20 + 2.1); + + // 10.5 + floor((20+2.1)) + double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).floor().closeParenthesisAndPlus().build().get(); + double expected2 = 10.5 + Math.floor(20 + 2.1); + + assertEquals(expected, actual); + assertEquals(expected2, actual2); + } + + @Test + @DisplayName("Close parenthesis Test") + void closeParenthesisTest() { + // 10.5 - (20+2.1) + double actual = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndMinus().build().get(); + double expected = 10.5 - (20 + 2.1); + + // 10.5 / (20+2.1) + double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndDivide().build().get(); + double expected2 = 10.5 / (20 + 2.1); + + assertEquals(expected, actual); + assertEquals(expected2, actual2); + } @Test @DisplayName("open parenthesis Test") void openParenthesisAndABSTest() { // 10.5 - (20+2.1) - double actual = new MathBuilder.Builder(10.5).openParenthesis(20).minus(2.1).closeParenthesisAndMinus() - .build().get(); + double actual = new MathBuilder.Builder(10.5).openParenthesis(20).minus(2.1).closeParenthesisAndMinus().build().get(); double expected = 10.5 - (20 - 2.1); // 10.5 / (20+2.1) - double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.2).abs().closeParenthesisAndPlus().abs() - .build().get(); + double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.2).abs().closeParenthesisAndPlus().abs().build().get(); double expected2 = 10.5 + (20 + 2.2); assertEquals(expected, actual); assertEquals(expected2, actual2); } - @Test - @DisplayName("Runtime Errors Tests") - void runtimeErrorTest() { - MathBuilder.Builder actual = new MathBuilder.Builder(10.5); - - - assertAll( - () -> assertThrows(RuntimeException.class, () -> actual.rand(1)), - () -> assertThrows(RuntimeException.class, () -> actual.randomInRange(1, 10)), - () -> assertThrows(RuntimeException.class, () -> actual.pi()), - () -> assertThrows(RuntimeException.class, () -> actual.e()), - () -> assertThrows(RuntimeException.class, () -> actual.set(1)) - ); - } - - @Test - @DisplayName("Should divide 10 by 2") - void divideByNum() { - double actual = new MathBuilder.Builder(10).divide(2).build().get(); - - double expected = 10 / 2; + @Test + @DisplayName("Runtime Errors Tests") + void runtimeErrorTest() { + MathBuilder.Builder actual = new MathBuilder.Builder(10.5); + + assertAll(() + -> assertThrows(RuntimeException.class, () -> actual.rand(1)), + () -> assertThrows(RuntimeException.class, () -> actual.randomInRange(1, 10)), () -> assertThrows(RuntimeException.class, () -> actual.pi()), () -> assertThrows(RuntimeException.class, () -> actual.e()), () -> assertThrows(RuntimeException.class, () -> actual.set(1))); + } + + @Test + @DisplayName("Should divide 10 by 2") + void divideByNum() { + double actual = new MathBuilder.Builder(10).divide(2).build().get(); + + double expected = 10 / 2; double expected2 = 10 / 4; - assertEquals(expected, actual); - assertNotEquals(expected2,actual); - } + assertEquals(expected, actual); + assertNotEquals(expected2, actual); + } @ParameterizedTest @MethodSource("divideDoubleByZeroHelper") @DisplayName("Test that ensures dividing a double by zero follows IEEE 754") void divideDoubleByZero(double expected, MathBuilder.Builder actual, String error) { - assertAll( - () -> assertDoesNotThrow(() -> actual.build().get(), "Dividing a double with zero should not throw"), - () -> assertDoesNotThrow(() -> actual.divide(0).build().get(), "Dividing infinity with 0 should not throw"), - () -> assertTrue(Double.isInfinite(actual.build().get()), "Dividing a double by zero should result in infinity"), - () -> assertEquals(expected, actual.build().get(), error) - ); + assertAll(() + -> assertDoesNotThrow(() -> actual.build().get(), "Dividing a double with zero should not throw"), + () -> assertDoesNotThrow(() -> actual.divide(0).build().get(), "Dividing infinity with 0 should not throw"), () -> assertTrue(Double.isInfinite(actual.build().get()), "Dividing a double by zero should result in infinity"), () -> assertEquals(expected, actual.build().get(), error)); } static List divideDoubleByZeroHelper() { - return List.of( - Arguments.of(Double.POSITIVE_INFINITY, new MathBuilder.Builder(10.5).openParenthesis(0) - .closeParenthesisAndDivide(), "10.5 / 0 should be +Infinity"), - Arguments.of(Double.NEGATIVE_INFINITY, new MathBuilder.Builder(-10.5).openParenthesis(0) - .closeParenthesisAndDivide(), "-10.5 / 0 should be -Infinity") - ); + return List.of(Arguments.of(Double.POSITIVE_INFINITY, new MathBuilder.Builder(10.5).openParenthesis(0).closeParenthesisAndDivide(), "10.5 / 0 should be +Infinity"), + Arguments.of(Double.NEGATIVE_INFINITY, new MathBuilder.Builder(-10.5).openParenthesis(0).closeParenthesisAndDivide(), "-10.5 / 0 should be -Infinity")); } - @Test - void randomFunctionsTest() { + @Test + void randomFunctionsTest() { - double minValue = 0.0; - double maxValue = 2.1; - double actual = new MathBuilder.Builder().rand(2L).build().get(); - double actual2 = new MathBuilder.Builder().randomInRange(minValue, maxValue).build().get(); - assertAll( - () ->assertTrue(actual < maxValue), - () ->assertTrue(actual2 >= minValue), - () ->assertTrue(actual2 <= maxValue) + double minValue = 0.0; + double maxValue = 2.1; + double actual = new MathBuilder.Builder().rand(2L).build().get(); + double actual2 = new MathBuilder.Builder().randomInRange(minValue, maxValue).build().get(); + assertAll(() -> assertTrue(actual < maxValue), () -> assertTrue(actual2 >= minValue), () -> assertTrue(actual2 <= maxValue) ); - - - } + } @ParameterizedTest @MethodSource("radiansHelper") @@ -185,23 +155,13 @@ void toRadiansTests(double expectedAngle, double actualAngle) { } private static List radiansHelper() { - return List.of( - Arguments.of( - Math.toRadians(10), - new MathBuilder.Builder(10).toRadians().build().get() - ), - Arguments.of( - 2 + Math.toRadians(10), - new MathBuilder.Builder(2).openParenthesis(10).toRadians().closeParenthesisAndPlus() - .build().get() - ) - ); + return List.of(Arguments.of(Math.toRadians(10), new MathBuilder.Builder(10).toRadians().build().get()), Arguments.of(2 + Math.toRadians(10), new MathBuilder.Builder(2).openParenthesis(10).toRadians().closeParenthesisAndPlus().build().get())); } @Test void roundCielABSTest() { double actual = new MathBuilder.Builder(10).openParenthesis(10.5).round().closeParenthesisAndPlus().build().get(); - double expected = 10+ (Math.round(10.5)); + double expected = 10 + (Math.round(10.5)); double expected2 = 10 + Math.ceil(10.5); double actual2 = new MathBuilder.Builder(10).openParenthesis(10.5).ceil().closeParenthesisAndPlus().build().get(); @@ -212,37 +172,21 @@ void roundCielABSTest() { double expected4 = Math.abs(10 + 10.5); double actual4 = new MathBuilder.Builder(10).openParenthesis(10.5).closeParenthesisAndPlus().abs().build().get(); - assertAll( - () -> assertNotEquals(0,actual), - () -> assertNotEquals(1,actual2), - () -> assertEquals(expected, actual), - () -> assertEquals(expected2, actual2), - () -> assertEquals(expected3, actual3), - () -> assertEquals(expected4, actual4) - ); + assertAll(() -> assertNotEquals(0, actual), () -> assertNotEquals(1, actual2), () -> assertEquals(expected, actual), () -> assertEquals(expected2, actual2), () -> assertEquals(expected3, actual3), () -> assertEquals(expected4, actual4)); } - @Test void toLongTest() { + MathBuilder actual = new MathBuilder.Builder(10.5).openParenthesis(0).closeParenthesisAndDivide().divide(0).build(); - MathBuilder actual = new MathBuilder.Builder(10.5).openParenthesis(0) - .closeParenthesisAndDivide().divide(0).build(); - - MathBuilder actual2 = new MathBuilder.Builder(10.5).openParenthesis(0) - .closeParenthesisAndDivide().divide(0).multiply(-1).build(); + MathBuilder actual2 = new MathBuilder.Builder(10.5).openParenthesis(0).closeParenthesisAndDivide().divide(0).multiply(-1).build(); MathBuilder actual3 = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(3).build(); MathBuilder actual4 = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(-3).build(); - assertAll( - () -> assertEquals(Long.MAX_VALUE,actual.toLong()), - () -> assertEquals(Long.MIN_VALUE,actual2.toLong()), - () -> assertEquals(Long.MAX_VALUE,actual3.toLong()), - () -> assertEquals(Long.MIN_VALUE,actual4.toLong()), - () -> assertNotEquals(0,actual.toLong()), - () -> assertNotEquals(1,actual2.toLong()) - ); + assertAll(() + -> assertEquals(Long.MAX_VALUE, actual.toLong()), + () -> assertEquals(Long.MIN_VALUE, actual2.toLong()), () -> assertEquals(Long.MAX_VALUE, actual3.toLong()), () -> assertEquals(Long.MIN_VALUE, actual4.toLong()), () -> assertNotEquals(0, actual.toLong()), () -> assertNotEquals(1, actual2.toLong())); } @Test @@ -252,14 +196,7 @@ void maxTest() { MathBuilder actual3 = new MathBuilder.Builder(10.5).openParenthesis(10).max(20).closeParenthesisAndPlus().build(); MathBuilder actual4 = new MathBuilder.Builder(12.5).openParenthesis(10).closeParenthesisAndPlus().max(20).build(); - assertAll( - () -> assertEquals(20,actual.get()), - () -> assertEquals(13.5,actual2.get()), - () -> assertEquals(30.5,actual3.get()), - () -> assertEquals(22.5,actual4.get()), - () -> assertNotEquals(30,actual4.get()), - () -> assertNotEquals(5,actual4.get()) - ); + assertAll(() -> assertEquals(20, actual.get()), () -> assertEquals(13.5, actual2.get()), () -> assertEquals(30.5, actual3.get()), () -> assertEquals(22.5, actual4.get()), () -> assertNotEquals(30, actual4.get()), () -> assertNotEquals(5, actual4.get())); } @Test void minTest() { @@ -268,13 +205,6 @@ void minTest() { MathBuilder actual3 = new MathBuilder.Builder(10.5).openParenthesis(10).min(20).closeParenthesisAndPlus().build(); MathBuilder actual4 = new MathBuilder.Builder(12.5).openParenthesis(10).closeParenthesisAndPlus().min(20).build(); -assertAll( - () -> assertEquals(10.5,actual.get()), - () -> assertEquals(8.5,actual2.get()), - () -> assertEquals(20.5,actual3.get()), - () -> assertEquals(20,actual4.get()), - () -> assertNotEquals(5, actual.get()), - () -> assertNotEquals(-1000, actual3.get()) -); + assertAll(() -> assertEquals(10.5, actual.get()), () -> assertEquals(8.5, actual2.get()), () -> assertEquals(20.5, actual3.get()), () -> assertEquals(20, actual4.get()), () -> assertNotEquals(5, actual.get()), () -> assertNotEquals(-1000, actual3.get())); } } From 34fdc901cbb6795901f466dbd41ef300279935cf Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Mon, 27 Oct 2025 18:20:37 +0200 Subject: [PATCH 39/56] Corrected our additions Made corrections according to checkstyle and spotbugs in all our files. Clang is left though... --- .../com/thealgorithms/ciphers/MyAESTest.java | 4 +- .../thealgorithms/ciphers/MyCaesarTest.java | 7 ++- .../MyColumnarTranspositionCipherTest.java | 8 ++-- .../ciphers/MyPlayfairCipherTest.java | 13 ++---- .../thealgorithms/maths/MathBuilderTest.java | 21 +++++---- .../maths/RomanNumeralUtilTest.java | 43 +++++++------------ .../maths/SimpsonIntegrationTest.java | 4 +- .../utils/ConsoleInterceptor.java | 13 ++++-- 8 files changed, 54 insertions(+), 59 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MyAESTest.java b/src/test/java/com/thealgorithms/ciphers/MyAESTest.java index f67362c810cf..afea810867f7 100644 --- a/src/test/java/com/thealgorithms/ciphers/MyAESTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MyAESTest.java @@ -19,7 +19,7 @@ void testScheduleCore() { BigInteger result = AES.scheduleCore(input, rconCounter); assertEquals(expected, result, "Should return " + expected); } - + @Test void testKeyExpansion() { BigInteger initialKey = new BigInteger("000102030405060708090a0b0c0d0e0f", 16); @@ -54,7 +54,7 @@ void testSplitBlockIntoCells() { @Test void testMergeCellsIntoBlocks() { int[] cells = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; - + StringBuilder expectedBinary = new StringBuilder(); for (int cell : cells) { diff --git a/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java b/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java index 28c90a4ca50a..14180ea985fb 100644 --- a/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java @@ -1,10 +1,9 @@ package com.thealgorithms.ciphers; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class MyCaesarTest { private final Caesar caesar = new Caesar(); @@ -14,7 +13,7 @@ void shouldReturnSameAsInputWhenShiftingZeroOr26() { String encoded1 = caesar.encode(message, 0); String encoded2 = caesar.encode(message, 26); - + assertEquals(message, encoded1, "Encoded should be same as original"); assertEquals(message, encoded2, "Encoded should be same as original"); } diff --git a/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java b/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java index 484318bfdeff..0ae3304a2d69 100644 --- a/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java @@ -22,7 +22,7 @@ public void setUp() { @Test void shouldNotProduceNullOrEmptyEncryptedText() { String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword); - + assertNotNull(encrypted, "Encrypted text should not be null"); assertFalse(encrypted.isEmpty(), "Encrypted text should not be empty"); } @@ -59,7 +59,7 @@ void shouldMatchWithUnsortedKeyword() { String encrypted = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); String expected = "8≈7≈2≈4≈5≈3≈6≈19"; - + assertEquals(expected, encrypted, "Should match"); } @@ -67,7 +67,7 @@ void shouldMatchWithUnsortedKeyword() { void shouldMatchEncryptionAndDecryptionWithNoSpacesOrPadding() { String myPlaintext = "NoSpacesOrPadding"; - String encrypted = ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); + ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); String decrypted = ColumnarTranspositionCipher.decrypt(); assertEquals(myPlaintext, decrypted, "Decrypted text should match original plaintext"); @@ -77,7 +77,7 @@ void shouldMatchEncryptionAndDecryptionWithNoSpacesOrPadding() { void shouldNotContainPaddingInDecryption() { String myPlaintext = "text"; - String encrypted = ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); + ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); String decrypted = ColumnarTranspositionCipher.decrypt(); assertFalse(decrypted.contains("≈"), "Should not contain padding characters"); diff --git a/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java b/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java index 5b1e35dbe128..46c270a83d34 100644 --- a/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java @@ -1,15 +1,10 @@ package com.thealgorithms.ciphers; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.lang.reflect.Method; -import java.util.Arrays; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class MyPlayfairCipherTest { private PlayfairCipher playfair; private final String keyword = "KEYWORD"; @@ -30,11 +25,11 @@ void shouldEncryptAndDecryptDuringSameRowDigraph() { } @Test - void shouldPadOddLengthplaintext() { + void shouldPadOddLengthPlaintext() { String plaintext = "cat"; String encrypted = playfair.encrypt(plaintext); - assertTrue(encrypted.length() % 2 == 0, "Should be even length"); + assertEquals(0, encrypted.length() % 2, "Should be even length"); } } diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index c3711d063374..731fa95b0ba7 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -1,16 +1,21 @@ package com.thealgorithms.maths; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.doubleThat; - -import java.util.List; -import java.util.Random; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + class MathBuilderTest { @Test @@ -107,7 +112,7 @@ void runtimeErrorTest() { assertAll(() -> assertThrows(RuntimeException.class, () -> actual.rand(1)), - () -> assertThrows(RuntimeException.class, () -> actual.randomInRange(1, 10)), () -> assertThrows(RuntimeException.class, () -> actual.pi()), () -> assertThrows(RuntimeException.class, () -> actual.e()), () -> assertThrows(RuntimeException.class, () -> actual.set(1))); + () -> assertThrows(RuntimeException.class, () -> actual.randomInRange(1, 10)), () -> assertThrows(RuntimeException.class, actual::pi), () -> assertThrows(RuntimeException.class, actual::e), () -> assertThrows(RuntimeException.class, () -> actual.set(1))); } @Test @@ -115,8 +120,8 @@ void runtimeErrorTest() { void divideByNum() { double actual = new MathBuilder.Builder(10).divide(2).build().get(); - double expected = 10 / 2; - double expected2 = 10 / 4; + double expected = 10.0 / 2.0; + double expected2 = 10.0 / 4.0; assertEquals(expected, actual); assertNotEquals(expected2, actual); diff --git a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java index 158c9ca8435d..7339d0b732f1 100644 --- a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java +++ b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java @@ -3,40 +3,29 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import static org.junit.jupiter.api.Assertions.assertThrows; + import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - - -class RomanNumeralUtilTest{ - +import static org.junit.jupiter.api.Assertions.assertThrows; +class RomanNumeralUtilTest { @ParameterizedTest() - @CsvSource( - { - "2000,MM", - "1,I", - "2,II", - "1003,MIII", - "1004,MIV" - + @CsvSource({ + "2000,MM", + "1,I", + "2,II", + "1003,MIII", + "1004,MIV" }) - void minimumMaximumTest(int testint,String word){ - - - System.out.println(word + ": " + testint); - assertEquals(RomanNumeralUtil.generate(testint),word); - + void minimumMaximumTest(int testInt, String word) { + System.out.println(word + ": " + testInt); + assertEquals(RomanNumeralUtil.generate(testInt), word); } - @Test - void calcSomeNumerals(){ - - assertThrows(IllegalArgumentException.class, ()->RomanNumeralUtil.generate(0)); - assertThrows(IllegalArgumentException.class, ()->RomanNumeralUtil.generate(6000)); - + @Test + void calcSomeNumerals() { + assertThrows(IllegalArgumentException.class, () -> RomanNumeralUtil.generate(0)); + assertThrows(IllegalArgumentException.class, () -> RomanNumeralUtil.generate(6000)); } @Test diff --git a/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java b/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java index ea4fae2b651a..be27bc8a6c4b 100644 --- a/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java +++ b/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java @@ -25,11 +25,11 @@ void shouldCalculateCorrectMethod() { int n = 4; double a = -1.0; double b = 1.0; - double h = (b - a)/n; + double h = (b - a) / n; double result = simpson.simpsonsMethod(n, h, a); double expected = 8.51454379418048; - + assertEquals(expected, result, DELTA); } diff --git a/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java b/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java index d773ce46aaad..fb598c17247d 100644 --- a/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java +++ b/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java @@ -32,8 +32,15 @@ public class ConsoleInterceptor implements AutoCloseable { /** * Mock System.in with the provided input. - * Used in test, mainly for simulating input from the keyboard for scanners. - * */ + * Used in test, mainly for simulating user input from the keyboard for scanners. + *

+ * Each line of user input must end with a newline character (\n), + * because {@link java.util.Scanner#nextLine()} and similar methods read input line by line. + *

+ * Example input: + *

+ * "This is input line one\nAnd this is the second line of input\nAnd so on...\n" + */ public void mockInput(String mockedInput) { System.setIn(new ByteArrayInputStream(mockedInput.getBytes())); } @@ -45,7 +52,7 @@ public void mockInput(String mockedInput) { /** * Start capturing System.out by replacing stdout with a custom PrintStream. * All printed data will be stored in outContent for later retrieval. - * */ + */ public void captureOutput() { if (!isCapturing) { System.setOut(new PrintStream(outContent)); From a84d8d9eca806016bb901b8cacda9fc286e4da06 Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Mon, 27 Oct 2025 19:15:12 +0200 Subject: [PATCH 40/56] Corrected imports Autocorrected import order according to Clang's wishes --- .../com/thealgorithms/ciphers/MyAESTest.java | 1 - .../com/thealgorithms/ciphers/MyCaesarTest.java | 4 ++-- .../ciphers/MyPlayfairCipherTest.java | 4 ++-- .../datastructures/trees/AVLSimpleTest.java | 9 ++++----- .../datastructures/trees/GenericTreeTest.java | 17 ++++++++--------- .../datastructures/trees/LCATest.java | 17 ++++++++--------- .../{utils => devutils}/ConsoleInterceptor.java | 2 +- .../thealgorithms/maths/MathBuilderTest.java | 15 +++++++-------- .../maths/RomanNumeralUtilTest.java | 8 ++++---- 9 files changed, 36 insertions(+), 41 deletions(-) rename src/test/java/com/thealgorithms/{utils => devutils}/ConsoleInterceptor.java (98%) diff --git a/src/test/java/com/thealgorithms/ciphers/MyAESTest.java b/src/test/java/com/thealgorithms/ciphers/MyAESTest.java index afea810867f7..c58604139b12 100644 --- a/src/test/java/com/thealgorithms/ciphers/MyAESTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MyAESTest.java @@ -5,7 +5,6 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import java.math.BigInteger; - import org.junit.jupiter.api.Test; public class MyAESTest { diff --git a/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java b/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java index 14180ea985fb..d41cb33eecde 100644 --- a/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java @@ -1,9 +1,9 @@ package com.thealgorithms.ciphers; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + public class MyCaesarTest { private final Caesar caesar = new Caesar(); diff --git a/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java b/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java index 46c270a83d34..43a132de8efa 100644 --- a/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java @@ -1,10 +1,10 @@ package com.thealgorithms.ciphers; +import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; - public class MyPlayfairCipherTest { private PlayfairCipher playfair; private final String keyword = "KEYWORD"; diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java index 1bf90e6415be..022f3f84f99f 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java @@ -1,6 +1,9 @@ package com.thealgorithms.datastructures.trees; -import com.thealgorithms.utils.ConsoleInterceptor; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.thealgorithms.devutils.ConsoleInterceptor; +import java.util.stream.Stream; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; @@ -10,10 +13,6 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertEquals; - public class AVLSimpleTest { AVLSimple tree = new AVLSimple(); ConsoleInterceptor interceptor = new ConsoleInterceptor(); diff --git a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java index 88196c808595..651a3752fbe4 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java @@ -1,20 +1,19 @@ package com.thealgorithms.datastructures.trees; -import com.thealgorithms.utils.ConsoleInterceptor; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import java.util.InputMismatchException; - import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import com.thealgorithms.devutils.ConsoleInterceptor; +import java.util.InputMismatchException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + public class GenericTreeTest { GenericTree tree; ConsoleInterceptor interceptor = new ConsoleInterceptor(); diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java index e45457388a3d..3164489327ec 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java @@ -1,6 +1,13 @@ package com.thealgorithms.datastructures.trees; -import com.thealgorithms.utils.ConsoleInterceptor; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.thealgorithms.devutils.ConsoleInterceptor; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.stream.Stream; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; @@ -10,14 +17,6 @@ import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.MethodSource; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertAll; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; - public class LCATest { /** * This input creates the following tree: diff --git a/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java b/src/test/java/com/thealgorithms/devutils/ConsoleInterceptor.java similarity index 98% rename from src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java rename to src/test/java/com/thealgorithms/devutils/ConsoleInterceptor.java index fb598c17247d..94dc82f801c5 100644 --- a/src/test/java/com/thealgorithms/utils/ConsoleInterceptor.java +++ b/src/test/java/com/thealgorithms/devutils/ConsoleInterceptor.java @@ -1,4 +1,4 @@ -package com.thealgorithms.utils; +package com.thealgorithms.devutils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java index 731fa95b0ba7..9de788f8efa8 100644 --- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java +++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java @@ -1,13 +1,5 @@ package com.thealgorithms.maths; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import java.util.List; - import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -16,6 +8,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + class MathBuilderTest { @Test diff --git a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java index 7339d0b732f1..1aca8a45b0e0 100644 --- a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java +++ b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java @@ -1,13 +1,13 @@ package com.thealgorithms.maths; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; - import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + class RomanNumeralUtilTest { @ParameterizedTest() @CsvSource({ From fad091642b9d9e3d8379f3d45944608b4662fe8a Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Mon, 27 Oct 2025 19:47:46 +0200 Subject: [PATCH 41/56] Made style changes According to Clang's fugly ass suggestions. --- pom.xml | 58 ++++++++++ .../datastructures/trees/AVLSimple.java | 7 +- .../datastructures/trees/LCA.java | 6 +- .../datastructures/trees/AVLSimpleTest.java | 12 +- .../datastructures/trees/GenericTreeTest.java | 3 +- .../datastructures/trees/LCATest.java | 104 +----------------- .../RomanNumeralUtilConstructorTest.java | 5 +- .../maths/RomanNumeralUtilTest.java | 8 +- .../maths/SimpsonIntegrationTest.java | 7 +- 9 files changed, 75 insertions(+), 135 deletions(-) diff --git a/pom.xml b/pom.xml index 07ea84531152..4d419cdc0fb6 100644 --- a/pom.xml +++ b/pom.xml @@ -202,4 +202,62 @@ + + + + jolla-dev + + + + + maven-surefire-plugin + + + com/thealgorithms/datastructures/trees/**/*Test.java + + + + + + + org.pitest + pitest-maven + + com.thealgorithms.datastructures.trees.LCA + com.thealgorithms.datastructures.trees.LCATest + + + + pitest + verify + + mutationCoverage + + + + + + + + org.jacoco + jacoco-maven-plugin + + + + prepare-agent + + + + generate-code-coverage-report + verify + + report + + + + + + + + diff --git a/src/main/java/com/thealgorithms/datastructures/trees/AVLSimple.java b/src/main/java/com/thealgorithms/datastructures/trees/AVLSimple.java index e0309122cc12..57088d403a43 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/AVLSimple.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/AVLSimple.java @@ -28,7 +28,7 @@ public class AVLSimple { - private class Node { + private static class Node { int data; int height; @@ -82,6 +82,11 @@ private Node insert(Node node, int item) { } public void display() { + if (root == null) { + System.out.println("Tree is empty"); + return; + } + this.display(this.root); System.out.println(this.root.height); } diff --git a/src/main/java/com/thealgorithms/datastructures/trees/LCA.java b/src/main/java/com/thealgorithms/datastructures/trees/LCA.java index 95a289493007..e761b1624a14 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/LCA.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/LCA.java @@ -7,9 +7,9 @@ public final class LCA { private LCA() { } - private static final Scanner SCANNER = new Scanner(System.in); - public static void main(String[] args) { + Scanner SCANNER = new Scanner(System.in); + // The adjacency list representation of a tree: ArrayList> adj = new ArrayList<>(); @@ -18,7 +18,7 @@ public static void main(String[] args) { int e = v - 1; for (int i = 0; i < v; i++) { - adj.add(new ArrayList()); + adj.add(new ArrayList<>()); } // Storing the given tree as an adjacency list diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java index 022f3f84f99f..b87991cf4af5 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java @@ -6,7 +6,6 @@ import java.util.stream.Stream; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -83,10 +82,6 @@ void testTreeCreation() { assertEquals(expectedTree, getActualTree()); } - @Disabled( - "This test should pass for empty trees to protect against crashes " - + "when trying to access the display method before inserting anything" - ) @Test @DisplayName("A test where an empty tree should return the string \"Tree is empty\".") void testEmptyTree() { @@ -112,12 +107,7 @@ void testAllRotations(int node1, int node2, int node3) { } public static Stream getTreeNodesInput() { - return Stream.of( - Arguments.of(30, 20, 10), - Arguments.of(30, 10, 20), - Arguments.of(10, 20, 30), - Arguments.of(10, 30, 20) - ); + return Stream.of(Arguments.of(30, 20, 10), Arguments.of(30, 10, 20), Arguments.of(10, 20, 30), Arguments.of(10, 30, 20)); } @ParameterizedTest diff --git a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java index 651a3752fbe4..505ee05d4df7 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java @@ -71,8 +71,7 @@ String getConsoleOutput() { void testCreateValidTree() { tree.display(); - assertEquals( - getExpectedTree(), getConsoleOutput()); + assertEquals(getExpectedTree(), getConsoleOutput()); } @Test diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java index 3164489327ec..585ea2ec6548 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java @@ -1,20 +1,12 @@ package com.thealgorithms.datastructures.trees; -import static org.junit.jupiter.api.Assertions.assertAll; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import com.thealgorithms.devutils.ConsoleInterceptor; -import java.lang.reflect.Method; -import java.util.ArrayList; import java.util.stream.Stream; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.MethodSource; public class LCATest { @@ -32,39 +24,12 @@ public class LCATest { * 9 8 *

      */
-    private final int[] treeInput = new int[] {0, 1, 0, 2, 1, 5, 5, 6, 2, 4, 2, 3, 3, 7, 7, 9, 7, 8};
-    private final int arrayLength = 10;
-    private int[] parent = new int[arrayLength];
-    private int[] depth = new int[arrayLength];
-    private final int startingSourceVertex = 0;
-    private final int startParentOfSource = -1;
-    private final ArrayList> adj = new ArrayList<>();
-
-    /* ========================
-        Setup
-    ======================== */
-
-    @BeforeEach
-    void setup() {
-        for (int i = 0; i < arrayLength; i++) {
-            adj.add(new ArrayList<>());
-        }
-
-        for (int i = 0; i < treeInput.length - 1; i += 2) {
-            int to = treeInput[i];
-            int from = treeInput[i + 1];
-
-            adj.get(to).add(from);
-            adj.get(from).add(to);
-        }
-    }
+    private final static String TREE = "10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n";
 
     /* ========================
         Tests
     ======================== */
 
-    @Disabled("This would be the best way to test LCA but since the scanner is a\n"
-      + "static global variable it doesn't work, it should be moved into the main method.")
     @ParameterizedTest
     @MethodSource("getInput")
     @DisplayName("Should return correct common ancestor for any two nodes in the tree")
@@ -82,71 +47,6 @@ void shouldReturnCorrectLCAThroughMain(String simulatedInput, String expectedPar
     }
 
     public static Stream getInput() {
-        return Stream.of(
-          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n9\n4\n", "2"),
-          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n5\n6\n", "5"),
-          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n5\n4\n", "0"),
-          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n3\n8\n", "3"),
-          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n6\n3\n", "0"),
-          Arguments.of("10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n3\n3\n", "3")
-        );
-    }
-
-    @SuppressWarnings("RFI_SET_ACCESSIBLE")
-    @ParameterizedTest
-    @CsvSource({"9,4,2", "5,6,5", "5,4,0", "3,8,3", "6,3,0", "3,3,3"})
-    @DisplayName("Should return correct common ancestor for any two nodes in the tree")
-    void shouldReturnCorrectLCA(int v1, int v2, int expectedParent) throws Exception {
-            Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class);
-            Method getLCA = LCA.class.getDeclaredMethod("getLCA", int.class, int.class, int[].class, int[].class);
-            dfs.setAccessible(true);
-            getLCA.setAccessible(true);
-
-            dfs.invoke(null, adj, startingSourceVertex, startParentOfSource, parent, depth);
-
-            assertEquals(expectedParent, getLCA.invoke(null, v1, v2, depth, parent));
-    }
-
-    @SuppressWarnings("RFI_SET_ACCESSIBLE")
-    @Test
-    void shouldReturnCorrectDepthsForArray() throws Exception {
-        Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class);
-        dfs.setAccessible(true);
-
-        dfs.invoke(null, adj, startingSourceVertex, startParentOfSource, parent, depth);
-
-        assertArrayEquals(new int[] {0, 1, 1, 2, 2, 2, 3, 3, 4, 4}, depth);
-    }
-
-    @SuppressWarnings("RFI_SET_ACCESSIBLE")
-    @Test
-    void shouldReturnCorrectParentsForArray() throws Exception {
-        Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class);
-        dfs.setAccessible(true);
-
-        dfs.invoke(null, adj, startingSourceVertex, startParentOfSource, parent, depth);
-
-        assertArrayEquals(new int[] {0, 0, 0, 2, 2, 1, 5, 3, 7, 7}, parent);
-    }
-
-    @Test
-    void testMinimalTreeDepthAndParent() throws Exception {
-        int v = 1;
-        ArrayList> minimalAdj = new ArrayList<>();
-        for (int i = 0; i < v; i++) {
-            minimalAdj.add(new ArrayList<>());
-        }
-        parent = new int[v];
-        depth = new int[v];
-
-        Method dfs = LCA.class.getDeclaredMethod("dfs", ArrayList.class, int.class, int.class, int[].class, int[].class);
-        dfs.setAccessible(true);
-
-        dfs.invoke(null, minimalAdj, startingSourceVertex, startParentOfSource, parent, depth);
-
-        assertAll(
-          () -> assertArrayEquals(new int[] {0}, depth),
-          () -> assertArrayEquals(new int[] {0}, parent)
-        );
+        return Stream.of(Arguments.of(TREE + "9\n4\n", "2"), Arguments.of(TREE + "5\n6\n", "5"), Arguments.of(TREE + "5\n4\n", "0"), Arguments.of(TREE + "3\n8\n", "3"), Arguments.of(TREE + "6\n3\n", "0"), Arguments.of(TREE + "3\n3\n", "3"));
     }
 }
diff --git a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java
index 2b583d14093d..90faeb6b4449 100644
--- a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java
+++ b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java
@@ -1,9 +1,8 @@
 package com.thealgorithms.maths;
 
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 
 import java.lang.reflect.Constructor;
-
 import org.junit.jupiter.api.Test;
 
 // Covers the private constructor for code coverage tools
@@ -14,6 +13,6 @@ void shouldInvokePrivateConstructor() throws Exception {
         constructor.setAccessible(true);
         RomanNumeralUtil instance = constructor.newInstance();
 
-        assertTrue(instance instanceof RomanNumeralUtil);
+        assertInstanceOf(RomanNumeralUtil.class, instance);
     }
 }
diff --git a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java
index 1aca8a45b0e0..12ff448317ba 100644
--- a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java
+++ b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java
@@ -10,13 +10,7 @@
 
 class RomanNumeralUtilTest {
 		@ParameterizedTest()
-		@CsvSource({
-            "2000,MM",
-            "1,I",
-            "2,II",
-            "1003,MIII",
-            "1004,MIV"
-		})
+		@CsvSource({"2000,MM", "1,I", "2,II", "1003,MIII", "1004,MIV"})
 		void minimumMaximumTest(int testInt, String word) {
             System.out.println(word + ": " + testInt);
             assertEquals(RomanNumeralUtil.generate(testInt), word);
diff --git a/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java b/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java
index be27bc8a6c4b..28553d06a584 100644
--- a/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java
+++ b/src/test/java/com/thealgorithms/maths/SimpsonIntegrationTest.java
@@ -12,12 +12,7 @@ public class SimpsonIntegrationTest {
 
     @Test
     void shouldCalculateCorrectFunction() {
-        assertAll(
-            () -> assertEquals(-0.24893534183931973, simpson.f(3)),
-            () -> assertEquals(0.0, simpson.f(2)),
-            () -> assertEquals(4.0, simpson.f(0)),
-            () -> assertEquals(8.154845485377136, simpson.f(-1))
-        );
+        assertAll(() -> assertEquals(-0.24893534183931973, simpson.f(3)), () -> assertEquals(0.0, simpson.f(2)), () -> assertEquals(4.0, simpson.f(0)), () -> assertEquals(8.154845485377136, simpson.f(-1)));
     }
 
     @Test

From bdc9369ddd1c353e8a65efa95371c0f98bf2e247 Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Mon, 27 Oct 2025 21:49:46 +0200
Subject: [PATCH 42/56] Made style changes

According to Clang's fugly ass
suggestions.
---
 .../maths/RomanNumeralUtilTest.java           | 36 +++++++++----------
 1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java
index 12ff448317ba..fde79d8d73ac 100644
--- a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java
+++ b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilTest.java
@@ -9,25 +9,25 @@
 import org.junit.jupiter.params.provider.CsvSource;
 
 class RomanNumeralUtilTest {
-		@ParameterizedTest()
-		@CsvSource({"2000,MM", "1,I", "2,II", "1003,MIII", "1004,MIV"})
-		void minimumMaximumTest(int testInt, String word) {
-            System.out.println(word + ": " + testInt);
-            assertEquals(RomanNumeralUtil.generate(testInt), word);
-		}
+    @ParameterizedTest()
+    @CsvSource({"2000,MM", "1,I", "2,II", "1003,MIII", "1004,MIV"})
+    void minimumMaximumTest(int testInt, String word) {
+        System.out.println(word + ": " + testInt);
+        assertEquals(RomanNumeralUtil.generate(testInt), word);
+    }
 
-		@Test
-		void calcSomeNumerals() {
-            assertThrows(IllegalArgumentException.class, () -> RomanNumeralUtil.generate(0));
-            assertThrows(IllegalArgumentException.class, () -> RomanNumeralUtil.generate(6000));
-		}
+    @Test
+    void calcSomeNumerals() {
+        assertThrows(IllegalArgumentException.class, () -> RomanNumeralUtil.generate(0));
+        assertThrows(IllegalArgumentException.class, () -> RomanNumeralUtil.generate(6000));
+    }
 
-		@Test
-		void shouldNotThrowOnBounds() {
-			int min = 1;
-			int max = 5999;
+    @Test
+    void shouldNotThrowOnBounds() {
+        int min = 1;
+        int max = 5999;
 
-			assertDoesNotThrow(() -> RomanNumeralUtil.generate(min), "Should not throw exception on " + min);
-			assertDoesNotThrow(() -> RomanNumeralUtil.generate(max), "Should not throw exception on " + max);
-		}
+        assertDoesNotThrow(() -> RomanNumeralUtil.generate(min), "Should not throw exception on " + min);
+        assertDoesNotThrow(() -> RomanNumeralUtil.generate(max), "Should not throw exception on " + max);
+    }
 }

From 3230715d22a13142b0dc8519b52311f5c9367d4e Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Mon, 27 Oct 2025 21:51:24 +0200
Subject: [PATCH 43/56] Made style changes

According to Clang's fugly ass
suggestions.
---
 .../com/thealgorithms/datastructures/trees/AVLSimpleTest.java    | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index b87991cf4af5..1fd9f7954123 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -92,7 +92,6 @@ void testEmptyTree() {
         assertEquals("Tree is empty", getActualTree());
     }
 
-
     @ParameterizedTest
     @MethodSource("getTreeNodesInput")
     @DisplayName("Test to ensure all rotation paths are covered")

From e2cbf27583675e2c6550735fe4443d006364691f Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Mon, 27 Oct 2025 23:24:04 +0200
Subject: [PATCH 44/56] Made style changes

According to Clang's fugly ass
suggestions.
---
 pom.xml                                       | 64 ++-----------------
 .../datastructures/trees/LCA.java             | 12 ++--
 .../ciphers/MyPlayfairCipherTest.java         |  6 +-
 .../datastructures/trees/LCATest.java         |  2 +-
 .../RomanNumeralUtilConstructorTest.java      |  2 +
 5 files changed, 18 insertions(+), 68 deletions(-)

diff --git a/pom.xml b/pom.xml
index 4d419cdc0fb6..4114ba33974d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -55,6 +55,12 @@
             commons-collections4
             4.5.0
         
+        
+            com.github.spotbugs
+            spotbugs-annotations
+            4.9.6
+            test
+        
     
 
     
@@ -202,62 +208,4 @@
             
         
     
-
-    
-        
-            jolla-dev
-            
-                
-                    
-                    
-                        maven-surefire-plugin
-                        
-                            
-                                com/thealgorithms/datastructures/trees/**/*Test.java
-                            
-                        
-                    
-
-                    
-                    
-                        org.pitest
-                        pitest-maven
-                        
-                            com.thealgorithms.datastructures.trees.LCA
-                            com.thealgorithms.datastructures.trees.LCATest
-                        
-                        
-                            
-                                pitest
-                                verify 
-                                
-                                    mutationCoverage
-                                
-                            
-                        
-                    
-
-                    
-                    
-                        org.jacoco
-                        jacoco-maven-plugin
-                        
-                            
-                                
-                                    prepare-agent
-                                
-                            
-                            
-                                generate-code-coverage-report
-                                verify 
-                                
-                                    report
-                                
-                            
-                        
-                    
-                
-            
-        
-    
 
diff --git a/src/main/java/com/thealgorithms/datastructures/trees/LCA.java b/src/main/java/com/thealgorithms/datastructures/trees/LCA.java
index e761b1624a14..34e97cca576f 100644
--- a/src/main/java/com/thealgorithms/datastructures/trees/LCA.java
+++ b/src/main/java/com/thealgorithms/datastructures/trees/LCA.java
@@ -8,13 +8,13 @@ private LCA() {
     }
 
     public static void main(String[] args) {
-        Scanner SCANNER = new Scanner(System.in);
+        Scanner scanner = new Scanner(System.in);
 
         // The adjacency list representation of a tree:
         ArrayList> adj = new ArrayList<>();
 
         // v is the number of vertices and e is the number of edges
-        int v = SCANNER.nextInt();
+        int v = scanner.nextInt();
         int e = v - 1;
 
         for (int i = 0; i < v; i++) {
@@ -25,8 +25,8 @@ public static void main(String[] args) {
         int to;
         int from;
         for (int i = 0; i < e; i++) {
-            to = SCANNER.nextInt();
-            from = SCANNER.nextInt();
+            to = scanner.nextInt();
+            from = scanner.nextInt();
 
             adj.get(to).add(from);
             adj.get(from).add(to);
@@ -42,8 +42,8 @@ public static void main(String[] args) {
         dfs(adj, 0, -1, parent, depth);
 
         // Inputting the two vertices whose LCA is to be calculated
-        int v1 = SCANNER.nextInt();
-        int v2 = SCANNER.nextInt();
+        int v1 = scanner.nextInt();
+        int v2 = scanner.nextInt();
 
         // Outputting the LCA
         System.out.println(getLCA(v1, v2, depth, parent));
diff --git a/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java b/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java
index 43a132de8efa..2df9a302da86 100644
--- a/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java
+++ b/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java
@@ -7,16 +7,16 @@
 
 public class MyPlayfairCipherTest {
     private PlayfairCipher playfair;
-    private final String keyword = "KEYWORD";
+    private static final String KEYWORD = "KEYWORD";
 
     @BeforeEach
     public void setup() {
-        playfair = new PlayfairCipher(keyword);
+        playfair = new PlayfairCipher(KEYWORD);
     }
 
     @Test
     void shouldEncryptAndDecryptDuringSameRowDigraph() {
-        String plaintext = keyword.substring(0, 2);
+        String plaintext = KEYWORD.substring(0, 2);
 
         String encrypted = playfair.encrypt(plaintext);
         String decrypted = playfair.decrypt(encrypted);
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java
index 585ea2ec6548..7528a16c67ff 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java
@@ -24,7 +24,7 @@ public class LCATest {
      *        9   8
      * 
      */
-    private final static String TREE = "10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n";
+    private static final String TREE = "10\n0\n1\n0\n2\n1\n5\n5\n6\n2\n4\n2\n3\n3\n7\n7\n9\n7\n8\n";
 
     /* ========================
         Tests
diff --git a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java
index 90faeb6b4449..b80204335351 100644
--- a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java
+++ b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java
@@ -4,9 +4,11 @@
 
 import java.lang.reflect.Constructor;
 import org.junit.jupiter.api.Test;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
 // Covers the private constructor for code coverage tools
 public class RomanNumeralUtilConstructorTest {
+    @SuppressFBWarnings("RFI_SET_ACCESSIBLE")
     @Test
     void shouldInvokePrivateConstructor() throws Exception {
         Constructor constructor = RomanNumeralUtil.class.getDeclaredConstructor();

From 87f15fb04a592ab71fb183f2441115fe8c36d19b Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Mon, 27 Oct 2025 23:30:19 +0200
Subject: [PATCH 45/56] Made style changes

According to Clang's fugly ass
suggestions.
---
 .../thealgorithms/maths/RomanNumeralUtilConstructorTest.java    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java
index b80204335351..554795299234 100644
--- a/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java
+++ b/src/test/java/com/thealgorithms/maths/RomanNumeralUtilConstructorTest.java
@@ -2,9 +2,9 @@
 
 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.lang.reflect.Constructor;
 import org.junit.jupiter.api.Test;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
 // Covers the private constructor for code coverage tools
 public class RomanNumeralUtilConstructorTest {

From c24cc401e43ca20a967997a2c257d5fec2f56893 Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Tue, 28 Oct 2025 00:30:43 +0200
Subject: [PATCH 46/56] Made style changes

According to PMD...
---
 .../MyColumnarTranspositionCipherTest.java    | 33 +++++-----
 .../datastructures/trees/AVLSimpleTest.java   | 16 ++---
 .../datastructures/trees/GenericTreeTest.java | 33 +++++-----
 .../thealgorithms/maths/MathBuilderTest.java  | 63 +++++++------------
 4 files changed, 60 insertions(+), 85 deletions(-)

diff --git a/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java b/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java
index 0ae3304a2d69..c3115ae28df3 100644
--- a/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java
+++ b/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java
@@ -1,11 +1,6 @@
 package com.thealgorithms.ciphers;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertNotEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -23,15 +18,15 @@ public void setUp() {
     void shouldNotProduceNullOrEmptyEncryptedText() {
         String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
 
-        assertNotNull(encrypted, "Encrypted text should not be null");
-        assertFalse(encrypted.isEmpty(), "Encrypted text should not be empty");
+        Assertions.assertNotNull(encrypted, "Encrypted text should not be null");
+        Assertions.assertFalse(encrypted.isEmpty(), "Encrypted text should not be empty");
     }
 
     @Test
     void shouldChangePlaintextToEncrypted() {
         String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
 
-        assertNotEquals(plaintext, encrypted, "Encrypted text should differ from plaintext");
+        Assertions.assertNotEquals(plaintext, encrypted, "Encrypted text should differ from plaintext");
     }
 
     @Test
@@ -39,7 +34,7 @@ void shouldEncryptDetermenistically() {
         String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
         String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
 
-        assertEquals(encrypted1, encrypted2, "Encryptions should be equal");
+        Assertions.assertEquals(encrypted1, encrypted2, "Encryptions should be equal");
     }
 
     @Test
@@ -49,7 +44,7 @@ void shouldProduceDifferentEncryptionsWithDifferentKeywoards() {
         String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
         String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword2);
 
-        assertNotEquals(encrypted1, encrypted2, "Should produce different encryptions");
+        Assertions.assertNotEquals(encrypted1, encrypted2, "Should produce different encryptions");
     }
 
     @Test
@@ -60,7 +55,7 @@ void shouldMatchWithUnsortedKeyword() {
         String encrypted = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword);
         String expected = "8≈7≈2≈4≈5≈3≈6≈19";
 
-        assertEquals(expected, encrypted, "Should match");
+        Assertions.assertEquals(expected, encrypted, "Should match");
     }
 
     @Test
@@ -70,7 +65,7 @@ void shouldMatchEncryptionAndDecryptionWithNoSpacesOrPadding() {
         ColumnarTranspositionCipher.encrypt(myPlaintext, keyword);
         String decrypted = ColumnarTranspositionCipher.decrypt();
 
-        assertEquals(myPlaintext, decrypted, "Decrypted text should match original plaintext");
+        Assertions.assertEquals(myPlaintext, decrypted, "Decrypted text should match original plaintext");
     }
 
     @Test
@@ -80,7 +75,7 @@ void shouldNotContainPaddingInDecryption() {
         ColumnarTranspositionCipher.encrypt(myPlaintext, keyword);
         String decrypted = ColumnarTranspositionCipher.decrypt();
 
-        assertFalse(decrypted.contains("≈"), "Should not contain padding characters");
+        Assertions.assertFalse(decrypted.contains("≈"), "Should not contain padding characters");
     }
 
     @Test
@@ -90,7 +85,7 @@ void shouldEncryptWithKeywordLongerThanPlaintext() {
 
         String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword);
 
-        assertNotNull(encryption, "Should encrypt where plaintext.length() < keyword.length()");
+        Assertions.assertNotNull(encryption, "Should encrypt where plaintext.length() < keyword.length()");
     }
 
     @Test
@@ -100,7 +95,7 @@ void shouldEncryptWithKeywordWithSameLengthAsPlaintext() {
 
         String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword);
 
-        assertNotNull(encryption, "Should encrypt where plaintext.length() == keyword.length()");
+        Assertions.assertNotNull(encryption, "Should encrypt where plaintext.length() == keyword.length()");
     }
 
     @Test
@@ -111,7 +106,7 @@ void shouldProduceDifferentEncryptionForSameKeywordButSortedDifferently() {
         String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword1);
         String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword2);
 
-        assertNotEquals(encrypted1, encrypted2, "Should differ with different keywords");
+        Assertions.assertNotEquals(encrypted1, encrypted2, "Should differ with different keywords");
     }
 
     @Test
@@ -120,13 +115,13 @@ void shouldEncryptWithCustomAbecedarium() {
 
         String encryption = ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium);
 
-        assertNotNull(encryption, "Should encrypt with custom abecedarium");
+        Assertions.assertNotNull(encryption, "Should encrypt with custom abecedarium");
     }
 
     @Test
     void shouldNotEncryptWithInvalidAbecedarium() {
         String myAbecedarium = "abcde";
 
-        assertThrows(NullPointerException.class, () -> ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium), "Should throw error when keyword contains characters not present in abecedarium");
+        Assertions.assertThrows(NullPointerException.class, () -> ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium), "Should throw error when keyword contains characters not present in abecedarium");
     }
 }
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index 1fd9f7954123..8e3f5471f3a0 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -35,11 +35,11 @@ void tearDown() {
     ======================== */
 
     String getExpectedTree() {
-        return ("""
+        return """
           10=>20<=30
           END=>10<=END
           END=>30<=END
-          2""")
+          2"""
           .replace("\n", "");
     }
 
@@ -66,7 +66,7 @@ void testTreeCreation() {
 
         tree.display();
 
-        String expectedTree = ("""
+        String expectedTree = """
           15=>25<=30
           10=>15<=19
           5=>10<=END
@@ -76,7 +76,7 @@ void testTreeCreation() {
           END=>20<=END
           27=>30<=END
           END=>27<=END
-          4""")
+          4"""
           .replace("\n", "");
 
         assertEquals(expectedTree, getActualTree());
@@ -125,19 +125,19 @@ void testRotatesNotTriggeredWhenBFEqualsOne(int node, String expectedTree) {
 
     public static Stream getTreeNodesInputForBFEqualsOneRotations() {
         return Stream.of(
-          Arguments.of(5, ("""
+          Arguments.of(5, """
           10=>20<=30
           5=>10<=END
           END=>5<=END
           END=>30<=END
-          3""")
+          3"""
             .replace("\n", "")),
-          Arguments.of(35, ("""
+          Arguments.of(35, """
           10=>20<=30
           END=>10<=END
           END=>30<=35
           END=>35<=END
-          3""")
+          3"""
             .replace("\n", ""))
         );
     }
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java
index 505ee05d4df7..98464a1abb96 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java
@@ -1,14 +1,9 @@
 package com.thealgorithms.datastructures.trees;
 
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
 import com.thealgorithms.devutils.ConsoleInterceptor;
 import java.util.InputMismatchException;
 import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
@@ -71,14 +66,14 @@ String getConsoleOutput() {
     void testCreateValidTree() {
         tree.display();
 
-        assertEquals(getExpectedTree(), getConsoleOutput());
+        Assertions.assertEquals(getExpectedTree(), getConsoleOutput());
     }
 
     @Test
     void testCreateValidTreeIsNotEmpty() {
         tree.display();
 
-        assertDoesNotThrow(interceptor::getAndClearConsoleOutput);
+        Assertions.assertDoesNotThrow(interceptor::getAndClearConsoleOutput);
     }
 
     @Test
@@ -86,34 +81,34 @@ void testCreateInValidTree() {
         String invalidTreeValues = "a\nb\nc\n";
         interceptor.mockInput(invalidTreeValues);
 
-        assertThrows(InputMismatchException.class, GenericTree::new);
+        Assertions.assertThrows(InputMismatchException.class, GenericTree::new);
     }
 
     @Test
     void testGettingCorrectSizeOfTree() {
-        assertEquals(9, tree.size2call());
+        Assertions.assertEquals(9, tree.size2call());
     }
 
     @Test
     void testGettingTheMaximalValueOfTreesNodes() {
-        assertEquals(342, tree.maxcall());
+        Assertions.assertEquals(342, tree.maxcall());
     }
 
     @Test
     void testGettingTheHeightOfTree() {
-        assertEquals(2, tree.heightcall());
+        Assertions.assertEquals(2, tree.heightcall());
     }
 
     @ParameterizedTest
     @ValueSource(ints = {40, 34, 1, 32, 342, 123, 98, 35})
     void testFindingAllPresentValuesInTree(int value) {
-        assertTrue(tree.findcall(value));
+        Assertions.assertTrue(tree.findcall(value));
     }
 
     @ParameterizedTest
     @ValueSource(ints = {41, 31, 2, 52, 542, 223, 92, 38})
     void testFindingAbsentValuesInTree(int value) {
-        assertFalse(tree.findcall(value));
+        Assertions.assertFalse(tree.findcall(value));
     }
 
     @Test
@@ -124,25 +119,25 @@ void testFindingAllNodesOfCertainDepthInTree() {
             tree.depthcaller(i);
         }
 
-        assertEquals("4034132342121239835", getConsoleOutput());
+        Assertions.assertEquals("4034132342121239835", getConsoleOutput());
     }
 
     @Test
     void testPreOrderPrintsAsExpected() {
         tree.preordercall();
-        assertEquals("40 34 1 32 123 342 98 35 12 .", getConsoleOutput());
+        Assertions.assertEquals("40 34 1 32 123 342 98 35 12 .", getConsoleOutput());
     }
 
     @Test
     void testPostOrderPrintsAsExpected() {
         tree.postordercall();
-        assertEquals("34 1 123 32 98 35 342 12 40 .", getConsoleOutput());
+        Assertions.assertEquals("34 1 123 32 98 35 342 12 40 .", getConsoleOutput());
     }
 
     @Test
     void testLevelOrderPrintsAsExpected() {
         tree.levelorder();
-        assertEquals("40 34 1 32 342 12 123 98 35 .", getConsoleOutput());
+        Assertions.assertEquals("40 34 1 32 342 12 123 98 35 .", getConsoleOutput());
     }
 
     @Test
@@ -150,6 +145,6 @@ void testLeavesAreRemoved() {
         tree.removeleavescall();
         tree.display();
 
-        assertEquals("40=>32 342 .32=>.342=>.", getConsoleOutput());
+        Assertions.assertEquals("40=>32 342 .32=>.342=>.", getConsoleOutput());
     }
 }
diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
index 9de788f8efa8..a7c4dc4afb9b 100644
--- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
+++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
@@ -1,14 +1,7 @@
 package com.thealgorithms.maths;
 
-import static org.junit.jupiter.api.Assertions.assertAll;
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
 import java.util.List;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
@@ -20,19 +13,19 @@ class MathBuilderTest {
     @Test
     void simpleMath() {
         double result = new MathBuilder.Builder(100).add(200).multiply(10).print().divideIf(20, (a, b) -> a % 2 == 0).sqrt().print().ceil().build().get();
-        assertEquals(13, result);
+        Assertions.assertEquals(13, result);
     }
 
     @Test
     void memoryTest() {
         long result = new MathBuilder.Builder().set(100).sqrt().remember().add(21).recallIf(a -> a < 50, true).mod(2).build().toLong();
-        assertEquals(0, result);
+        Assertions.assertEquals(0, result);
     }
 
     @Test
     void freeFallDistance() {
         long distance = new MathBuilder.Builder(9.81).multiply(0.5).multiply(5 * 5).round().build().toLong();
-        assertEquals(123, distance); // Expected result: 0.5 * 9.81 * 25 = 122.625 ≈ 123
+        Assertions.assertEquals(123, distance); // Expected result: 0.5 * 9.81 * 25 = 122.625 ≈ 123
     }
 
     @Test
@@ -43,21 +36,21 @@ void batchSalaryProcessing() {
             processedSalaries[i] = new MathBuilder.Builder(salaries[i]).addIf(salaries[i] * 0.1, (sal, bonus) -> sal > 2500).multiply(0.92).round().build().toLong();
         }
         long[] expectedSalaries = {1840, 3036, 4048, 5060};
-        assertArrayEquals(expectedSalaries, processedSalaries);
+        Assertions.assertArrayEquals(expectedSalaries, processedSalaries);
     }
 
     @Test
     void parenthesis() {
         // 10 + (20*5) - 40 + (100 / 10) = 80
         double result = new MathBuilder.Builder(10).openParenthesis(20).multiply(5).closeParenthesisAndPlus().minus(40).openParenthesis(100).divide(10).closeParenthesisAndPlus().build().get();
-        assertEquals(80, result);
+        Assertions.assertEquals(80, result);
     }
 
     @Test
     void areaOfCircle() {
         // Radius is 4
         double area = new MathBuilder.Builder().pi().openParenthesis(4).multiply(4).closeParenthesisAndMultiply().build().get();
-        assertEquals(Math.PI * 4 * 4, area);
+        Assertions.assertEquals(Math.PI * 4 * 4, area);
     }
 
     @Test
@@ -71,8 +64,8 @@ void floorTest() {
         double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).floor().closeParenthesisAndPlus().build().get();
         double expected2 = 10.5 + Math.floor(20 + 2.1);
 
-        assertEquals(expected, actual);
-        assertEquals(expected2, actual2);
+        Assertions.assertEquals(expected, actual);
+        Assertions.assertEquals(expected2, actual2);
     }
 
     @Test
@@ -86,8 +79,8 @@ void closeParenthesisTest() {
         double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.1).closeParenthesisAndDivide().build().get();
         double expected2 = 10.5 / (20 + 2.1);
 
-        assertEquals(expected, actual);
-        assertEquals(expected2, actual2);
+        Assertions.assertEquals(expected, actual);
+        Assertions.assertEquals(expected2, actual2);
     }
 
     @Test
@@ -101,17 +94,15 @@ void openParenthesisAndABSTest() {
         double actual2 = new MathBuilder.Builder(10.5).openParenthesis(20).add(2.2).abs().closeParenthesisAndPlus().abs().build().get();
         double expected2 = 10.5 + (20 + 2.2);
 
-        assertEquals(expected, actual);
-        assertEquals(expected2, actual2);
+        Assertions.assertEquals(expected, actual);
+        Assertions.assertEquals(expected2, actual2);
     }
     @Test
     @DisplayName("Runtime Errors Tests")
     void runtimeErrorTest() {
         MathBuilder.Builder actual = new MathBuilder.Builder(10.5);
 
-        assertAll(()
-                      -> assertThrows(RuntimeException.class, () -> actual.rand(1)),
-            () -> assertThrows(RuntimeException.class, () -> actual.randomInRange(1, 10)), () -> assertThrows(RuntimeException.class, actual::pi), () -> assertThrows(RuntimeException.class, actual::e), () -> assertThrows(RuntimeException.class, () -> actual.set(1)));
+        Assertions.assertAll(() -> Assertions.assertThrows(RuntimeException.class, () -> actual.rand(1)), () -> Assertions.assertThrows(RuntimeException.class, () -> actual.randomInRange(1, 10)), () -> Assertions.assertThrows(RuntimeException.class, actual::pi), () -> Assertions.assertThrows(RuntimeException.class, actual::e), () -> Assertions.assertThrows(RuntimeException.class, () -> actual.set(1)));
     }
 
     @Test
@@ -122,17 +113,15 @@ void divideByNum() {
         double expected = 10.0 / 2.0;
         double expected2 = 10.0 / 4.0;
 
-        assertEquals(expected, actual);
-        assertNotEquals(expected2, actual);
+        Assertions.assertEquals(expected, actual);
+        Assertions.assertNotEquals(expected2, actual);
     }
 
     @ParameterizedTest
     @MethodSource("divideDoubleByZeroHelper")
     @DisplayName("Test that ensures dividing a double by zero follows IEEE 754")
     void divideDoubleByZero(double expected, MathBuilder.Builder actual, String error) {
-        assertAll(()
-                      -> assertDoesNotThrow(() -> actual.build().get(), "Dividing a double with zero should not throw"),
-            () -> assertDoesNotThrow(() -> actual.divide(0).build().get(), "Dividing infinity with 0 should not throw"), () -> assertTrue(Double.isInfinite(actual.build().get()), "Dividing a double by zero should result in infinity"), () -> assertEquals(expected, actual.build().get(), error));
+        Assertions.assertAll(() -> Assertions.assertDoesNotThrow(() -> actual.build().get(), "Dividing a double with zero should not throw"), () -> Assertions.assertDoesNotThrow(() -> actual.divide(0).build().get(), "Dividing infinity with 0 should not throw"), () -> Assertions.assertTrue(Double.isInfinite(actual.build().get()), "Dividing a double by zero should result in infinity"), () -> Assertions.assertEquals(expected, actual.build().get(), error));
     }
 
     static List divideDoubleByZeroHelper() {
@@ -147,15 +136,13 @@ void randomFunctionsTest() {
         double maxValue = 2.1;
         double actual = new MathBuilder.Builder().rand(2L).build().get();
         double actual2 = new MathBuilder.Builder().randomInRange(minValue, maxValue).build().get();
-        assertAll(() -> assertTrue(actual < maxValue), () -> assertTrue(actual2 >= minValue), () -> assertTrue(actual2 <= maxValue)
-
-        );
+        Assertions.assertAll(() -> Assertions.assertTrue(actual < maxValue), () -> Assertions.assertTrue(actual2 >= minValue), () -> Assertions.assertTrue(actual2 <= maxValue));
     }
 
     @ParameterizedTest
     @MethodSource("radiansHelper")
     void toRadiansTests(double expectedAngle, double actualAngle) {
-        assertEquals(expectedAngle, actualAngle);
+        Assertions.assertEquals(expectedAngle, actualAngle);
     }
 
     private static List radiansHelper() {
@@ -165,7 +152,7 @@ private static List radiansHelper() {
     void roundCielABSTest() {
 
         double actual = new MathBuilder.Builder(10).openParenthesis(10.5).round().closeParenthesisAndPlus().build().get();
-        double expected = 10 + (Math.round(10.5));
+        double expected = 10 + Math.round(10.5);
 
         double expected2 = 10 + Math.ceil(10.5);
         double actual2 = new MathBuilder.Builder(10).openParenthesis(10.5).ceil().closeParenthesisAndPlus().build().get();
@@ -176,7 +163,7 @@ void roundCielABSTest() {
         double expected4 = Math.abs(10 + 10.5);
         double actual4 = new MathBuilder.Builder(10).openParenthesis(10.5).closeParenthesisAndPlus().abs().build().get();
 
-        assertAll(() -> assertNotEquals(0, actual), () -> assertNotEquals(1, actual2), () -> assertEquals(expected, actual), () -> assertEquals(expected2, actual2), () -> assertEquals(expected3, actual3), () -> assertEquals(expected4, actual4));
+        Assertions.assertAll(() -> Assertions.assertNotEquals(0, actual), () -> Assertions.assertNotEquals(1, actual2), () -> Assertions.assertEquals(expected, actual), () -> Assertions.assertEquals(expected2, actual2), () -> Assertions.assertEquals(expected3, actual3), () -> Assertions.assertEquals(expected4, actual4));
     }
 
     @Test
@@ -188,9 +175,7 @@ void toLongTest() {
 
         MathBuilder actual3 = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(3).build();
         MathBuilder actual4 = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(-3).build();
-        assertAll(()
-                      -> assertEquals(Long.MAX_VALUE, actual.toLong()),
-            () -> assertEquals(Long.MIN_VALUE, actual2.toLong()), () -> assertEquals(Long.MAX_VALUE, actual3.toLong()), () -> assertEquals(Long.MIN_VALUE, actual4.toLong()), () -> assertNotEquals(0, actual.toLong()), () -> assertNotEquals(1, actual2.toLong()));
+        Assertions.assertAll(() -> Assertions.assertEquals(Long.MAX_VALUE, actual.toLong()), () -> Assertions.assertEquals(Long.MIN_VALUE, actual2.toLong()), () -> Assertions.assertEquals(Long.MAX_VALUE, actual3.toLong()), () -> Assertions.assertEquals(Long.MIN_VALUE, actual4.toLong()), () -> Assertions.assertNotEquals(0, actual.toLong()), () -> Assertions.assertNotEquals(1, actual2.toLong()));
     }
 
     @Test
@@ -200,7 +185,7 @@ void maxTest() {
 
         MathBuilder actual3 = new MathBuilder.Builder(10.5).openParenthesis(10).max(20).closeParenthesisAndPlus().build();
         MathBuilder actual4 = new MathBuilder.Builder(12.5).openParenthesis(10).closeParenthesisAndPlus().max(20).build();
-        assertAll(() -> assertEquals(20, actual.get()), () -> assertEquals(13.5, actual2.get()), () -> assertEquals(30.5, actual3.get()), () -> assertEquals(22.5, actual4.get()), () -> assertNotEquals(30, actual4.get()), () -> assertNotEquals(5, actual4.get()));
+        Assertions.assertAll(() -> Assertions.assertEquals(20, actual.get()), () -> Assertions.assertEquals(13.5, actual2.get()), () -> Assertions.assertEquals(30.5, actual3.get()), () -> Assertions.assertEquals(22.5, actual4.get()), () -> Assertions.assertNotEquals(30, actual4.get()), () -> Assertions.assertNotEquals(5, actual4.get()));
     }
     @Test
     void minTest() {
@@ -209,6 +194,6 @@ void minTest() {
 
         MathBuilder actual3 = new MathBuilder.Builder(10.5).openParenthesis(10).min(20).closeParenthesisAndPlus().build();
         MathBuilder actual4 = new MathBuilder.Builder(12.5).openParenthesis(10).closeParenthesisAndPlus().min(20).build();
-        assertAll(() -> assertEquals(10.5, actual.get()), () -> assertEquals(8.5, actual2.get()), () -> assertEquals(20.5, actual3.get()), () -> assertEquals(20, actual4.get()), () -> assertNotEquals(5, actual.get()), () -> assertNotEquals(-1000, actual3.get()));
+        Assertions.assertAll(() -> Assertions.assertEquals(10.5, actual.get()), () -> Assertions.assertEquals(8.5, actual2.get()), () -> Assertions.assertEquals(20.5, actual3.get()), () -> Assertions.assertEquals(20, actual4.get()), () -> Assertions.assertNotEquals(5, actual.get()), () -> Assertions.assertNotEquals(-1000, actual3.get()));
     }
 }

From 67743850c2446b8b7adc21b226919bac28845d2f Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Tue, 28 Oct 2025 01:02:39 +0200
Subject: [PATCH 47/56] Made style changes

According to Clang again...
---
 .../datastructures/trees/AVLSimpleTest.java   | 12 +--
 .../thealgorithms/maths/MathBuilderTest.java  | 87 +++++++++++++++----
 2 files changed, 72 insertions(+), 27 deletions(-)

diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index 8e3f5471f3a0..16cf8ac9cd17 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -39,8 +39,7 @@ String getExpectedTree() {
           10=>20<=30
           END=>10<=END
           END=>30<=END
-          2"""
-          .replace("\n", "");
+          2""".replace("\n", "");
     }
 
     String getActualTree() {
@@ -76,8 +75,7 @@ void testTreeCreation() {
           END=>20<=END
           27=>30<=END
           END=>27<=END
-          4"""
-          .replace("\n", "");
+          4""".replace("\n", "");
 
         assertEquals(expectedTree, getActualTree());
     }
@@ -130,15 +128,13 @@ public static Stream getTreeNodesInputForBFEqualsOneRotations() {
           5=>10<=END
           END=>5<=END
           END=>30<=END
-          3"""
-            .replace("\n", "")),
+          3""".replace("\n", "")),
           Arguments.of(35, """
           10=>20<=30
           END=>10<=END
           END=>30<=35
           END=>35<=END
-          3"""
-            .replace("\n", ""))
+          3""".replace("\n", ""))
         );
     }
 
diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
index a7c4dc4afb9b..b35ef7616b5f 100644
--- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
+++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
@@ -4,6 +4,7 @@
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.function.Executable;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.MethodSource;
@@ -97,14 +98,22 @@ void openParenthesisAndABSTest() {
         Assertions.assertEquals(expected, actual);
         Assertions.assertEquals(expected2, actual2);
     }
+
     @Test
     @DisplayName("Runtime Errors Tests")
     void runtimeErrorTest() {
         MathBuilder.Builder actual = new MathBuilder.Builder(10.5);
 
-        Assertions.assertAll(() -> Assertions.assertThrows(RuntimeException.class, () -> actual.rand(1)), () -> Assertions.assertThrows(RuntimeException.class, () -> actual.randomInRange(1, 10)), () -> Assertions.assertThrows(RuntimeException.class, actual::pi), () -> Assertions.assertThrows(RuntimeException.class, actual::e), () -> Assertions.assertThrows(RuntimeException.class, () -> actual.set(1)));
+        Executable randCheck = () -> Assertions.assertThrows(RuntimeException.class, () -> actual.rand(1));
+        Executable rangeCheck = () -> Assertions.assertThrows(RuntimeException.class, () -> actual.randomInRange(1, 10));
+        Executable piCheck = () -> Assertions.assertThrows(RuntimeException.class, actual::pi);
+        Executable eCheck = () -> Assertions.assertThrows(RuntimeException.class, actual::e);
+        Executable setCheck = () -> Assertions.assertThrows(RuntimeException.class, () -> actual.set(1));
+
+        Assertions.assertAll(randCheck, rangeCheck, piCheck, eCheck, setCheck);
     }
 
+
     @Test
     @DisplayName("Should divide 10 by 2")
     void divideByNum() {
@@ -121,9 +130,15 @@ void divideByNum() {
     @MethodSource("divideDoubleByZeroHelper")
     @DisplayName("Test that ensures dividing a double by zero follows IEEE 754")
     void divideDoubleByZero(double expected, MathBuilder.Builder actual, String error) {
-        Assertions.assertAll(() -> Assertions.assertDoesNotThrow(() -> actual.build().get(), "Dividing a double with zero should not throw"), () -> Assertions.assertDoesNotThrow(() -> actual.divide(0).build().get(), "Dividing infinity with 0 should not throw"), () -> Assertions.assertTrue(Double.isInfinite(actual.build().get()), "Dividing a double by zero should result in infinity"), () -> Assertions.assertEquals(expected, actual.build().get(), error));
+        Executable noThrowOnBuild = () -> Assertions.assertDoesNotThrow(() -> actual.build().get(), "Dividing a double with zero should not throw");
+        Executable noThrowOnDivideZero = () -> Assertions.assertDoesNotThrow(() -> actual.divide(0).build().get(), "Dividing infinity with 0 should not throw");
+        Executable resultIsInfinite = () -> Assertions.assertTrue(Double.isInfinite(actual.build().get()), "Dividing a double by zero should result in infinity");
+        Executable equalsExpected = () -> Assertions.assertEquals(expected, actual.build().get(), error);
+
+        Assertions.assertAll(noThrowOnBuild, noThrowOnDivideZero, resultIsInfinite, equalsExpected);
     }
 
+
     static List divideDoubleByZeroHelper() {
         return List.of(Arguments.of(Double.POSITIVE_INFINITY, new MathBuilder.Builder(10.5).openParenthesis(0).closeParenthesisAndDivide(), "10.5 / 0 should be +Infinity"),
             Arguments.of(Double.NEGATIVE_INFINITY, new MathBuilder.Builder(-10.5).openParenthesis(0).closeParenthesisAndDivide(), "-10.5 / 0 should be -Infinity"));
@@ -131,14 +146,20 @@ static List divideDoubleByZeroHelper() {
 
     @Test
     void randomFunctionsTest() {
-
         double minValue = 0.0;
         double maxValue = 2.1;
+
         double actual = new MathBuilder.Builder().rand(2L).build().get();
         double actual2 = new MathBuilder.Builder().randomInRange(minValue, maxValue).build().get();
-        Assertions.assertAll(() -> Assertions.assertTrue(actual < maxValue), () -> Assertions.assertTrue(actual2 >= minValue), () -> Assertions.assertTrue(actual2 <= maxValue));
+
+        Executable isBelowMax = () -> Assertions.assertTrue(actual < maxValue, "Random value should be less than maxValue");
+        Executable isAboveMin = () -> Assertions.assertTrue(actual2 >= minValue, "RandomInRange value should be >= minValue");
+        Executable isWithinRange = () -> Assertions.assertTrue(actual2 <= maxValue, "RandomInRange value should be <= maxValue");
+
+        Assertions.assertAll(isBelowMax, isAboveMin, isWithinRange);
     }
 
+
     @ParameterizedTest
     @MethodSource("radiansHelper")
     void toRadiansTests(double expectedAngle, double actualAngle) {
@@ -148,52 +169,80 @@ void toRadiansTests(double expectedAngle, double actualAngle) {
     private static List radiansHelper() {
         return List.of(Arguments.of(Math.toRadians(10), new MathBuilder.Builder(10).toRadians().build().get()), Arguments.of(2 + Math.toRadians(10), new MathBuilder.Builder(2).openParenthesis(10).toRadians().closeParenthesisAndPlus().build().get()));
     }
+
     @Test
     void roundCielABSTest() {
-
         double actual = new MathBuilder.Builder(10).openParenthesis(10.5).round().closeParenthesisAndPlus().build().get();
         double expected = 10 + Math.round(10.5);
 
-        double expected2 = 10 + Math.ceil(10.5);
         double actual2 = new MathBuilder.Builder(10).openParenthesis(10.5).ceil().closeParenthesisAndPlus().build().get();
+        double expected2 = 10 + Math.ceil(10.5);
 
-        double expected3 = 10 + Math.abs(10.5);
         double actual3 = new MathBuilder.Builder(10).openParenthesis(10.5).abs().closeParenthesisAndPlus().build().get();
+        double expected3 = 10 + Math.abs(10.5);
 
-        double expected4 = Math.abs(10 + 10.5);
         double actual4 = new MathBuilder.Builder(10).openParenthesis(10.5).closeParenthesisAndPlus().abs().build().get();
+        double expected4 = Math.abs(10 + 10.5);
 
-        Assertions.assertAll(() -> Assertions.assertNotEquals(0, actual), () -> Assertions.assertNotEquals(1, actual2), () -> Assertions.assertEquals(expected, actual), () -> Assertions.assertEquals(expected2, actual2), () -> Assertions.assertEquals(expected3, actual3), () -> Assertions.assertEquals(expected4, actual4));
+        Executable roundIsNotZero = () -> Assertions.assertNotEquals(0, actual);
+        Executable ceilIsNotOne = () -> Assertions.assertNotEquals(1, actual2);
+        Executable roundIsCorrect = () -> Assertions.assertEquals(expected, actual);
+        Executable ceilIsCorrect = () -> Assertions.assertEquals(expected2, actual2);
+        Executable absIsCorrect = () -> Assertions.assertEquals(expected3, actual3);
+        Executable absAfterPlusIsCorrect = () -> Assertions.assertEquals(expected4, actual4);
+
+        Assertions.assertAll(roundIsNotZero, ceilIsNotOne, roundIsCorrect, ceilIsCorrect, absIsCorrect, absAfterPlusIsCorrect);
     }
 
     @Test
     void toLongTest() {
+        MathBuilder posOverflow = new MathBuilder.Builder(10.5).openParenthesis(0).closeParenthesisAndDivide().divide(0).build();
+        MathBuilder negOverflow = new MathBuilder.Builder(10.5).openParenthesis(0).closeParenthesisAndDivide().divide(0).multiply(-1).build();
+        MathBuilder maxRange = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(3).build();
+        MathBuilder minRange = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(-3).build();
 
-        MathBuilder actual = new MathBuilder.Builder(10.5).openParenthesis(0).closeParenthesisAndDivide().divide(0).build();
-
-        MathBuilder actual2 = new MathBuilder.Builder(10.5).openParenthesis(0).closeParenthesisAndDivide().divide(0).multiply(-1).build();
+        Executable posMaxCheck = () -> Assertions.assertEquals(Long.MAX_VALUE, posOverflow.toLong());
+        Executable negMinCheck = () -> Assertions.assertEquals(Long.MIN_VALUE, negOverflow.toLong());
+        Executable maxRangeCheck = () -> Assertions.assertEquals(Long.MAX_VALUE, maxRange.toLong());
+        Executable minRangeCheck = () -> Assertions.assertEquals(Long.MIN_VALUE, minRange.toLong());
+        Executable notZeroCheck = () -> Assertions.assertNotEquals(0, posOverflow.toLong());
+        Executable notOneCheck = () -> Assertions.assertNotEquals(1, negOverflow.toLong());
 
-        MathBuilder actual3 = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(3).build();
-        MathBuilder actual4 = new MathBuilder.Builder(1999999999).multiply(2139999999).multiply(-3).build();
-        Assertions.assertAll(() -> Assertions.assertEquals(Long.MAX_VALUE, actual.toLong()), () -> Assertions.assertEquals(Long.MIN_VALUE, actual2.toLong()), () -> Assertions.assertEquals(Long.MAX_VALUE, actual3.toLong()), () -> Assertions.assertEquals(Long.MIN_VALUE, actual4.toLong()), () -> Assertions.assertNotEquals(0, actual.toLong()), () -> Assertions.assertNotEquals(1, actual2.toLong()));
+        Assertions.assertAll(posMaxCheck, negMinCheck, maxRangeCheck, minRangeCheck, notZeroCheck, notOneCheck);
     }
 
     @Test
     void maxTest() {
         MathBuilder actual = new MathBuilder.Builder(10.5).max(20).build();
         MathBuilder actual2 = new MathBuilder.Builder(13.5).max(10).build();
-
         MathBuilder actual3 = new MathBuilder.Builder(10.5).openParenthesis(10).max(20).closeParenthesisAndPlus().build();
         MathBuilder actual4 = new MathBuilder.Builder(12.5).openParenthesis(10).closeParenthesisAndPlus().max(20).build();
-        Assertions.assertAll(() -> Assertions.assertEquals(20, actual.get()), () -> Assertions.assertEquals(13.5, actual2.get()), () -> Assertions.assertEquals(30.5, actual3.get()), () -> Assertions.assertEquals(22.5, actual4.get()), () -> Assertions.assertNotEquals(30, actual4.get()), () -> Assertions.assertNotEquals(5, actual4.get()));
+
+        Executable maxCheck1 = () -> Assertions.assertEquals(20, actual.get());
+        Executable maxCheck2 = () -> Assertions.assertEquals(13.5, actual2.get());
+        Executable maxCheck3 = () -> Assertions.assertEquals(30.5, actual3.get());
+        Executable maxCheck4 = () -> Assertions.assertEquals(22.5, actual4.get());
+        Executable notEqualsCheck1 = () -> Assertions.assertNotEquals(30, actual4.get());
+        Executable notEqualsCheck2 = () -> Assertions.assertNotEquals(5, actual4.get());
+
+        Assertions.assertAll(maxCheck1, maxCheck2, maxCheck3, maxCheck4, notEqualsCheck1, notEqualsCheck2);
     }
+
     @Test
     void minTest() {
         MathBuilder actual = new MathBuilder.Builder(10.5).min(20).build();
         MathBuilder actual2 = new MathBuilder.Builder(8.5).min(10).build();
-
         MathBuilder actual3 = new MathBuilder.Builder(10.5).openParenthesis(10).min(20).closeParenthesisAndPlus().build();
         MathBuilder actual4 = new MathBuilder.Builder(12.5).openParenthesis(10).closeParenthesisAndPlus().min(20).build();
-        Assertions.assertAll(() -> Assertions.assertEquals(10.5, actual.get()), () -> Assertions.assertEquals(8.5, actual2.get()), () -> Assertions.assertEquals(20.5, actual3.get()), () -> Assertions.assertEquals(20, actual4.get()), () -> Assertions.assertNotEquals(5, actual.get()), () -> Assertions.assertNotEquals(-1000, actual3.get()));
+
+        Executable minCheck1 = () -> Assertions.assertEquals(10.5, actual.get());
+        Executable minCheck2 = () -> Assertions.assertEquals(8.5, actual2.get());
+        Executable minCheck3 = () -> Assertions.assertEquals(20.5, actual3.get());
+        Executable minCheck4 = () -> Assertions.assertEquals(20, actual4.get());
+        Executable notEqualsCheck1 = () -> Assertions.assertNotEquals(5, actual.get());
+        Executable notEqualsCheck2 = () -> Assertions.assertNotEquals(-1000, actual3.get());
+
+        Assertions.assertAll(minCheck1, minCheck2, minCheck3, minCheck4, notEqualsCheck1, notEqualsCheck2);
     }
+
 }

From eaf81c7699df5c79411f184bbab5ec516b28bee5 Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Tue, 28 Oct 2025 01:06:33 +0200
Subject: [PATCH 48/56] Made style changes

According to Clang again...
---
 .../thealgorithms/datastructures/trees/AVLSimpleTest.java   | 6 ++++--
 src/test/java/com/thealgorithms/maths/MathBuilderTest.java  | 4 ----
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index 16cf8ac9cd17..44e1fdb2c528 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -35,11 +35,13 @@ void tearDown() {
     ======================== */
 
     String getExpectedTree() {
-        return """
+        String tree = """
           10=>20<=30
           END=>10<=END
           END=>30<=END
-          2""".replace("\n", "");
+          2""";
+
+        return tree.replace("\n", "");
     }
 
     String getActualTree() {
diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
index b35ef7616b5f..6e1a0ebe4bb4 100644
--- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
+++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
@@ -113,7 +113,6 @@ void runtimeErrorTest() {
         Assertions.assertAll(randCheck, rangeCheck, piCheck, eCheck, setCheck);
     }
 
-
     @Test
     @DisplayName("Should divide 10 by 2")
     void divideByNum() {
@@ -138,7 +137,6 @@ void divideDoubleByZero(double expected, MathBuilder.Builder actual, String erro
         Assertions.assertAll(noThrowOnBuild, noThrowOnDivideZero, resultIsInfinite, equalsExpected);
     }
 
-
     static List divideDoubleByZeroHelper() {
         return List.of(Arguments.of(Double.POSITIVE_INFINITY, new MathBuilder.Builder(10.5).openParenthesis(0).closeParenthesisAndDivide(), "10.5 / 0 should be +Infinity"),
             Arguments.of(Double.NEGATIVE_INFINITY, new MathBuilder.Builder(-10.5).openParenthesis(0).closeParenthesisAndDivide(), "-10.5 / 0 should be -Infinity"));
@@ -159,7 +157,6 @@ void randomFunctionsTest() {
         Assertions.assertAll(isBelowMax, isAboveMin, isWithinRange);
     }
 
-
     @ParameterizedTest
     @MethodSource("radiansHelper")
     void toRadiansTests(double expectedAngle, double actualAngle) {
@@ -244,5 +241,4 @@ void minTest() {
 
         Assertions.assertAll(minCheck1, minCheck2, minCheck3, minCheck4, notEqualsCheck1, notEqualsCheck2);
     }
-
 }

From f61786be2856ce4f37b67d233db906578c660345 Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Tue, 28 Oct 2025 01:15:25 +0200
Subject: [PATCH 49/56] Made style changes

According to Clang again...
---
 .../datastructures/trees/AVLSimpleTest.java   | 34 +++----------------
 1 file changed, 4 insertions(+), 30 deletions(-)

diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index 44e1fdb2c528..032e0682f4ed 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -35,13 +35,7 @@ void tearDown() {
     ======================== */
 
     String getExpectedTree() {
-        String tree = """
-          10=>20<=30
-          END=>10<=END
-          END=>30<=END
-          2""";
-
-        return tree.replace("\n", "");
+        return "10=>20<=30END=>10<=ENDEND=>30<=END2";
     }
 
     String getActualTree() {
@@ -67,17 +61,7 @@ void testTreeCreation() {
 
         tree.display();
 
-        String expectedTree = """
-          15=>25<=30
-          10=>15<=19
-          5=>10<=END
-          END=>5<=END
-          16=>19<=20
-          END=>16<=END
-          END=>20<=END
-          27=>30<=END
-          END=>27<=END
-          4""".replace("\n", "");
+        String expectedTree = "15=>25<=3010=>15<=195=>10<=ENDEND=>5<=END16=>19<=20END=>16<=ENDEND=>20<=END27=>30<=ENDEND=>27<=END4";
 
         assertEquals(expectedTree, getActualTree());
     }
@@ -125,18 +109,8 @@ void testRotatesNotTriggeredWhenBFEqualsOne(int node, String expectedTree) {
 
     public static Stream getTreeNodesInputForBFEqualsOneRotations() {
         return Stream.of(
-          Arguments.of(5, """
-          10=>20<=30
-          5=>10<=END
-          END=>5<=END
-          END=>30<=END
-          3""".replace("\n", "")),
-          Arguments.of(35, """
-          10=>20<=30
-          END=>10<=END
-          END=>30<=35
-          END=>35<=END
-          3""".replace("\n", ""))
+          Arguments.of(5, "10=>20<=305=>10<=ENDEND=>5<=ENDEND=>30<=END3"),
+          Arguments.of(35, "10=>20<=30END=>10<=ENDEND=>30<=35END=>35<=END3")
         );
     }
 

From 96b539df8708da7005e78356972ffb5d2ce501f0 Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Tue, 28 Oct 2025 01:16:26 +0200
Subject: [PATCH 50/56] Made style changes

According to Clang again...
---
 .../thealgorithms/datastructures/trees/AVLSimpleTest.java    | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index 032e0682f4ed..279107c28484 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -108,10 +108,7 @@ void testRotatesNotTriggeredWhenBFEqualsOne(int node, String expectedTree) {
     }
 
     public static Stream getTreeNodesInputForBFEqualsOneRotations() {
-        return Stream.of(
-          Arguments.of(5, "10=>20<=305=>10<=ENDEND=>5<=ENDEND=>30<=END3"),
-          Arguments.of(35, "10=>20<=30END=>10<=ENDEND=>30<=35END=>35<=END3")
-        );
+        return Stream.of(Arguments.of(5, "10=>20<=305=>10<=ENDEND=>5<=ENDEND=>30<=END3"), Arguments.of(35, "10=>20<=30END=>10<=ENDEND=>30<=35END=>35<=END3"));
     }
 
     @Test

From 177f115e62c8d8328d76f355f74146588f2db6d2 Mon Sep 17 00:00:00 2001
From: Joel Lansgren 
Date: Tue, 28 Oct 2025 22:22:33 +0200
Subject: [PATCH 51/56] Made style changes

According to Clang again...
---
 .../datastructures/trees/AVLSimpleTest.java   | 64 +++++++++++++------
 .../datastructures/trees/GenericTreeTest.java | 52 +++++++++++----
 .../datastructures/trees/LCATest.java         |  6 +-
 .../devutils/ConsoleInterceptor.java          |  2 +-
 4 files changed, 85 insertions(+), 39 deletions(-)

diff --git a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
index 279107c28484..f8acfdadf8d8 100644
--- a/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java
@@ -1,5 +1,7 @@
 package com.thealgorithms.datastructures.trees;
 
+import static org.junit.jupiter.api.Assertions.assertAll;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import com.thealgorithms.devutils.ConsoleInterceptor;
@@ -20,11 +22,13 @@ public class AVLSimpleTest {
         Setup/TearDown
     ======================== */
 
+    /** Starts capturing System.in. */
     @BeforeEach
     void setup() {
         interceptor.captureOutput();
     }
 
+    /** Cleans up after each test by closing the interceptor. */
     @AfterEach
     void tearDown() {
         interceptor.close();
@@ -34,10 +38,29 @@ void tearDown() {
         Helper methods
     ======================== */
 
+    /**
+     * Returns a string representation of the expected tree after inserting
+     * the values 10, 20, and 30 in any order.
+     * 

+ * The returned string follows the same format as the tree's display method, + * where each node is represented in the form "left=>value<=right" and "END" + * denotes an empty (null) child. + * @return a string representing the expected tree structure + */ String getExpectedTree() { return "10=>20<=30END=>10<=ENDEND=>30<=END2"; } + /** + * Returns the actual string representation of the tree as printed + * by the display method. + * + *

This method captures the console output via the interceptor and + * removes all newline characters, returning a single-line string + * suitable for comparison with the expected tree string. + * + * @return the actual tree structure as a single-line string + */ String getActualTree() { return interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", ""); } @@ -47,7 +70,7 @@ String getActualTree() { ======================== */ @Test - @DisplayName("A longer generic AVL tree creation that should pass") + @DisplayName("Creates a longer simple AVL tree that matches the expected layout") void testTreeCreation() { tree.insert(25); tree.insert(30); @@ -77,52 +100,51 @@ void testEmptyTree() { } @ParameterizedTest - @MethodSource("getTreeNodesInput") + @MethodSource("getTreeNodesValues") @DisplayName("Test to ensure all rotation paths are covered") - void testAllRotations(int node1, int node2, int node3) { - tree.insert(node1); - tree.insert(node2); - tree.insert(node3); + void testAllRotations(int node1, int node2, int node3, String errorMessage) { + assertAll(() -> assertDoesNotThrow(() -> tree.insert(node1), "inserting: " + node1), () -> assertDoesNotThrow(() -> tree.insert(node2), "inserting: " + node2), () -> assertDoesNotThrow(() -> tree.insert(node3), "inserting: " + node3)); tree.display(); - assertEquals(getExpectedTree(), getActualTree()); + assertEquals(getExpectedTree(), getActualTree(), errorMessage); } - public static Stream getTreeNodesInput() { - return Stream.of(Arguments.of(30, 20, 10), Arguments.of(30, 10, 20), Arguments.of(10, 20, 30), Arguments.of(10, 30, 20)); + public static Stream getTreeNodesValues() { + return Stream.of(Arguments.of(30, 20, 10, "LL rotation failed"), Arguments.of(30, 10, 20, "LR rotation failed"), Arguments.of(10, 20, 30, "RR rotation failed"), Arguments.of(10, 30, 20, "RL rotation failed")); } @ParameterizedTest @MethodSource("getTreeNodesInputForBFEqualsOneRotations") - @DisplayName("Rotation not triggered when balance factor equals threshold") - void testRotatesNotTriggeredWhenBFEqualsOne(int node, String expectedTree) { - tree.insert(30); + @DisplayName("Checks rotation isn't triggered when balance factor equals threshold") + void testRotationsNotTriggeredWhenBFEqualsOne(int boundaryNode, String expectedTree, String errorMessage) { tree.insert(20); + tree.insert(30); tree.insert(10); - tree.insert(node); + tree.insert(boundaryNode); tree.display(); - assertEquals(expectedTree, getActualTree()); + assertEquals(expectedTree, getActualTree(), errorMessage); } public static Stream getTreeNodesInputForBFEqualsOneRotations() { - return Stream.of(Arguments.of(5, "10=>20<=305=>10<=ENDEND=>5<=ENDEND=>30<=END3"), Arguments.of(35, "10=>20<=30END=>10<=ENDEND=>30<=35END=>35<=END3")); + return Stream.of(Arguments.of(5, "10=>20<=305=>10<=ENDEND=>5<=ENDEND=>30<=END3", "Insertion of 5 should not trigger rotation"), Arguments.of(15, "10=>20<=30END=>10<=15END=>15<=ENDEND=>30<=END3", "Insertion of 15 should not trigger rotation"), + Arguments.of(25, "10=>20<=30END=>10<=END25=>30<=ENDEND=>25<=END3", "Insertion of 25 should not trigger rotation"), Arguments.of(35, "10=>20<=30END=>10<=ENDEND=>30<=35END=>35<=END3", "Insertion of 35 should not trigger rotation")); } @Test - @DisplayName("Should return true for a tree that don't account for duplicates") - void testDuplicatesInTreeCreationDoNotStick() { + @DisplayName("Ignores duplicate insertions and create a tree with depth one") + void testDuplicateInsertionsIgnored() { int duplicate = 20; - tree.insert(30); tree.insert(duplicate); - tree.insert(20); tree.insert(duplicate); - tree.insert(10); + tree.insert(duplicate); tree.display(); - assertEquals(getExpectedTree(), getActualTree()); + String actualTree = getActualTree(); + + assertEquals('1', actualTree.charAt(actualTree.length() - 1)); } } diff --git a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java index 98464a1abb96..7f7423a408e1 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java @@ -5,6 +5,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -17,17 +18,25 @@ public class GenericTreeTest { Setup/TearDown ======================== */ + /** + * Sets up the test environment before each test: + *

    + *
  1. Makes the interceptor provide a predefined sequence of values for the tree.
  2. + *
  3. Initializes a new GenericTree instance.
  4. + *
  5. Starts capturing console output via the interceptor.
  6. + *
+ */ @BeforeEach void setup() { String treeValues = "40\n5\n34\n0\n1\n0\n32\n1\n123\n0\n342\n2\n98\n0\n35\n0\n12\n0"; interceptor.mockInput(treeValues); - interceptor.captureOutput(); tree = new GenericTree(); - interceptor.clearConsoleOutput(); + interceptor.captureOutput(); } + /** Cleans up after each test by closing the interceptor. */ @AfterEach void tearDown() { interceptor.close(); @@ -38,7 +47,8 @@ void tearDown() { ======================== */ /** - * Creates a generic tree that looks something like this: + * Returns the string representation of the generic tree that's created in the {@link #setup()} method. + * It looks something like this: * *
      *                    40
@@ -54,6 +64,15 @@ String getExpectedTree() {
         return "40=>34 1 32 342 12 .34=>.1=>.32=>123 .123=>.342=>98 35 .98=>.35=>.12=>.";
     }
 
+    /**
+     * Returns the actual string representation of the tree as printed
+     * by the display method.
+     * 

+ * This method captures the console output via the interceptor and + * removes all newline characters, returning a single-line string + * suitable for comparison with the expected tree string. + * @return the actual tree structure as a single-line string + */ String getConsoleOutput() { return interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", ""); } @@ -63,20 +82,15 @@ String getConsoleOutput() { ======================== */ @Test - void testCreateValidTree() { + @DisplayName("Asserts the created tree have the expected structure") + void testValidTree() { tree.display(); Assertions.assertEquals(getExpectedTree(), getConsoleOutput()); } @Test - void testCreateValidTreeIsNotEmpty() { - tree.display(); - - Assertions.assertDoesNotThrow(interceptor::getAndClearConsoleOutput); - } - - @Test + @DisplayName("Creating GenericTree with invalid input throws InputMismatchException") void testCreateInValidTree() { String invalidTreeValues = "a\nb\nc\n"; interceptor.mockInput(invalidTreeValues); @@ -85,33 +99,39 @@ void testCreateInValidTree() { } @Test + @DisplayName("Gets the correct number of nodes in the tree") void testGettingCorrectSizeOfTree() { - Assertions.assertEquals(9, tree.size2call()); + Assertions.assertEquals(3, tree.size2call()); } @Test + @DisplayName("Gets the highest value among the trees nodes") void testGettingTheMaximalValueOfTreesNodes() { Assertions.assertEquals(342, tree.maxcall()); } @Test + @DisplayName("Returns the correct height of the tree") void testGettingTheHeightOfTree() { Assertions.assertEquals(2, tree.heightcall()); } @ParameterizedTest @ValueSource(ints = {40, 34, 1, 32, 342, 123, 98, 35}) + @DisplayName("Returns true when searching for values that is in the tree") void testFindingAllPresentValuesInTree(int value) { - Assertions.assertTrue(tree.findcall(value)); + Assertions.assertTrue(tree.findcall(value), "The expected value " + value + " wasn't in the tree"); } @ParameterizedTest @ValueSource(ints = {41, 31, 2, 52, 542, 223, 92, 38}) + @DisplayName("Returns false when searching for values that isn't in the tree") void testFindingAbsentValuesInTree(int value) { - Assertions.assertFalse(tree.findcall(value)); + Assertions.assertFalse(tree.findcall(value), "The value " + value + " was unexpectedly in the tree"); } @Test + @DisplayName("Outputs all nodes at each tree level from left to right") void testFindingAllNodesOfCertainDepthInTree() { int height = tree.heightcall(); @@ -123,24 +143,28 @@ void testFindingAllNodesOfCertainDepthInTree() { } @Test + @DisplayName("Prints all node values in pre order") void testPreOrderPrintsAsExpected() { tree.preordercall(); Assertions.assertEquals("40 34 1 32 123 342 98 35 12 .", getConsoleOutput()); } @Test + @DisplayName("Prints all node values in post order") void testPostOrderPrintsAsExpected() { tree.postordercall(); Assertions.assertEquals("34 1 123 32 98 35 342 12 40 .", getConsoleOutput()); } @Test + @DisplayName("Prints all node values in level order") void testLevelOrderPrintsAsExpected() { tree.levelorder(); Assertions.assertEquals("40 34 1 32 342 12 123 98 35 .", getConsoleOutput()); } @Test + @DisplayName("Removes all leaves from the tree") void testLeavesAreRemoved() { tree.removeleavescall(); tree.display(); diff --git a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java index 7528a16c67ff..7b42d3c61fb2 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/LCATest.java @@ -31,7 +31,7 @@ public class LCATest { ======================== */ @ParameterizedTest - @MethodSource("getInput") + @MethodSource("getSimulatedInputAndExpectedParent") @DisplayName("Should return correct common ancestor for any two nodes in the tree") void shouldReturnCorrectLCAThroughMain(String simulatedInput, String expectedParent) { try (ConsoleInterceptor interceptor = new ConsoleInterceptor()) { @@ -42,11 +42,11 @@ void shouldReturnCorrectLCAThroughMain(String simulatedInput, String expectedPar String actualParent = interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", ""); - assertEquals(expectedParent, actualParent); + assertEquals(expectedParent, actualParent, "The two nodes Lowest Common Ancestor wasn't the expected one."); } } - public static Stream getInput() { + public static Stream getSimulatedInputAndExpectedParent() { return Stream.of(Arguments.of(TREE + "9\n4\n", "2"), Arguments.of(TREE + "5\n6\n", "5"), Arguments.of(TREE + "5\n4\n", "0"), Arguments.of(TREE + "3\n8\n", "3"), Arguments.of(TREE + "6\n3\n", "0"), Arguments.of(TREE + "3\n3\n", "3")); } } diff --git a/src/test/java/com/thealgorithms/devutils/ConsoleInterceptor.java b/src/test/java/com/thealgorithms/devutils/ConsoleInterceptor.java index 94dc82f801c5..ee72c23cdd6a 100644 --- a/src/test/java/com/thealgorithms/devutils/ConsoleInterceptor.java +++ b/src/test/java/com/thealgorithms/devutils/ConsoleInterceptor.java @@ -51,7 +51,7 @@ public void mockInput(String mockedInput) { /** * Start capturing System.out by replacing stdout with a custom PrintStream. - * All printed data will be stored in outContent for later retrieval. + * All printed data will be stored in outContent - available via {@link #getAndClearConsoleOutput}, for later retrieval. */ public void captureOutput() { if (!isCapturing) { From 79771a39dc342d2ea327f8e1921c47db5113d8d4 Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Tue, 28 Oct 2025 22:26:15 +0200 Subject: [PATCH 52/56] Added test documentation Added error messages when needed and display names to all methods. --- .../com/thealgorithms/datastructures/trees/GenericTreeTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java index 7f7423a408e1..44653d43936e 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java @@ -101,7 +101,7 @@ void testCreateInValidTree() { @Test @DisplayName("Gets the correct number of nodes in the tree") void testGettingCorrectSizeOfTree() { - Assertions.assertEquals(3, tree.size2call()); + Assertions.assertEquals(9, tree.size2call()); } @Test From 1f9d15aca914d62c82b758e415b42e04fbd42465 Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Wed, 29 Oct 2025 16:35:32 +0200 Subject: [PATCH 53/56] Added pom dev profile --- pom.xml | 114 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 47 deletions(-) diff --git a/pom.xml b/pom.xml index 4114ba33974d..285e3ead6212 100644 --- a/pom.xml +++ b/pom.xml @@ -159,53 +159,73 @@ pmd-exclude.properties - - - org.pitest - pitest-maven - 1.20.2 - - - org.pitest - pitest-junit5-plugin - 1.2.3 - - - - - com.thealgorithms.* - - - com.thealgorithms.*Test - - - com.thealgorithms.*Test - - - DEFAULTS - - 4 - - HTML - XML - - true - 4000 - - -Xmx1024m - - true - - - - pitest - test - - mutationCoverage - - - - + + + + dev + + + datastructures + trees + AVLSimple + + + + + + maven-surefire-plugin + + + com/thealgorithms/${modulename}/${packagename}/${classname}Test.java + + + + + + org.pitest + pitest-maven + 1.21.0 + + + org.pitest + pitest-junit5-plugin + 1.2.3 + + + + + com.thealgorithms.${modulename}.${packagename}.${classname}* + + com.thealgorithms.${modulename}.${packagename}.${classname}Test + + DEFAULTS + + 2 + + HTML + XML + + 4000 + + -Xmx2048m + + + true + + + + run-pitest + test + + mutationCoverage + + + + + + + + From fd3e28d7e8b0363c72a3bb335dd8dbdc20984511 Mon Sep 17 00:00:00 2001 From: labe2301 Date: Thu, 30 Oct 2025 00:00:09 +0100 Subject: [PATCH 54/56] refactor: Merge files testing the same packages --- .../ciphers/{MyAESTest.java => AESTest.java} | 2 +- .../com/thealgorithms/ciphers/CaesarTest.java | 74 ++++++++++ .../ColumnarTranspositionCipherTest.java | 112 +++++++++++++++ .../thealgorithms/ciphers/MyCaesarTest.java | 83 ------------ .../MyColumnarTranspositionCipherTest.java | 127 ------------------ .../ciphers/MyPlayfairCipherTest.java | 35 ----- .../thealgorithms/ciphers/PlayfairTest.java | 41 ++++-- 7 files changed, 218 insertions(+), 256 deletions(-) rename src/test/java/com/thealgorithms/ciphers/{MyAESTest.java => AESTest.java} (99%) delete mode 100644 src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java delete mode 100644 src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java delete mode 100644 src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java diff --git a/src/test/java/com/thealgorithms/ciphers/MyAESTest.java b/src/test/java/com/thealgorithms/ciphers/AESTest.java similarity index 99% rename from src/test/java/com/thealgorithms/ciphers/MyAESTest.java rename to src/test/java/com/thealgorithms/ciphers/AESTest.java index c58604139b12..026ddef6d2b3 100644 --- a/src/test/java/com/thealgorithms/ciphers/MyAESTest.java +++ b/src/test/java/com/thealgorithms/ciphers/AESTest.java @@ -7,7 +7,7 @@ import java.math.BigInteger; import org.junit.jupiter.api.Test; -public class MyAESTest { +public class AESTest { @Test void testScheduleCore() { BigInteger input = new BigInteger("1a2b3c4d", 16); diff --git a/src/test/java/com/thealgorithms/ciphers/CaesarTest.java b/src/test/java/com/thealgorithms/ciphers/CaesarTest.java index 7aa41c4cf423..d71d35dbdfc5 100644 --- a/src/test/java/com/thealgorithms/ciphers/CaesarTest.java +++ b/src/test/java/com/thealgorithms/ciphers/CaesarTest.java @@ -43,4 +43,78 @@ void caesarBruteForce() { assertEquals(27, allPossibleAnswers.length); assertEquals("Encrypt this text", allPossibleAnswers[5]); } + + @Test + void shouldReturnSameAsInputWhenShiftingZeroOr26() { + String message = "message"; + + String encoded1 = caesar.encode(message, 0); + String encoded2 = caesar.encode(message, 26); + + assertEquals(message, encoded1, "Encoded should be same as original"); + assertEquals(message, encoded2, "Encoded should be same as original"); + } + + @Test + void shouldReturnsameAsInputWhenUsingCharactersOutsideLatinAlphabet() { + String message = "!#¤%&/()=?`^½§@£$€{[]}´`¨~'*-.,_:;<>|"; + + String encoded = caesar.encode(message, 10); + + assertEquals(message, encoded); + } + + @Test + void shouldWrapZToAWhenEncodingSingleShift() { + String message = "zZ"; + + String encoded = caesar.encode(message, 1); + String exptected = "aA"; + + assertEquals(exptected, encoded, "zZ should wrap to aA"); + } + + @Test + void shouldWrapAToZWhenDecodingSingleShift() { + String message = "aA"; + + String decoded = caesar.decode(message, 1); + String expected = "zZ"; + + assertEquals(expected, decoded); + } + + @Test + void shouldNotWrapWhenEncodingFromYToZ() { + String message = "yY"; + + String encoded = caesar.encode(message, 1); + String expected = "zZ"; + + assertEquals(expected, encoded); + } + + @Test + void shouldNotWrapWhenDecodingFromBToA() { + String message = "bB"; + + String decoded = caesar.decode(message, 1); + String expected = "aA"; + + assertEquals(expected, decoded); + } + + @Test + void shouldContain27CombinationsFromBruteForce() { + String message = "message"; + + String encoded = caesar.encode(message, 10); + String[] combinations = caesar.bruteforce(encoded); + String expected = "wocckqo"; + + assertEquals(27, combinations.length, "Should contain 27 possible decoded combinations"); + assertEquals(expected, combinations[0], "First combination should contain encoded message"); + assertEquals(message, combinations[10], "10:th entry should contain original message"); + assertEquals(expected, combinations[26], "26:th entry should be the same as the 0:th entry, the encoded message"); + } } diff --git a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java index 8deae45099d6..d3feac261bd8 100644 --- a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java +++ b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -44,4 +45,115 @@ public void testLongPlainText() { assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces."); assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(longText, keyword), "The encrypted text should be the same when encrypted again."); } + + @Test + void shouldNotProduceNullOrEmptyEncryptedText() { + String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword); + + Assertions.assertNotNull(encrypted, "Encrypted text should not be null"); + Assertions.assertFalse(encrypted.isEmpty(), "Encrypted text should not be empty"); + } + + @Test + void shouldChangePlaintextToEncrypted() { + String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword); + + Assertions.assertNotEquals(plaintext, encrypted, "Encrypted text should differ from plaintext"); + } + + @Test + void shouldEncryptDetermenistically() { + String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); + String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); + + Assertions.assertEquals(encrypted1, encrypted2, "Encryptions should be equal"); + } + + @Test + void shouldProduceDifferentEncryptionsWithDifferentKeywoards() { + String keyword2 = keyword + "a"; + + String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); + String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword2); + + Assertions.assertNotEquals(encrypted1, encrypted2, "Should produce different encryptions"); + } + + @Test + void shouldMatchWithUnsortedKeyword() { + String myPlaintext = "123456789"; + String myKeyword = "unsorted"; + + String encrypted = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); + String expected = "8≈7≈2≈4≈5≈3≈6≈19"; + + Assertions.assertEquals(expected, encrypted, "Should match"); + } + + @Test + void shouldMatchEncryptionAndDecryptionWithNoSpacesOrPadding() { + String myPlaintext = "NoSpacesOrPadding"; + + ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); + String decrypted = ColumnarTranspositionCipher.decrypt(); + + Assertions.assertEquals(myPlaintext, decrypted, "Decrypted text should match original plaintext"); + } + + @Test + void shouldNotContainPaddingInDecryption() { + String myPlaintext = "text"; + + ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); + String decrypted = ColumnarTranspositionCipher.decrypt(); + + Assertions.assertFalse(decrypted.contains("≈"), "Should not contain padding characters"); + } + + @Test + void shouldEncryptWithKeywordLongerThanPlaintext() { + String myPlaintext = "text"; + String myKeyword = "averylongkeyword"; + + String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); + + Assertions.assertNotNull(encryption, "Should encrypt where plaintext.length() < keyword.length()"); + } + + @Test + void shouldEncryptWithKeywordWithSameLengthAsPlaintext() { + String myPlaintext = "textaslongaskeyword"; + String myKeyword = "keywordaslongastext"; + + String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); + + Assertions.assertNotNull(encryption, "Should encrypt where plaintext.length() == keyword.length()"); + } + + @Test + void shouldProduceDifferentEncryptionForSameKeywordButSortedDifferently() { + String unsertedKeyword1 = "EFGHABCD"; + String unsertedKeyword2 = "AEBFCGDH"; + + String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword1); + String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword2); + + Assertions.assertNotEquals(encrypted1, encrypted2, "Should differ with different keywords"); + } + + @Test + void shouldEncryptWithCustomAbecedarium() { + String myAbecedarium = "abcdefghijklmnopqrstuvwxyz"; + + String encryption = ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium); + + Assertions.assertNotNull(encryption, "Should encrypt with custom abecedarium"); + } + + @Test + void shouldNotEncryptWithInvalidAbecedarium() { + String myAbecedarium = "abcde"; + + Assertions.assertThrows(NullPointerException.class, () -> ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium), "Should throw error when keyword contains characters not present in abecedarium"); + } } diff --git a/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java b/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java deleted file mode 100644 index d41cb33eecde..000000000000 --- a/src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.thealgorithms.ciphers; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -public class MyCaesarTest { - private final Caesar caesar = new Caesar(); - - @Test - void shouldReturnSameAsInputWhenShiftingZeroOr26() { - String message = "message"; - - String encoded1 = caesar.encode(message, 0); - String encoded2 = caesar.encode(message, 26); - - assertEquals(message, encoded1, "Encoded should be same as original"); - assertEquals(message, encoded2, "Encoded should be same as original"); - } - - @Test - void shouldReturnsameAsInputWhenUsingCharactersOutsideLatinAlphabet() { - String message = "!#¤%&/()=?`^½§@£$€{[]}´`¨~'*-.,_:;<>|"; - - String encoded = caesar.encode(message, 10); - - assertEquals(message, encoded); - } - - @Test - void shouldWrapZToAWhenEncodingSingleShift() { - String message = "zZ"; - - String encoded = caesar.encode(message, 1); - String exptected = "aA"; - - assertEquals(exptected, encoded, "zZ should wrap to aA"); - } - - @Test - void shouldWrapAToZWhenDecodingSingleShift() { - String message = "aA"; - - String decoded = caesar.decode(message, 1); - String expected = "zZ"; - - assertEquals(expected, decoded); - } - - @Test - void shouldNotWrapWhenEncodingFromYToZ() { - String message = "yY"; - - String encoded = caesar.encode(message, 1); - String expected = "zZ"; - - assertEquals(expected, encoded); - } - - @Test - void shouldNotWrapWhenDecodingFromBToA() { - String message = "bB"; - - String decoded = caesar.decode(message, 1); - String expected = "aA"; - - assertEquals(expected, decoded); - } - - @Test - void shouldContain27CombinationsFromBruteForce() { - String message = "message"; - - String encoded = caesar.encode(message, 10); - String[] combinations = caesar.bruteforce(encoded); - String expected = "wocckqo"; - - assertEquals(27, combinations.length, "Should contain 27 possible decoded combinations"); - assertEquals(expected, combinations[0], "First combination should contain encoded message"); - assertEquals(message, combinations[10], "10:th entry should contain original message"); - assertEquals(expected, combinations[26], "26:th entry should be the same as the 0:th entry, the encoded message"); - } -} diff --git a/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java b/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java deleted file mode 100644 index c3115ae28df3..000000000000 --- a/src/test/java/com/thealgorithms/ciphers/MyColumnarTranspositionCipherTest.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.thealgorithms.ciphers; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class MyColumnarTranspositionCipherTest { - private String keyword; - private String plaintext; - - @BeforeEach - public void setUp() { - keyword = "keyword"; - plaintext = "This is a test message for Columnar Transposition Cipher"; - } - - @Test - void shouldNotProduceNullOrEmptyEncryptedText() { - String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword); - - Assertions.assertNotNull(encrypted, "Encrypted text should not be null"); - Assertions.assertFalse(encrypted.isEmpty(), "Encrypted text should not be empty"); - } - - @Test - void shouldChangePlaintextToEncrypted() { - String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword); - - Assertions.assertNotEquals(plaintext, encrypted, "Encrypted text should differ from plaintext"); - } - - @Test - void shouldEncryptDetermenistically() { - String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); - String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); - - Assertions.assertEquals(encrypted1, encrypted2, "Encryptions should be equal"); - } - - @Test - void shouldProduceDifferentEncryptionsWithDifferentKeywoards() { - String keyword2 = keyword + "a"; - - String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword); - String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword2); - - Assertions.assertNotEquals(encrypted1, encrypted2, "Should produce different encryptions"); - } - - @Test - void shouldMatchWithUnsortedKeyword() { - String myPlaintext = "123456789"; - String myKeyword = "unsorted"; - - String encrypted = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); - String expected = "8≈7≈2≈4≈5≈3≈6≈19"; - - Assertions.assertEquals(expected, encrypted, "Should match"); - } - - @Test - void shouldMatchEncryptionAndDecryptionWithNoSpacesOrPadding() { - String myPlaintext = "NoSpacesOrPadding"; - - ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); - String decrypted = ColumnarTranspositionCipher.decrypt(); - - Assertions.assertEquals(myPlaintext, decrypted, "Decrypted text should match original plaintext"); - } - - @Test - void shouldNotContainPaddingInDecryption() { - String myPlaintext = "text"; - - ColumnarTranspositionCipher.encrypt(myPlaintext, keyword); - String decrypted = ColumnarTranspositionCipher.decrypt(); - - Assertions.assertFalse(decrypted.contains("≈"), "Should not contain padding characters"); - } - - @Test - void shouldEncryptWithKeywordLongerThanPlaintext() { - String myPlaintext = "text"; - String myKeyword = "averylongkeyword"; - - String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); - - Assertions.assertNotNull(encryption, "Should encrypt where plaintext.length() < keyword.length()"); - } - - @Test - void shouldEncryptWithKeywordWithSameLengthAsPlaintext() { - String myPlaintext = "textaslongaskeyword"; - String myKeyword = "keywordaslongastext"; - - String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword); - - Assertions.assertNotNull(encryption, "Should encrypt where plaintext.length() == keyword.length()"); - } - - @Test - void shouldProduceDifferentEncryptionForSameKeywordButSortedDifferently() { - String unsertedKeyword1 = "EFGHABCD"; - String unsertedKeyword2 = "AEBFCGDH"; - - String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword1); - String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword2); - - Assertions.assertNotEquals(encrypted1, encrypted2, "Should differ with different keywords"); - } - - @Test - void shouldEncryptWithCustomAbecedarium() { - String myAbecedarium = "abcdefghijklmnopqrstuvwxyz"; - - String encryption = ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium); - - Assertions.assertNotNull(encryption, "Should encrypt with custom abecedarium"); - } - - @Test - void shouldNotEncryptWithInvalidAbecedarium() { - String myAbecedarium = "abcde"; - - Assertions.assertThrows(NullPointerException.class, () -> ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium), "Should throw error when keyword contains characters not present in abecedarium"); - } -} diff --git a/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java b/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java deleted file mode 100644 index 2df9a302da86..000000000000 --- a/src/test/java/com/thealgorithms/ciphers/MyPlayfairCipherTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.thealgorithms.ciphers; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class MyPlayfairCipherTest { - private PlayfairCipher playfair; - private static final String KEYWORD = "KEYWORD"; - - @BeforeEach - public void setup() { - playfair = new PlayfairCipher(KEYWORD); - } - - @Test - void shouldEncryptAndDecryptDuringSameRowDigraph() { - String plaintext = KEYWORD.substring(0, 2); - - String encrypted = playfair.encrypt(plaintext); - String decrypted = playfair.decrypt(encrypted); - - assertEquals(plaintext, decrypted, "Should not decrypt to the same letters"); - } - - @Test - void shouldPadOddLengthPlaintext() { - String plaintext = "cat"; - - String encrypted = playfair.encrypt(plaintext); - - assertEquals(0, encrypted.length() % 2, "Should be even length"); - } -} diff --git a/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java b/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java index fa497e4682e8..8bd0480ff5e3 100644 --- a/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java +++ b/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java @@ -2,35 +2,56 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class PlayfairTest { + private PlayfairCipher playfair; + private static final String KEYWORD = "KEYWORD"; + + @BeforeEach + public void setup() { + playfair = new PlayfairCipher(KEYWORD); + } @Test - public void testEncryption() { - PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD"); + void shouldEncryptAndDecryptDuringSameRowDigraph() { + String plaintext = KEYWORD.substring(0, 2); + + String encrypted = playfair.encrypt(plaintext); + String decrypted = playfair.decrypt(encrypted); + + assertEquals(plaintext, decrypted, "Should not decrypt to the same letters"); + } + + @Test + void shouldPadOddLengthPlaintext() { + String plaintext = "cat"; + String encrypted = playfair.encrypt(plaintext); + + assertEquals(0, encrypted.length() % 2, "Should be even length"); + } + + @Test + public void testEncryption() { String plaintext = "HELLO"; - String encryptedText = playfairCipher.encrypt(plaintext); + String encryptedText = playfair.encrypt(plaintext); assertEquals("GYIZSC", encryptedText); } @Test public void testDecryption() { - PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD"); - String encryptedText = "UDRIYP"; - String decryptedText = playfairCipher.decrypt(encryptedText); + String decryptedText = playfair.decrypt(encryptedText); assertEquals("NEBFVH", decryptedText); } @Test public void testEncryptionAndDecryption() { - PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD"); - String plaintext = "PLAYFAIR"; - String encryptedText = playfairCipher.encrypt(plaintext); - String decryptedText = playfairCipher.decrypt(encryptedText); + String encryptedText = playfair.encrypt(plaintext); + String decryptedText = playfair.decrypt(encryptedText); assertEquals(plaintext, decryptedText); } From a8889448cb64b978ae9e059a6d65b70f73c2d60a Mon Sep 17 00:00:00 2001 From: labe2301 Date: Thu, 30 Oct 2025 00:22:14 +0100 Subject: [PATCH 55/56] refactor: Fix imports to satisfy PMD --- .../ColumnarTranspositionCipherTest.java | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java index d3feac261bd8..bf1a5c86de40 100644 --- a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java +++ b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java @@ -1,10 +1,5 @@ package com.thealgorithms.ciphers; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -22,10 +17,10 @@ public void setUp() { @Test public void testEncryption() { String encryptedText = ColumnarTranspositionCipher.encrypt(plaintext, keyword); - assertNotNull(encryptedText, "The encrypted text should not be null."); - assertFalse(encryptedText.isEmpty(), "The encrypted text should not be empty."); + Assertions.assertNotNull(encryptedText, "The encrypted text should not be null."); + Assertions.assertFalse(encryptedText.isEmpty(), "The encrypted text should not be empty."); // Check if the encrypted text is different from the plaintext - assertNotEquals(plaintext, encryptedText, "The encrypted text should be different from the plaintext."); + Assertions.assertNotEquals(plaintext, encryptedText, "The encrypted text should be different from the plaintext."); } @Test @@ -33,8 +28,8 @@ public void testDecryption() { String encryptedText = ColumnarTranspositionCipher.encrypt(plaintext, keyword); String decryptedText = ColumnarTranspositionCipher.decrypt(); - assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original plaintext, ignoring spaces."); - assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(plaintext, keyword), "The encrypted text should be the same when encrypted again."); + Assertions.assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original plaintext, ignoring spaces."); + Assertions.assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(plaintext, keyword), "The encrypted text should be the same when encrypted again."); } @Test @@ -42,8 +37,8 @@ public void testLongPlainText() { String longText = "This is a significantly longer piece of text to test the encryption and decryption capabilities of the Columnar Transposition Cipher. It should handle long strings gracefully."; String encryptedText = ColumnarTranspositionCipher.encrypt(longText, keyword); String decryptedText = ColumnarTranspositionCipher.decrypt(); - assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces."); - assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(longText, keyword), "The encrypted text should be the same when encrypted again."); + Assertions.assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces."); + Assertions.assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(longText, keyword), "The encrypted text should be the same when encrypted again."); } @Test From a16e8ed3b7c3f5fb27a41cee43573aa2f9b03b93 Mon Sep 17 00:00:00 2001 From: Joel Lansgren Date: Fri, 31 Oct 2025 21:52:07 +0200 Subject: [PATCH 56/56] Added pom dev profile --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 285e3ead6212..f97b55d45853 100644 --- a/pom.xml +++ b/pom.xml @@ -166,7 +166,7 @@ dev - + datastructures trees AVLSimple