protobuf-go/internal/mapsort/mapsort_test.go
Damien Neil e89e6244e0 all: change module to google.golang.org/protobuf
Temporarily remove go.mod, since we can't generate an accurate one until
the corresponding v1 change is submitted.

Change-Id: I1e1ad97f2b455e33f61ffaeb8676289795e47e72
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177000
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-14 17:28:29 +00:00

68 lines
1.3 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mapsort_test
import (
"reflect"
"testing"
"google.golang.org/protobuf/internal/mapsort"
"google.golang.org/protobuf/internal/value"
pref "google.golang.org/protobuf/reflect/protoreflect"
)
func TestRange(t *testing.T) {
for _, test := range []struct {
mapv interface{}
kind pref.Kind
}{
{
mapv: &map[bool]int32{
false: 0,
true: 1,
},
kind: pref.BoolKind,
},
{
mapv: &map[int32]int32{
0: 0,
1: 1,
2: 2,
},
kind: pref.Int32Kind,
},
{
mapv: &map[uint64]int32{
0: 0,
1: 1,
2: 2,
},
kind: pref.Uint64Kind,
},
{
mapv: &map[string]int32{
"a": 0,
"b": 1,
"c": 2,
},
kind: pref.StringKind,
},
} {
rv := reflect.TypeOf(test.mapv).Elem()
mapv := value.MapOf(test.mapv, value.NewConverter(rv.Key(), test.kind), value.NewConverter(rv.Elem(), pref.Int32Kind))
var got []pref.MapKey
mapsort.Range(mapv, test.kind, func(key pref.MapKey, _ pref.Value) bool {
got = append(got, key)
return true
})
for i, key := range got {
if int64(i) != mapv.Get(key).Int() {
t.Errorf("out of order range over map: %v", got)
break
}
}
}
}