Skip to content

Commit 7ef7598

Browse files
Add unit tests for Vigenere cipher (TheAlgorithms#3666)
1 parent fd3386a commit 7ef7598

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
*/
99
public class Vigenere {
1010

11-
public static String encrypt(final String message, final String key) {
11+
public String encrypt(final String message, final String key) {
1212
StringBuilder result = new StringBuilder();
1313

14-
for (int i = 0, j = 0; i < message.length(); i++) {
14+
int j = 0;
15+
for (int i = 0; i < message.length(); i++) {
1516
char c = message.charAt(i);
1617
if (Character.isLetter(c)) {
1718
if (Character.isUpperCase(c)) {
@@ -39,10 +40,11 @@ public static String encrypt(final String message, final String key) {
3940
return result.toString();
4041
}
4142

42-
public static String decrypt(final String message, final String key) {
43+
public String decrypt(final String message, final String key) {
4344
StringBuilder result = new StringBuilder();
4445

45-
for (int i = 0, j = 0; i < message.length(); i++) {
46+
int j = 0;
47+
for (int i = 0; i < message.length(); i++) {
4648
char c = message.charAt(i);
4749
if (Character.isLetter(c)) {
4850
if (Character.isUpperCase(c)) {
@@ -66,13 +68,4 @@ public static String decrypt(final String message, final String key) {
6668
}
6769
return result.toString();
6870
}
69-
70-
public static void main(String[] args) {
71-
String text = "Hello World!";
72-
String key = "itsakey";
73-
System.out.println(text);
74-
String ciphertext = encrypt(text, key);
75-
System.out.println(ciphertext);
76-
System.out.println(decrypt(ciphertext, key));
77-
}
7871
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
class VigenereTest {
8+
9+
Vigenere vigenere = new Vigenere();
10+
11+
@Test
12+
void vigenereEncryptTest() {
13+
// given
14+
String text = "Hello World!";
15+
String key = "suchsecret";
16+
17+
// when
18+
String cipherText = vigenere.encrypt(text, key);
19+
20+
// then
21+
assertEquals("Zynsg Yfvev!", cipherText);
22+
}
23+
24+
@Test
25+
void vigenereDecryptTest() {
26+
// given
27+
String encryptedText = "Zynsg Yfvev!";
28+
String key = "suchsecret";
29+
30+
// when
31+
String decryptedText = vigenere.decrypt(encryptedText, key);
32+
33+
// then
34+
assertEquals("Hello World!", decryptedText);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)