github.com/coreos/mantle@v0.13.0/lang/natsort/sort_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 "math/rand" 19 "strings" 20 "testing" 21 22 "github.com/kylelemons/godebug/pretty" 23 ) 24 25 const ( 26 testDates = `2000-1-10 27 2000-1-2 28 1999-12-25 29 2000-3-23 30 1999-3-3` 31 sortedDates = `1999-3-3 32 1999-12-25 33 2000-1-2 34 2000-1-10 35 2000-3-23` 36 testFractions = `Fractional release numbers 37 1.011.02 38 1.010.12 39 1.009.02 40 1.009.20 41 1.009.10 42 1.002.08 43 1.002.03 44 1.002.01` 45 sortedFractions = `1.002.01 46 1.002.03 47 1.002.08 48 1.009.02 49 1.009.10 50 1.009.20 51 1.010.12 52 1.011.02 53 Fractional release numbers` 54 testWords = `fred 55 pic2 56 pic100a 57 pic120 58 pic121 59 jane 60 tom 61 pic02a 62 pic3 63 pic4 64 1-20 65 pic100 66 pic02000 67 10-20 68 1-02 69 1-2 70 x2-y7 71 x8-y8 72 x2-y08 73 x2-g8 74 pic01 75 pic02 76 pic 6 77 pic 7 78 pic 5 79 pic05 80 pic 5 81 pic 5 something 82 pic 4 else` 83 sortedWords = `1-02 84 1-2 85 1-20 86 10-20 87 fred 88 jane 89 pic01 90 pic02 91 pic02a 92 pic02000 93 pic05 94 pic2 95 pic3 96 pic4 97 pic 4 else 98 pic 5 99 pic 5 100 pic 5 something 101 pic 6 102 pic 7 103 pic100 104 pic100a 105 pic120 106 pic121 107 tom 108 x2-g8 109 x2-y08 110 x2-y7 111 x8-y8` 112 testEq = `a a 113 a a 114 aa 115 a a 116 a a 117 a a` 118 sortedEq = `a a 119 a a 120 a a 121 a a 122 a a 123 aa` 124 ) 125 126 func randomize(ss []string) { 127 for i := range ss { 128 j := rand.Intn(i + 1) 129 ss[i], ss[j] = ss[j], ss[i] 130 } 131 } 132 133 func doTestSlice(t *testing.T, testSlice, sortedSlice []string) { 134 Strings(testSlice) 135 if diff := pretty.Compare(sortedSlice, testSlice); diff != "" { 136 t.Errorf("Unexpected order: %s", diff) 137 } 138 } 139 140 func doTest(t *testing.T, testData, sortedData string) { 141 testSlice := strings.Split(testData, "\n") 142 sortedSlice := strings.Split(sortedData, "\n") 143 if !StringsAreSorted(sortedSlice) { 144 t.Errorf("StringsAreSorted claims unsorted: %#v", sortedSlice) 145 } 146 doTestSlice(t, testSlice, sortedSlice) 147 randomize(testSlice) 148 doTestSlice(t, testSlice, sortedSlice) 149 } 150 151 func TestDates(t *testing.T) { 152 doTest(t, testDates, sortedDates) 153 } 154 155 func TestFractions(t *testing.T) { 156 doTest(t, testFractions, sortedFractions) 157 } 158 159 func TestWords(t *testing.T) { 160 doTest(t, testWords, sortedWords) 161 } 162 163 func TestEqual(t *testing.T) { 164 doTest(t, testEq, sortedEq) 165 }