array_test.go 943 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package tests
  2. import (
  3. "testing"
  4. "github.com/modern-go/reflect2"
  5. )
  6. func Test_array(t *testing.T) {
  7. var pInt = func(val int) *int {
  8. return &val
  9. }
  10. t.Run("New", testOp(func(api reflect2.API) interface{} {
  11. valType := api.TypeOf([2]int{})
  12. obj := valType.New()
  13. (*(obj.(*[2]int)))[0] = 100
  14. (*(obj.(*[2]int)))[1] = 200
  15. return obj
  16. }))
  17. t.Run("Indirect", testOp(func(api reflect2.API) interface{} {
  18. valType := api.TypeOf([2]int{})
  19. return valType.Indirect(&[2]int{})
  20. }))
  21. t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
  22. obj := [2]int{}
  23. valType := api.TypeOf(obj).(reflect2.ArrayType)
  24. valType.SetIndex(&obj, 0, pInt(100))
  25. valType.SetIndex(&obj, 1, pInt(200))
  26. return obj
  27. }))
  28. t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
  29. obj := [2]int{1, 2}
  30. valType := api.TypeOf(obj).(reflect2.ArrayType)
  31. return []interface{} {
  32. valType.GetIndex(&obj, 0),
  33. valType.GetIndex(&obj, 1),
  34. }
  35. }))
  36. }