github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/strings/compare_test.go (about)

     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  )
    15  
    16  var compareTests = []struct {
    17  	a, b string
    18  	i    int
    19  }{
    20  	{"", "", 0},
    21  	{"a", "", 1},
    22  	{"", "a", -1},
    23  	{"abc", "abc", 0},
    24  	{"ab", "abc", -1},
    25  	{"abc", "ab", 1},
    26  	{"x", "ab", 1},
    27  	{"ab", "x", -1},
    28  	{"x", "a", 1},
    29  	{"b", "x", -1},
    30  	// test runtime·memeq's chunked implementation
    31  	{"abcdefgh", "abcdefgh", 0},
    32  	{"abcdefghi", "abcdefghi", 0},
    33  	{"abcdefghi", "abcdefghj", -1},
    34  }
    35  
    36  func TestCompare(t *testing.T) {
    37  	for _, tt := range compareTests {
    38  		cmp := Compare(tt.a, tt.b)
    39  		if cmp != tt.i {
    40  			t.Errorf(`Compare(%q, %q) = %v`, tt.a, tt.b, cmp)
    41  		}
    42  	}
    43  }
    44  
    45  func TestCompareIdenticalString(t *testing.T) {
    46  	var s = "Hello Gophers!"
    47  	if Compare(s, s) != 0 {
    48  		t.Error("s != s")
    49  	}
    50  	if Compare(s, s[:1]) != 1 {
    51  		t.Error("s > s[:1] failed")
    52  	}
    53  }
    54  
    55  func TestCompareStrings(t *testing.T) {
    56  	lengths := make([]int, 0) // lengths to test in ascending order
    57  	for i := 0; i <= 128; i++ {
    58  		lengths = append(lengths, i)
    59  	}
    60  	lengths = append(lengths, 256, 512, 1024, 1333, 4095, 4096, 4097)
    61  
    62  	if !testing.Short() || testenv.Builder() != "" {
    63  		lengths = append(lengths, 65535, 65536, 65537, 99999)
    64  	}
    65  
    66  	n := lengths[len(lengths)-1]
    67  	a := make([]byte, n+1)
    68  	b := make([]byte, n+1)
    69  	lastLen := 0
    70  	for _, len := range lengths {
    71  		// randomish but deterministic data. No 0 or 255.
    72  		for i := 0; i < len; i++ {
    73  			a[i] = byte(1 + 31*i%254)
    74  			b[i] = byte(1 + 31*i%254)
    75  		}
    76  		// data past the end is different
    77  		for i := len; i <= n; i++ {
    78  			a[i] = 8
    79  			b[i] = 9
    80  		}
    81  
    82  		sa, sb := string(a), string(b)
    83  		cmp := Compare(sa[:len], sb[:len])
    84  		if cmp != 0 {
    85  			t.Errorf(`CompareIdentical(%d) = %d`, len, cmp)
    86  		}
    87  		if len > 0 {
    88  			cmp = Compare(sa[:len-1], sb[:len])
    89  			if cmp != -1 {
    90  				t.Errorf(`CompareAshorter(%d) = %d`, len, cmp)
    91  			}
    92  			cmp = Compare(sa[:len], sb[:len-1])
    93  			if cmp != 1 {
    94  				t.Errorf(`CompareBshorter(%d) = %d`, len, cmp)
    95  			}
    96  		}
    97  		for k := lastLen; k < len; k++ {
    98  			b[k] = a[k] - 1
    99  			cmp = Compare(string(a[:len]), string(b[:len]))
   100  			if cmp != 1 {
   101  				t.Errorf(`CompareAbigger(%d,%d) = %d`, len, k, cmp)
   102  			}
   103  			b[k] = a[k] + 1
   104  			cmp = Compare(string(a[:len]), string(b[:len]))
   105  			if cmp != -1 {
   106  				t.Errorf(`CompareBbigger(%d,%d) = %d`, len, k, cmp)
   107  			}
   108  			b[k] = a[k]
   109  		}
   110  		lastLen = len
   111  	}
   112  }