forked from lunixbochs/struc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
53 lines (48 loc) · 1.12 KB
/
types_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
package struc
import (
"bytes"
"testing"
)
func TestBadType(t *testing.T) {
defer func() { recover() }()
Type(-1).Size()
t.Fatal("failed to panic for invalid Type.Size()")
}
func TestTypeString(t *testing.T) {
if Pad.String() != "pad" {
t.Fatal("type string representation failed")
}
}
type sizeOffTest struct {
Size Size_t
Off Off_t
}
func TestSizeOffTypes(t *testing.T) {
bits := []int{8, 16, 32, 64}
var buf bytes.Buffer
test := &sizeOffTest{1, 2}
for _, b := range bits {
if err := PackWithOptions(&buf, test, &Options{PtrSize: b}); err != nil {
t.Fatal(err)
}
}
reference := []byte{
1, 2,
0, 1, 0, 2,
0, 0, 0, 1, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2,
}
if !bytes.Equal(reference, buf.Bytes()) {
t.Errorf("reference: %v != bytes: %v", reference, buf.Bytes())
}
reader := bytes.NewReader(buf.Bytes())
for _, b := range bits {
out := &sizeOffTest{}
if err := UnpackWithOptions(reader, out, &Options{PtrSize: b}); err != nil {
t.Fatal(err)
}
if out.Size != 1 || out.Off != 2 {
t.Errorf("Size_t/Off_t mismatch: {%d, %d}\n%v", out.Size, out.Off, buf.Bytes())
}
}
}