Skip to content

Commit fd3e28d

Browse files
committed
refactor: Merge files testing the same packages
1 parent 1f9d15a commit fd3e28d

File tree

7 files changed

+218
-256
lines changed

7 files changed

+218
-256
lines changed

src/test/java/com/thealgorithms/ciphers/MyAESTest.java renamed to src/test/java/com/thealgorithms/ciphers/AESTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.math.BigInteger;
88
import org.junit.jupiter.api.Test;
99

10-
public class MyAESTest {
10+
public class AESTest {
1111
@Test
1212
void testScheduleCore() {
1313
BigInteger input = new BigInteger("1a2b3c4d", 16);

src/test/java/com/thealgorithms/ciphers/CaesarTest.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,78 @@ void caesarBruteForce() {
4343
assertEquals(27, allPossibleAnswers.length);
4444
assertEquals("Encrypt this text", allPossibleAnswers[5]);
4545
}
46+
47+
@Test
48+
void shouldReturnSameAsInputWhenShiftingZeroOr26() {
49+
String message = "message";
50+
51+
String encoded1 = caesar.encode(message, 0);
52+
String encoded2 = caesar.encode(message, 26);
53+
54+
assertEquals(message, encoded1, "Encoded should be same as original");
55+
assertEquals(message, encoded2, "Encoded should be same as original");
56+
}
57+
58+
@Test
59+
void shouldReturnsameAsInputWhenUsingCharactersOutsideLatinAlphabet() {
60+
String message = "!#¤%&/()=?`^½§@£$€{[]}´`¨~'*-.,_:;<>|";
61+
62+
String encoded = caesar.encode(message, 10);
63+
64+
assertEquals(message, encoded);
65+
}
66+
67+
@Test
68+
void shouldWrapZToAWhenEncodingSingleShift() {
69+
String message = "zZ";
70+
71+
String encoded = caesar.encode(message, 1);
72+
String exptected = "aA";
73+
74+
assertEquals(exptected, encoded, "zZ should wrap to aA");
75+
}
76+
77+
@Test
78+
void shouldWrapAToZWhenDecodingSingleShift() {
79+
String message = "aA";
80+
81+
String decoded = caesar.decode(message, 1);
82+
String expected = "zZ";
83+
84+
assertEquals(expected, decoded);
85+
}
86+
87+
@Test
88+
void shouldNotWrapWhenEncodingFromYToZ() {
89+
String message = "yY";
90+
91+
String encoded = caesar.encode(message, 1);
92+
String expected = "zZ";
93+
94+
assertEquals(expected, encoded);
95+
}
96+
97+
@Test
98+
void shouldNotWrapWhenDecodingFromBToA() {
99+
String message = "bB";
100+
101+
String decoded = caesar.decode(message, 1);
102+
String expected = "aA";
103+
104+
assertEquals(expected, decoded);
105+
}
106+
107+
@Test
108+
void shouldContain27CombinationsFromBruteForce() {
109+
String message = "message";
110+
111+
String encoded = caesar.encode(message, 10);
112+
String[] combinations = caesar.bruteforce(encoded);
113+
String expected = "wocckqo";
114+
115+
assertEquals(27, combinations.length, "Should contain 27 possible decoded combinations");
116+
assertEquals(expected, combinations[0], "First combination should contain encoded message");
117+
assertEquals(message, combinations[10], "10:th entry should contain original message");
118+
assertEquals(expected, combinations[26], "26:th entry should be the same as the 0:th entry, the encoded message");
119+
}
46120
}

src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.jupiter.api.Assertions.assertNotEquals;
66
import static org.junit.jupiter.api.Assertions.assertNotNull;
77

8+
import org.junit.jupiter.api.Assertions;
89
import org.junit.jupiter.api.BeforeEach;
910
import org.junit.jupiter.api.Test;
1011

@@ -44,4 +45,115 @@ public void testLongPlainText() {
4445
assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces.");
4546
assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(longText, keyword), "The encrypted text should be the same when encrypted again.");
4647
}
48+
49+
@Test
50+
void shouldNotProduceNullOrEmptyEncryptedText() {
51+
String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
52+
53+
Assertions.assertNotNull(encrypted, "Encrypted text should not be null");
54+
Assertions.assertFalse(encrypted.isEmpty(), "Encrypted text should not be empty");
55+
}
56+
57+
@Test
58+
void shouldChangePlaintextToEncrypted() {
59+
String encrypted = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
60+
61+
Assertions.assertNotEquals(plaintext, encrypted, "Encrypted text should differ from plaintext");
62+
}
63+
64+
@Test
65+
void shouldEncryptDetermenistically() {
66+
String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
67+
String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
68+
69+
Assertions.assertEquals(encrypted1, encrypted2, "Encryptions should be equal");
70+
}
71+
72+
@Test
73+
void shouldProduceDifferentEncryptionsWithDifferentKeywoards() {
74+
String keyword2 = keyword + "a";
75+
76+
String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
77+
String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, keyword2);
78+
79+
Assertions.assertNotEquals(encrypted1, encrypted2, "Should produce different encryptions");
80+
}
81+
82+
@Test
83+
void shouldMatchWithUnsortedKeyword() {
84+
String myPlaintext = "123456789";
85+
String myKeyword = "unsorted";
86+
87+
String encrypted = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword);
88+
String expected = "8≈7≈2≈4≈5≈3≈6≈19";
89+
90+
Assertions.assertEquals(expected, encrypted, "Should match");
91+
}
92+
93+
@Test
94+
void shouldMatchEncryptionAndDecryptionWithNoSpacesOrPadding() {
95+
String myPlaintext = "NoSpacesOrPadding";
96+
97+
ColumnarTranspositionCipher.encrypt(myPlaintext, keyword);
98+
String decrypted = ColumnarTranspositionCipher.decrypt();
99+
100+
Assertions.assertEquals(myPlaintext, decrypted, "Decrypted text should match original plaintext");
101+
}
102+
103+
@Test
104+
void shouldNotContainPaddingInDecryption() {
105+
String myPlaintext = "text";
106+
107+
ColumnarTranspositionCipher.encrypt(myPlaintext, keyword);
108+
String decrypted = ColumnarTranspositionCipher.decrypt();
109+
110+
Assertions.assertFalse(decrypted.contains("≈"), "Should not contain padding characters");
111+
}
112+
113+
@Test
114+
void shouldEncryptWithKeywordLongerThanPlaintext() {
115+
String myPlaintext = "text";
116+
String myKeyword = "averylongkeyword";
117+
118+
String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword);
119+
120+
Assertions.assertNotNull(encryption, "Should encrypt where plaintext.length() < keyword.length()");
121+
}
122+
123+
@Test
124+
void shouldEncryptWithKeywordWithSameLengthAsPlaintext() {
125+
String myPlaintext = "textaslongaskeyword";
126+
String myKeyword = "keywordaslongastext";
127+
128+
String encryption = ColumnarTranspositionCipher.encrypt(myPlaintext, myKeyword);
129+
130+
Assertions.assertNotNull(encryption, "Should encrypt where plaintext.length() == keyword.length()");
131+
}
132+
133+
@Test
134+
void shouldProduceDifferentEncryptionForSameKeywordButSortedDifferently() {
135+
String unsertedKeyword1 = "EFGHABCD";
136+
String unsertedKeyword2 = "AEBFCGDH";
137+
138+
String encrypted1 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword1);
139+
String encrypted2 = ColumnarTranspositionCipher.encrypt(plaintext, unsertedKeyword2);
140+
141+
Assertions.assertNotEquals(encrypted1, encrypted2, "Should differ with different keywords");
142+
}
143+
144+
@Test
145+
void shouldEncryptWithCustomAbecedarium() {
146+
String myAbecedarium = "abcdefghijklmnopqrstuvwxyz";
147+
148+
String encryption = ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium);
149+
150+
Assertions.assertNotNull(encryption, "Should encrypt with custom abecedarium");
151+
}
152+
153+
@Test
154+
void shouldNotEncryptWithInvalidAbecedarium() {
155+
String myAbecedarium = "abcde";
156+
157+
Assertions.assertThrows(NullPointerException.class, () -> ColumnarTranspositionCipher.encrypt(plaintext, keyword, myAbecedarium), "Should throw error when keyword contains characters not present in abecedarium");
158+
}
47159
}

src/test/java/com/thealgorithms/ciphers/MyCaesarTest.java

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)