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