|
| 1 | +package productofarrayexceptself |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestProductExceptSelf(t *testing.T) { |
| 9 | + cases := []struct { |
| 10 | + name string |
| 11 | + fn func([]int) []int |
| 12 | + nums []int |
| 13 | + expected []int |
| 14 | + }{ |
| 15 | + // ProductExceptSelfBruteForce |
| 16 | + {"Brute Force - Basic", ProductExceptSelfBruteForce, []int{1, 2, 3, 4}, []int{24, 12, 8, 6}}, |
| 17 | + {"Brute Force - One Zero", ProductExceptSelfBruteForce, []int{1, 2, 0, 4}, []int{0, 0, 8, 0}}, |
| 18 | + {"Brute Force - Two Zeros", ProductExceptSelfBruteForce, []int{0, 2, 0, 4}, []int{0, 0, 0, 0}}, |
| 19 | + {"Brute Force - Single", ProductExceptSelfBruteForce, []int{10}, []int{1}}, |
| 20 | + {"Brute Force - Same Elements", ProductExceptSelfBruteForce, []int{3, 3, 3}, []int{9, 9, 9}}, |
| 21 | + |
| 22 | + // ProductExceptSelfDivision |
| 23 | + {"Division - Basic", ProductExceptSelfDivision, []int{1, 2, 3, 4}, []int{24, 12, 8, 6}}, |
| 24 | + {"Division - One Zero", ProductExceptSelfDivision, []int{1, 2, 0, 4}, []int{0, 0, 8, 0}}, |
| 25 | + {"Division - Two Zeros", ProductExceptSelfDivision, []int{0, 2, 0, 4}, []int{0, 0, 0, 0}}, |
| 26 | + {"Division - Single", ProductExceptSelfDivision, []int{10}, []int{1}}, |
| 27 | + {"Division - Same Elements", ProductExceptSelfDivision, []int{3, 3, 3}, []int{9, 9, 9}}, |
| 28 | + |
| 29 | + // ProductExceptSelfPrefixSuffix |
| 30 | + {"Prefix & Suffix - Basic", ProductExceptSelfPrefixSuffix, []int{1, 2, 3, 4}, []int{24, 12, 8, 6}}, |
| 31 | + {"Prefix & Suffix - One Zero", ProductExceptSelfPrefixSuffix, []int{1, 2, 0, 4}, []int{0, 0, 8, 0}}, |
| 32 | + {"Prefix & Suffix - Two Zeros", ProductExceptSelfPrefixSuffix, []int{0, 2, 0, 4}, []int{0, 0, 0, 0}}, |
| 33 | + {"Prefix & Suffix - Single", ProductExceptSelfPrefixSuffix, []int{10}, []int{1}}, |
| 34 | + {"Prefix & Suffix - Same Elements", ProductExceptSelfPrefixSuffix, []int{3, 3, 3}, []int{9, 9, 9}}, |
| 35 | + |
| 36 | + // ProductExceptSelfPrefixSuffixOptimal |
| 37 | + {"Prefix & Suffix Optimal - Basic", ProductExceptSelfPrefixSuffixOptimal, []int{1, 2, 3, 4}, []int{24, 12, 8, 6}}, |
| 38 | + {"Prefix & Suffix Optimal - One Zero", ProductExceptSelfPrefixSuffixOptimal, []int{1, 2, 0, 4}, []int{0, 0, 8, 0}}, |
| 39 | + {"Prefix & Suffix Optimal - Two Zeros", ProductExceptSelfPrefixSuffixOptimal, []int{0, 2, 0, 4}, []int{0, 0, 0, 0}}, |
| 40 | + {"Prefix & Suffix Optimal - Single", ProductExceptSelfPrefixSuffixOptimal, []int{10}, []int{1}}, |
| 41 | + {"Prefix & Suffix Optimal - Same Elements", ProductExceptSelfPrefixSuffixOptimal, []int{3, 3, 3}, []int{9, 9, 9}}, |
| 42 | + } |
| 43 | + |
| 44 | + for _, c := range cases { |
| 45 | + t.Run(c.name, func(t *testing.T) { |
| 46 | + got := c.fn(c.nums) |
| 47 | + if !reflect.DeepEqual(got, c.expected) { |
| 48 | + t.Errorf("%s failed: expected %v, got %v", c.name, c.expected, got) |
| 49 | + } |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
0 commit comments