Skip to content

Commit 25dc55e

Browse files
authored
Add Junit tests for ColumnarTranspositionCipher.java (TheAlgorithms#5599)
1 parent f80850b commit 25dc55e

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@
641641
* [AutokeyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java)
642642
* [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java)
643643
* [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java)
644+
* [ColumnarTranspositionCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java)
644645
* [DESTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/DESTest.java)
645646
* [HillCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/HillCipherTest.java)
646647
* [PlayfairTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java)

src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -184,24 +184,4 @@ private static void abecedariumBuilder(int value) {
184184
}
185185
abecedarium = t.toString();
186186
}
187-
188-
private static void showTable() {
189-
for (Object[] table1 : table) {
190-
for (Object item : table1) {
191-
System.out.print(item + " ");
192-
}
193-
System.out.println();
194-
}
195-
}
196-
197-
public static void main(String[] args) {
198-
String keywordForExample = "asd215";
199-
String wordBeingEncrypted = "This is a test of the Columnar Transposition Cipher";
200-
System.out.println("### Example of Columnar Transposition Cipher ###\n");
201-
System.out.println("Word being encryped ->>> " + wordBeingEncrypted);
202-
System.out.println("Word encrypted ->>> " + ColumnarTranspositionCipher.encrpyter(wordBeingEncrypted, keywordForExample));
203-
System.out.println("Word decryped ->>> " + ColumnarTranspositionCipher.decrypter());
204-
System.out.println("\n### Encrypted Table ###");
205-
showTable();
206-
}
207187
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class ColumnarTranspositionCipherTest {
12+
private String keyword;
13+
private String plaintext;
14+
15+
@BeforeEach
16+
public void setUp() {
17+
keyword = "keyword";
18+
plaintext = "This is a test message for Columnar Transposition Cipher";
19+
}
20+
21+
@Test
22+
public void testEncryption() {
23+
String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword);
24+
assertNotNull(encryptedText, "The encrypted text should not be null.");
25+
assertFalse(encryptedText.isEmpty(), "The encrypted text should not be empty.");
26+
// Check if the encrypted text is different from the plaintext
27+
assertNotEquals(plaintext, encryptedText, "The encrypted text should be different from the plaintext.");
28+
}
29+
30+
@Test
31+
public void testDecryption() {
32+
String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword);
33+
String decryptedText = ColumnarTranspositionCipher.decrypter();
34+
35+
assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original plaintext, ignoring spaces.");
36+
assertEquals(encryptedText, ColumnarTranspositionCipher.encrpyter(plaintext, keyword), "The encrypted text should be the same when encrypted again.");
37+
}
38+
39+
@Test
40+
public void testLongPlainText() {
41+
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.";
42+
String encryptedText = ColumnarTranspositionCipher.encrpyter(longText, keyword);
43+
String decryptedText = ColumnarTranspositionCipher.decrypter();
44+
assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces.");
45+
assertEquals(encryptedText, ColumnarTranspositionCipher.encrpyter(longText, keyword), "The encrypted text should be the same when encrypted again.");
46+
}
47+
}

0 commit comments

Comments
 (0)