-
Notifications
You must be signed in to change notification settings - Fork 21.2k
Expand file tree
/
Copy pathCaesarTest.java
More file actions
69 lines (53 loc) · 1.86 KB
/
Copy pathCaesarTest.java
File metadata and controls
69 lines (53 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.thealgorithms.ciphers;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class CaesarTest {
Caesar caesar = new Caesar();
@Test
void caesarEncryptTest() {
// given
String textToEncrypt = "Encrypt this text";
// when
String cipherText = caesar.encode(textToEncrypt, 5);
// then
assertEquals("Jshwduy ymnx yjcy", cipherText);
}
@Test
void caesarDecryptTest() {
// given
String encryptedText = "Jshwduy ymnx yjcy";
// when
String cipherText = caesar.decode(encryptedText, 5);
// then
assertEquals("Encrypt this text", cipherText);
}
@Test
void caesarEncryptWithNegativeShiftTest() {
// a shift of -1 must wrap 'A' backwards to 'Z', like a shift of +25 would
assertEquals("Z", caesar.encode("A", -1));
assertEquals("z", caesar.encode("a", -1));
assertEquals("EBIIL", caesar.encode("HELLO", -3));
}
@Test
void caesarDecryptWithNegativeShiftTest() {
assertEquals("A", caesar.decode("Z", -1));
assertEquals("HELLO", caesar.decode("EBIIL", -3));
}
@Test
void caesarNegativeShiftRoundTripTest() {
// encode followed by decode with the same shift must return the original text
for (int shift : new int[] {-1, -5, -25, -26, -27, -52}) {
String message = "The quick brown Fox";
assertEquals(message, caesar.decode(caesar.encode(message, shift), shift));
}
}
@Test
void caesarBruteForce() {
// given
String encryptedText = "Jshwduy ymnx yjcy";
// when
String[] allPossibleAnswers = caesar.bruteforce(encryptedText);
assertEquals(27, allPossibleAnswers.length);
assertEquals("Encrypt this text", allPossibleAnswers[5]);
}
}