Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/test/java/g0001_0100/s0037_sudoku_solver/SolutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,60 @@ void solveSudoku() {
new Solution().solveSudoku(board);
assertThat(board, equalTo(expected));
}

@Test
void solveSudoku2() {
char[][] expected = {
{'5', '3', '4', '6', '7', '8', '9', '1', '2'},
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}
};
char[][] board = {
{'5', '3', '4', '6', '7', '8', '9', '1', '2'},
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}
};
new Solution().solveSudoku(board);
assertThat(board, equalTo(expected));
}

@Test
void solveSudoku3() {
char[][] expected = {
{'5', '3', '4', '6', '7', '8', '9', '1', '2'},
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}
};
char[][] board = {
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'6', '.', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '.', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '.', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '.', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '.', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '.', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '.', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '.'}
};
new Solution().solveSudoku(board);
assertThat(board, equalTo(expected));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,48 @@ void solutionTest() {
}
assertThat(contain, equalTo(true));
}

@Test
void solutionTest2() {
List<Integer> result = new ArrayList<>();
ListNode listNode = new ListNode(7);
Solution solution = new Solution(listNode);
result.add(solution.getRandom());
result.add(solution.getRandom());
result.add(solution.getRandom());
result.add(solution.getRandom());
result.add(solution.getRandom());
List<Integer> expected = new ArrayList<>(Arrays.asList(7, 7, 7, 7, 7));
assertThat(result, equalTo(expected));
}

@Test
void solutionTest3() {
List<Integer> result = new ArrayList<>();
ListNode listNode4 = new ListNode(0);
ListNode listNode3 = new ListNode(-5, listNode4);
ListNode listNode2 = new ListNode(10, listNode3);
ListNode listNode = new ListNode(-1, listNode2);
Solution solution = new Solution(listNode);
for (int i = 0; i < 1000; i++) {
result.add(solution.getRandom());
}
boolean contain = true;
List<Integer> expected = new ArrayList<>(Arrays.asList(-1, 10, -5, 0));
for (Integer i : result) {
if (!expected.contains(i)) {
contain = false;
break;
}
}
boolean allReturned = true;
for (Integer value : expected) {
if (!result.contains(value)) {
allReturned = false;
break;
}
}
assertThat(contain, equalTo(true));
assertThat(allReturned, equalTo(true));
}
}
19 changes: 19 additions & 0 deletions src/test/java/g0301_0400/s0384_shuffle_an_array/SolutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,23 @@ void solutionTest() {
assertThat(shuffled, equalTo(new int[] {1, 2, 3}));
assertThat(solution.reset(), equalTo(new int[] {1, 2, 3}));
}

@Test
void solutionTest2() {
Solution solution = new Solution(new int[] {5});
int[] shuffled = solution.shuffle();
assertThat(shuffled, equalTo(new int[] {5}));
assertThat(solution.reset(), equalTo(new int[] {5}));
}

@Test
void solutionTest3() {
Solution solution = new Solution(new int[] {4, -1, 4, 0, -7});
for (int i = 0; i < 100; i++) {
int[] shuffled = solution.shuffle();
Arrays.sort(shuffled);
assertThat(shuffled, equalTo(new int[] {-7, -1, 0, 4, 4}));
}
assertThat(solution.reset(), equalTo(new int[] {4, -1, 4, 0, -7}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,44 @@
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void pick() {
int[] input = new int[] {1, 2, 3, 3, 3};
int[] input = {1, 2, 3, 3, 3};
Solution solution = new Solution(input);
assertThat(Arrays.asList(2, 3, 4).contains(solution.pick(3)), equalTo(true));
assertThat(solution.pick(1), equalTo(0));
assertThat(Arrays.asList(2, 3, 4).contains(solution.pick(3)), equalTo(true));
}

@Test
void pick2() {
int[] input = {7, -2, 5, 0};
Solution solution = new Solution(input);
assertThat(solution.pick(7), equalTo(0));
assertThat(solution.pick(-2), equalTo(1));
assertThat(solution.pick(5), equalTo(2));
assertThat(solution.pick(0), equalTo(3));
}

@Test
void pick3() {
int[] input = {4, 1, 4, 2, 4, 4};
Solution solution = new Solution(input);
List<Integer> expected = Arrays.asList(0, 2, 4, 5);
Set<Integer> picked = new HashSet<>();
for (int i = 0; i < 1000; i++) {
int index = solution.pick(4);
assertThat(expected.contains(index), equalTo(true));
picked.add(index);
}
assertThat(picked, equalTo(new HashSet<>(expected)));
assertThat(solution.pick(1), equalTo(1));
assertThat(solution.pick(2), equalTo(3));
}
}
Loading