mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 15:02:46 +08:00
Format code with prettier (#3375)
This commit is contained in:
@ -1,23 +1,20 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BinarySearch2dArrayTest {
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestMiddle() {
|
||||
int[][] arr = { {1, 2, 3, 4},
|
||||
{5, 6, 7, 8},
|
||||
{9,10,11,12}};
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int target = 6;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {1,1};
|
||||
int[] expected = { 1, 1 };
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(1, ans[0]);
|
||||
assertEquals(1, ans[1]);
|
||||
@ -26,13 +23,11 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestMiddleSide() {
|
||||
int[][] arr = { {1, 2, 3, 4},
|
||||
{5, 6, 7, 8},
|
||||
{9,10,11,12}};
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int target = 8;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {1,3};
|
||||
int[] expected = { 1, 3 };
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(1, ans[0]);
|
||||
assertEquals(3, ans[1]);
|
||||
@ -41,13 +36,11 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestUpper() {
|
||||
int[][] arr = { {1, 2, 3, 4},
|
||||
{5, 6, 7, 8},
|
||||
{9,10,11,12}};
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int target = 2;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {0,1};
|
||||
int[] expected = { 0, 1 };
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(0, ans[0]);
|
||||
assertEquals(1, ans[1]);
|
||||
@ -56,13 +49,11 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestUpperSide() {
|
||||
int[][] arr = { {1, 2, 3, 4},
|
||||
{5, 6, 7, 8},
|
||||
{9,10,11,12}};
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int target = 1;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {0,0};
|
||||
int[] expected = { 0, 0 };
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(0, ans[0]);
|
||||
assertEquals(0, ans[1]);
|
||||
@ -71,13 +62,11 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestLower() {
|
||||
int[][] arr = { {1, 2, 3, 4},
|
||||
{5, 6, 7, 8},
|
||||
{9,10,11,12}};
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int target = 10;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {2,1};
|
||||
int[] expected = { 2, 1 };
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(2, ans[0]);
|
||||
assertEquals(1, ans[1]);
|
||||
@ -86,13 +75,11 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestLowerSide() {
|
||||
int[][] arr = { {1, 2, 3, 4},
|
||||
{5, 6, 7, 8},
|
||||
{9,10,11,12}};
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int target = 11;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {2,2};
|
||||
int[] expected = { 2, 2 };
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(2, ans[0]);
|
||||
assertEquals(2, ans[1]);
|
||||
@ -101,17 +88,13 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestNotFound() {
|
||||
int[][] arr = { {1, 2, 3, 4},
|
||||
{5, 6, 7, 8},
|
||||
{9,10,11,12}};
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int target = 101;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = {-1,-1};
|
||||
int[] expected = { -1, -1 };
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(-1, ans[0]);
|
||||
assertEquals(-1, ans[1]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,66 +1,63 @@
|
||||
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class KMPSearchTest {
|
||||
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void KMPSearchTestLast() {
|
||||
String txt = "ABABDABACDABABCABAB";
|
||||
String txt = "ABABDABACDABABCABAB";
|
||||
String pat = "ABABCABAB";
|
||||
KMPSearch kmpSearch = new KMPSearch();
|
||||
int value = kmpSearch.KMPSearch(pat, txt);
|
||||
System.out.println(value);
|
||||
assertEquals(value, 10);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void KMPSearchTestFront() {
|
||||
String txt = "AAAAABAAABA";
|
||||
String pat = "AAAA";
|
||||
String txt = "AAAAABAAABA";
|
||||
String pat = "AAAA";
|
||||
KMPSearch kmpSearch = new KMPSearch();
|
||||
int value = kmpSearch.KMPSearch(pat, txt);
|
||||
System.out.println(value);
|
||||
assertEquals(value, 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void KMPSearchTestMiddle() {
|
||||
String txt = "AAACAAAAAC";
|
||||
String pat = "AAAA";
|
||||
String txt = "AAACAAAAAC";
|
||||
String pat = "AAAA";
|
||||
KMPSearch kmpSearch = new KMPSearch();
|
||||
int value = kmpSearch.KMPSearch(pat, txt);
|
||||
System.out.println(value);
|
||||
assertEquals(value, 4);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void KMPSearchTestNotFound() {
|
||||
String txt = "AAABAAAA";
|
||||
String pat = "AAAA";
|
||||
String txt = "AAABAAAA";
|
||||
String pat = "AAAA";
|
||||
KMPSearch kmpSearch = new KMPSearch();
|
||||
int value = kmpSearch.KMPSearch(pat, txt);
|
||||
System.out.println(value);
|
||||
assertEquals(value, 4);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
// not valid test case
|
||||
public void KMPSearchTest4() {
|
||||
String txt = "AABAAA";
|
||||
String pat = "AAAA";
|
||||
String txt = "AABAAA";
|
||||
String pat = "AAAA";
|
||||
KMPSearch kmpSearch = new KMPSearch();
|
||||
int value = kmpSearch.KMPSearch(pat, txt);
|
||||
System.out.println(value);
|
||||
assertEquals(value, -1);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class QuickSelectTest {
|
||||
|
||||
@Test
|
||||
void quickSelectMinimumOfOneElement() {
|
||||
List<Integer> elements = Collections.singletonList(42);
|
||||
@ -181,8 +181,8 @@ class QuickSelectTest {
|
||||
@Test
|
||||
void quickSelectNullList() {
|
||||
NullPointerException exception = assertThrows(
|
||||
NullPointerException.class,
|
||||
() -> QuickSelect.select(null, 0)
|
||||
NullPointerException.class,
|
||||
() -> QuickSelect.select(null, 0)
|
||||
);
|
||||
String expectedMsg = "The list of elements must not be null.";
|
||||
assertEquals(expectedMsg, exception.getMessage());
|
||||
@ -192,8 +192,8 @@ class QuickSelectTest {
|
||||
void quickSelectEmptyList() {
|
||||
List<String> objects = Collections.emptyList();
|
||||
IllegalArgumentException exception = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> QuickSelect.select(objects, 0)
|
||||
IllegalArgumentException.class,
|
||||
() -> QuickSelect.select(objects, 0)
|
||||
);
|
||||
String expectedMsg = "The list of elements must not be empty.";
|
||||
assertEquals(expectedMsg, exception.getMessage());
|
||||
@ -202,8 +202,8 @@ class QuickSelectTest {
|
||||
@Test
|
||||
void quickSelectIndexOutOfLeftBound() {
|
||||
IndexOutOfBoundsException exception = assertThrows(
|
||||
IndexOutOfBoundsException.class,
|
||||
() -> QuickSelect.select(Collections.singletonList(1), -1)
|
||||
IndexOutOfBoundsException.class,
|
||||
() -> QuickSelect.select(Collections.singletonList(1), -1)
|
||||
);
|
||||
String expectedMsg = "The index must not be negative.";
|
||||
assertEquals(expectedMsg, exception.getMessage());
|
||||
@ -212,10 +212,11 @@ class QuickSelectTest {
|
||||
@Test
|
||||
void quickSelectIndexOutOfRightBound() {
|
||||
IndexOutOfBoundsException exception = assertThrows(
|
||||
IndexOutOfBoundsException.class,
|
||||
() -> QuickSelect.select(Collections.singletonList(1), 1)
|
||||
IndexOutOfBoundsException.class,
|
||||
() -> QuickSelect.select(Collections.singletonList(1), 1)
|
||||
);
|
||||
String expectedMsg = "The index must be less than the number of elements.";
|
||||
String expectedMsg =
|
||||
"The index must be less than the number of elements.";
|
||||
assertEquals(expectedMsg, exception.getMessage());
|
||||
}
|
||||
|
||||
@ -229,12 +230,15 @@ class QuickSelectTest {
|
||||
}
|
||||
|
||||
private static List<Character> generateRandomCharacters(int n) {
|
||||
return RANDOM.ints(n, ASCII_A, ASCII_Z)
|
||||
.mapToObj(i -> (char) i)
|
||||
.collect(Collectors.toList());
|
||||
return RANDOM
|
||||
.ints(n, ASCII_A, ASCII_Z)
|
||||
.mapToObj(i -> (char) i)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static <T extends Comparable<T>> List<T> getSortedCopyOfList(List<T> list) {
|
||||
private static <T extends Comparable<T>> List<T> getSortedCopyOfList(
|
||||
List<T> list
|
||||
) {
|
||||
return list.stream().sorted().collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,13 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RabinKarpAlgorithmTest {
|
||||
|
||||
|
||||
RabinKarpAlgorithm RKA= new RabinKarpAlgorithm();
|
||||
int q= 101;
|
||||
RabinKarpAlgorithm RKA = new RabinKarpAlgorithm();
|
||||
int q = 101;
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
@ -16,9 +15,9 @@ class RabinKarpAlgorithmTest {
|
||||
String txt = "This is an example for rabin karp algorithmn";
|
||||
String pat = "algorithmn";
|
||||
int value = RKA.search(pat, txt, q);
|
||||
assertEquals(value,34);
|
||||
assertEquals(value, 34);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void RabinKarpAlgorithmTestFront() {
|
||||
@ -54,5 +53,4 @@ class RabinKarpAlgorithmTest {
|
||||
int value = RKA.search(pat, txt, q);
|
||||
assertEquals(value, -1);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user