-
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
package com.thealgorithms.ciphers;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class RailFenceTest {
@Test
void testEncryption() {
RailFenceCipher cipher = new RailFenceCipher();
String input = "We are discovered! Flee at once";
int rails = 3;
String encrypted = cipher.encrypt(input, rails);
assertEquals("Wrivdlaneaedsoee!Fe toc cr e e", encrypted);
String singleChar = "A";
int singleRail = 2;
String encryptedSingleChar = cipher.encrypt(singleChar, singleRail);
assertEquals("A", encryptedSingleChar);
String shortString = "Hello";
int moreRails = 10;
String encryptedShortString = cipher.encrypt(shortString, moreRails);
assertEquals("Hello", encryptedShortString);
String inputSingleRail = "Single line";
int singleRailOnly = 1;
String encryptedSingleRail = cipher.encrypt(inputSingleRail, singleRailOnly);
assertEquals("Single line", encryptedSingleRail);
}
@Test
void testDecryption() {
RailFenceCipher cipher = new RailFenceCipher();
// Scenario 1: Basic decryption with multiple rails
String encryptedInput = "Wrivdlaneaedsoee!Fe toc cr e e";
int rails = 3;
String decrypted = cipher.decrypt(encryptedInput, rails);
assertEquals("We are discovered! Flee at once", decrypted);
// Scenario 2: Single character string decryption
String encryptedSingleChar = "A";
int singleRail = 2; // More than 1 rail
String decryptedSingleChar = cipher.decrypt(encryptedSingleChar, singleRail);
assertEquals("A", decryptedSingleChar);
// Scenario 3: String length less than the number of rails
String encryptedShortString = "Hello";
int moreRails = 10; // More rails than characters
String decryptedShortString = cipher.decrypt(encryptedShortString, moreRails);
assertEquals("Hello", decryptedShortString);
// Scenario 4: Single rail decryption (output should be the same as input)
String encryptedSingleRail = "Single line";
int singleRailOnly = 1;
String decryptedSingleRail = cipher.decrypt(encryptedSingleRail, singleRailOnly);
assertEquals("Single line", decryptedSingleRail);
}
}