mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 06:55:02 +08:00
@ -10,30 +10,16 @@ public class HammingDistanceTest {
|
||||
@Test
|
||||
void testHammingDistance() throws Exception {
|
||||
assertEquals(HammingDistance.calculateHammingDistance("", ""), 0);
|
||||
assertEquals(
|
||||
HammingDistance.calculateHammingDistance("java", "java"),
|
||||
0
|
||||
);
|
||||
assertEquals(
|
||||
HammingDistance.calculateHammingDistance("karolin", "kathrin"),
|
||||
3
|
||||
);
|
||||
assertEquals(
|
||||
HammingDistance.calculateHammingDistance("kathrin", "kerstin"),
|
||||
4
|
||||
);
|
||||
assertEquals(
|
||||
HammingDistance.calculateHammingDistance("00000", "11111"),
|
||||
5
|
||||
);
|
||||
assertEquals(HammingDistance.calculateHammingDistance("java", "java"), 0);
|
||||
assertEquals(HammingDistance.calculateHammingDistance("karolin", "kathrin"), 3);
|
||||
assertEquals(HammingDistance.calculateHammingDistance("kathrin", "kerstin"), 4);
|
||||
assertEquals(HammingDistance.calculateHammingDistance("00000", "11111"), 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNotEqualStringLengths() {
|
||||
Exception exception = assertThrows(
|
||||
Exception.class,
|
||||
() -> HammingDistance.calculateHammingDistance("ab", "abc")
|
||||
);
|
||||
Exception.class, () -> HammingDistance.calculateHammingDistance("ab", "abc"));
|
||||
assertEquals("String lengths must be equal", exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HorspoolSearchTest {
|
||||
|
||||
@Test
|
||||
@ -76,13 +76,12 @@ class HorspoolSearchTest {
|
||||
|
||||
@Test
|
||||
void testFindFirstPatternNull() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> HorspoolSearch.findFirst(null, "Hello World"));
|
||||
assertThrows(
|
||||
NullPointerException.class, () -> HorspoolSearch.findFirst(null, "Hello World"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindFirstTextNull() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> HorspoolSearch.findFirst("Hello", null));
|
||||
assertThrows(NullPointerException.class, () -> HorspoolSearch.findFirst("Hello", null));
|
||||
}
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import java.util.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class LetterCombinationsOfPhoneNumberTest {
|
||||
@ -21,14 +22,14 @@ public class LetterCombinationsOfPhoneNumberTest {
|
||||
// ** Test 2 **
|
||||
// Input: digits = "2"
|
||||
// Output: ["a","b","c"]
|
||||
int[] numbers2 = { 2 };
|
||||
int[] numbers2 = {2};
|
||||
List<String> output2 = Arrays.asList("a", "b", "c");
|
||||
assertTrue(ob.printWords(numbers2, numbers2.length, 0, "").equals(output2));
|
||||
|
||||
// ** Test 3 **
|
||||
// Input: digits = "23"
|
||||
// Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
|
||||
int[] numbers3 = { 2, 3 };
|
||||
int[] numbers3 = {2, 3};
|
||||
List<String> output3 = Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf");
|
||||
assertTrue(ob.printWords(numbers3, numbers3.length, 0, "").equals(output3));
|
||||
|
||||
@ -37,10 +38,10 @@ public class LetterCombinationsOfPhoneNumberTest {
|
||||
// Output: ["adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi",
|
||||
// "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh",
|
||||
// "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"]
|
||||
int[] numbers4 = { 2, 3, 4 };
|
||||
List<String> output4 = Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg",
|
||||
"bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg",
|
||||
"cfh", "cfi");
|
||||
int[] numbers4 = {2, 3, 4};
|
||||
List<String> output4 = Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh",
|
||||
"afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh",
|
||||
"cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi");
|
||||
assertTrue(ob.printWords(numbers4, numbers4.length, 0, "").equals(output4));
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class LowerTest {
|
||||
@Test
|
||||
public void toLowerCase() {
|
||||
String input1 = "hello world";
|
||||
String input2 = "HelLO WoRld";
|
||||
String input3 = "HELLO WORLD";
|
||||
|
||||
|
||||
assertEquals("hello world", Lower.toLowerCase(input1));
|
||||
assertEquals("hello world", Lower.toLowerCase(input2));
|
||||
assertEquals("hello world", Lower.toLowerCase(input3));
|
||||
|
@ -1,26 +1,26 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class MyAtoiTest {
|
||||
|
||||
|
||||
@Test
|
||||
void testOne() {
|
||||
assertEquals(42, MyAtoi.myAtoi("42"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testTwo() {
|
||||
assertEquals(-42, MyAtoi.myAtoi(" -42"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testThree() {
|
||||
assertEquals(4193, MyAtoi.myAtoi("4193 with words"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testFour() {
|
||||
assertEquals(0, MyAtoi.myAtoi("0"));
|
||||
|
@ -8,19 +8,16 @@ public class PalindromeTest {
|
||||
@Test
|
||||
public void palindrome() {
|
||||
|
||||
String[] palindromes = { null, "", "aba", "123321", "kayak" };
|
||||
String[] palindromes = {null, "", "aba", "123321", "kayak"};
|
||||
for (String s : palindromes) {
|
||||
Assertions.assertTrue(Palindrome.isPalindrome(s) &&
|
||||
Palindrome.isPalindromeRecursion(s) &&
|
||||
Palindrome.isPalindromeTwoPointer(s));
|
||||
Assertions.assertTrue(Palindrome.isPalindrome(s) && Palindrome.isPalindromeRecursion(s)
|
||||
&& Palindrome.isPalindromeTwoPointer(s));
|
||||
}
|
||||
|
||||
String[] notPalindromes = { "abb", "abc", "abc123", "kayaks" };
|
||||
String[] notPalindromes = {"abb", "abc", "abc123", "kayaks"};
|
||||
for (String s : notPalindromes) {
|
||||
Assertions.assertFalse(Palindrome.isPalindrome(s) ||
|
||||
Palindrome.isPalindromeRecursion(s) ||
|
||||
Palindrome.isPalindromeTwoPointer(s));
|
||||
Assertions.assertFalse(Palindrome.isPalindrome(s) || Palindrome.isPalindromeRecursion(s)
|
||||
|| Palindrome.isPalindromeTwoPointer(s));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class PangramTest {
|
||||
@ -8,12 +9,14 @@ public class PangramTest {
|
||||
@Test
|
||||
public void testPangram() {
|
||||
assertTrue(Pangram.isPangram("The quick brown fox jumps over the lazy dog"));
|
||||
assertFalse(Pangram.isPangram("The quick brown fox jumps over the azy dog")); // L is missing
|
||||
assertFalse(
|
||||
Pangram.isPangram("The quick brown fox jumps over the azy dog")); // L is missing
|
||||
assertFalse(Pangram.isPangram("+-1234 This string is not alphabetical"));
|
||||
assertFalse(Pangram.isPangram("\u0000/\\ Invalid characters are alright too"));
|
||||
|
||||
|
||||
assertTrue(Pangram.isPangram2("The quick brown fox jumps over the lazy dog"));
|
||||
assertFalse(Pangram.isPangram2("The quick brown fox jumps over the azy dog")); // L is missing
|
||||
assertFalse(
|
||||
Pangram.isPangram2("The quick brown fox jumps over the azy dog")); // L is missing
|
||||
assertFalse(Pangram.isPangram2("+-1234 This string is not alphabetical"));
|
||||
assertFalse(Pangram.isPangram2("\u0000/\\ Invalid characters are alright too"));
|
||||
}
|
||||
|
@ -1,33 +1,33 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ReverseStringRecursiveTest {
|
||||
ReverseStringRecursive stringRecursive = new ReverseStringRecursive();
|
||||
|
||||
@Test
|
||||
void shouldAcceptWhenEmptyStringIsPassed() {
|
||||
String expected = "";
|
||||
String reversed = stringRecursive.reverse("");
|
||||
String reversed = stringRecursive.reverse("");
|
||||
|
||||
assertEquals(expected,reversed);
|
||||
assertEquals(expected, reversed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAcceptNotWhenWhenSingleCharacterIsPassed() {
|
||||
String expected = "a";
|
||||
String reversed = stringRecursive.reverse("a");
|
||||
String reversed = stringRecursive.reverse("a");
|
||||
|
||||
assertEquals(expected,reversed);
|
||||
assertEquals(expected, reversed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAcceptWhenStringIsPassed() {
|
||||
String expected = "dlroWolleH";
|
||||
String reversed = stringRecursive.reverse("HelloWorld");
|
||||
String reversed = stringRecursive.reverse("HelloWorld");
|
||||
|
||||
assertEquals(expected,reversed);
|
||||
assertEquals(expected, reversed);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ReverseStringTest {
|
||||
|
||||
@Test
|
||||
|
@ -11,5 +11,5 @@ public class RotationTest {
|
||||
assertEquals("eksge", Rotation.rotation("geeks", 2));
|
||||
assertEquals("anasban", Rotation.rotation("bananas", 3));
|
||||
assertEquals("abracadabra", Rotation.rotation("abracadabra", 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
package com.thealgorithms.strings;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
public class StringCompressionTest {
|
||||
@ParameterizedTest
|
||||
@CsvSource({"a,a","aabbb,a2b3","abbbc,ab3c","aabccd,a2bc2d"})
|
||||
void stringCompressionTest(String input,String expectedOutput){
|
||||
String output=StringCompression.compress(input);
|
||||
@CsvSource({"a,a", "aabbb,a2b3", "abbbc,ab3c", "aabccd,a2bc2d"})
|
||||
void stringCompressionTest(String input, String expectedOutput) {
|
||||
String output = StringCompression.compress(input);
|
||||
assertEquals(expectedOutput, output);
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,23 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ValidParenthesesTest {
|
||||
|
||||
|
||||
@Test
|
||||
void testOne() {
|
||||
assertEquals(true, ValidParentheses.isValid("()"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testTwo() {
|
||||
assertEquals(true, ValidParentheses.isValid("()[]{}"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testThree() {
|
||||
assertEquals(false, ValidParentheses.isValid("(]"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,15 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class WordLadderTest {
|
||||
|
||||
@Test
|
||||
public void testWordLadder() {
|
||||
|
||||
|
||||
/**
|
||||
* Test 1:
|
||||
* Input: beginWord = "hit", endWord = "cog", wordList =
|
||||
@ -33,6 +34,5 @@ public class WordLadderTest {
|
||||
|
||||
List<String> wordList2 = Arrays.asList("hot", "dot", "dog", "lot", "log");
|
||||
assertEquals(WordLadder.ladderLength("hit", "cog", wordList2), 0);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,13 +9,7 @@ public class longestNonRepeativeSubstringTest {
|
||||
public void palindrome() {
|
||||
String input1 = "HelloWorld";
|
||||
String input2 = "javaIsAProgrammingLanguage";
|
||||
Assertions.assertEquals(
|
||||
longestNonRepeativeSubstring.lengthOfLongestSubstring(input1),
|
||||
5
|
||||
);
|
||||
Assertions.assertEquals(
|
||||
longestNonRepeativeSubstring.lengthOfLongestSubstring(input2),
|
||||
9
|
||||
);
|
||||
Assertions.assertEquals(longestNonRepeativeSubstring.lengthOfLongestSubstring(input1), 5);
|
||||
Assertions.assertEquals(longestNonRepeativeSubstring.lengthOfLongestSubstring(input2), 9);
|
||||
}
|
||||
}
|
||||
|
@ -9,13 +9,7 @@ public class zigZagPatternTest {
|
||||
public void palindrome() {
|
||||
String input1 = "HelloWorldFromJava";
|
||||
String input2 = "javaIsAProgrammingLanguage";
|
||||
Assertions.assertEquals(
|
||||
zigZagPattern.encode(input1, 4),
|
||||
"HooeWrrmalolFJvlda"
|
||||
);
|
||||
Assertions.assertEquals(
|
||||
zigZagPattern.encode(input2, 4),
|
||||
"jAaLgasPrmgaaevIrgmnnuaoig"
|
||||
);
|
||||
Assertions.assertEquals(zigZagPattern.encode(input1, 4), "HooeWrrmalolFJvlda");
|
||||
Assertions.assertEquals(zigZagPattern.encode(input2, 4), "jAaLgasPrmgaaevIrgmnnuaoig");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user