Skip to content

Commit f8897f1

Browse files
Add unit tests for Caesar cipher (TheAlgorithms#3665)
Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent 7ef7598 commit f8897f1

File tree

2 files changed

+56
-54
lines changed

2 files changed

+56
-54
lines changed
Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.ciphers;
22

3-
import java.util.Scanner;
4-
53
/**
64
* A Java implementation of Caesar Cipher. /It is a type of substitution cipher
75
* in which each letter in the plaintext is replaced by a letter some fixed
@@ -18,7 +16,7 @@ public class Caesar {
1816
*
1917
* @return Encrypted message
2018
*/
21-
public static String encode(String message, int shift) {
19+
public String encode(String message, int shift) {
2220
StringBuilder encoded = new StringBuilder();
2321

2422
shift %= 26;
@@ -29,10 +27,10 @@ public static String encode(String message, int shift) {
2927
// is in-order latin alphabet
3028
char current = message.charAt(i); // Java law : char + int = char
3129

32-
if (IsCapitalLatinLetter(current)) {
30+
if (isCapitalLatinLetter(current)) {
3331
current += shift;
3432
encoded.append((char) (current > 'Z' ? current - 26 : current)); // 26 = number of latin letters
35-
} else if (IsSmallLatinLetter(current)) {
33+
} else if (isSmallLatinLetter(current)) {
3634
current += shift;
3735
encoded.append((char) (current > 'z' ? current - 26 : current)); // 26 = number of latin letters
3836
} else {
@@ -48,18 +46,18 @@ public static String encode(String message, int shift) {
4846
*
4947
* @return message
5048
*/
51-
public static String decode(String encryptedMessage, int shift) {
49+
public String decode(String encryptedMessage, int shift) {
5250
StringBuilder decoded = new StringBuilder();
5351

5452
shift %= 26;
5553

5654
final int length = encryptedMessage.length();
5755
for (int i = 0; i < length; i++) {
5856
char current = encryptedMessage.charAt(i);
59-
if (IsCapitalLatinLetter(current)) {
57+
if (isCapitalLatinLetter(current)) {
6058
current -= shift;
6159
decoded.append((char) (current < 'A' ? current + 26 : current)); // 26 = number of latin letters
62-
} else if (IsSmallLatinLetter(current)) {
60+
} else if (isSmallLatinLetter(current)) {
6361
current -= shift;
6462
decoded.append((char) (current < 'a' ? current + 26 : current)); // 26 = number of latin letters
6563
} else {
@@ -72,69 +70,26 @@ public static String decode(String encryptedMessage, int shift) {
7270
/**
7371
* @return true if character is capital Latin letter or false for others
7472
*/
75-
private static boolean IsCapitalLatinLetter(char c) {
73+
private static boolean isCapitalLatinLetter(char c) {
7674
return c >= 'A' && c <= 'Z';
7775
}
7876

7977
/**
8078
* @return true if character is small Latin letter or false for others
8179
*/
82-
private static boolean IsSmallLatinLetter(char c) {
80+
private static boolean isSmallLatinLetter(char c) {
8381
return c >= 'a' && c <= 'z';
8482
}
8583

8684
/**
8785
* @return string array which contains all the possible decoded combination.
8886
*/
89-
public static String[] bruteforce(String encryptedMessage) {
87+
public String[] bruteforce(String encryptedMessage) {
9088
String[] listOfAllTheAnswers = new String[27];
9189
for (int i = 0; i <= 26; i++) {
9290
listOfAllTheAnswers[i] = decode(encryptedMessage, i);
9391
}
9492

9593
return listOfAllTheAnswers;
9694
}
97-
98-
public static void main(String[] args) {
99-
Scanner input = new Scanner(System.in);
100-
int shift = 0;
101-
System.out.println("Please enter the message (Latin Alphabet)");
102-
String message = input.nextLine();
103-
System.out.println(message);
104-
System.out.println("(E)ncode or (D)ecode or (B)ruteforce?");
105-
char choice = input.next().charAt(0);
106-
switch (choice) {
107-
case 'E':
108-
case 'e':
109-
System.out.println("Please enter the shift number");
110-
shift = input.nextInt() % 26;
111-
System.out.println(
112-
"ENCODED MESSAGE IS \n" + encode(message, shift)
113-
); // send our function to handle
114-
break;
115-
case 'D':
116-
case 'd':
117-
System.out.println("Please enter the shift number");
118-
shift = input.nextInt() % 26;
119-
System.out.println(
120-
"DECODED MESSAGE IS \n" + decode(message, shift)
121-
);
122-
break;
123-
case 'B':
124-
case 'b':
125-
String[] listOfAllTheAnswers = bruteforce(message);
126-
for (int i = 0; i <= 26; i++) {
127-
System.out.println(
128-
"FOR SHIFT " +
129-
String.valueOf(i) +
130-
" decoded message is " +
131-
listOfAllTheAnswers[i]
132-
);
133-
}
134-
default:
135-
System.out.println("default case");
136-
}
137-
138-
input.close();
139-
}
14095
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class CaesarTest {
8+
9+
Caesar caesar = new Caesar();
10+
11+
@Test
12+
void caesarEncryptTest() {
13+
// given
14+
String textToEncrypt = "Encrypt this text";
15+
16+
// when
17+
String cipherText = caesar.encode(textToEncrypt, 5);
18+
19+
// then
20+
assertEquals("Jshwduy ymnx yjcy", cipherText);
21+
}
22+
23+
@Test
24+
void caesarDecryptTest() {
25+
// given
26+
String encryptedText = "Jshwduy ymnx yjcy";
27+
28+
// when
29+
String cipherText = caesar.decode(encryptedText, 5);
30+
31+
// then
32+
assertEquals("Encrypt this text", cipherText);
33+
}
34+
35+
@Test
36+
void caesarBruteForce() {
37+
// given
38+
String encryptedText = "Jshwduy ymnx yjcy";
39+
40+
// when
41+
String[] allPossibleAnswers = caesar.bruteforce(encryptedText);
42+
43+
assertEquals(27, allPossibleAnswers.length);
44+
assertEquals("Encrypt this text", allPossibleAnswers[5]);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)