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