compare_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2013 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 bytes_test
  5. import (
  6. . "bytes"
  7. "testing"
  8. )
  9. var compareTests = []struct {
  10. a, b []byte
  11. i int
  12. }{
  13. {[]byte(""), []byte(""), 0},
  14. {[]byte("a"), []byte(""), 1},
  15. {[]byte(""), []byte("a"), -1},
  16. {[]byte("abc"), []byte("abc"), 0},
  17. {[]byte("ab"), []byte("abc"), -1},
  18. {[]byte("abc"), []byte("ab"), 1},
  19. {[]byte("x"), []byte("ab"), 1},
  20. {[]byte("ab"), []byte("x"), -1},
  21. {[]byte("x"), []byte("a"), 1},
  22. {[]byte("b"), []byte("x"), -1},
  23. // test runtime·memeq's chunked implementation
  24. {[]byte("abcdefgh"), []byte("abcdefgh"), 0},
  25. {[]byte("abcdefghi"), []byte("abcdefghi"), 0},
  26. {[]byte("abcdefghi"), []byte("abcdefghj"), -1},
  27. // nil tests
  28. {nil, nil, 0},
  29. {[]byte(""), nil, 0},
  30. {nil, []byte(""), 0},
  31. {[]byte("a"), nil, 1},
  32. {nil, []byte("a"), -1},
  33. }
  34. func TestCompare(t *testing.T) {
  35. for _, tt := range compareTests {
  36. cmp := Compare(tt.a, tt.b)
  37. if cmp != tt.i {
  38. t.Errorf(`Compare(%q, %q) = %v`, tt.a, tt.b, cmp)
  39. }
  40. }
  41. }
  42. func TestCompareIdenticalSlice(t *testing.T) {
  43. var b = []byte("Hello Gophers!")
  44. if Compare(b, b) != 0 {
  45. t.Error("b != b")
  46. }
  47. if Compare(b, b[:1]) != 1 {
  48. t.Error("b > b[:1] failed")
  49. }
  50. }
  51. func TestCompareBytes(t *testing.T) {
  52. n := 128
  53. a := make([]byte, n+1)
  54. b := make([]byte, n+1)
  55. for len := 0; len < 128; len++ {
  56. // randomish but deterministic data. No 0 or 255.
  57. for i := 0; i < len; i++ {
  58. a[i] = byte(1 + 31*i%254)
  59. b[i] = byte(1 + 31*i%254)
  60. }
  61. // data past the end is different
  62. for i := len; i <= n; i++ {
  63. a[i] = 8
  64. b[i] = 9
  65. }
  66. cmp := Compare(a[:len], b[:len])
  67. if cmp != 0 {
  68. t.Errorf(`CompareIdentical(%d) = %d`, len, cmp)
  69. }
  70. if len > 0 {
  71. cmp = Compare(a[:len-1], b[:len])
  72. if cmp != -1 {
  73. t.Errorf(`CompareAshorter(%d) = %d`, len, cmp)
  74. }
  75. cmp = Compare(a[:len], b[:len-1])
  76. if cmp != 1 {
  77. t.Errorf(`CompareBshorter(%d) = %d`, len, cmp)
  78. }
  79. }
  80. for k := 0; k < len; k++ {
  81. b[k] = a[k] - 1
  82. cmp = Compare(a[:len], b[:len])
  83. if cmp != 1 {
  84. t.Errorf(`CompareAbigger(%d,%d) = %d`, len, k, cmp)
  85. }
  86. b[k] = a[k] + 1
  87. cmp = Compare(a[:len], b[:len])
  88. if cmp != -1 {
  89. t.Errorf(`CompareBbigger(%d,%d) = %d`, len, k, cmp)
  90. }
  91. b[k] = a[k]
  92. }
  93. }
  94. }
  95. func BenchmarkCompareBytesEqual(b *testing.B) {
  96. b1 := []byte("Hello Gophers!")
  97. b2 := []byte("Hello Gophers!")
  98. for i := 0; i < b.N; i++ {
  99. if Compare(b1, b2) != 0 {
  100. b.Fatal("b1 != b2")
  101. }
  102. }
  103. }
  104. func BenchmarkCompareBytesToNil(b *testing.B) {
  105. b1 := []byte("Hello Gophers!")
  106. var b2 []byte
  107. for i := 0; i < b.N; i++ {
  108. if Compare(b1, b2) != 1 {
  109. b.Fatal("b1 > b2 failed")
  110. }
  111. }
  112. }
  113. func BenchmarkCompareBytesEmpty(b *testing.B) {
  114. b1 := []byte("")
  115. b2 := b1
  116. for i := 0; i < b.N; i++ {
  117. if Compare(b1, b2) != 0 {
  118. b.Fatal("b1 != b2")
  119. }
  120. }
  121. }
  122. func BenchmarkCompareBytesIdentical(b *testing.B) {
  123. b1 := []byte("Hello Gophers!")
  124. b2 := b1
  125. for i := 0; i < b.N; i++ {
  126. if Compare(b1, b2) != 0 {
  127. b.Fatal("b1 != b2")
  128. }
  129. }
  130. }
  131. func BenchmarkCompareBytesSameLength(b *testing.B) {
  132. b1 := []byte("Hello Gophers!")
  133. b2 := []byte("Hello, Gophers")
  134. for i := 0; i < b.N; i++ {
  135. if Compare(b1, b2) != -1 {
  136. b.Fatal("b1 < b2 failed")
  137. }
  138. }
  139. }
  140. func BenchmarkCompareBytesDifferentLength(b *testing.B) {
  141. b1 := []byte("Hello Gophers!")
  142. b2 := []byte("Hello, Gophers!")
  143. for i := 0; i < b.N; i++ {
  144. if Compare(b1, b2) != -1 {
  145. b.Fatal("b1 < b2 failed")
  146. }
  147. }
  148. }
  149. func BenchmarkCompareBytesBigUnaligned(b *testing.B) {
  150. b.StopTimer()
  151. b1 := make([]byte, 0, 1<<20)
  152. for len(b1) < 1<<20 {
  153. b1 = append(b1, "Hello Gophers!"...)
  154. }
  155. b2 := append([]byte("hello"), b1...)
  156. b.StartTimer()
  157. for i := 0; i < b.N; i++ {
  158. if Compare(b1, b2[len("hello"):]) != 0 {
  159. b.Fatal("b1 != b2")
  160. }
  161. }
  162. b.SetBytes(int64(len(b1)))
  163. }
  164. func BenchmarkCompareBytesBig(b *testing.B) {
  165. b.StopTimer()
  166. b1 := make([]byte, 0, 1<<20)
  167. for len(b1) < 1<<20 {
  168. b1 = append(b1, "Hello Gophers!"...)
  169. }
  170. b2 := append([]byte{}, b1...)
  171. b.StartTimer()
  172. for i := 0; i < b.N; i++ {
  173. if Compare(b1, b2) != 0 {
  174. b.Fatal("b1 != b2")
  175. }
  176. }
  177. b.SetBytes(int64(len(b1)))
  178. }
  179. func BenchmarkCompareBytesBigIdentical(b *testing.B) {
  180. b.StopTimer()
  181. b1 := make([]byte, 0, 1<<20)
  182. for len(b1) < 1<<20 {
  183. b1 = append(b1, "Hello Gophers!"...)
  184. }
  185. b2 := b1
  186. b.StartTimer()
  187. for i := 0; i < b.N; i++ {
  188. if Compare(b1, b2) != 0 {
  189. b.Fatal("b1 != b2")
  190. }
  191. }
  192. b.SetBytes(int64(len(b1)))
  193. }