mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
Refactor AtbashCipher
, add ParameterizedTest
(#5808)
This commit is contained in:
@ -2,27 +2,42 @@ package com.thealgorithms.ciphers;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
public class AtbashTest {
|
||||
|
||||
@Test
|
||||
public void atbashEncrypt() {
|
||||
AtbashCipher normalToEncrypt = new AtbashCipher("Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!");
|
||||
String expectedText = "Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!";
|
||||
|
||||
normalToEncrypt.setString(normalToEncrypt.convert());
|
||||
|
||||
assertEquals(expectedText, normalToEncrypt.getString());
|
||||
@ParameterizedTest
|
||||
@MethodSource("cipherTestProvider")
|
||||
public void testAtbashCipher(String input, String expected) {
|
||||
AtbashCipher cipher = new AtbashCipher(input);
|
||||
assertEquals(expected, cipher.convert());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void atbashDecrypt() {
|
||||
AtbashCipher encryptToNormal = new AtbashCipher("Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!");
|
||||
String expectedText = "Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!";
|
||||
private static Stream<Arguments> cipherTestProvider() {
|
||||
return Stream.of(
|
||||
// Basic tests with lowercase and uppercase
|
||||
Arguments.of("Hello", "Svool"), Arguments.of("WORLD", "DLIOW"),
|
||||
|
||||
encryptToNormal.setString(encryptToNormal.convert());
|
||||
// Mixed case with spaces and punctuation
|
||||
Arguments.of("Hello World!", "Svool Dliow!"), Arguments.of("123 ABC xyz", "123 ZYX cba"),
|
||||
|
||||
assertEquals(expectedText, encryptToNormal.getString());
|
||||
// Palindromes and mixed cases
|
||||
Arguments.of("madam", "nzwzn"), Arguments.of("Palindrome", "Kzormwilnv"),
|
||||
|
||||
// Non-alphabetic characters should remain unchanged
|
||||
Arguments.of("@cipher 123!", "@xrksvi 123!"), Arguments.of("no-change", "ml-xszmtv"),
|
||||
|
||||
// Empty string and single characters
|
||||
Arguments.of("", ""), Arguments.of("A", "Z"), Arguments.of("z", "a"),
|
||||
|
||||
// Numbers and symbols
|
||||
Arguments.of("!@#123", "!@#123"),
|
||||
|
||||
// Full sentence with uppercase, lowercase, symbols, and numbers
|
||||
Arguments.of("Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!", "Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!"),
|
||||
Arguments.of("Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!", "Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!"));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user