Skip to content

Commit 8f28a55

Browse files
committed
Refactor AtbashCipher, add ParameterizedTest
1 parent 9c76b30 commit 8f28a55

File tree

2 files changed

+90
-45
lines changed

2 files changed

+90
-45
lines changed

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

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,98 @@
11
package com.thealgorithms.ciphers;
22

33
/**
4-
* The Atbash cipher is a simple substitution cipher that replaces each letter
5-
* in the alphabet with its reverse.
6-
* For example, 'A' becomes 'Z', 'B' becomes 'Y', and so on. It works
7-
* identically for both uppercase and lowercase letters.
8-
* It's a symmetric cipher, meaning applying it twice returns the original text.
9-
* Hence, the encrypting and the decrypting functions are identical
10-
* @author https://github.com/Krounosity
11-
* Learn more: https://en.wikipedia.org/wiki/Atbash
4+
* The Atbash cipher is a classic substitution cipher that substitutes each letter
5+
* with its opposite letter in the alphabet.
6+
*
7+
* For example:
8+
* - 'A' becomes 'Z', 'B' becomes 'Y', 'C' becomes 'X', and so on.
9+
* - Similarly, 'a' becomes 'z', 'b' becomes 'y', and so on.
10+
*
11+
* The cipher works identically for both uppercase and lowercase letters.
12+
* Non-alphabetical characters remain unchanged in the output.
13+
*
14+
* This cipher is symmetric, meaning that applying the cipher twice will return
15+
* the original text. Therefore, the same function is used for both encryption and decryption.
16+
*
17+
* <p>Usage Example:</p>
18+
* <pre>
19+
* AtbashCipher cipher = new AtbashCipher("Hello World!");
20+
* String encrypted = cipher.convert(); // Output: "Svool Dliow!"
21+
* </pre>
22+
*
23+
* @author <a href="https://github.com/Krounosity">Krounosity</a>
24+
* @see <a href="https://en.wikipedia.org/wiki/Atbash">Atbash Cipher (Wikipedia)</a>
1225
*/
13-
1426
public class AtbashCipher {
1527

1628
private String toConvert;
1729

18-
// Default constructor.
19-
AtbashCipher() {
30+
public AtbashCipher() {
2031
}
2132

22-
// String setting constructor.
23-
AtbashCipher(String str) {
24-
toConvert = str;
33+
/**
34+
* Constructor with a string parameter.
35+
*
36+
* @param str The string to be converted using the Atbash cipher
37+
*/
38+
public AtbashCipher(String str) {
39+
this.toConvert = str;
2540
}
2641

27-
// String getter method.
42+
/**
43+
* Returns the current string set for conversion.
44+
*
45+
* @return The string to be converted
46+
*/
2847
public String getString() {
2948
return toConvert;
3049
}
3150

32-
// String setter method.
51+
/**
52+
* Sets the string to be converted using the Atbash cipher.
53+
*
54+
* @param str The new string to convert
55+
*/
3356
public void setString(String str) {
34-
toConvert = str;
57+
this.toConvert = str;
3558
}
3659

37-
// Checking whether the current character is capital.
60+
/**
61+
* Checks if a character is uppercase.
62+
*
63+
* @param ch The character to check
64+
* @return {@code true} if the character is uppercase, {@code false} otherwise
65+
*/
3866
private boolean isCapital(char ch) {
3967
return ch >= 'A' && ch <= 'Z';
4068
}
4169

42-
// Checking whether the current character is smallcased.
70+
/**
71+
* Checks if a character is lowercase.
72+
*
73+
* @param ch The character to check
74+
* @return {@code true} if the character is lowercase, {@code false} otherwise
75+
*/
4376
private boolean isSmall(char ch) {
4477
return ch >= 'a' && ch <= 'z';
4578
}
4679

47-
// Converting text to atbash cipher code or vice versa.
80+
/**
81+
* Converts the input string using the Atbash cipher.
82+
* Alphabetic characters are substituted with their opposite in the alphabet,
83+
* while non-alphabetic characters remain unchanged.
84+
*
85+
* @return The converted string after applying the Atbash cipher
86+
*/
4887
public String convert() {
49-
50-
// Using StringBuilder to store new string.
5188
StringBuilder convertedString = new StringBuilder();
5289

53-
// Iterating for each character.
5490
for (char ch : toConvert.toCharArray()) {
55-
56-
// If the character is smallcased.
5791
if (isSmall(ch)) {
5892
convertedString.append((char) ('z' - (ch - 'a')));
59-
}
60-
// If the character is capital cased.
61-
else if (isCapital(ch)) {
93+
} else if (isCapital(ch)) {
6294
convertedString.append((char) ('Z' - (ch - 'A')));
63-
}
64-
// Non-alphabetical character.
65-
else {
95+
} else {
6696
convertedString.append(ch);
6797
}
6898
}

src/test/java/com/thealgorithms/ciphers/AtbashTest.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,42 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
69

710
public class AtbashTest {
811

9-
@Test
10-
public void atbashEncrypt() {
11-
AtbashCipher normalToEncrypt = new AtbashCipher("Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!");
12-
String expectedText = "Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!";
12+
@ParameterizedTest
13+
@MethodSource("cipherTestProvider")
14+
public void testAtbashCipher(String input, String expected) {
15+
AtbashCipher cipher = new AtbashCipher(input);
16+
assertEquals(expected, cipher.convert());
17+
}
1318

14-
normalToEncrypt.setString(normalToEncrypt.convert());
19+
private static Stream<Arguments> cipherTestProvider() {
20+
return Stream.of(
21+
// Basic tests with lowercase and uppercase
22+
Arguments.of("Hello", "Svool"), Arguments.of("WORLD", "DLIOW"),
1523

16-
assertEquals(expectedText, normalToEncrypt.getString());
17-
}
24+
// Mixed case with spaces and punctuation
25+
Arguments.of("Hello World!", "Svool Dliow!"), Arguments.of("123 ABC xyz", "123 ZYX cba"),
26+
27+
// Palindromes and mixed cases
28+
Arguments.of("madam", "nzwzn"), Arguments.of("Palindrome", "Kzormwilnv"),
29+
30+
// Non-alphabetic characters should remain unchanged
31+
Arguments.of("@cipher 123!", "@xrksvi 123!"), Arguments.of("no-change", "ml-xszmtv"),
1832

19-
@Test
20-
public void atbashDecrypt() {
21-
AtbashCipher encryptToNormal = new AtbashCipher("Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!");
22-
String expectedText = "Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!";
33+
// Empty string and single characters
34+
Arguments.of("", ""), Arguments.of("A", "Z"), Arguments.of("z", "a"),
2335

24-
encryptToNormal.setString(encryptToNormal.convert());
36+
// Numbers and symbols
37+
Arguments.of("!@#123", "!@#123"),
2538

26-
assertEquals(expectedText, encryptToNormal.getString());
39+
// Full sentence with uppercase, lowercase, symbols, and numbers
40+
Arguments.of("Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!", "Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!"),
41+
Arguments.of("Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!", "Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!"));
2742
}
2843
}

0 commit comments

Comments
 (0)