-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings_test.go
79 lines (71 loc) · 1.58 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
// Byteman
// For the full copyright and license information, please view the LICENSE.txt file.
package byteman_test
import (
"bytes"
"testing"
"github.com/devfacet/byteman"
)
func TestFromString(t *testing.T) {
table := []struct {
arg0 string
arg1 int
out []byte
}{
{"foo", 0, []byte("foo")},
{"bar", 1, []byte("b")},
{"baz", 2, []byte("ba")},
{"qux", 3, []byte("qux")},
{"foo", 4, []byte{0x66, 0x6f, 0x6f, 0x00}},
{"foo", -1, []byte("fo")},
{"bar", -2, []byte("b")},
{"baz", -3, []byte{}},
{"qux", -4, []byte{}},
{"", 0, []byte("")},
{"", 0, []byte{}},
}
for _, v := range table {
b := byteman.FromString(v.arg0, v.arg1)
if !bytes.Equal(b, v.out) {
t.Errorf("got %v, want %v", b, v.out)
}
}
}
func BenchmarkFromString(b *testing.B) {
for i := 0; i < b.N; i++ {
byteman.FromString("foo", 3)
}
}
func TestFromHex(t *testing.T) {
table := []struct {
arg0 string
arg1 int
out []byte
}{
{"414243", 0, []byte("ABC")},
{"414243", 1, []byte("A")},
{"414243", 2, []byte("AB")},
{"414243", 3, []byte("ABC")},
{"414243", 4, []byte{0x41, 0x42, 0x43, 0x00}},
{"414243", -1, []byte("AB")},
{"414243", -2, []byte("A")},
{"414243", -3, []byte{}},
{"414243", -4, []byte{}},
{"", 0, []byte("")},
{"", 0, []byte{}},
}
for _, v := range table {
b := byteman.FromHex(v.arg0, v.arg1)
if !bytes.Equal(b, v.out) {
t.Errorf("got %v, want %v", b, v.out)
}
}
if b := byteman.FromHex("9", 1); b != nil {
t.Errorf("got %v, want nil", b)
}
}
func BenchmarkFromHex(b *testing.B) {
for i := 0; i < b.N; i++ {
byteman.FromHex("A", 1)
}
}