hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

compare_strings_test.go (2000B)

    1 // Copyright 2019 The Hugo Authors. All rights reserved.
    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 // http://www.apache.org/licenses/LICENSE-2.0
    7 //
    8 // Unless required by applicable law or agreed to in writing, software
    9 // distributed under the License is distributed on an "AS IS" BASIS,
   10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   11 // See the License for the specific language governing permissions and
   12 // limitations under the License.
   13 
   14 package compare
   15 
   16 import (
   17 	"sort"
   18 	"strings"
   19 	"testing"
   20 
   21 	qt "github.com/frankban/quicktest"
   22 )
   23 
   24 func TestCompare(t *testing.T) {
   25 	c := qt.New(t)
   26 	for _, test := range []struct {
   27 		a string
   28 		b string
   29 	}{
   30 		{"a", "a"},
   31 		{"A", "a"},
   32 		{"Ab", "Ac"},
   33 		{"az", "Za"},
   34 		{"C", "D"},
   35 		{"B", "a"},
   36 		{"C", ""},
   37 		{"", ""},
   38 		{"αβδC", "ΑΒΔD"},
   39 		{"αβδC", "ΑΒΔ"},
   40 		{"αβδ", "ΑΒΔD"},
   41 		{"αβδ", "ΑΒΔ"},
   42 		{"β", "δ"},
   43 		{"好", strings.ToLower("好")},
   44 	} {
   45 
   46 		expect := strings.Compare(strings.ToLower(test.a), strings.ToLower(test.b))
   47 		got := compareFold(test.a, test.b)
   48 
   49 		c.Assert(got, qt.Equals, expect)
   50 
   51 	}
   52 }
   53 
   54 func TestLexicographicSort(t *testing.T) {
   55 	c := qt.New(t)
   56 
   57 	s := []string{"b", "Bz", "ba", "A", "Ba", "ba"}
   58 
   59 	sort.Slice(s, func(i, j int) bool {
   60 		return LessStrings(s[i], s[j])
   61 	})
   62 
   63 	c.Assert(s, qt.DeepEquals, []string{"A", "b", "Ba", "ba", "ba", "Bz"})
   64 }
   65 
   66 func BenchmarkStringSort(b *testing.B) {
   67 	prototype := []string{"b", "Bz", "zz", "ba", "αβδ αβδ αβδ", "A", "Ba", "ba", "nnnnasdfnnn", "AAgæåz", "αβδC"}
   68 	b.Run("LessStrings", func(b *testing.B) {
   69 		ss := make([][]string, b.N)
   70 		for i := 0; i < b.N; i++ {
   71 			ss[i] = make([]string, len(prototype))
   72 			copy(ss[i], prototype)
   73 		}
   74 		b.ResetTimer()
   75 		for i := 0; i < b.N; i++ {
   76 			sss := ss[i]
   77 			sort.Slice(sss, func(i, j int) bool {
   78 				return LessStrings(sss[i], sss[j])
   79 			})
   80 		}
   81 	})
   82 
   83 }