slice_map_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package tests
  2. import (
  3. "github.com/modern-go/reflect2"
  4. "testing"
  5. )
  6. func Test_slice_map(t *testing.T) {
  7. t.Run("MakeSlice", testOp(func(api reflect2.API) interface{} {
  8. valType := api.TypeOf([]map[int]int{}).(reflect2.SliceType)
  9. obj := valType.MakeSlice(5, 10)
  10. (*obj.(*[]map[int]int))[0] = map[int]int{1: 1}
  11. (*obj.(*[]map[int]int))[4] = map[int]int{2: 2}
  12. return obj
  13. }))
  14. t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
  15. obj := []map[int]int{{1: 1}, nil}
  16. valType := api.TypeOf(obj).(reflect2.SliceType)
  17. valType.SetIndex(&obj, 0, &map[int]int{10: 10})
  18. valType.SetIndex(&obj, 1, &map[int]int{2: 2})
  19. return obj
  20. }))
  21. t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
  22. obj := []map[int]int{{1: 1}, nil}
  23. valType := api.TypeOf(obj).(reflect2.SliceType)
  24. return []interface{}{
  25. valType.GetIndex(&obj, 0),
  26. valType.GetIndex(&obj, 1),
  27. }
  28. }))
  29. t.Run("Append", testOp(func(api reflect2.API) interface{} {
  30. obj := make([]map[int]int, 2, 3)
  31. obj[0] = map[int]int{1: 1}
  32. obj[1] = map[int]int{2: 2}
  33. valType := api.TypeOf(obj).(reflect2.SliceType)
  34. valType.Append(&obj, &map[int]int{3: 3})
  35. // will trigger grow
  36. valType.Append(&obj, &map[int]int{4: 4})
  37. return obj
  38. }))
  39. }