slice_map_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package tests
  2. import (
  3. "testing"
  4. "github.com/modern-go/reflect2"
  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. valType.GetIndex(obj, 0),
  28. valType.GetIndex(obj, 1),
  29. }
  30. }))
  31. t.Run("Append", testOp(func(api reflect2.API) interface{} {
  32. obj := make([]map[int]int, 2, 3)
  33. obj[0] = map[int]int{1:1}
  34. obj[1] = map[int]int{2:2}
  35. valType := api.TypeOf(obj).(reflect2.SliceType)
  36. valType.Append(obj, map[int]int{3:3})
  37. // will trigger grow
  38. valType.Append(obj, map[int]int{4:4})
  39. return obj
  40. }))
  41. }