int_test.go 991 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package tests
  2. import (
  3. "testing"
  4. "github.com/modern-go/reflect2"
  5. "github.com/modern-go/test"
  6. "unsafe"
  7. "github.com/modern-go/test/must"
  8. "context"
  9. )
  10. func Test_int(t *testing.T) {
  11. t.Run("New", testOp(func(api reflect2.API) interface{} {
  12. valType := api.TypeOf(1)
  13. obj := valType.New()
  14. *obj.(*int) = 100
  15. return obj
  16. }))
  17. t.Run("PackEFace", test.Case(func(ctx context.Context) {
  18. valType := reflect2.TypeOf(1)
  19. hundred := 100
  20. must.Equal(&hundred, valType.PackEFace(unsafe.Pointer(&hundred)))
  21. }))
  22. t.Run("Indirect", test.Case(func(ctx context.Context) {
  23. valType := reflect2.TypeOf(1)
  24. hundred := 100
  25. must.Equal(100, valType.Indirect(&hundred))
  26. }))
  27. t.Run("Indirect", test.Case(func(ctx context.Context) {
  28. valType := reflect2.TypeOf(1)
  29. hundred := 100
  30. must.Equal(100, valType.UnsafeIndirect(unsafe.Pointer(&hundred)))
  31. }))
  32. t.Run("Set", testOp(func(api reflect2.API) interface{} {
  33. valType := api.TypeOf(1)
  34. i := 1
  35. j := 10
  36. valType.Set(&i, &j)
  37. return i
  38. }))
  39. }