mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 22:43:30 +08:00
Format code with prettier (#3375)
This commit is contained in:
@ -1,30 +1,38 @@
|
||||
package com.thealgorithms.backtracking;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.TreeSet;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CombinationTest {
|
||||
|
||||
@Test
|
||||
void testNoElement()
|
||||
{
|
||||
List<TreeSet<Integer>> result = Combination.combination(new Integer[]{1, 2}, 0);
|
||||
void testNoElement() {
|
||||
List<TreeSet<Integer>> result = Combination.combination(
|
||||
new Integer[] { 1, 2 },
|
||||
0
|
||||
);
|
||||
assertTrue(result == null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLengthOne()
|
||||
{
|
||||
List<TreeSet<Integer>> result = Combination.combination(new Integer[]{1, 2}, 1);
|
||||
void testLengthOne() {
|
||||
List<TreeSet<Integer>> result = Combination.combination(
|
||||
new Integer[] { 1, 2 },
|
||||
1
|
||||
);
|
||||
assertTrue(result.get(0).iterator().next() == 1);
|
||||
assertTrue(result.get(1).iterator().next() == 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLengthTwo()
|
||||
{
|
||||
List<TreeSet<Integer>> result = Combination.combination(new Integer[]{1, 2}, 2);
|
||||
void testLengthTwo() {
|
||||
List<TreeSet<Integer>> result = Combination.combination(
|
||||
new Integer[] { 1, 2 },
|
||||
2
|
||||
);
|
||||
Integer[] arr = result.get(0).toArray(new Integer[2]);
|
||||
assertTrue(arr[0] == 1);
|
||||
assertTrue(arr[1] == 2);
|
||||
|
@ -1,14 +1,13 @@
|
||||
package com.thealgorithms.backtracking;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FloodFillTest {
|
||||
|
||||
@Test
|
||||
void testForEmptyImage()
|
||||
{
|
||||
void testForEmptyImage() {
|
||||
int image[][] = {};
|
||||
int expected[][] = {};
|
||||
|
||||
@ -16,91 +15,82 @@ class FloodFillTest {
|
||||
assertArrayEquals(expected, image);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testForSingleElementImage()
|
||||
{
|
||||
int image[][] = {{1}};
|
||||
int expected[][] = {{3}};
|
||||
void testForSingleElementImage() {
|
||||
int image[][] = { { 1 } };
|
||||
int expected[][] = { { 3 } };
|
||||
|
||||
FloodFill.floodFill(image, 0, 0, 3, 1);
|
||||
assertArrayEquals(expected, image);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testForImageOne()
|
||||
{
|
||||
void testForImageOne() {
|
||||
int image[][] = {
|
||||
{ 0,0,0,0,0,0,0 },
|
||||
{ 0,3,3,3,3,0,0 },
|
||||
{ 0,3,1,1,5,0,0 },
|
||||
{ 0,3,1,1,5,5,3 },
|
||||
{ 0,3,5,5,1,1,3 },
|
||||
{ 0,0,0,5,1,1,3 },
|
||||
{ 0,0,0,3,3,3,3 }
|
||||
{ 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 0, 3, 3, 3, 3, 0, 0 },
|
||||
{ 0, 3, 1, 1, 5, 0, 0 },
|
||||
{ 0, 3, 1, 1, 5, 5, 3 },
|
||||
{ 0, 3, 5, 5, 1, 1, 3 },
|
||||
{ 0, 0, 0, 5, 1, 1, 3 },
|
||||
{ 0, 0, 0, 3, 3, 3, 3 },
|
||||
};
|
||||
|
||||
int expected[][] = {
|
||||
{ 0,0,0,0,0,0,0 },
|
||||
{ 0,3,3,3,3,0,0 },
|
||||
{ 0,3,2,2,5,0,0 },
|
||||
{ 0,3,2,2,5,5,3 },
|
||||
{ 0,3,5,5,2,2,3 },
|
||||
{ 0,0,0,5,2,2,3 },
|
||||
{ 0,0,0,3,3,3,3 }
|
||||
};
|
||||
|
||||
FloodFill.floodFill(image,2,2,2,1);
|
||||
assertArrayEquals(expected, image);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testForImageTwo()
|
||||
{
|
||||
int image[][] = {
|
||||
{ 0,0,1,1,0,0,0 },
|
||||
{ 1,1,3,3,3,0,0 },
|
||||
{ 1,3,1,1,5,0,0 },
|
||||
{ 0,3,1,1,5,5,3 },
|
||||
{ 0,3,5,5,1,1,3 },
|
||||
{ 0,0,0,5,1,1,3 },
|
||||
{ 0,0,0,1,3,1,3 }
|
||||
};
|
||||
|
||||
int expected[][] = {
|
||||
{ 0,0,2,2,0,0,0 },
|
||||
{ 2,2,3,3,3,0,0 },
|
||||
{ 2,3,2,2,5,0,0 },
|
||||
{ 0,3,2,2,5,5,3 },
|
||||
{ 0,3,5,5,2,2,3 },
|
||||
{ 0,0,0,5,2,2,3 },
|
||||
{ 0,0,0,2,3,2,3 }
|
||||
{ 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 0, 3, 3, 3, 3, 0, 0 },
|
||||
{ 0, 3, 2, 2, 5, 0, 0 },
|
||||
{ 0, 3, 2, 2, 5, 5, 3 },
|
||||
{ 0, 3, 5, 5, 2, 2, 3 },
|
||||
{ 0, 0, 0, 5, 2, 2, 3 },
|
||||
{ 0, 0, 0, 3, 3, 3, 3 },
|
||||
};
|
||||
|
||||
FloodFill.floodFill(image, 2, 2, 2, 1);
|
||||
assertArrayEquals(expected, image);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testForImageThree()
|
||||
{
|
||||
void testForImageTwo() {
|
||||
int image[][] = {
|
||||
{ 1,1,2,3,1,1,1 },
|
||||
{ 1,0,0,1,0,0,1 },
|
||||
{ 1,1,1,0,3,1,2 }
|
||||
{ 0, 0, 1, 1, 0, 0, 0 },
|
||||
{ 1, 1, 3, 3, 3, 0, 0 },
|
||||
{ 1, 3, 1, 1, 5, 0, 0 },
|
||||
{ 0, 3, 1, 1, 5, 5, 3 },
|
||||
{ 0, 3, 5, 5, 1, 1, 3 },
|
||||
{ 0, 0, 0, 5, 1, 1, 3 },
|
||||
{ 0, 0, 0, 1, 3, 1, 3 },
|
||||
};
|
||||
|
||||
int expected[][] = {
|
||||
{ 4,4,2,3,4,4,4 },
|
||||
{ 4,0,0,4,0,0,4 },
|
||||
{ 4,4,4,0,3,4,2 },
|
||||
{ 0, 0, 2, 2, 0, 0, 0 },
|
||||
{ 2, 2, 3, 3, 3, 0, 0 },
|
||||
{ 2, 3, 2, 2, 5, 0, 0 },
|
||||
{ 0, 3, 2, 2, 5, 5, 3 },
|
||||
{ 0, 3, 5, 5, 2, 2, 3 },
|
||||
{ 0, 0, 0, 5, 2, 2, 3 },
|
||||
{ 0, 0, 0, 2, 3, 2, 3 },
|
||||
};
|
||||
|
||||
FloodFill.floodFill(image, 2, 2, 2, 1);
|
||||
assertArrayEquals(expected, image);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForImageThree() {
|
||||
int image[][] = {
|
||||
{ 1, 1, 2, 3, 1, 1, 1 },
|
||||
{ 1, 0, 0, 1, 0, 0, 1 },
|
||||
{ 1, 1, 1, 0, 3, 1, 2 },
|
||||
};
|
||||
|
||||
int expected[][] = {
|
||||
{ 4, 4, 2, 3, 4, 4, 4 },
|
||||
{ 4, 0, 0, 4, 0, 0, 4 },
|
||||
{ 4, 4, 4, 0, 3, 4, 2 },
|
||||
};
|
||||
|
||||
FloodFill.floodFill(image, 0, 1, 4, 1);
|
||||
assertArrayEquals(expected, image);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package com.thealgorithms.backtracking;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author onglipwei
|
||||
* @create 2022-08-03 5:17 AM
|
||||
@ -11,8 +12,6 @@ public class MazeRecursionTest {
|
||||
|
||||
@Test
|
||||
public void testMaze() {
|
||||
|
||||
|
||||
// First create a 2 dimensions array to mimic a maze map
|
||||
int[][] map = new int[8][7];
|
||||
int[][] map2 = new int[8][7];
|
||||
@ -28,7 +27,6 @@ public class MazeRecursionTest {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
map[i][0] = 1;
|
||||
map[i][6] = 1;
|
||||
|
||||
}
|
||||
|
||||
// Now we have created a maze with its wall initialized
|
||||
@ -38,41 +36,38 @@ public class MazeRecursionTest {
|
||||
map[3][2] = 1;
|
||||
|
||||
//clone another map for setWay2 method
|
||||
for (int i = 0; i < map.length;i++) {
|
||||
for (int j = 0; j < map[i].length;j++) {
|
||||
map2[i][j]=map[i][j];
|
||||
for (int i = 0; i < map.length; i++) {
|
||||
for (int j = 0; j < map[i].length; j++) {
|
||||
map2[i][j] = map[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
MazeRecursion.setWay(map, 1, 1);
|
||||
MazeRecursion.setWay2(map2, 1, 1);
|
||||
|
||||
|
||||
int expectedMap[][] = new int[][]{
|
||||
{1,1,1,1,1,1,1},
|
||||
{1,2,0,0,0,0,1},
|
||||
{1,2,2,2,0,0,1},
|
||||
{1,1,1,2,0,0,1},
|
||||
{1,0,0,2,0,0,1},
|
||||
{1,0,0,2,0,0,1},
|
||||
{1,0,0,2,2,2,1},
|
||||
{1,1,1,1,1,1,1}
|
||||
int expectedMap[][] = new int[][] {
|
||||
{ 1, 1, 1, 1, 1, 1, 1 },
|
||||
{ 1, 2, 0, 0, 0, 0, 1 },
|
||||
{ 1, 2, 2, 2, 0, 0, 1 },
|
||||
{ 1, 1, 1, 2, 0, 0, 1 },
|
||||
{ 1, 0, 0, 2, 0, 0, 1 },
|
||||
{ 1, 0, 0, 2, 0, 0, 1 },
|
||||
{ 1, 0, 0, 2, 2, 2, 1 },
|
||||
{ 1, 1, 1, 1, 1, 1, 1 },
|
||||
};
|
||||
|
||||
int expectedMap2[][] = new int[][]{
|
||||
{1,1,1,1,1,1,1},
|
||||
{1,2,2,2,2,2,1},
|
||||
{1,0,0,0,0,2,1},
|
||||
{1,1,1,0,0,2,1},
|
||||
{1,0,0,0,0,2,1},
|
||||
{1,0,0,0,0,2,1},
|
||||
{1,0,0,0,0,2,1},
|
||||
{1,1,1,1,1,1,1}
|
||||
int expectedMap2[][] = new int[][] {
|
||||
{ 1, 1, 1, 1, 1, 1, 1 },
|
||||
{ 1, 2, 2, 2, 2, 2, 1 },
|
||||
{ 1, 0, 0, 0, 0, 2, 1 },
|
||||
{ 1, 1, 1, 0, 0, 2, 1 },
|
||||
{ 1, 0, 0, 0, 0, 2, 1 },
|
||||
{ 1, 0, 0, 0, 0, 2, 1 },
|
||||
{ 1, 0, 0, 0, 0, 2, 1 },
|
||||
{ 1, 1, 1, 1, 1, 1, 1 },
|
||||
};
|
||||
|
||||
assertArrayEquals(map, expectedMap);
|
||||
assertArrayEquals(map2, expectedMap2);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,32 +1,31 @@
|
||||
package com.thealgorithms.backtracking;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class PermutationTest {
|
||||
|
||||
@Test
|
||||
void testNoElement()
|
||||
{
|
||||
List<Integer []> result = Permutation.permutation(new Integer[]{});
|
||||
void testNoElement() {
|
||||
List<Integer[]> result = Permutation.permutation(new Integer[] {});
|
||||
assertEquals(result.get(0).length, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSingleElement()
|
||||
{
|
||||
List<Integer []> result = Permutation.permutation(new Integer[]{1});
|
||||
void testSingleElement() {
|
||||
List<Integer[]> result = Permutation.permutation(new Integer[] { 1 });
|
||||
assertEquals(result.get(0)[0], 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMultipleElements()
|
||||
{
|
||||
List<Integer []> result = Permutation.permutation(new Integer[]{1, 2});
|
||||
assertTrue(Arrays.equals(result.get(0), new Integer[]{1,2}));
|
||||
assertTrue(Arrays.equals(result.get(1), new Integer[]{2,1}));
|
||||
void testMultipleElements() {
|
||||
List<Integer[]> result = Permutation.permutation(
|
||||
new Integer[] { 1, 2 }
|
||||
);
|
||||
assertTrue(Arrays.equals(result.get(0), new Integer[] { 1, 2 }));
|
||||
assertTrue(Arrays.equals(result.get(1), new Integer[] { 2, 1 }));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user