From 627eed1d620910f494056330733db6c6187b8fe9 Mon Sep 17 00:00:00 2001 From: Bjørn Erik Pedersen Date: Sun, 10 Apr 2022 20:30:52 +0200 Subject: Make string sorting (e.g. ByTitle, ByLinkTitle and ByParam) language aware Fixes #2180 --- langs/language_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'langs/language_test.go') diff --git a/langs/language_test.go b/langs/language_test.go index e6ef94824..264e813a0 100644 --- a/langs/language_test.go +++ b/langs/language_test.go @@ -14,10 +14,13 @@ package langs import ( + "sync" "testing" qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/config" + "golang.org/x/text/collate" + "golang.org/x/text/language" ) func TestGetGlobalOnlySetting(t *testing.T) { @@ -47,3 +50,59 @@ func TestLanguageParams(t *testing.T) { c.Assert(lang.Params()["p1"], qt.Equals, "p1p") c.Assert(lang.Get("p1"), qt.Equals, "p1cfg") } + +func TestCollator(t *testing.T) { + + c := qt.New(t) + + var wg sync.WaitGroup + + coll := &Collator{c: collate.New(language.English, collate.Loose)} + + for i := 0; i < 10; i++ { + wg.Add(1) + go func() { + coll.Lock() + defer coll.Unlock() + defer wg.Done() + for j := 0; j < 10; j++ { + k := coll.CompareStrings("abc", "def") + c.Assert(k, qt.Equals, -1) + } + }() + } + wg.Wait() + +} + +func BenchmarkCollator(b *testing.B) { + s := []string{"foo", "bar", "éntre", "baz", "qux", "quux", "corge", "grault", "garply", "waldo", "fred", "plugh", "xyzzy", "thud"} + + doWork := func(coll *Collator) { + for i := 0; i < len(s); i++ { + for j := i + 1; j < len(s); j++ { + _ = coll.CompareStrings(s[i], s[j]) + } + } + } + + b.Run("Single", func(b *testing.B) { + coll := &Collator{c: collate.New(language.English, collate.Loose)} + for i := 0; i < b.N; i++ { + doWork(coll) + } + }) + + b.Run("Para", func(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + coll := &Collator{c: collate.New(language.English, collate.Loose)} + + for pb.Next() { + coll.Lock() + doWork(coll) + coll.Unlock() + } + }) + }) + +} -- cgit v1.2.3