Source file src/strings/compare_test.go

     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  
     5  package strings_test
     6  
     7  // Derived from bytes/compare_test.go.
     8  // Benchmarks omitted since the underlying implementation is identical.
     9  
    10  import (
    11  	"internal/testenv"
    12  	. "strings"
    13  	"testing"
    14  	"unsafe"
    15  )
    16  
    17  var compareTests = []struct {
    18  	a, b string
    19  	i    int
    20  }{
    21  	{"", "", 0},
    22  	{"a", "", 1},
    23  	{"", "a", -1},
    24  	{"abc", "abc", 0},
    25  	{"ab", "abc", -1},
    26  	{"abc", "ab", 1},
    27  	{"x", "ab", 1},
    28  	{"ab", "x", -1},
    29  	{"x", "a", 1},
    30  	{"b", "x", -1},
    31  	// test runtime·memeq's chunked implementation
    32  	{"abcdefgh", "abcdefgh", 0},
    33  	{"abcdefghi", "abcdefghi", 0},
    34  	{"abcdefghi", "abcdefghj", -1},
    35  }
    36  
    37  func TestCompare(t *testing.T) {
    38  	for _, tt := range compareTests {
    39  		numShifts := 16
    40  		for offset := 0; offset <= numShifts; offset++ {
    41  			shiftedB := (Repeat("*", offset) + tt.b)[offset:]
    42  			cmp := Compare(tt.a, shiftedB)
    43  			if cmp != tt.i {
    44  				t.Errorf(`Compare(%q, %q), offset %d = %v; want %v`, tt.a, tt.b, offset, cmp, tt.i)
    45  			}
    46  		}
    47  	}
    48  }
    49  
    50  func TestCompareIdenticalString(t *testing.T) {
    51  	var s = "Hello Gophers!"
    52  	if Compare(s, s) != 0 {
    53  		t.Error("s != s")
    54  	}
    55  	if Compare(s, s[:1]) != 1 {
    56  		t.Error("s > s[:1] failed")
    57  	}
    58  }
    59  
    60  func TestCompareStrings(t *testing.T) {
    61  	// unsafeString converts a []byte to a string with no allocation.
    62  	// The caller must not modify b while the result string is in use.
    63  	unsafeString := func(b []byte) string {
    64  		return unsafe.String(unsafe.SliceData(b), len(b))
    65  	}
    66  
    67  	lengths := make([]int, 0) // lengths to test in ascending order
    68  	for i := 0; i <= 128; i++ {
    69  		lengths = append(lengths, i)
    70  	}
    71  	lengths = append(lengths, 256, 512, 1024, 1333, 4095, 4096, 4097)
    72  
    73  	if !testing.Short() || testenv.Builder() != "" {
    74  		lengths = append(lengths, 65535, 65536, 65537, 99999)
    75  	}
    76  
    77  	n := lengths[len(lengths)-1]
    78  	a := make([]byte, n+1)
    79  	b := make([]byte, n+1)
    80  	lastLen := 0
    81  	for _, len := range lengths {
    82  		// randomish but deterministic data. No 0 or 255.
    83  		for i := 0; i < len; i++ {
    84  			a[i] = byte(1 + 31*i%254)
    85  			b[i] = byte(1 + 31*i%254)
    86  		}
    87  		// data past the end is different
    88  		for i := len; i <= n; i++ {
    89  			a[i] = 8
    90  			b[i] = 9
    91  		}
    92  
    93  		sa, sb := unsafeString(a), unsafeString(b)
    94  		cmp := Compare(sa[:len], sb[:len])
    95  		if cmp != 0 {
    96  			t.Errorf(`CompareIdentical(%d) = %d`, len, cmp)
    97  		}
    98  		if len > 0 {
    99  			cmp = Compare(sa[:len-1], sb[:len])
   100  			if cmp != -1 {
   101  				t.Errorf(`CompareAshorter(%d) = %d`, len, cmp)
   102  			}
   103  			cmp = Compare(sa[:len], sb[:len-1])
   104  			if cmp != 1 {
   105  				t.Errorf(`CompareBshorter(%d) = %d`, len, cmp)
   106  			}
   107  		}
   108  		for k := lastLen; k < len; k++ {
   109  			b[k] = a[k] - 1
   110  			cmp = Compare(unsafeString(a[:len]), unsafeString(b[:len]))
   111  			if cmp != 1 {
   112  				t.Errorf(`CompareAbigger(%d,%d) = %d`, len, k, cmp)
   113  			}
   114  			b[k] = a[k] + 1
   115  			cmp = Compare(unsafeString(a[:len]), unsafeString(b[:len]))
   116  			if cmp != -1 {
   117  				t.Errorf(`CompareBbigger(%d,%d) = %d`, len, k, cmp)
   118  			}
   119  			b[k] = a[k]
   120  		}
   121  		lastLen = len
   122  	}
   123  }
   124  

View as plain text