language_test.go - hugo - [fork] hugo port for 9front
(HTM) git clone https://git.drkhsh.at/hugo.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
language_test.go (1825B)
---
1 // Copyright 2018 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 langs
15
16 import (
17 "sync"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "golang.org/x/text/collate"
22 "golang.org/x/text/language"
23 )
24
25 func TestCollator(t *testing.T) {
26 c := qt.New(t)
27
28 var wg sync.WaitGroup
29
30 coll := &Collator{c: collate.New(language.English, collate.Loose)}
31
32 for range 10 {
33 wg.Add(1)
34 go func() {
35 coll.Lock()
36 defer coll.Unlock()
37 defer wg.Done()
38 for range 10 {
39 k := coll.CompareStrings("abc", "def")
40 c.Assert(k, qt.Equals, -1)
41 }
42 }()
43 }
44 wg.Wait()
45 }
46
47 func BenchmarkCollator(b *testing.B) {
48 s := []string{"foo", "bar", "éntre", "baz", "qux", "quux", "corge", "grault", "garply", "waldo", "fred", "plugh", "xyzzy", "thud"}
49
50 doWork := func(coll *Collator) {
51 for i := range s {
52 for j := i + 1; j < len(s); j++ {
53 _ = coll.CompareStrings(s[i], s[j])
54 }
55 }
56 }
57
58 b.Run("Single", func(b *testing.B) {
59 coll := &Collator{c: collate.New(language.English, collate.Loose)}
60 for i := 0; i < b.N; i++ {
61 doWork(coll)
62 }
63 })
64
65 b.Run("Para", func(b *testing.B) {
66 b.RunParallel(func(pb *testing.PB) {
67 coll := &Collator{c: collate.New(language.English, collate.Loose)}
68
69 for pb.Next() {
70 coll.Lock()
71 doWork(coll)
72 coll.Unlock()
73 }
74 })
75 })
76 }