map_elem_bytes_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. package test
  2. import (
  3. "testing"
  4. "github.com/modern-go/reflect2"
  5. "github.com/modern-go/test"
  6. "github.com/modern-go/test/must"
  7. "context"
  8. )
  9. func Test_map_elem_bytes(t *testing.T) {
  10. t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
  11. obj := map[int][]byte{}
  12. valType := api.TypeOf(obj).(reflect2.MapType)
  13. valType.SetIndex(obj, 2, []byte("hello"))
  14. valType.SetIndex(obj, 3, nil)
  15. return obj
  16. }))
  17. t.Run("UnsafeSetIndex", test.Case(func(ctx context.Context) {
  18. obj := map[int][]byte{}
  19. valType := reflect2.TypeOf(obj).(reflect2.MapType)
  20. hello := []byte("hello")
  21. valType.UnsafeSetIndex(reflect2.PtrOf(obj), reflect2.PtrOf(2), reflect2.PtrOf(hello))
  22. valType.UnsafeSetIndex(reflect2.PtrOf(obj), reflect2.PtrOf(3), nil)
  23. must.Equal([]byte("hello"), obj[2])
  24. must.Nil(obj[3])
  25. }))
  26. t.Run("UnsafeGetIndex", test.Case(func(ctx context.Context) {
  27. obj := map[int][]byte{2: []byte("hello")}
  28. valType := reflect2.TypeOf(obj).(reflect2.MapType)
  29. elem := valType.UnsafeGetIndex(reflect2.PtrOf(obj), reflect2.PtrOf(2))
  30. must.Equal([]byte("hello"), valType.Elem().PackEFace(elem))
  31. }))
  32. }