Skip to content

Commit

Permalink
Add unit tests for Vigenere cipher (TheAlgorithms#3666)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreVelloso authored Oct 26, 2022
1 parent fd3386a commit 7ef7598
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/main/java/com/thealgorithms/ciphers/Vigenere.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
*/
public class Vigenere {

public static String encrypt(final String message, final String key) {
public String encrypt(final String message, final String key) {
StringBuilder result = new StringBuilder();

for (int i = 0, j = 0; i < message.length(); i++) {
int j = 0;
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (Character.isLetter(c)) {
if (Character.isUpperCase(c)) {
Expand Down Expand Up @@ -39,10 +40,11 @@ public static String encrypt(final String message, final String key) {
return result.toString();
}

public static String decrypt(final String message, final String key) {
public String decrypt(final String message, final String key) {
StringBuilder result = new StringBuilder();

for (int i = 0, j = 0; i < message.length(); i++) {
int j = 0;
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (Character.isLetter(c)) {
if (Character.isUpperCase(c)) {
Expand All @@ -66,13 +68,4 @@ public static String decrypt(final String message, final String key) {
}
return result.toString();
}

public static void main(String[] args) {
String text = "Hello World!";
String key = "itsakey";
System.out.println(text);
String ciphertext = encrypt(text, key);
System.out.println(ciphertext);
System.out.println(decrypt(ciphertext, key));
}
}
37 changes: 37 additions & 0 deletions src/test/java/com/thealgorithms/ciphers/VigenereTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.thealgorithms.ciphers;

import org.junit.jupiter.api.Test;

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

class VigenereTest {

Vigenere vigenere = new Vigenere();

@Test
void vigenereEncryptTest() {
// given
String text = "Hello World!";
String key = "suchsecret";

// when
String cipherText = vigenere.encrypt(text, key);

// then
assertEquals("Zynsg Yfvev!", cipherText);
}

@Test
void vigenereDecryptTest() {
// given
String encryptedText = "Zynsg Yfvev!";
String key = "suchsecret";

// when
String decryptedText = vigenere.decrypt(encryptedText, key);

// then
assertEquals("Hello World!", decryptedText);
}

}

0 comments on commit 7ef7598

Please sign in to comment.