From cc17d60d5cb0a4f2b9d99d9d259d8887b71683fb Mon Sep 17 00:00:00 2001 From: Alexandre Velloso <4320811+AlexandreVelloso@users.noreply.github.com> Date: Sun, 6 Nov 2022 10:21:22 +0000 Subject: [PATCH] Add unit tests for SimpleSubCipher (#3688) --- .../ciphers/SimpleSubCipher.java | 30 ++++++--------- .../ciphers/SimpleSubCipherTest.java | 37 +++++++++++++++++++ 2 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 src/test/java/com/thealgorithms/ciphers/SimpleSubCipherTest.java diff --git a/src/main/java/com/thealgorithms/ciphers/SimpleSubCipher.java b/src/main/java/com/thealgorithms/ciphers/SimpleSubCipher.java index 8fd7744c..32b08f0c 100644 --- a/src/main/java/com/thealgorithms/ciphers/SimpleSubCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/SimpleSubCipher.java @@ -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 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 cipherMap = new HashMap(); + Map 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); - } } diff --git a/src/test/java/com/thealgorithms/ciphers/SimpleSubCipherTest.java b/src/test/java/com/thealgorithms/ciphers/SimpleSubCipherTest.java new file mode 100644 index 00000000..e9bbcf95 --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/SimpleSubCipherTest.java @@ -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); + } + +}