|
1 | 1 | package problem283 |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "reflect" |
6 | 5 | "testing" |
7 | 6 | ) |
8 | 7 |
|
9 | 8 | func TestMoveZeroes(t *testing.T) { |
| 9 | + testMoveZeroes(t, moveZeroes) |
| 10 | +} |
| 11 | + |
| 12 | +func TestMoveZeroesV1(t *testing.T) { |
| 13 | + testMoveZeroes(t, moveZeroesV1) |
| 14 | +} |
| 15 | + |
| 16 | +func testMoveZeroes(t *testing.T, fn func([]int)) { |
10 | 17 | testCases := []struct { |
11 | | - nums []int |
12 | | - ans []int |
| 18 | + testName string |
| 19 | + nums []int |
| 20 | + ans []int |
13 | 21 | }{ |
14 | | - {[]int{0, 1, 0, 3, 12}, []int{1, 3, 12, 0, 0}}, |
15 | | - {[]int{0}, []int{0}}, |
16 | | - {[]int{}, []int{}}, |
17 | | - {[]int{1, 3, 12, 0, 0}, []int{1, 3, 12, 0, 0}}, |
18 | | - {[]int{0, 1, 15, 0, 12, 3, 15}, []int{1, 15, 12, 3, 15, 0, 0}}, |
| 22 | + // PRESET CASES |
| 23 | + {"Preset_case_1", []int{0, 1, 0, 3, 12}, []int{1, 3, 12, 0, 0}}, |
| 24 | + {"Preset_case_2", []int{0}, []int{0}}, |
| 25 | + // COMMON CASES |
| 26 | + {"Trailing_zeros", []int{1, 3, 12, 0, 0}, []int{1, 3, 12, 0, 0}}, |
| 27 | + {"Leading_zeros", []int{0, 0, 0, 1, 2, 3}, []int{1, 2, 3, 0, 0, 0}}, |
| 28 | + {"Embedded_zeros", []int{1, 0, 2, 0, 3, 0}, []int{1, 2, 3, 0, 0, 0}}, |
| 29 | + { |
| 30 | + "Alternating_zeros_and_non_zeros_(zero_start)", |
| 31 | + []int{0, 1, 0, 2, 0, 3, 0, 4}, |
| 32 | + []int{1, 2, 3, 4, 0, 0, 0, 0}, |
| 33 | + }, |
19 | 34 | { |
20 | | - []int{0, 0, 0, -15, 10, -1, 0, 12, 0, 3, 15}, |
21 | | - []int{-15, 10, -1, 12, 3, 15, 0, 0, 0, 0, 0}, |
| 35 | + "Alternating_zeros and non_zeros (non_zero_start)", |
| 36 | + []int{1, 0, 2, 0, 3, 0, 4}, |
| 37 | + []int{1, 2, 3, 4, 0, 0, 0}, |
22 | 38 | }, |
| 39 | + { |
| 40 | + "Leading_and_trailing_zeros", |
| 41 | + []int{0, 0, 0, 1, 3, 12, 0, 0}, |
| 42 | + []int{1, 3, 12, 0, 0, 0, 0, 0}, |
| 43 | + }, |
| 44 | + { |
| 45 | + "Large_numbers", |
| 46 | + []int{1000000, 0, 2147483648, 0, 0, 42}, |
| 47 | + []int{1000000, 2147483648, 42, 0, 0, 0}, |
| 48 | + }, |
| 49 | + // CORNER CASES |
| 50 | + {"No_zeros", []int{1, 6, 1, 24}, []int{1, 6, 1, 24}}, |
| 51 | + {"All_zeros", []int{0, 0, 0}, []int{0, 0, 0}}, |
| 52 | + {"Single_Zero", []int{0}, []int{0}}, |
| 53 | + {"Single_Non_Zero", []int{5}, []int{5}}, |
23 | 54 | } |
24 | | - for _, testCase := range testCases { |
25 | | - testName := fmt.Sprintf("%v", testCase.nums) |
26 | | - t.Run(testName, func(t *testing.T) { |
27 | | - moveZeroes(testCase.nums) |
28 | | - if !reflect.DeepEqual(testCase.nums, testCase.ans) { |
29 | | - t.Errorf("got %v, want %v", testCase.nums, testCase.ans) |
| 55 | + |
| 56 | + for _, tc := range testCases { |
| 57 | + t.Run(tc.testName, func(t *testing.T) { |
| 58 | + fn(tc.nums) |
| 59 | + if !reflect.DeepEqual(tc.nums, tc.ans) { |
| 60 | + t.Errorf("got %v, want %v", tc.nums, tc.ans) |
30 | 61 | } |
31 | 62 | }) |
32 | 63 | } |
|
0 commit comments