You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

119 lines
3.5 KiB

  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package firestore
  15. import (
  16. "math"
  17. "testing"
  18. "time"
  19. pb "google.golang.org/genproto/googleapis/firestore/v1"
  20. "google.golang.org/genproto/googleapis/type/latlng"
  21. )
  22. func TestCompareValues(t *testing.T) {
  23. // Ordered list of values.
  24. vals := []*pb.Value{
  25. nullValue,
  26. boolval(false),
  27. boolval(true),
  28. floatval(math.NaN()),
  29. floatval(math.Inf(-1)),
  30. floatval(-math.MaxFloat64),
  31. int64val(math.MinInt64),
  32. floatval(-1.1),
  33. intval(-1),
  34. intval(0),
  35. floatval(math.SmallestNonzeroFloat64),
  36. intval(1),
  37. floatval(1.1),
  38. intval(2),
  39. int64val(math.MaxInt64),
  40. floatval(math.MaxFloat64),
  41. floatval(math.Inf(1)),
  42. tsval(time.Date(2016, 5, 20, 10, 20, 0, 0, time.UTC)),
  43. tsval(time.Date(2016, 10, 21, 15, 32, 0, 0, time.UTC)),
  44. strval(""),
  45. strval("\u0000\ud7ff\ue000\uffff"),
  46. strval("(╯°□°)╯︵ ┻━┻"),
  47. strval("a"),
  48. strval("abc def"),
  49. strval("e\u0301b"),
  50. strval("æ"),
  51. strval("\u00e9a"),
  52. bytesval([]byte{}),
  53. bytesval([]byte{0}),
  54. bytesval([]byte{0, 1, 2, 3, 4}),
  55. bytesval([]byte{0, 1, 2, 4, 3}),
  56. bytesval([]byte{255}),
  57. refval("projects/p1/databases/d1/documents/c1/doc1"),
  58. refval("projects/p1/databases/d1/documents/c1/doc2"),
  59. refval("projects/p1/databases/d1/documents/c1/doc2/c2/doc1"),
  60. refval("projects/p1/databases/d1/documents/c1/doc2/c2/doc2"),
  61. refval("projects/p1/databases/d1/documents/c10/doc1"),
  62. refval("projects/p1/databases/dkkkkklkjnjkkk1/documents/c2/doc1"),
  63. refval("projects/p2/databases/d2/documents/c1/doc1"),
  64. refval("projects/p2/databases/d2/documents/c1-/doc1"),
  65. geopoint(-90, -180),
  66. geopoint(-90, 0),
  67. geopoint(-90, 180),
  68. geopoint(0, -180),
  69. geopoint(0, 0),
  70. geopoint(0, 180),
  71. geopoint(1, -180),
  72. geopoint(1, 0),
  73. geopoint(1, 180),
  74. geopoint(90, -180),
  75. geopoint(90, 0),
  76. geopoint(90, 180),
  77. arrayval(),
  78. arrayval(strval("bar")),
  79. arrayval(strval("foo")),
  80. arrayval(strval("foo"), intval(1)),
  81. arrayval(strval("foo"), intval(2)),
  82. arrayval(strval("foo"), strval("0")),
  83. mapval(map[string]*pb.Value{"bar": intval(0)}),
  84. mapval(map[string]*pb.Value{"bar": intval(0), "foo": intval(1)}),
  85. mapval(map[string]*pb.Value{"foo": intval(1)}),
  86. mapval(map[string]*pb.Value{"foo": intval(2)}),
  87. mapval(map[string]*pb.Value{"foo": strval("0")}),
  88. }
  89. for i, v1 := range vals {
  90. if got := compareValues(v1, v1); got != 0 {
  91. t.Errorf("compare(%v, %v) == %d, want 0", v1, v1, got)
  92. }
  93. for _, v2 := range vals[i+1:] {
  94. if got := compareValues(v1, v2); got != -1 {
  95. t.Errorf("compare(%v, %v) == %d, want -1", v1, v2, got)
  96. }
  97. if got := compareValues(v2, v1); got != 1 {
  98. t.Errorf("compare(%v, %v) == %d, want 1", v1, v2, got)
  99. }
  100. }
  101. }
  102. // Integers and Doubles order the same.
  103. n1 := intval(17)
  104. n2 := floatval(17)
  105. if got := compareValues(n1, n2); got != 0 {
  106. t.Errorf("compare(%v, %v) == %d, want 0", n1, n2, got)
  107. }
  108. }
  109. func geopoint(lat, lng float64) *pb.Value {
  110. return geoval(&latlng.LatLng{Latitude: lat, Longitude: lng})
  111. }