From a76c2752931b1727428660ef3c4a684d25645563 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 11:23:40 +0530 Subject: [PATCH 1/6] Add Junit tests for `ColumnarTranspositionCipher.java` --- .../ciphers/ColumnarTranspositionCipher.java | 11 ----- .../ColumnarTranspositionCipherTest.java | 48 +++++++++++++++++++ 2 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java diff --git a/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java b/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java index e59cfb12d816..5d58d93f46d2 100644 --- a/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java @@ -193,15 +193,4 @@ private static void showTable() { System.out.println(); } } - - public static void main(String[] args) { - String keywordForExample = "asd215"; - String wordBeingEncrypted = "This is a test of the Columnar Transposition Cipher"; - System.out.println("### Example of Columnar Transposition Cipher ###\n"); - System.out.println("Word being encryped ->>> " + wordBeingEncrypted); - System.out.println("Word encrypted ->>> " + ColumnarTranspositionCipher.encrpyter(wordBeingEncrypted, keywordForExample)); - System.out.println("Word decryped ->>> " + ColumnarTranspositionCipher.decrypter()); - System.out.println("\n### Encrypted Table ###"); - showTable(); - } } diff --git a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java new file mode 100644 index 000000000000..dc53acdf574e --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java @@ -0,0 +1,48 @@ +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.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ColumnarTranspositionCipherTest { + private String keyword; + private String plaintext; + + @BeforeEach + public void setUp() { + keyword = "keyword"; + plaintext = "This is a test message for Columnar Transposition Cipher"; + } + + @Test + public void testEncryption() { + String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword); + assertNotNull(encryptedText, "The encrypted text should not be null."); + 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."); + } + + @Test + public void testDecryption() { + String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword); + String decryptedText = ColumnarTranspositionCipher.decrypter(); + + assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), + "The decrypted text should match the original plaintext, ignoring spaces."); + } + + @Test + 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.encrpyter(longText, keyword); + String decryptedText = ColumnarTranspositionCipher.decrypter(); + + assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), + "The decrypted text should match the original long plaintext, ignoring spaces."); + } +} From 7d03a55801dcd39ea56aad44260b0b0ff2205e8c Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 6 Oct 2024 05:53:57 +0000 Subject: [PATCH 2/6] Update directory --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1bad5d3b98a3..3346e81370b5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -620,6 +620,7 @@ * [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java) * [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java) * [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java) + * [ColumnarTranspositionCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java) * [DESTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/DESTest.java) * [HillCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/HillCipherTest.java) * [PlayfairTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java) From dc649ad032b49e652f23905358f997438cfeec9a Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 11:26:49 +0530 Subject: [PATCH 3/6] Fix --- .../ciphers/ColumnarTranspositionCipherTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java index dc53acdf574e..b4ff5a06beaa 100644 --- a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java +++ b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java @@ -32,8 +32,7 @@ public void testDecryption() { String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword); String decryptedText = ColumnarTranspositionCipher.decrypter(); - assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), - "The decrypted text should match the original plaintext, ignoring spaces."); + assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original plaintext, ignoring spaces."); } @Test @@ -42,7 +41,6 @@ public void testLongPlainText() { String encryptedText = ColumnarTranspositionCipher.encrpyter(longText, keyword); String decryptedText = ColumnarTranspositionCipher.decrypter(); - assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), - "The decrypted text should match the original long plaintext, ignoring spaces."); + assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces."); } } From 06ec601a4dc0cfe8fcb87a845c6204d4dedd2936 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 11:31:11 +0530 Subject: [PATCH 4/6] Fix --- .../thealgorithms/ciphers/ColumnarTranspositionCipherTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java index b4ff5a06beaa..fe2cdfb90e3b 100644 --- a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java +++ b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java @@ -29,7 +29,6 @@ public void testEncryption() { @Test public void testDecryption() { - String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword); String decryptedText = ColumnarTranspositionCipher.decrypter(); assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original plaintext, ignoring spaces."); @@ -38,7 +37,6 @@ public void testDecryption() { @Test 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.encrpyter(longText, keyword); String decryptedText = ColumnarTranspositionCipher.decrypter(); assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces."); From 74c3c87d6e4799c37caed91c0daf0c209b1d1a74 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 11:36:55 +0530 Subject: [PATCH 5/6] Fix --- .../ciphers/ColumnarTranspositionCipherTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java index fe2cdfb90e3b..0d306a6623db 100644 --- a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java +++ b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java @@ -29,16 +29,19 @@ public void testEncryption() { @Test public void testDecryption() { + String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword); String decryptedText = ColumnarTranspositionCipher.decrypter(); assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original plaintext, ignoring spaces."); + assertEquals(encryptedText, ColumnarTranspositionCipher.encrpyter(plaintext, keyword), "The encrypted text should be the same when encrypted again."); } @Test 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.encrpyter(longText, keyword); String decryptedText = ColumnarTranspositionCipher.decrypter(); - assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces."); + assertEquals(encryptedText, ColumnarTranspositionCipher.encrpyter(longText, keyword), "The encrypted text should be the same when encrypted again."); } } From 28b0b634d1a74da63c8f8ba4f9b3b61934e08d14 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 11:40:00 +0530 Subject: [PATCH 6/6] Remove `showTable` method --- .../ciphers/ColumnarTranspositionCipher.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java b/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java index 5d58d93f46d2..d7e64a12ebfd 100644 --- a/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java @@ -184,13 +184,4 @@ private static void abecedariumBuilder(int value) { } abecedarium = t.toString(); } - - private static void showTable() { - for (Object[] table1 : table) { - for (Object item : table1) { - System.out.print(item + " "); - } - System.out.println(); - } - } }