mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 06:55:02 +08:00
Code cleanup (#4246)
This commit is contained in:
@ -3,7 +3,6 @@ package com.thealgorithms.datastructures.hashmap;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.thealgorithms.datastructures.hashmap.hashing.HashMapCuckooHashing;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HashMapCuckooHashingTest {
|
||||
@ -51,7 +50,6 @@ class HashMapCuckooHashingTest {
|
||||
@Test
|
||||
void removeNone() {
|
||||
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
|
||||
int initialSize = hashTable.getNumberOfKeysInTable();
|
||||
try {
|
||||
hashTable.deleteKeyFromHashTable(3);
|
||||
} catch (Exception e) {
|
||||
@ -90,11 +88,4 @@ class HashMapCuckooHashingTest {
|
||||
assertTrue(hashTable.checkTableContainsKey(10));
|
||||
assertTrue(hashTable.checkTableContainsKey(100));
|
||||
}
|
||||
|
||||
private HashMapCuckooHashing createHashMapCuckooHashing() {
|
||||
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
|
||||
int[] values = {11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222};
|
||||
Arrays.stream(values).forEach(hashTable::insertKey2HashTable);
|
||||
return hashTable;
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package com.thealgorithms.datastructures.hashmap.hashing;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.thealgorithms.datastructures.hashmap.hashing.MajorityElement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
@ -3,9 +3,7 @@ package com.thealgorithms.io;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class BufferedReaderTest {
|
||||
|
@ -8,7 +8,6 @@ public class FactorialTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Factorial fact = new Factorial();
|
||||
assertEquals(120, fact.factorial(5));
|
||||
assertEquals(120, Factorial.factorial(5));
|
||||
}
|
||||
}
|
||||
|
@ -10,22 +10,22 @@ class SumOfDigitsTest {
|
||||
|
||||
@Test
|
||||
void testZero() {
|
||||
assertEquals(0, SoD.sumOfDigits(0));
|
||||
assertEquals(0, SoD.sumOfDigitsRecursion(0));
|
||||
assertEquals(0, SoD.sumOfDigitsFast(0));
|
||||
assertEquals(0, SumOfDigits.sumOfDigits(0));
|
||||
assertEquals(0, SumOfDigits.sumOfDigitsRecursion(0));
|
||||
assertEquals(0, SumOfDigits.sumOfDigitsFast(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPositive() {
|
||||
assertEquals(15, SoD.sumOfDigits(12345));
|
||||
assertEquals(15, SoD.sumOfDigitsRecursion(12345));
|
||||
assertEquals(15, SoD.sumOfDigitsFast(12345));
|
||||
assertEquals(15, SumOfDigits.sumOfDigits(12345));
|
||||
assertEquals(15, SumOfDigits.sumOfDigitsRecursion(12345));
|
||||
assertEquals(15, SumOfDigits.sumOfDigitsFast(12345));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNegative() {
|
||||
assertEquals(6, SoD.sumOfDigits(-123));
|
||||
assertEquals(6, SoD.sumOfDigitsRecursion(-123));
|
||||
assertEquals(6, SoD.sumOfDigitsFast(-123));
|
||||
assertEquals(6, SumOfDigits.sumOfDigits(-123));
|
||||
assertEquals(6, SumOfDigits.sumOfDigitsRecursion(-123));
|
||||
assertEquals(6, SumOfDigits.sumOfDigitsFast(-123));
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ class CRC16Test {
|
||||
String textToCRC16 = "hacktoberfest!";
|
||||
|
||||
// when
|
||||
String resultCRC16 = crc.crc16(textToCRC16); // Algorithm CRC16-CCITT-FALSE
|
||||
String resultCRC16 = CRC16.crc16(textToCRC16); // Algorithm CRC16-CCITT-FALSE
|
||||
|
||||
// then
|
||||
assertEquals("10FC", resultCRC16);
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.thealgorithms.others.cn;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.assertj.core.api.ThrowableTypeAssert;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class HammingDistanceTest {
|
||||
@ -42,14 +40,14 @@ public class HammingDistanceTest {
|
||||
|
||||
@Test
|
||||
public void mismatchDataBits() {
|
||||
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("100010", "00011"); });
|
||||
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("100010", "00011"); });
|
||||
|
||||
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mismatchDataBits2() {
|
||||
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("1", "11"); });
|
||||
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("1", "11"); });
|
||||
|
||||
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
|
||||
}
|
||||
@ -77,7 +75,7 @@ public class HammingDistanceTest {
|
||||
|
||||
@Test
|
||||
public void computeThrowsExceptionWhenInputsAreNotBitStrs() {
|
||||
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { int answer = HammingDistance.compute("1A", "11"); });
|
||||
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("1A", "11"); });
|
||||
|
||||
Assertions.assertThat(ex.getMessage()).contains("must be a binary string");
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BinarySearch2dArrayTest {
|
||||
@ -16,7 +15,6 @@ public class BinarySearch2dArrayTest {
|
||||
int target = 6;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {1, 1};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(1, ans[0]);
|
||||
assertEquals(1, ans[1]);
|
||||
@ -29,7 +27,6 @@ public class BinarySearch2dArrayTest {
|
||||
int target = 8;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {1, 3};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(1, ans[0]);
|
||||
assertEquals(3, ans[1]);
|
||||
@ -42,7 +39,6 @@ public class BinarySearch2dArrayTest {
|
||||
int target = 2;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {0, 1};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(0, ans[0]);
|
||||
assertEquals(1, ans[1]);
|
||||
@ -55,7 +51,6 @@ public class BinarySearch2dArrayTest {
|
||||
int target = 1;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {0, 0};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(0, ans[0]);
|
||||
assertEquals(0, ans[1]);
|
||||
@ -68,7 +63,6 @@ public class BinarySearch2dArrayTest {
|
||||
int target = 10;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {2, 1};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(2, ans[0]);
|
||||
assertEquals(1, ans[1]);
|
||||
@ -81,7 +75,6 @@ public class BinarySearch2dArrayTest {
|
||||
int target = 11;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {2, 2};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(2, ans[0]);
|
||||
assertEquals(2, ans[1]);
|
||||
@ -94,7 +87,6 @@ public class BinarySearch2dArrayTest {
|
||||
int target = 101;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {-1, -1};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(-1, ans[0]);
|
||||
assertEquals(-1, ans[1]);
|
||||
|
@ -2,8 +2,6 @@ package com.thealgorithms.searches;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.thealgorithms.searches.OrderAgnosticBinarySearch;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class OrderAgnosticBinarySearchTest {
|
||||
|
@ -2,7 +2,6 @@ package com.thealgorithms.sorts;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -10,12 +10,10 @@ import org.junit.jupiter.api.Test;
|
||||
*/
|
||||
|
||||
public class OddEvenSortTest {
|
||||
private OddEvenSort oddEvenSort = new OddEvenSort();
|
||||
|
||||
@Test
|
||||
public void oddEvenSortEmptyArray() {
|
||||
int[] inputArray = {};
|
||||
oddEvenSort.oddEvenSort(inputArray);
|
||||
OddEvenSort.oddEvenSort(inputArray);
|
||||
int[] expectedOutput = {};
|
||||
assertArrayEquals(inputArray, expectedOutput);
|
||||
}
|
||||
@ -23,7 +21,7 @@ public class OddEvenSortTest {
|
||||
@Test
|
||||
public void oddEvenSortNaturalNumberArray() {
|
||||
int[] inputArray = {18, 91, 86, 60, 21, 44, 37, 78, 98, 67};
|
||||
oddEvenSort.oddEvenSort(inputArray);
|
||||
OddEvenSort.oddEvenSort(inputArray);
|
||||
int[] expectedOutput = {18, 21, 37, 44, 60, 67, 78, 86, 91, 98};
|
||||
assertArrayEquals(inputArray, expectedOutput);
|
||||
}
|
||||
@ -31,7 +29,7 @@ public class OddEvenSortTest {
|
||||
@Test
|
||||
public void oddEvenSortIntegerArray() {
|
||||
int[] inputArray = {57, 69, -45, 12, -85, 3, -76, 36, 67, -14};
|
||||
oddEvenSort.oddEvenSort(inputArray);
|
||||
OddEvenSort.oddEvenSort(inputArray);
|
||||
int[] expectedOutput = {-85, -76, -45, -14, 3, 12, 36, 57, 67, 69};
|
||||
assertArrayEquals(inputArray, expectedOutput);
|
||||
}
|
||||
|
@ -3,9 +3,7 @@ package com.thealgorithms.sorts;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public abstract class SortingAlgorithmTest {
|
||||
|
@ -1,10 +1,5 @@
|
||||
package com.thealgorithms.sorts;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class TimSortTest extends SortingAlgorithmTest {
|
||||
@Override
|
||||
SortAlgorithm getSortAlgorithm() {
|
||||
|
@ -2,7 +2,6 @@ package com.thealgorithms.strings;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class IsomorphicTest {
|
||||
@ -21,11 +20,9 @@ public class IsomorphicTest {
|
||||
String str7 = "aaammmnnn";
|
||||
String str8 = "ggghhhbbj";
|
||||
|
||||
Isomorphic isomorphic = new Isomorphic();
|
||||
|
||||
assertTrue(isomorphic.checkStrings(str1, str2));
|
||||
assertTrue(isomorphic.checkStrings(str3, str4));
|
||||
assertFalse(isomorphic.checkStrings(str5, str6));
|
||||
assertFalse(isomorphic.checkStrings(str7, str8));
|
||||
assertTrue(Isomorphic.checkStrings(str1, str2));
|
||||
assertTrue(Isomorphic.checkStrings(str3, str4));
|
||||
assertFalse(Isomorphic.checkStrings(str5, str6));
|
||||
assertFalse(Isomorphic.checkStrings(str7, str8));
|
||||
}
|
||||
}
|
||||
|
@ -9,29 +9,28 @@ public class LetterCombinationsOfPhoneNumberTest {
|
||||
|
||||
@Test
|
||||
public void letterCombinationsOfPhoneNumber() {
|
||||
LetterCombinationsOfPhoneNumber ob = new LetterCombinationsOfPhoneNumber();
|
||||
ob.generateNumberToCharMap();
|
||||
LetterCombinationsOfPhoneNumber.generateNumberToCharMap();
|
||||
|
||||
// ** Test 1 **
|
||||
// Input: digits = ""
|
||||
// Output: []
|
||||
int[] numbers1 = {};
|
||||
List<String> output1 = Arrays.asList("");
|
||||
assertTrue(ob.printWords(numbers1, numbers1.length, 0, "").equals(output1));
|
||||
assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers1, numbers1.length, 0, "").equals(output1));
|
||||
|
||||
// ** Test 2 **
|
||||
// Input: digits = "2"
|
||||
// Output: ["a","b","c"]
|
||||
int[] numbers2 = {2};
|
||||
List<String> output2 = Arrays.asList("a", "b", "c");
|
||||
assertTrue(ob.printWords(numbers2, numbers2.length, 0, "").equals(output2));
|
||||
assertTrue(LetterCombinationsOfPhoneNumber.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};
|
||||
List<String> output3 = Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf");
|
||||
assertTrue(ob.printWords(numbers3, numbers3.length, 0, "").equals(output3));
|
||||
assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers3, numbers3.length, 0, "").equals(output3));
|
||||
|
||||
// ** Test 4 **
|
||||
// Input: digits = "234"
|
||||
@ -40,6 +39,6 @@ public class LetterCombinationsOfPhoneNumberTest {
|
||||
// "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));
|
||||
assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers4, numbers4.length, 0, "").equals(output4));
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ public class ReverseStringRecursiveTest {
|
||||
@Test
|
||||
void shouldAcceptWhenEmptyStringIsPassed() {
|
||||
String expected = "";
|
||||
String reversed = stringRecursive.reverse("");
|
||||
String reversed = ReverseStringRecursive.reverse("");
|
||||
|
||||
assertEquals(expected, reversed);
|
||||
}
|
||||
@ -18,7 +18,7 @@ public class ReverseStringRecursiveTest {
|
||||
@Test
|
||||
void shouldAcceptNotWhenWhenSingleCharacterIsPassed() {
|
||||
String expected = "a";
|
||||
String reversed = stringRecursive.reverse("a");
|
||||
String reversed = ReverseStringRecursive.reverse("a");
|
||||
|
||||
assertEquals(expected, reversed);
|
||||
}
|
||||
@ -26,7 +26,7 @@ public class ReverseStringRecursiveTest {
|
||||
@Test
|
||||
void shouldAcceptWhenStringIsPassed() {
|
||||
String expected = "dlroWolleH";
|
||||
String reversed = stringRecursive.reverse("HelloWorld");
|
||||
String reversed = ReverseStringRecursive.reverse("HelloWorld");
|
||||
|
||||
assertEquals(expected, reversed);
|
||||
}
|
||||
|
Reference in New Issue
Block a user