-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_test.go
124 lines (100 loc) · 2.34 KB
/
string_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package bytecat
import (
"testing"
)
func TestString_Append(t *testing.T) {
b := StringOf(1, 2, 3, 4, 5, 6, 7, 8)
b1 := b.Append(9, 10, 11, 12, 13, 14, 15)
if b1.Length() != 15 {
t.Error("expected byte string length after value appending to be 15")
}
for i := 1; i <= b1.Length(); i++ {
v, err := b1.ByteAt(i - 1)
if err != nil {
t.Error(err)
}
if v != byte(i) {
t.Error("value mismatch")
}
}
}
func TestString_Prepend(t *testing.T) {
b := StringOf(9, 10, 11, 12, 13, 14, 15)
b1 := b.Prepend(1, 2, 3, 4, 5, 6, 7, 8)
if b1.Length() != 15 {
t.Error("expected byte string length after value prepending to be 15")
}
for i := 1; i <= b1.Length(); i++ {
v, err := b1.ByteAt(i - 1)
if err != nil {
t.Error(err)
}
if v != byte(i) {
t.Error("value mismatch")
}
}
}
func TestString_Concat(t *testing.T) {
b1 := StringOf(1, 2, 3, 4, 5)
b2 := StringOf(6, 7, 8, 9, 10)
b3 := b1.Concat(b2)
if b3.Length() != 10 {
t.Error("expected byte string length after concatenation to be 10")
}
}
func TestString_Drop(t *testing.T) {
b1 := StringOf(1, 2, 3, 4, 5)
b2 := b1.Drop(3)
if b2.Length() != 2 {
t.Error("expected byte string length after dropping to be 2")
}
}
func TestString_Take(t *testing.T) {
b1 := StringOf(1, 2, 3, 4, 5)
b2 := b1.Take(3)
if b2.Length() != 3 {
t.Error("expected byte string length after dropping to be 3")
}
}
func TestIterator_ReadByte(t *testing.T) {
b := StringOf(1, 2, 3, 4)
itr := b.Iterator()
for i := 1; i <= 4; i++ {
v, _ := itr.ReadByte()
if int(v) != i {
t.Errorf("expected byte value at index %v to equal %v", i-1, v)
}
}
}
func TestBuilder_WriteByte(t *testing.T) {
bs := NewDefaultBuilder().
WriteByte(8).
Build()
v, _ := bs.ByteAt(0)
if v != 8 {
t.Error("expected byte value at index 0 to equal 8")
}
}
func TestByteBuilder_Growth(t *testing.T) {
bb := NewDefaultBuilder()
for i := 0; i < 128; i++ {
bb.WriteByte(byte(i))
}
if bb.Capacity() != 128 {
t.Error("expected capacity to equal 128")
}
bb.WriteByte(byte(2))
if bb.Capacity() != 256 {
t.Error("expected capacity to equal 256")
}
}
func TestByteBuilder_Write(t *testing.T) {
bb := NewDefaultBuilder()
bytes := []byte{1, 2, 3, 4, 5, 6, 7, 8}
if _, err := bb.Write(bytes); err != nil {
t.Error(err)
}
if bb.index != 8 {
t.Error("expected builder writer index to be at position 8")
}
}