forked from jmckaskill/go-capnproto
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcommon_test.go
85 lines (70 loc) · 1.71 KB
/
common_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
package capn_test
import (
"bytes"
capn "github.com/glycerine/go-capnproto"
air "github.com/glycerine/go-capnproto/aircraftlib"
)
func zdateFilledSegment(n int, packed bool) (*capn.Segment, []byte) {
seg := capn.NewBuffer(nil)
z := air.NewRootZ(seg)
list := air.NewZdateList(seg, n)
// hand added a Set() method to messages_test.go, so plist not needed
plist := capn.PointerList(list)
for i := 0; i < n; i++ {
d := air.NewZdate(seg)
d.SetMonth(12)
d.SetDay(7)
d.SetYear(int16(2004 + i))
plist.Set(i, capn.Object(d))
//list.Set(i, d)
}
z.SetZdatevec(list)
buf := bytes.Buffer{}
if packed {
seg.WriteToPacked(&buf)
} else {
seg.WriteTo(&buf)
}
return seg, buf.Bytes()
}
func zdateReader(n int, packed bool) *bytes.Reader {
_, byteSlice := zdateFilledSegment(n, packed)
return bytes.NewReader(byteSlice)
}
// actually return n segments back-to-back.
// WriteTo will automatically add the stream header word with message length.
//
func zdateReaderNBackToBack(n int, packed bool) *bytes.Reader {
buf := bytes.Buffer{}
for i := 0; i < n; i++ {
seg := capn.NewBuffer(nil)
d := air.NewRootZdate(seg)
d.SetMonth(12)
d.SetDay(7)
d.SetYear(int16(2004 + i))
if packed {
seg.WriteToPacked(&buf)
} else {
seg.WriteTo(&buf)
}
}
return bytes.NewReader(buf.Bytes())
}
func zdataFilledSegment(n int) (*capn.Segment, []byte) {
seg := capn.NewBuffer(nil)
z := air.NewRootZ(seg)
d := air.NewZdata(seg)
b := make([]byte, n)
for i := 0; i < len(b); i++ {
b[i] = byte(i)
}
d.SetData(b)
z.SetZdata(d)
buf := bytes.Buffer{}
seg.WriteTo(&buf)
return seg, buf.Bytes()
}
func zdataReader(n int) *bytes.Reader {
_, byteSlice := zdataFilledSegment(n)
return bytes.NewReader(byteSlice)
}