Skip to content

Commit 283b5f9

Browse files
authored
Update MonoAlphabeticTest.java
1 parent a31ae3b commit 283b5f9

File tree

1 file changed

+28
-46
lines changed

1 file changed

+28
-46
lines changed

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

+28-46
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,43 @@
22

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

5-
import org.junit.jupiter.api.BeforeEach;
6-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
76

8-
public class MonoAlphabeticTest {
9-
private MonoAlphabetic monoAlphabetic;
10-
11-
@BeforeEach
12-
public void setUp() {
13-
// Initialize the MonoAlphabetic cipher with a sample key
14-
String key = "QWERTYUIOPASDFGHJKLZXCVBNM"; // Example key
15-
monoAlphabetic = new MonoAlphabetic(key);
16-
}
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
1710

18-
@Test
19-
public void testEncrypt() {
20-
String plaintext = "HELLO";
21-
String expectedCiphertext = "ITSSG"; // Expected result based on the key
22-
String actualCiphertext = monoAlphabetic.encrypt(plaintext);
23-
24-
assertEquals(expectedCiphertext, actualCiphertext, "Encryption should match the expected ciphertext.");
25-
}
11+
public class MonoAlphabeticTest {
2612

27-
@Test
28-
public void testDecrypt() {
29-
String ciphertext = "ITSSG";
30-
String expectedPlaintext = "HELLO"; // Expected result based on the key
31-
String actualPlaintext = monoAlphabetic.decrypt(ciphertext);
13+
private static final String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ";
3214

33-
assertEquals(expectedPlaintext, actualPlaintext, "Decryption should match the expected plaintext.");
15+
// Test for encryption
16+
@ParameterizedTest
17+
@MethodSource("provideEncryptionData")
18+
public void testEncrypt(String data, String key, String expected) {
19+
assertEquals(expected, MonoAlphabetic.encrypt(data, key));
3420
}
3521

36-
@Test
37-
public void testEncryptAndDecrypt() {
38-
String plaintext = "HELLO";
39-
String ciphertext = monoAlphabetic.encrypt(plaintext);
40-
String decryptedText = monoAlphabetic.decrypt(ciphertext);
41-
42-
assertEquals(plaintext, decryptedText, "Decrypting the ciphertext should return the original plaintext.");
22+
// Test for decryption
23+
@ParameterizedTest
24+
@MethodSource("provideDecryptionData")
25+
public void testDecrypt(String data, String key, String expected) {
26+
assertEquals(expected, MonoAlphabetic.decrypt(data, key));
4327
}
4428

45-
@Test
46-
public void testEncryptWithSpecialCharacters() {
47-
String plaintext = "HELLO, WORLD!";
48-
String expectedCiphertext = "ITSSG, GQHSG!";
49-
String actualCiphertext = monoAlphabetic.encrypt(plaintext);
50-
51-
assertEquals(expectedCiphertext, actualCiphertext, "Encryption should correctly handle special characters.");
29+
// Provide test data for encryption
30+
private static Stream<Arguments> provideEncryptionData() {
31+
return Stream.of(
32+
Arguments.of("HELLO", key, "GFSSD"),
33+
Arguments.of("JAVA", key, "MZSM")
34+
);
5235
}
5336

54-
@Test
55-
public void testDecryptWithSpecialCharacters() {
56-
String ciphertext = "ITSSG, GQHSG!";
57-
String expectedPlaintext = "HELLO, WORLD!";
58-
String actualPlaintext = monoAlphabetic.decrypt(ciphertext);
59-
60-
assertEquals(expectedPlaintext, actualPlaintext, "Decryption should correctly handle special characters.");
37+
// Provide test data for decryption
38+
private static Stream<Arguments> provideDecryptionData() {
39+
return Stream.of(
40+
Arguments.of("GFSSD", key, "HELLO"),
41+
Arguments.of("MZSM", key, "JAVA")
42+
);
6143
}
6244
}

0 commit comments

Comments
 (0)