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.
7 // Derived from bytes/compare_test.go.
8 // Benchmarks omitted since the underlying implementation is identical.
15 var compareTests
= []struct {
29 // test runtime·memeq's chunked implementation
30 {"abcdefgh", "abcdefgh", 0},
31 {"abcdefghi", "abcdefghi", 0},
32 {"abcdefghi", "abcdefghj", -1},
35 func TestCompare(t
*testing
.T
) {
36 for _
, tt
:= range compareTests
{
37 cmp
:= Compare(tt
.a
, tt
.b
)
39 t
.Errorf(`Compare(%q, %q) = %v`, tt
.a
, tt
.b
, cmp
)
44 func TestCompareIdenticalString(t
*testing
.T
) {
45 var s
= "Hello Gophers!"
46 if Compare(s
, s
) != 0 {
49 if Compare(s
, s
[:1]) != 1 {
50 t
.Error("s > s[:1] failed")
54 func TestCompareStrings(t
*testing
.T
) {
56 a
:= make([]byte, n
+1)
57 b
:= make([]byte, n
+1)
58 for len := 0; len < 128; len++ {
59 // randomish but deterministic data. No 0 or 255.
60 for i
:= 0; i
< len; i
++ {
61 a
[i
] = byte(1 + 31*i%254
)
62 b
[i
] = byte(1 + 31*i%254
)
64 // data past the end is different
65 for i
:= len; i
<= n
; i
++ {
70 cmp
:= Compare(string(a
[:len]), string(b
[:len]))
72 t
.Errorf(`CompareIdentical(%d) = %d`, len, cmp
)
75 cmp
= Compare(string(a
[:len-1]), string(b
[:len]))
77 t
.Errorf(`CompareAshorter(%d) = %d`, len, cmp
)
79 cmp
= Compare(string(a
[:len]), string(b
[:len-1]))
81 t
.Errorf(`CompareBshorter(%d) = %d`, len, cmp
)
84 for k
:= 0; k
< len; k
++ {
86 cmp
= Compare(string(a
[:len]), string(b
[:len]))
88 t
.Errorf(`CompareAbigger(%d,%d) = %d`, len, k
, cmp
)
91 cmp
= Compare(string(a
[:len]), string(b
[:len]))
93 t
.Errorf(`CompareBbigger(%d,%d) = %d`, len, k
, cmp
)