Debian Codegraph @

util_test.go

raw
1 package parse
2
3 import (
4 "bytes"
5 "math/rand"
6 "strings"
7 "testing"
8
9 "github.com/tdewolff/test"
10 )
11
12 func helperRandChars(n, m int, chars string) [][]byte {
13 r := make([][]byte, n)
14 for i := range r {
15 for j := 0; j < m; j++ {
16 r[i] = append(r[i], chars[rand.Intn(len(chars))])
17 }
18 }
19 return r
20 }
21
22 func helperRandStrings(n, m int, ss []string) [][]byte {
23 r := make([][]byte, n)
24 for i := range r {
25 for j := 0; j < m; j++ {
26 r[i] = append(r[i], []byte(ss[rand.Intn(len(ss))])...)
27 }
28 }
29 return r
30 }
31
32 ////////////////////////////////////////////////////////////////
33
34 var wsSlices [][]byte
35
36 func init() {
37 wsSlices = helperRandChars(10000, 50, "abcdefg \n\r\f\t")
38 }
39
40 func TestCopy(t *testing.T) {
41 foo := []byte("abc")
42 bar := Copy(foo)
43 foo[0] = 'b'
44 test.String(t, string(foo), "bbc")
45 test.String(t, string(bar), "abc")
46 }
47
48 func TestToLower(t *testing.T) {
49 foo := []byte("Abc")
50 bar := ToLower(foo)
51 bar[1] = 'B'
52 test.String(t, string(foo), "aBc")
53 test.String(t, string(bar), "aBc")
54 }
55
56 func TestEqualFold(t *testing.T) {
57 test.That(t, EqualFold([]byte("Abc"), []byte("abc")))
58 test.That(t, !EqualFold([]byte("Abcd"), []byte("abc")))
59 test.That(t, !EqualFold([]byte("Bbc"), []byte("abc")))
60 test.That(t, !EqualFold([]byte("[]"), []byte("{}"))) // same distance in ASCII as 'a' and 'A'
61 }
62
63 func TestWhitespace(t *testing.T) {
64 test.That(t, IsAllWhitespace([]byte("\t \r\n\f")))
65 test.That(t, !IsAllWhitespace([]byte("\t \r\n\fx")))
66 }
67
68 func TestTrim(t *testing.T) {
69 test.Bytes(t, TrimWhitespace([]byte("a")), []byte("a"))
70 test.Bytes(t, TrimWhitespace([]byte(" a")), []byte("a"))
71 test.Bytes(t, TrimWhitespace([]byte("a ")), []byte("a"))
72 test.Bytes(t, TrimWhitespace([]byte(" ")), []byte(""))
73 }
74
75 func TestPrintable(t *testing.T) {
76 var tests = []struct {
77 s string
78 printable string
79 }{
80 {"a", "a"},
81 {"\x00", "0x00"},
82 {"\x7F", "0x7F"},
83 {"\u0800", "ࠀ"},
84 {"\u200F", "U+200F"},
85 }
86 for _, tt := range tests {
87 t.Run(tt.s, func(t *testing.T) {
88 var printable strings.Builder
89 for _, r := range tt.s {
90 printable.WriteString(Printable(r))
91 }
92 test.T(t, printable.String(), tt.printable)
93 })
94 }
95 }
96
97 ////////////////////////////////////////////////////////////////
98
99 func BenchmarkBytesTrim(b *testing.B) {
100 for i := 0; i < b.N; i++ {
101 for _, e := range wsSlices {
102 bytes.TrimSpace(e)
103 }
104 }
105 }
106
107 func BenchmarkTrim(b *testing.B) {
108 for i := 0; i < b.N; i++ {
109 for _, e := range wsSlices {
110 TrimWhitespace(e)
111 }
112 }
113 }
114
115 func BenchmarkWhitespaceTable(b *testing.B) {
116 n := 0
117 for i := 0; i < b.N; i++ {
118 for _, e := range wsSlices {
119 for _, c := range e {
120 if IsWhitespace(c) {
121 n++
122 }
123 }
124 }
125 }
126 }
127
128 func BenchmarkWhitespaceIf1(b *testing.B) {
129 n := 0
130 for i := 0; i < b.N; i++ {
131 for _, e := range wsSlices {
132 for _, c := range e {
133 if c == ' ' {
134 n++
135 }
136 }
137 }
138 }
139 }
140
141 func BenchmarkWhitespaceIf2(b *testing.B) {
142 n := 0
143 for i := 0; i < b.N; i++ {
144 for _, e := range wsSlices {
145 for _, c := range e {
146 if c == ' ' || c == '\n' {
147 n++
148 }
149 }
150 }
151 }
152 }
153
154 func BenchmarkWhitespaceIf3(b *testing.B) {
155 n := 0
156 for i := 0; i < b.N; i++ {
157 for _, e := range wsSlices {
158 for _, c := range e {
159 if c == ' ' || c == '\n' || c == '\r' {
160 n++
161 }
162 }
163 }
164 }
165 }
166
167 func BenchmarkWhitespaceIf4(b *testing.B) {
168 n := 0
169 for i := 0; i < b.N; i++ {
170 for _, e := range wsSlices {
171 for _, c := range e {
172 if c == ' ' || c == '\n' || c == '\r' || c == '\t' {
173 n++
174 }
175 }
176 }
177 }
178 }
179
180 func BenchmarkWhitespaceIf5(b *testing.B) {
181 n := 0
182 for i := 0; i < b.N; i++ {
183 for _, e := range wsSlices {
184 for _, c := range e {
185 if c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f' {
186 n++
187 }
188 }
189 }
190 }
191 }