github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/langs/language_test.go (about)

     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  
    27  	c := qt.New(t)
    28  
    29  	var wg sync.WaitGroup
    30  
    31  	coll := &Collator{c: collate.New(language.English, collate.Loose)}
    32  
    33  	for i := 0; i < 10; i++ {
    34  		wg.Add(1)
    35  		go func() {
    36  			coll.Lock()
    37  			defer coll.Unlock()
    38  			defer wg.Done()
    39  			for j := 0; j < 10; j++ {
    40  				k := coll.CompareStrings("abc", "def")
    41  				c.Assert(k, qt.Equals, -1)
    42  			}
    43  		}()
    44  	}
    45  	wg.Wait()
    46  
    47  }
    48  
    49  func BenchmarkCollator(b *testing.B) {
    50  	s := []string{"foo", "bar", "éntre", "baz", "qux", "quux", "corge", "grault", "garply", "waldo", "fred", "plugh", "xyzzy", "thud"}
    51  
    52  	doWork := func(coll *Collator) {
    53  		for i := 0; i < len(s); i++ {
    54  			for j := i + 1; j < len(s); j++ {
    55  				_ = coll.CompareStrings(s[i], s[j])
    56  			}
    57  		}
    58  	}
    59  
    60  	b.Run("Single", func(b *testing.B) {
    61  		coll := &Collator{c: collate.New(language.English, collate.Loose)}
    62  		for i := 0; i < b.N; i++ {
    63  			doWork(coll)
    64  		}
    65  	})
    66  
    67  	b.Run("Para", func(b *testing.B) {
    68  		b.RunParallel(func(pb *testing.PB) {
    69  			coll := &Collator{c: collate.New(language.English, collate.Loose)}
    70  
    71  			for pb.Next() {
    72  				coll.Lock()
    73  				doWork(coll)
    74  				coll.Unlock()
    75  			}
    76  		})
    77  	})
    78  
    79  }