-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings_test.go
110 lines (86 loc) · 2.38 KB
/
strings_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package split_test
import (
"io"
"reflect"
"strings"
"testing"
"github.com/clipperhouse/split"
)
var bench = "Hello, 世界. Nice dog! 👍🐶 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
func BenchmarkSplitString(b *testing.B) {
sep := " "
b.SetBytes(int64(len(bench)))
for i := 0; i < b.N; i++ {
split := split.String(bench, sep)
for split.Next() {
io.WriteString(io.Discard, split.Value())
}
}
}
func BenchmarkSplitStringToArray(b *testing.B) {
sep := " "
b.SetBytes(int64(len(bench)))
for i := 0; i < b.N; i++ {
split := split.String(bench, sep).ToArray()
for _, v := range split {
io.WriteString(io.Discard, v)
}
}
}
func BenchmarkStringsSplit(b *testing.B) {
sep := " "
b.SetBytes(int64(len(bench)))
for i := 0; i < b.N; i++ {
split := strings.Split(bench, sep)
for _, v := range split {
io.WriteString(io.Discard, v)
}
}
}
func TestStrings(t *testing.T) {
for _, sep := range testSeparators {
for _, s := range testStrings {
got := split.String(s, sep).ToArray() // this package
expected := strings.Split(s, sep) // standard library
if !reflect.DeepEqual(got, expected) {
t.Fatalf("\nFor input %q, separator %q\nexpected: %q,\ngot: %q", s, sep, expected, got)
}
}
}
}
func TestStringAny(t *testing.T) {
var sep = ", "
input := "Hello, how a,re, you "
split := split.StringAny(input, sep)
var got []string
for split.Next() {
got = append(got, split.Value())
}
expected := []string{
"Hello",
"",
"how",
"a",
"re",
"",
"you",
"",
}
if !reflect.DeepEqual(got, expected) {
t.Fatalf("\nexpected: %v,\ngot: %v", expected, got)
}
}
func TestResetString(t *testing.T) {
text := "Hello, how are you."
s1 := split.String(text, " ")
a1 := s1.ToArray()
a2 := s1.ToArray()
if reflect.DeepEqual(a1, a2) {
t.Fatal("They should not be equal, as s1 has not been reset")
}
s1.Reset()
a3 := s1.ToArray()
if !reflect.DeepEqual(a1, a3) {
t.Fatal("They should be equal, as s1 has been reset")
}
}