-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascii_test.go
65 lines (53 loc) · 1.1 KB
/
ascii_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package asciicheck
import (
"fmt"
"testing"
"unicode"
)
func TestIsASCII(t *testing.T) {
t.Run("ascii", func(t *testing.T) {
s := generateString(unicode.MaxASCII + 1)
ch, ok := isASCII(s)
if !ok {
t.Error("expected that string contains only ASCII symbols")
}
if ch != 0 {
t.Error("expected that ch is zero")
}
})
t.Run("non-ascii", func(t *testing.T) {
s := "привет!"
ch, ok := isASCII(s)
if ok {
t.Error("expected that string contains non-ASCII symbols")
}
if ch != 'п' {
t.Error("expected that ch is equal to first letter")
}
})
}
func BenchmarkIsASCII(b *testing.B) {
// We are usually check small strings, that represents identifiers.
sizes := []int{
1, 8, 16, 32,
}
for _, size := range sizes {
b.Run(fmt.Sprintf("Len=%d", size), func(b *testing.B) {
s := generateString(size)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, ok := isASCII(s); !ok {
b.Fatal("unexpected result")
}
}
})
}
}
func generateString(l int) string {
s := make([]byte, l)
for i := range s {
s[i] = byte(i)
}
return string(s)
}