Debian Codegraph @

common/hstrings/strings_test.go

raw
1 // Copyright 2025 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 hstrings
15
16 import (
17 "reflect"
18 "regexp"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 )
23
24 func TestStringEqualFold(t *testing.T) {
25 c := qt.New(t)
26
27 s1 := "A"
28 s2 := "a"
29
30 c.Assert(StringEqualFold(s1).EqualFold(s2), qt.Equals, true)
31 c.Assert(StringEqualFold(s1).EqualFold(s1), qt.Equals, true)
32 c.Assert(StringEqualFold(s2).EqualFold(s1), qt.Equals, true)
33 c.Assert(StringEqualFold(s2).EqualFold(s2), qt.Equals, true)
34 c.Assert(StringEqualFold(s1).EqualFold("b"), qt.Equals, false)
35 c.Assert(StringEqualFold(s1).Eq(s2), qt.Equals, true)
36 c.Assert(StringEqualFold(s1).Eq("b"), qt.Equals, false)
37 }
38
39 func TestGetOrCompileRegexp(t *testing.T) {
40 c := qt.New(t)
41
42 re, err := GetOrCompileRegexp(`\d+`)
43 c.Assert(err, qt.IsNil)
44 c.Assert(re.MatchString("123"), qt.Equals, true)
45 }
46
47 func TestUniqueStrings(t *testing.T) {
48 in := []string{"a", "b", "a", "b", "c", "", "a", "", "d"}
49 output := UniqueStrings(in)
50 expected := []string{"a", "b", "c", "", "d"}
51 if !reflect.DeepEqual(output, expected) {
52 t.Errorf("Expected %#v, got %#v\n", expected, output)
53 }
54 }
55
56 func TestUniqueStringsReuse(t *testing.T) {
57 in := []string{"a", "b", "a", "b", "c", "", "a", "", "d"}
58 output := UniqueStringsReuse(in)
59 expected := []string{"a", "b", "c", "", "d"}
60 if !reflect.DeepEqual(output, expected) {
61 t.Errorf("Expected %#v, got %#v\n", expected, output)
62 }
63 }
64
65 func TestUniqueStringsSorted(t *testing.T) {
66 c := qt.New(t)
67 in := []string{"a", "a", "b", "c", "b", "", "a", "", "d"}
68 output := UniqueStringsSorted(in)
69 expected := []string{"", "a", "b", "c", "d"}
70 c.Assert(output, qt.DeepEquals, expected)
71 c.Assert(UniqueStringsSorted(nil), qt.IsNil)
72 }
73
74 func TestHasUppercase(t *testing.T) {
75 c := qt.New(t)
76
77 c.Assert(HasUppercase("abc"), qt.Equals, false)
78 c.Assert(HasUppercase("Abc"), qt.Equals, true)
79 c.Assert(HasUppercase("aBc"), qt.Equals, true)
80 c.Assert(HasUppercase("abC"), qt.Equals, true)
81 c.Assert(HasUppercase("ABC"), qt.Equals, true)
82 }
83
84 // Note that these cannot use b.Loop() because of golang/go#27217.
85 func BenchmarkUniqueStrings(b *testing.B) {
86 input := []string{"a", "b", "d", "e", "d", "h", "a", "i"}
87
88 b.Run("Safe", func(b *testing.B) {
89 for b.Loop() {
90 result := UniqueStrings(input)
91 if len(result) != 6 {
92 b.Fatalf("invalid count: %d", len(result))
93 }
94 }
95 })
96
97 b.Run("Reuse slice", func(b *testing.B) {
98 inputs := make([][]string, b.N)
99 for i := 0; i < b.N; i++ {
100 inputc := make([]string, len(input))
101 copy(inputc, input)
102 inputs[i] = inputc
103 }
104 b.ResetTimer()
105 for i := 0; i < b.N; i++ {
106 inputc := inputs[i]
107
108 result := UniqueStringsReuse(inputc)
109 if len(result) != 6 {
110 b.Fatalf("invalid count: %d", len(result))
111 }
112 }
113 })
114
115 b.Run("Reuse slice sorted", func(b *testing.B) {
116 inputs := make([][]string, b.N)
117 for i := 0; i < b.N; i++ {
118 inputc := make([]string, len(input))
119 copy(inputc, input)
120 inputs[i] = inputc
121 }
122 b.ResetTimer()
123 for i := 0; i < b.N; i++ {
124 inputc := inputs[i]
125
126 result := UniqueStringsSorted(inputc)
127 if len(result) != 6 {
128 b.Fatalf("invalid count: %d", len(result))
129 }
130 }
131 })
132 }
133
134 func BenchmarkGetOrCompileRegexp(b *testing.B) {
135 for b.Loop() {
136 GetOrCompileRegexp(`\d+`)
137 }
138 }
139
140 func BenchmarkCompileRegexp(b *testing.B) {
141 for b.Loop() {
142 regexp.MustCompile(`\d+`)
143 }
144 }