github.com/coreos/mantle@v0.13.0/lang/natsort/cmp_test.go (about)

     1  // Copyright 2016 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package natsort
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  )
    21  
    22  func testCompare(t *testing.T, a, b string) {
    23  	if result := Compare(a, b); result != -1 {
    24  		t.Errorf("Compare(%q, %q) = %+d, expected -1", a, b, result)
    25  	}
    26  	if result := Compare(b, a); result != +1 {
    27  		t.Errorf("Compare(%q, %q) = %+d, expected +1", a, b, result)
    28  	}
    29  }
    30  
    31  func testCompareEq(t *testing.T, a, b string) {
    32  	if result := Compare(a, b); result != 0 {
    33  		t.Errorf("Compare(%q, %q) = %+d, expected 0", a, b, result)
    34  	}
    35  }
    36  
    37  func testList(t *testing.T, l []string) {
    38  	for i := 0; i < len(l)-1; i++ {
    39  		testCompare(t, l[i], l[i+1])
    40  	}
    41  }
    42  
    43  func TestCompare01(t *testing.T) {
    44  	testCompare(t, "01", "02")
    45  }
    46  
    47  func TestCompare02(t *testing.T) {
    48  	testCompare(t, "02", "2")
    49  }
    50  
    51  func TestCompare10(t *testing.T) {
    52  	testCompare(t, "2", "10")
    53  }
    54  
    55  func TestCompare100a(t *testing.T) {
    56  	testCompare(t, "100a", "120")
    57  }
    58  
    59  func TestCompare001a(t *testing.T) {
    60  	testCompare(t, "001a", "0012")
    61  }
    62  
    63  func TestCompareSpace(t *testing.T) {
    64  	testCompare(t, "a 1", "a2")
    65  	testCompare(t, "a1", "a 2")
    66  	testCompare(t, " 1", "2")
    67  	testCompare(t, "1", " 2")
    68  	testCompareEq(t, "a a", "aa")
    69  }
    70  
    71  func TestExample1(t *testing.T) {
    72  	testList(t, strings.Split("a a0 a1 a1a a1b a2 a10 a20", " "))
    73  }
    74  
    75  func TestExample2(t *testing.T) {
    76  	testList(t, strings.Split("1.001 1.002 1.010 1.02 1.1 1.3", " "))
    77  }
    78  
    79  func BenchmarkCompareFraction(b *testing.B) {
    80  	for i := 0; i < b.N; i++ {
    81  		Compare("0000005", "0000006")
    82  	}
    83  }
    84  
    85  func BenchmarkCompareInteger(b *testing.B) {
    86  	for i := 0; i < b.N; i++ {
    87  		Compare("5000000", "6000000")
    88  	}
    89  }
    90  
    91  func BenchmarkCompareWords(b *testing.B) {
    92  	for i := 0; i < b.N; i++ {
    93  		Compare("notnum.", "notnum!")
    94  	}
    95  }