mapsort_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package mapsort_test
  5. import (
  6. "reflect"
  7. "testing"
  8. "google.golang.org/protobuf/internal/mapsort"
  9. "google.golang.org/protobuf/internal/value"
  10. pref "google.golang.org/protobuf/reflect/protoreflect"
  11. )
  12. func TestRange(t *testing.T) {
  13. for _, test := range []struct {
  14. mapv interface{}
  15. kind pref.Kind
  16. }{
  17. {
  18. mapv: &map[bool]int32{
  19. false: 0,
  20. true: 1,
  21. },
  22. kind: pref.BoolKind,
  23. },
  24. {
  25. mapv: &map[int32]int32{
  26. 0: 0,
  27. 1: 1,
  28. 2: 2,
  29. },
  30. kind: pref.Int32Kind,
  31. },
  32. {
  33. mapv: &map[uint64]int32{
  34. 0: 0,
  35. 1: 1,
  36. 2: 2,
  37. },
  38. kind: pref.Uint64Kind,
  39. },
  40. {
  41. mapv: &map[string]int32{
  42. "a": 0,
  43. "b": 1,
  44. "c": 2,
  45. },
  46. kind: pref.StringKind,
  47. },
  48. } {
  49. rv := reflect.TypeOf(test.mapv).Elem()
  50. mapv := value.MapOf(test.mapv, value.NewConverter(rv.Key(), test.kind), value.NewConverter(rv.Elem(), pref.Int32Kind))
  51. var got []pref.MapKey
  52. mapsort.Range(mapv, test.kind, func(key pref.MapKey, _ pref.Value) bool {
  53. got = append(got, key)
  54. return true
  55. })
  56. for i, key := range got {
  57. if int64(i) != mapv.Get(key).Int() {
  58. t.Errorf("out of order range over map: %v", got)
  59. break
  60. }
  61. }
  62. }
  63. }