-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathosc_test.go
155 lines (150 loc) · 2.73 KB
/
osc_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package osc
import (
"bytes"
"testing"
)
func TestToBytes(t *testing.T) {
for _, testcase := range []struct {
Input string
Expected []byte
}{
{
Input: "",
Expected: []byte{},
},
{
Input: "a",
Expected: []byte{'a', 0, 0, 0},
},
{
Input: "abc",
Expected: []byte{'a', 'b', 'c', 0},
},
} {
got := ToBytes(testcase.Input)
if expected := testcase.Expected; !bytes.Equal(expected, got) {
t.Fatalf("expected %q, got %q", expected, got)
}
}
}
func TestPad(t *testing.T) {
for _, testcase := range []struct {
Input []byte
Expected []byte
}{
{
Input: []byte{},
Expected: []byte{},
},
{
Input: []byte("a"),
Expected: []byte{'a', 0, 0, 0},
},
{
Input: []byte("abc"),
Expected: []byte{'a', 'b', 'c', 0},
},
} {
got := Pad(testcase.Input)
if expected := testcase.Expected; !bytes.Equal(expected, got) {
t.Fatalf("expected %q, got %q", expected, got)
}
}
}
func TestReadString(t *testing.T) {
type Output struct {
String string
Length int64
}
for _, testcase := range []struct {
Input []byte
Expected Output
}{
{
Input: []byte{},
Expected: Output{
String: "",
Length: 0,
},
},
{
Input: []byte{'a'},
Expected: Output{
String: "a",
Length: 4,
},
},
{
Input: []byte("abc"),
Expected: Output{
String: "abc",
Length: 4,
},
},
{
Input: []byte("/foo\x00\x00\x00\x00,ib\x00\x00\x00\x00\x01\x00\x00\x00\x03bar\x00"),
Expected: Output{
String: "/foo",
Length: 8,
},
},
{
Input: []byte{',', 'Q'},
Expected: Output{
String: ",Q",
Length: 4,
},
},
} {
s, bw := ReadString(testcase.Input)
if expected, got := testcase.Expected.Length, bw; expected != got {
t.Fatalf("expected %d, got %d", expected, got)
}
if expected, got := testcase.Expected.String, s; expected != got {
t.Fatalf("expected %s, got %s", expected, got)
}
}
}
func TestReadBlob(t *testing.T) {
type Input struct {
Length int32
Data []byte
}
type Output struct {
Data []byte
Length int64
}
for _, testcase := range []struct {
Input Input
Expected Output
}{
{
Input: Input{
Length: 0,
Data: []byte{},
},
Expected: Output{
Data: []byte{},
Length: 0,
},
},
{
Input: Input{
Length: 20,
Data: []byte{'a'},
},
Expected: Output{
Data: []byte{'a', 0, 0, 0},
Length: 4,
},
},
} {
b, bl := ReadBlob(testcase.Input.Length, testcase.Input.Data)
if expected, got := testcase.Expected.Length, bl; expected != got {
t.Fatalf("expected %d, got %d", expected, got)
}
if expected, got := testcase.Expected.Data, b; !bytes.Equal(expected, got) {
t.Fatalf("expected %q, got %q", expected, got)
}
}
}