Strings
Ciphers
Fundamentals
Cryptography
Algorithms
import java.util.List; import java.util.stream.Collectors; public class Kata { // <<-CONSTANTS->> private static final int UpperCase_LOWER_LIMIT = 65; private static final int UpperCase_UPPER_LIMIT = 90; private static final int LowerCase_LOWER_LIMIT = 97; private static final int LowerCase_UPPER_LIMIT = 122; private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1; // <<-METHODS->> private static int getLastDigit(final int digit) { String str = String.valueOf(digit); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } private static List<Integer> getEncryptationOffsets(String key) { return key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); } private static List<Integer> getPhraseChars(String phrase) { return phrase.chars() .boxed() .collect(Collectors.toList()); } private static String parseChars(List<Integer> translation) { return translation.stream() .map(n -> (char) n.intValue()) .map(c -> Character.toString(c)) .collect(Collectors.joining()); } public static String encode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> translation = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current + offsets.get(keyIndex); if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) { int offset = result - UpperCase_UPPER_LIMIT; result = LowerCase_LOWER_LIMIT + offset - 1; } if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) { result += INTERMEDIATE_OFFSET; } else if (result > LowerCase_UPPER_LIMIT) { result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1; } translation.add(result); keyIndex++; } return parseChars(translation); } public static String decode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> translation = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current - offsets.get(keyIndex); if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) { int offset = LowerCase_LOWER_LIMIT - result; result = UpperCase_UPPER_LIMIT - offset + 1; } if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) { result -= INTERMEDIATE_OFFSET; } else if (result < UpperCase_LOWER_LIMIT) { int offset = UpperCase_LOWER_LIMIT - result; result = LowerCase_UPPER_LIMIT - offset + 1; } translation.add(result); keyIndex++; } return parseChars(translation); } }
- import java.util.List;
- import java.util.stream.Collectors;
- public class Kata {
- // <<-CONSTANTS->>
- private static final int UpperCase_LOWER_LIMIT = 65;
- private static final int UpperCase_UPPER_LIMIT = 90;
- private static final int LowerCase_LOWER_LIMIT = 97;
- private static final int LowerCase_UPPER_LIMIT = 122;
- private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
- // <<-METHODS->>
- private static int getLastDigit(final int digit) {
- String str = String.valueOf(digit);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
- private static List<Integer> getEncryptationOffsets(String key) {
- return key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- }
- private static List<Integer> getPhraseChars(String phrase) {
- return phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- }
private static String parseChars(List<Integer> traduction) {return traduction.stream()- private static String parseChars(List<Integer> translation) {
- return translation.stream()
- .map(n -> (char) n.intValue())
- .map(c -> Character.toString(c))
- .collect(Collectors.joining());
- }
- public static String encode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
List<Integer> traduction = new java.util.ArrayList<>();- List<Integer> translation = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current + offsets.get(keyIndex);
- if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
- int offset = result - UpperCase_UPPER_LIMIT;
- result = LowerCase_LOWER_LIMIT + offset - 1;
- }
- if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
- result += INTERMEDIATE_OFFSET;
- } else if (result > LowerCase_UPPER_LIMIT) {
- result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
- }
traduction.add(result);- translation.add(result);
- keyIndex++;
- }
return parseChars(traduction);- return parseChars(translation);
- }
- public static String decode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
List<Integer> traduction = new java.util.ArrayList<>();- List<Integer> translation = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current - offsets.get(keyIndex);
- if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
- int offset = LowerCase_LOWER_LIMIT - result;
- result = UpperCase_UPPER_LIMIT - offset + 1;
- }
- if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
- result -= INTERMEDIATE_OFFSET;
- } else if (result < UpperCase_LOWER_LIMIT) {
- int offset = UpperCase_LOWER_LIMIT - result;
- result = LowerCase_UPPER_LIMIT - offset + 1;
- }
traduction.add(result);- translation.add(result);
- keyIndex++;
- }
return parseChars(traduction);- return parseChars(translation);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.util.List; import java.util.Random; import java.util.stream.Collectors; class SolutionTest { @Test void basicTest() { assertEquals("moqik", Kata.encode("abcde", "abcde")); assertEquals("abcde", Kata.decode("moqik", "abcde")); assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE")); assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE")); assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH")); assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH")); assertEquals("ikhwmvi", Kata.encode("deargod", "fgh")); assertEquals("deargod", Kata.decode("ikhwmvi", "fgh")); assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME")); assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME")); } @Test void specialCasesTest() { assertEquals("", Kata.encode("", "")); assertEquals("TernaryLove", Kata.encode("TernaryLove", "")); assertEquals("", Kata.encode(null, "abcde")); assertEquals("", Kata.encode("", null)); } @Test void fullTestSimpleOverflow() { assertEquals("CEEpB", Kata.encode("turbo", "TUXON")); assertEquals("turbo", Kata.decode("CEEpB", "TUXON")); assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh")); assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh")); assertEquals("IKM", Kata.encode("xyz", "ijklmn")); assertEquals("xyz", Kata.decode("IKM", "ijklmn")); assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA")); assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA")); assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID")); assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID")); } @Test void fullTestIntermediateOverflow() { assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT")); assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT")); assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS")); assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS")); assertEquals("lyynmpA", Kata.encode("Zumbido", "wow")); assertEquals("Zumbido", Kata.decode("lyynmpA", "wow")); } String generateString(int bound) { Random random = new Random(); byte[] buffer = new byte[random.nextInt(bound + 1)]; random.nextBytes(buffer); return new String(buffer, java.nio.charset.Charset.forName("UTF-8")); } String generatePhrase(int bound) { return generateString(bound); } String generateKey(int bound) { return generateString(bound); } @RepeatedTest(1500) void randomTestEasyMode() { String phrase = generatePhrase(1500); String key = generateKey(1500); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } @RepeatedTest(1000) void randomTestHardMode() { String phrase = generatePhrase(15000); String key = generateKey(15000); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } @RepeatedTest(101) void randomTestGodMode() { String phrase = generatePhrase(150000); String key = generateKey(150000); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } // [[ A N S W E R ]] // <<-CONSTANTS->> private static final int UpperCase_LOWER_LIMIT = 65; private static final int UpperCase_UPPER_LIMIT = 90; private static final int LowerCase_LOWER_LIMIT = 97; private static final int LowerCase_UPPER_LIMIT = 122; private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1; // <<-METHODS->> private static int getLastDigit(final int digit) { String str = String.valueOf(digit); str = str.substring(str.length() - 1, str.length()); return Integer.valueOf(str); } private static List<Integer> getEncryptationOffsets(String key) { return key.chars() .map(n -> getLastDigit(n) + key.length()) .boxed() .toList(); } private static List<Integer> getPhraseChars(String phrase) { return phrase.chars() .boxed() .collect(Collectors.toList()); } private static String parseChars(List<Integer> translation) { return translation.stream() .map(n -> (char) n.intValue()) .map(c -> Character.toString(c)) .collect(Collectors.joining()); } public static String encode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> translation = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current + offsets.get(keyIndex); if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) { int offset = result - UpperCase_UPPER_LIMIT; result = LowerCase_LOWER_LIMIT + offset - 1; } if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) { result += INTERMEDIATE_OFFSET; } else if (result > LowerCase_UPPER_LIMIT) { result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1; } translation.add(result); keyIndex++; } return parseChars(translation); } public static String decode(String phrase, String key) { if (phrase == null || phrase.isEmpty() || key == null) return ""; if (key.isEmpty()) return phrase; List<Integer> offsets = getEncryptationOffsets(key); List<Integer> phraseChars = getPhraseChars(phrase); List<Integer> translation = new java.util.ArrayList<>(); int keyIndex = 0; for (int i = 0; i < phrase.length(); i++) { keyIndex = i % offsets.size(); int current = phraseChars.get(i); int result = current - offsets.get(keyIndex); if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) { int offset = LowerCase_LOWER_LIMIT - result; result = UpperCase_UPPER_LIMIT - offset + 1; } if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) { result -= INTERMEDIATE_OFFSET; } else if (result < UpperCase_LOWER_LIMIT) { int offset = UpperCase_LOWER_LIMIT - result; result = LowerCase_UPPER_LIMIT - offset + 1; } translation.add(result); keyIndex++; } return parseChars(translation); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.util.List;
- import java.util.Random;
- import java.util.stream.Collectors;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals("moqik", Kata.encode("abcde", "abcde"));
- assertEquals("abcde", Kata.decode("moqik", "abcde"));
- assertEquals("KMOQS", Kata.encode("ABCDE", "ABCDE"));
- assertEquals("ABCDE", Kata.decode("KMOQS", "ABCDE"));
- assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH"));
- assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH"));
- assertEquals("ikhwmvi", Kata.encode("deargod", "fgh"));
- assertEquals("deargod", Kata.decode("ikhwmvi", "fgh"));
- assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME"));
- assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME"));
- }
- @Test
- void specialCasesTest() {
- assertEquals("", Kata.encode("", ""));
- assertEquals("TernaryLove", Kata.encode("TernaryLove", ""));
- assertEquals("", Kata.encode(null, "abcde"));
- assertEquals("", Kata.encode("", null));
- }
- @Test
- void fullTestSimpleOverflow() {
- assertEquals("CEEpB", Kata.encode("turbo", "TUXON"));
- assertEquals("turbo", Kata.decode("CEEpB", "TUXON"));
- assertEquals("skstzCvzwj", Kata.encode("helloworld", "afgh"));
- assertEquals("helloworld", Kata.decode("skstzCvzwj", "afgh"));
- assertEquals("IKM", Kata.encode("xyz", "ijklmn"));
- assertEquals("xyz", Kata.decode("IKM", "ijklmn"));
- assertEquals("qUlWuZXovQ", Kata.encode("gOdMoRNinG", "uSA"));
- assertEquals("gOdMoRNinG", Kata.decode("qUlWuZXovQ", "uSA"));
- assertEquals("BOywIJdrzafbLMKuCnVqNt", Kata.encode("wElovETerNaRyExpsaNdIj", "dAvID"));
- assertEquals("wElovETerNaRyExpsaNdIj", Kata.decode("BOywIJdrzafbLMKuCnVqNt", "dAvID"));
- }
- @Test
- void fullTestIntermediateOverflow() {
- assertEquals("rhCRDjxXEVyfu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT"));
- assertEquals("hOmEsWeEtHoMe", Kata.decode("rhCRDjxXEVyfu", "nOtSoSwEeT"));
- assertEquals("cyvNMxgiVt", Kata.encode("WonDErWaLl", "oASiS"));
- assertEquals("WonDErWaLl", Kata.decode("cyvNMxgiVt", "oASiS"));
- assertEquals("lyynmpA", Kata.encode("Zumbido", "wow"));
- assertEquals("Zumbido", Kata.decode("lyynmpA", "wow"));
- }
- String generateString(int bound) {
- Random random = new Random();
- byte[] buffer = new byte[random.nextInt(bound + 1)];
- random.nextBytes(buffer);
- return new String(buffer, java.nio.charset.Charset.forName("UTF-8"));
- }
- String generatePhrase(int bound) {
- return generateString(bound);
- }
- String generateKey(int bound) {
- return generateString(bound);
- }
- @RepeatedTest(1500)
- void randomTestEasyMode() {
- String phrase = generatePhrase(1500);
- String key = generateKey(1500);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- @RepeatedTest(1000)
- void randomTestHardMode() {
- String phrase = generatePhrase(15000);
- String key = generateKey(15000);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- @RepeatedTest(101)
- void randomTestGodMode() {
- String phrase = generatePhrase(150000);
- String key = generateKey(150000);
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- // [[ A N S W E R ]]
- // <<-CONSTANTS->>
- private static final int UpperCase_LOWER_LIMIT = 65;
- private static final int UpperCase_UPPER_LIMIT = 90;
- private static final int LowerCase_LOWER_LIMIT = 97;
- private static final int LowerCase_UPPER_LIMIT = 122;
- private static final int INTERMEDIATE_OFFSET = LowerCase_LOWER_LIMIT - UpperCase_UPPER_LIMIT - 1;
- // <<-METHODS->>
- private static int getLastDigit(final int digit) {
- String str = String.valueOf(digit);
- str = str.substring(str.length() - 1, str.length());
- return Integer.valueOf(str);
- }
- private static List<Integer> getEncryptationOffsets(String key) {
- return key.chars()
- .map(n -> getLastDigit(n) + key.length())
- .boxed()
- .toList();
- }
- private static List<Integer> getPhraseChars(String phrase) {
- return phrase.chars()
- .boxed()
- .collect(Collectors.toList());
- }
private static String parseChars(List<Integer> traduction) {return traduction.stream()- private static String parseChars(List<Integer> translation) {
- return translation.stream()
- .map(n -> (char) n.intValue())
- .map(c -> Character.toString(c))
- .collect(Collectors.joining());
- }
- public static String encode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
List<Integer> traduction = new java.util.ArrayList<>();- List<Integer> translation = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current + offsets.get(keyIndex);
- if (current <= UpperCase_UPPER_LIMIT && result >= LowerCase_LOWER_LIMIT) {
- int offset = result - UpperCase_UPPER_LIMIT;
- result = LowerCase_LOWER_LIMIT + offset - 1;
- }
- if (result > UpperCase_UPPER_LIMIT && result < LowerCase_LOWER_LIMIT) {
- result += INTERMEDIATE_OFFSET;
- } else if (result > LowerCase_UPPER_LIMIT) {
- result = result - LowerCase_UPPER_LIMIT + UpperCase_LOWER_LIMIT - 1;
- }
traduction.add(result);- translation.add(result);
- keyIndex++;
- }
return parseChars(traduction);- return parseChars(translation);
- }
- public static String decode(String phrase, String key) {
- if (phrase == null || phrase.isEmpty() || key == null) return "";
- if (key.isEmpty()) return phrase;
- List<Integer> offsets = getEncryptationOffsets(key);
- List<Integer> phraseChars = getPhraseChars(phrase);
List<Integer> traduction = new java.util.ArrayList<>();- List<Integer> translation = new java.util.ArrayList<>();
- int keyIndex = 0;
- for (int i = 0; i < phrase.length(); i++) {
- keyIndex = i % offsets.size();
- int current = phraseChars.get(i);
- int result = current - offsets.get(keyIndex);
- if (current >= LowerCase_LOWER_LIMIT && result <= UpperCase_UPPER_LIMIT) {
- int offset = LowerCase_LOWER_LIMIT - result;
- result = UpperCase_UPPER_LIMIT - offset + 1;
- }
- if (result < LowerCase_LOWER_LIMIT && result > UpperCase_UPPER_LIMIT) {
- result -= INTERMEDIATE_OFFSET;
- } else if (result < UpperCase_LOWER_LIMIT) {
- int offset = UpperCase_LOWER_LIMIT - result;
- result = LowerCase_UPPER_LIMIT - offset + 1;
- }
traduction.add(result);- translation.add(result);
- keyIndex++;
- }
return parseChars(traduction);- return parseChars(translation);
- }
Strings
Ciphers
Fundamentals
Cryptography
Algorithms
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.util.Random; class SolutionTest { @Test void basicTest() { assertEquals("moqik", Kata.encode("abcde", "abcde")); assertEquals("abcde", Kata.decode("moqik", "abcde")); assertEquals("KMOUS", Kata.encode("ABCDE", "ABCDE")); assertEquals("ABCDE", Kata.decode("KMOUS", "ABCDE")); assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH")); assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH")); assertEquals("ikhwmvi", Kata.encode("deargod", "fgh")); assertEquals("deargod", Kata.decode("ikhwmvi", "fgh")); assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME")); assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME")); } @Test void fullTest() { assertEquals("CDEfA", Kata.encode("turbo", "TUXON")); assertEquals("turbo", Kata.decode("CDEfA", "TUXON")); assertEquals("skstzDvzwj", Kata.encode("helloworld", "afgh")); assertEquals("helloworld", Kata.decode("skstzDvzwj", "afgh")); assertEquals("JLN", Kata.encode("xyz", "ijklmn")); assertEquals("xyz", Kata.decode("JLN", "ijklmn")); assertEquals("riDREkxXFVygu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT")); assertEquals("hOmEsWeEtHoMe", Kata.decode("riDREkxXFVygu", "nOtSoSwEeT")); } private String encode(String phrase, String key) { return ""; } private String decode(String phrase, String key) { return ""; } String generateString() { Random random = new Random(); byte[] buffer = new byte[random.nextInt(1501)]; random.nextBytes(buffer); return new String(buffer, java.nio.charset.Charset.forName("UTF-8")); } String generatePhrase() { return generateString(); } String generateKey() { return generateString(); } @RepeatedTest(100) void randomTest() { String phrase = generatePhrase(); String key = generateKey(); assertEquals(Kata.encode(phrase, key), encode(phrase, key)); assertEquals(Kata.decode(phrase, key), decode(phrase, key)); } }
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
import java.nio.charset.Charset;- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals("moqik", Kata.encode("abcde", "abcde"));
- assertEquals("abcde", Kata.decode("moqik", "abcde"));
- assertEquals("KMOUS", Kata.encode("ABCDE", "ABCDE"));
- assertEquals("ABCDE", Kata.decode("KMOUS", "ABCDE"));
- assertEquals("GIFUKTG", Kata.encode("DEARGOD", "FGH"));
- assertEquals("DEARGOD", Kata.decode("GIFUKTG", "FGH"));
- assertEquals("ikhwmvi", Kata.encode("deargod", "fgh"));
- assertEquals("deargod", Kata.decode("ikhwmvi", "fgh"));
- assertEquals("IQlOirL", Kata.encode("ALaBamA", "hoME"));
- assertEquals("ALaBamA", Kata.decode("IQlOirL", "hoME"));
- }
- @Test
- void fullTest() {
assertEquals("skstzDvzwj", Kata.encode("helloworld", "afgh"));assertEquals("helloworld", Kata.decode("skstzDvzwj", "afgh"));- assertEquals("CDEfA", Kata.encode("turbo", "TUXON"));
- assertEquals("turbo", Kata.decode("CDEfA", "TUXON"));
- assertEquals("skstzDvzwj", Kata.encode("helloworld", "afgh"));
- assertEquals("helloworld", Kata.decode("skstzDvzwj", "afgh"));
- assertEquals("JLN", Kata.encode("xyz", "ijklmn"));
- assertEquals("xyz", Kata.decode("JLN", "ijklmn"));
- assertEquals("riDREkxXFVygu", Kata.encode("hOmEsWeEtHoMe", "nOtSoSwEeT"));
- assertEquals("hOmEsWeEtHoMe", Kata.decode("riDREkxXFVygu", "nOtSoSwEeT"));
- }
- private String encode(String phrase, String key) {
- return "";
- }
- private String decode(String phrase, String key) {
- return "";
- }
- String generateString() {
- Random random = new Random();
- byte[] buffer = new byte[random.nextInt(1501)];
- random.nextBytes(buffer);
return new String(buffer, Charset.forName("UTF-8"));- return new String(buffer, java.nio.charset.Charset.forName("UTF-8"));
- }
- String generatePhrase() {
- return generateString();
- }
- String generateKey() {
- return generateString();
- }
- @RepeatedTest(100)
- void randomTest() {
- String phrase = generatePhrase();
- String key = generateKey();
- assertEquals(Kata.encode(phrase, key), encode(phrase, key));
- assertEquals(Kata.decode(phrase, key), decode(phrase, key));
- }
- }