(t *testing.T)
| 8 | ) |
| 9 | |
| 10 | func TestKnapsack(t *testing.T) { |
| 11 | td := []struct { |
| 12 | maxWeight int |
| 13 | weights []int |
| 14 | values []int |
| 15 | expected int |
| 16 | }{ |
| 17 | {0, []int{0}, []int{0}, 0}, |
| 18 | {10, []int{1, 2, 3}, []int{1, 1, 1}, 3}, // picks all |
| 19 | {10, []int{1, 2, 3, 4, 5, 6}, []int{1, 1, 1, 1, 1, 1}, 4}, // picks 1,2,3,4 |
| 20 | {10, []int{1, 2, 3, 4, 5, 6}, []int{1, 1, 1, 1, 1, 5}, 7}, // picks 1,3,6 |
| 21 | {10, []int{1, 2, 3, 4, 5, 6}, []int{-1, 10, -3, -4, 10, 1}, 20}, // picks 2,5 |
| 22 | {10, []int{1, 2, 3, 4, 5, 6}, []int{-10, -10, -10, -10, 10, 10}, 10}, // picks 5 or 6 |
| 23 | } |
| 24 | for _, tc := range td { |
| 25 | name := fmt.Sprintf("Knapsack problem with (maxWeight: %d, weights: %v, values: %v)", tc.maxWeight, tc.weights, tc.values) |
| 26 | t.Run(name, func(t *testing.T) { |
| 27 | actual := dynamic.Knapsack(tc.maxWeight, tc.weights, tc.values) |
| 28 | if actual != tc.expected { |
| 29 | t.Errorf("expecting knapsack with (maxWeight: %d, weights: %v, values: %v) to return %d but got %d", tc.maxWeight, tc.weights, tc.values, tc.expected, actual) |
| 30 | } |
| 31 | }) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func ExampleKnapsack() { |
| 36 | fmt.Print(dynamic.Knapsack(10, []int{4, 5, 8}, []int{50, 15, 60})) |
nothing calls this directly
no test coverage detected