Add unit tests for SimpleSubCipher (#3688)

This commit is contained in:
Alexandre Velloso 2022-11-06 10:21:22 +00:00 committed by GitHub
parent 1c7da7af25
commit cc17d60d5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 19 deletions

View File

@ -19,8 +19,8 @@ public class SimpleSubCipher {
* @param cipherSmall
* @return Encrypted message
*/
public static String encode(String message, String cipherSmall) {
String encoded = "";
public String encode(String message, String cipherSmall) {
StringBuilder encoded = new StringBuilder();
// This map is used to encode
Map<Character, Character> cipherMap = new HashMap<>();
@ -39,13 +39,13 @@ public class SimpleSubCipher {
for (int i = 0; i < message.length(); i++) {
if (Character.isAlphabetic(message.charAt(i))) {
encoded += cipherMap.get(message.charAt(i));
encoded.append(cipherMap.get(message.charAt(i)));
} else {
encoded += message.charAt(i);
encoded.append(message.charAt(i));
}
}
return encoded;
return encoded.toString();
}
/**
@ -56,10 +56,10 @@ public class SimpleSubCipher {
* @param cipherSmall
* @return message
*/
public static String decode(String encryptedMessage, String cipherSmall) {
String decoded = "";
public String decode(String encryptedMessage, String cipherSmall) {
StringBuilder decoded = new StringBuilder();
Map<Character, Character> cipherMap = new HashMap<Character, Character>();
Map<Character, Character> cipherMap = new HashMap<>();
char beginSmallLetter = 'a';
char beginCapitalLetter = 'A';
@ -74,21 +74,13 @@ public class SimpleSubCipher {
for (int i = 0; i < encryptedMessage.length(); i++) {
if (Character.isAlphabetic(encryptedMessage.charAt(i))) {
decoded += cipherMap.get(encryptedMessage.charAt(i));
decoded.append(cipherMap.get(encryptedMessage.charAt(i)));
} else {
decoded += encryptedMessage.charAt(i);
decoded.append(encryptedMessage.charAt(i));
}
}
return decoded;
return decoded.toString();
}
public static void main(String[] args) {
String a = encode(
"defend the east wall of the castle",
"phqgiumeaylnofdxjkrcvstzwb"
);
String b = decode(a, "phqgiumeaylnofdxjkrcvstzwb");
System.out.println(b);
}
}

View File

@ -0,0 +1,37 @@
package com.thealgorithms.ciphers;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SimpleSubCipherTest {
SimpleSubCipher simpleSubCipher = new SimpleSubCipher();
@Test
void simpleSubCipherEncryptTest() {
// given
String text = "defend the east wall of the castle";
String cipherSmall = "phqgiumeaylnofdxjkrcvstzwb";
// when
String cipherText = simpleSubCipher.encode(text, cipherSmall);
// then
assertEquals("giuifg cei iprc tpnn du cei qprcni", cipherText);
}
@Test
void simpleSubCipherDecryptTest() {
// given
String encryptedText = "giuifg cei iprc tpnn du cei qprcni";
String cipherSmall = "phqgiumeaylnofdxjkrcvstzwb";
// when
String decryptedText = simpleSubCipher.decode(encryptedText, cipherSmall);
// then
assertEquals("defend the east wall of the castle", decryptedText);
}
}