mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 11:07:46 +00:00
102 lines
1.7 KiB
Go
102 lines
1.7 KiB
Go
package reflect_test
|
|
|
|
import (
|
|
. "reflect"
|
|
"sort"
|
|
"testing"
|
|
)
|
|
|
|
func TestIndirectPointers(t *testing.T) {
|
|
var m = map[string]int{}
|
|
m["x"] = 1
|
|
|
|
var a = &m
|
|
|
|
if ValueOf(a).Elem().Len() != 1 {
|
|
t.Errorf("bad map length via reflect")
|
|
}
|
|
|
|
var b struct {
|
|
Decoded *[3]byte
|
|
}
|
|
|
|
v1 := New(TypeOf(b.Decoded).Elem())
|
|
|
|
var bb [3]byte
|
|
bb[0] = 0xaa
|
|
|
|
v1.Elem().Set(ValueOf(bb))
|
|
|
|
if v1.Elem().Index(0).Uint() != 0xaa {
|
|
t.Errorf("bad indirect array index via reflect")
|
|
}
|
|
}
|
|
|
|
func TestMap(t *testing.T) {
|
|
|
|
m := make(map[string]int)
|
|
|
|
mtyp := TypeOf(m)
|
|
|
|
if got, want := mtyp.Key().Kind().String(), "string"; got != want {
|
|
t.Errorf("m.Type().Key().String()=%q, want %q", got, want)
|
|
}
|
|
|
|
if got, want := mtyp.Elem().Kind().String(), "int"; got != want {
|
|
t.Errorf("m.Elem().String()=%q, want %q", got, want)
|
|
}
|
|
|
|
m["foo"] = 2
|
|
|
|
mref := ValueOf(m)
|
|
two := mref.MapIndex(ValueOf("foo"))
|
|
|
|
if got, want := two.Interface().(int), 2; got != want {
|
|
t.Errorf("MapIndex(`foo`)=%v, want %v", got, want)
|
|
}
|
|
|
|
m["bar"] = 3
|
|
m["baz"] = 4
|
|
m["qux"] = 5
|
|
|
|
it := mref.MapRange()
|
|
|
|
var gotKeys []string
|
|
for it.Next() {
|
|
k := it.Key()
|
|
v := it.Value()
|
|
|
|
kstr := k.Interface().(string)
|
|
vint := v.Interface().(int)
|
|
|
|
gotKeys = append(gotKeys, kstr)
|
|
|
|
if m[kstr] != vint {
|
|
t.Errorf("m[%v]=%v, want %v", kstr, vint, m[kstr])
|
|
}
|
|
}
|
|
var wantKeys []string
|
|
for k := range m {
|
|
wantKeys = append(wantKeys, k)
|
|
}
|
|
sort.Strings(gotKeys)
|
|
sort.Strings(wantKeys)
|
|
|
|
if !equal(gotKeys, wantKeys) {
|
|
t.Errorf("MapRange return unexpected keys: got %v, want %v", gotKeys, wantKeys)
|
|
}
|
|
}
|
|
|
|
func equal[T comparable](a, b []T) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
|
|
for i, aa := range a {
|
|
if b[i] != aa {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|