forked from RezaSi/go-interview-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution-template_test.go
More file actions
95 lines (85 loc) · 3.13 KB
/
solution-template_test.go
File metadata and controls
95 lines (85 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"reflect"
"testing"
)
func TestMinCoins(t *testing.T) {
tests := []struct {
name string
amount int
denominations []int
expected int
}{
{"Zero amount", 0, []int{1, 5, 10, 25, 50}, 0},
{"Only pennies needed", 4, []int{1, 5, 10, 25, 50}, 4},
{"Exact denomination", 5, []int{1, 5, 10, 25, 50}, 1},
{"Simple combination", 11, []int{1, 5, 10, 25, 50}, 2},
{"Example 1", 87, []int{1, 5, 10, 25, 50}, 5},
{"Example 2", 42, []int{1, 5, 10, 25, 50}, 5},
{"Larger amount", 99, []int{1, 5, 10, 25, 50}, 8},
{"Cannot make amount", 3, []int{5, 10, 25}, -1},
{"Custom denominations", 30, []int{1, 5, 10}, 3},
{"Larger denominations", 63, []int{1, 5, 10, 25}, 6},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := MinCoins(tt.amount, tt.denominations)
if result != tt.expected {
t.Errorf("MinCoins(%d, %v) = %d, expected %d",
tt.amount, tt.denominations, result, tt.expected)
}
})
}
}
func TestCoinCombination(t *testing.T) {
tests := []struct {
name string
amount int
denominations []int
expected map[int]int
}{
{"Zero amount", 0, []int{1, 5, 10, 25, 50}, map[int]int{}},
{"Only pennies needed", 4, []int{1, 5, 10, 25, 50}, map[int]int{1: 4}},
{"Exact denomination", 5, []int{1, 5, 10, 25, 50}, map[int]int{5: 1}},
{"Simple combination", 11, []int{1, 5, 10, 25, 50}, map[int]int{1: 1, 10: 1}},
{"Example 1", 87, []int{1, 5, 10, 25, 50}, map[int]int{1: 2, 10: 1, 25: 1, 50: 1}},
{"Example 2", 42, []int{1, 5, 10, 25, 50}, map[int]int{1: 2, 5: 1, 10: 1, 25: 1}},
{"Larger amount", 99, []int{1, 5, 10, 25, 50}, map[int]int{1: 4, 10: 2, 25: 1, 50: 1}},
{"Cannot make amount", 3, []int{5, 10, 25}, map[int]int{}},
{"Custom denominations", 30, []int{1, 6, 10}, map[int]int{10: 3}},
{"Larger denominations", 63, []int{1, 5, 10, 25}, map[int]int{1: 3, 10: 1, 25: 2}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := CoinCombination(tt.amount, tt.denominations)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("CoinCombination(%d, %v) = %v, expected %v",
tt.amount, tt.denominations, result, tt.expected)
}
})
}
}
func TestExampleCases(t *testing.T) {
// Standard U.S. denominations
denominations := []int{1, 5, 10, 25, 50}
// Example 1
if result := MinCoins(87, denominations); result != 5 {
t.Errorf("MinCoins(87, %v) = %d, expected 5", denominations, result)
}
// Example 2
expectedCombination1 := map[int]int{1: 2, 10: 1, 25: 1, 50: 1}
if result := CoinCombination(87, denominations); !reflect.DeepEqual(result, expectedCombination1) {
t.Errorf("CoinCombination(87, %v) = %v, expected %v",
denominations, result, expectedCombination1)
}
// Example 3
if result := MinCoins(42, denominations); result != 5 {
t.Errorf("MinCoins(42, %v) = %d, expected 5", denominations, result)
}
// Example 4
expectedCombination2 := map[int]int{1: 2, 5: 1, 10: 1, 25: 1}
if result := CoinCombination(42, denominations); !reflect.DeepEqual(result, expectedCombination2) {
t.Errorf("CoinCombination(42, %v) = %v, expected %v",
denominations, result, expectedCombination2)
}
}