Skip to content

Commit 11b5bde

Browse files
authored
Merge branch 'master' into astar_add_tests
2 parents 27c291e + d422bf5 commit 11b5bde

File tree

4 files changed

+104
-20
lines changed

4 files changed

+104
-20
lines changed

DIRECTORY.md

Lines changed: 2 additions & 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)
@@ -650,6 +651,7 @@
650651
* [VigenereTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/VigenereTest.java)
651652
* [XORCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/XORCipherTest.java)
652653
* conversions
654+
* [AffineConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AffineConverterTest.java)
653655
* [AnyBaseToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/AnyBaseToDecimalTest.java)
654656
* [BinaryToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java)
655657
* [BinaryToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToHexadecimalTest.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+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class AffineConverterTest {
10+
11+
private AffineConverter converter;
12+
13+
@BeforeEach
14+
void setUp() {
15+
converter = new AffineConverter(2.0, 3.0);
16+
}
17+
18+
@Test
19+
void testConstructor() {
20+
assertEquals(3.0, converter.convert(0.0), "Expected value when input is 0.0");
21+
assertEquals(5.0, converter.convert(1.0), "Expected value when input is 1.0");
22+
assertEquals(7.0, converter.convert(2.0), "Expected value when input is 2.0");
23+
}
24+
25+
@Test
26+
void testConvert() {
27+
assertEquals(3.0, converter.convert(0.0), "Conversion at 0.0 should equal the intercept");
28+
assertEquals(7.0, converter.convert(2.0), "2.0 should convert to 7.0");
29+
assertEquals(11.0, converter.convert(4.0), "4.0 should convert to 11.0");
30+
}
31+
32+
@Test
33+
void testInvert() {
34+
AffineConverter inverted = converter.invert();
35+
assertEquals(0.0, inverted.convert(3.0), "Inverted converter should return 0.0 for input 3.0");
36+
assertEquals(1.0, inverted.convert(5.0), "Inverted converter should return 1.0 for input 5.0");
37+
assertEquals(2.0, inverted.convert(7.0), "Inverted converter should return 2.0 for input 7.0");
38+
}
39+
40+
@Test
41+
void testInvertWithZeroSlope() {
42+
AffineConverter zeroSlopeConverter = new AffineConverter(0.0, 3.0);
43+
assertThrows(AssertionError.class, zeroSlopeConverter::invert, "Invert should throw assertion error when slope is zero");
44+
}
45+
46+
@Test
47+
void testCompose() {
48+
AffineConverter otherConverter = new AffineConverter(1.0, 2.0);
49+
AffineConverter composed = converter.compose(otherConverter);
50+
51+
assertEquals(7.0, composed.convert(0.0), "Expected composed conversion at 0.0");
52+
assertEquals(9.0, composed.convert(1.0), "Expected composed conversion at 1.0");
53+
assertEquals(11.0, composed.convert(2.0), "Expected composed conversion at 2.0");
54+
}
55+
}

0 commit comments

Comments
 (0)