Skip to content

Add unit tests for SimpleSubCipher #3688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions src/main/java/com/thealgorithms/ciphers/SimpleSubCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();
Expand All @@ -39,13 +39,13 @@ public static String encode(String message, String cipherSmall) {

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();
}

/**
Expand All @@ -56,10 +56,10 @@ public static String encode(String message, String cipherSmall) {
* @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';
Expand All @@ -74,21 +74,13 @@ public static String decode(String encryptedMessage, String cipherSmall) {

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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}