diff --git a/src/test/java/g0001_0100/s0037_sudoku_solver/SolutionTest.java b/src/test/java/g0001_0100/s0037_sudoku_solver/SolutionTest.java index 69be188de..4455cb4f6 100644 --- a/src/test/java/g0001_0100/s0037_sudoku_solver/SolutionTest.java +++ b/src/test/java/g0001_0100/s0037_sudoku_solver/SolutionTest.java @@ -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)); + } } diff --git a/src/test/java/g0301_0400/s0382_linked_list_random_node/SolutionTest.java b/src/test/java/g0301_0400/s0382_linked_list_random_node/SolutionTest.java index 5cf601f6c..41dd1f6bf 100644 --- a/src/test/java/g0301_0400/s0382_linked_list_random_node/SolutionTest.java +++ b/src/test/java/g0301_0400/s0382_linked_list_random_node/SolutionTest.java @@ -32,4 +32,48 @@ void solutionTest() { } assertThat(contain, equalTo(true)); } + + @Test + void solutionTest2() { + List 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 expected = new ArrayList<>(Arrays.asList(7, 7, 7, 7, 7)); + assertThat(result, equalTo(expected)); + } + + @Test + void solutionTest3() { + List 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 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)); + } } diff --git a/src/test/java/g0301_0400/s0384_shuffle_an_array/SolutionTest.java b/src/test/java/g0301_0400/s0384_shuffle_an_array/SolutionTest.java index 4cf2a3160..07d657191 100644 --- a/src/test/java/g0301_0400/s0384_shuffle_an_array/SolutionTest.java +++ b/src/test/java/g0301_0400/s0384_shuffle_an_array/SolutionTest.java @@ -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})); + } } diff --git a/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java b/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java index 5b3589042..7531104aa 100644 --- a/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java +++ b/src/test/java/g0301_0400/s0398_random_pick_index/SolutionTest.java @@ -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 expected = Arrays.asList(0, 2, 4, 5); + Set 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)); + } }