Skip to content

Commit cc17d60

Browse files
Add unit tests for SimpleSubCipher (TheAlgorithms#3688)
1 parent 1c7da7a commit cc17d60

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

src/main/java/com/thealgorithms/ciphers/SimpleSubCipher.java

+11-19
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public class SimpleSubCipher {
1919
* @param cipherSmall
2020
* @return Encrypted message
2121
*/
22-
public static String encode(String message, String cipherSmall) {
23-
String encoded = "";
22+
public String encode(String message, String cipherSmall) {
23+
StringBuilder encoded = new StringBuilder();
2424

2525
// This map is used to encode
2626
Map<Character, Character> cipherMap = new HashMap<>();
@@ -39,13 +39,13 @@ public static String encode(String message, String cipherSmall) {
3939

4040
for (int i = 0; i < message.length(); i++) {
4141
if (Character.isAlphabetic(message.charAt(i))) {
42-
encoded += cipherMap.get(message.charAt(i));
42+
encoded.append(cipherMap.get(message.charAt(i)));
4343
} else {
44-
encoded += message.charAt(i);
44+
encoded.append(message.charAt(i));
4545
}
4646
}
4747

48-
return encoded;
48+
return encoded.toString();
4949
}
5050

5151
/**
@@ -56,10 +56,10 @@ public static String encode(String message, String cipherSmall) {
5656
* @param cipherSmall
5757
* @return message
5858
*/
59-
public static String decode(String encryptedMessage, String cipherSmall) {
60-
String decoded = "";
59+
public String decode(String encryptedMessage, String cipherSmall) {
60+
StringBuilder decoded = new StringBuilder();
6161

62-
Map<Character, Character> cipherMap = new HashMap<Character, Character>();
62+
Map<Character, Character> cipherMap = new HashMap<>();
6363

6464
char beginSmallLetter = 'a';
6565
char beginCapitalLetter = 'A';
@@ -74,21 +74,13 @@ public static String decode(String encryptedMessage, String cipherSmall) {
7474

7575
for (int i = 0; i < encryptedMessage.length(); i++) {
7676
if (Character.isAlphabetic(encryptedMessage.charAt(i))) {
77-
decoded += cipherMap.get(encryptedMessage.charAt(i));
77+
decoded.append(cipherMap.get(encryptedMessage.charAt(i)));
7878
} else {
79-
decoded += encryptedMessage.charAt(i);
79+
decoded.append(encryptedMessage.charAt(i));
8080
}
8181
}
8282

83-
return decoded;
83+
return decoded.toString();
8484
}
8585

86-
public static void main(String[] args) {
87-
String a = encode(
88-
"defend the east wall of the castle",
89-
"phqgiumeaylnofdxjkrcvstzwb"
90-
);
91-
String b = decode(a, "phqgiumeaylnofdxjkrcvstzwb");
92-
System.out.println(b);
93-
}
9486
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
class SimpleSubCipherTest {
8+
9+
SimpleSubCipher simpleSubCipher = new SimpleSubCipher();
10+
11+
@Test
12+
void simpleSubCipherEncryptTest() {
13+
// given
14+
String text = "defend the east wall of the castle";
15+
String cipherSmall = "phqgiumeaylnofdxjkrcvstzwb";
16+
17+
// when
18+
String cipherText = simpleSubCipher.encode(text, cipherSmall);
19+
20+
// then
21+
assertEquals("giuifg cei iprc tpnn du cei qprcni", cipherText);
22+
}
23+
24+
@Test
25+
void simpleSubCipherDecryptTest() {
26+
// given
27+
String encryptedText = "giuifg cei iprc tpnn du cei qprcni";
28+
String cipherSmall = "phqgiumeaylnofdxjkrcvstzwb";
29+
30+
// when
31+
String decryptedText = simpleSubCipher.decode(encryptedText, cipherSmall);
32+
33+
// then
34+
assertEquals("defend the east wall of the castle", decryptedText);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)