Edit : While the compilation error in Missing type in composite literal is the same as that in my question, they were different enough in composition for me to not understand how I would apply the solution to my program, hence the creation of this question.
I am new to go, and I am trying to write a test for a function which I have verified can successfully can be called like so:
func main() {
items := []map[string]int{
map[string]int{
"value": 100,
"weight": 5,
},
map[string]int{
"value": 90,
"weight": 2,
},
map[string]int{
"value": 80,
"weight": 2,
},
}
fmt.Println(KnapSack(items, 0, 6))
}
Using this template (generated by my IDE) for convenience:
func TestKnapSack(t *testing.T) {
type args struct {
items []map[string]int
current_index int
remaining_weight int
}
tests := []struct {
name string
args args
want int
}{
{
"Only test", // name of test
{
{ // items
map[string]int{
"value": 100,
"weight": 5,
},
map[string]int{
"value": 90,
"weight": 2,
},
map[string]int{
"value": 80,
"weight": 2,
},
},
0, // current_index
4, // remaining_weight
},
170, // want
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := KnapSack(tt.args.items, tt.args.current_index, tt.args.remaining_weight); got != tt.want {
t.Errorf("KnapSack() = %v, want %v", got, tt.want)
}
})
}
}
The args
struct does not like my array of maps. How can I fill this struct so that it will compile?
seems you miss the type of args
and []map[string]int
tests := []struct {
name string
args args
want int
}{
{
"Only test", // name of test
args{
[]map[string]int{ // items
map[string]int{
"value": 100,
"weight": 5,
},
map[string]int{
"value": 90,
"weight": 2,
},
map[string]int{
"value": 80,
"weight": 2,
},
},
0, // current_index
4, // remaining_weight
},
170, // want
},
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.