- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathRailFenceTest.java
62 lines (49 loc) · 2.39 KB
/
RailFenceTest.java
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
packagecom.thealgorithms.ciphers;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importorg.junit.jupiter.api.Test;
publicclassRailFenceTest {
@Test
voidtestEncryption() {
RailFenceCiphercipher = newRailFenceCipher();
Stringinput = "We are discovered! Flee at once";
intrails = 3;
Stringencrypted = cipher.encrypt(input, rails);
assertEquals("Wrivdlaneaedsoee!Fe toc cr e e", encrypted);
StringsingleChar = "A";
intsingleRail = 2;
StringencryptedSingleChar = cipher.encrypt(singleChar, singleRail);
assertEquals("A", encryptedSingleChar);
StringshortString = "Hello";
intmoreRails = 10;
StringencryptedShortString = cipher.encrypt(shortString, moreRails);
assertEquals("Hello", encryptedShortString);
StringinputSingleRail = "Single line";
intsingleRailOnly = 1;
StringencryptedSingleRail = cipher.encrypt(inputSingleRail, singleRailOnly);
assertEquals("Single line", encryptedSingleRail);
}
@Test
voidtestDecryption() {
RailFenceCiphercipher = newRailFenceCipher();
// Scenario 1: Basic decryption with multiple rails
StringencryptedInput = "Wrivdlaneaedsoee!Fe toc cr e e";
intrails = 3;
Stringdecrypted = cipher.decrypt(encryptedInput, rails);
assertEquals("We are discovered! Flee at once", decrypted);
// Scenario 2: Single character string decryption
StringencryptedSingleChar = "A";
intsingleRail = 2; // More than 1 rail
StringdecryptedSingleChar = cipher.decrypt(encryptedSingleChar, singleRail);
assertEquals("A", decryptedSingleChar);
// Scenario 3: String length less than the number of rails
StringencryptedShortString = "Hello";
intmoreRails = 10; // More rails than characters
StringdecryptedShortString = cipher.decrypt(encryptedShortString, moreRails);
assertEquals("Hello", decryptedShortString);
// Scenario 4: Single rail decryption (output should be the same as input)
StringencryptedSingleRail = "Single line";
intsingleRailOnly = 1;
StringdecryptedSingleRail = cipher.decrypt(encryptedSingleRail, singleRailOnly);
assertEquals("Single line", decryptedSingleRail);
}
}