-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
151 lines (120 loc) · 3.46 KB
/
test.js
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
var tape = require('tape')
var encodings = require('./')
tape('name', function (t) {
t.same(encodings.name(encodings.varint), 'varint')
t.same(encodings.name(encodings.float), 'float')
t.end()
})
tape('varint', function (t) {
test(t, encodings.varint, [42, 10, 0, 999999])
})
tape('string', function (t) {
test(t, encodings.string, ['a', 'abefest', 'øøø'])
})
tape('bytes (node style)', function (t) {
test(
t,
encodings.bytes,
[Buffer.alloc(4096), Buffer.from('hi')],
[allocBuffer]
)
})
tape('bytes (browser style)', function (t) {
test(
t,
encodings.bytes,
[new Uint8Array(4096), new Uint8Array([104, 105])],
[allocUint8Array]
)
})
tape('bool', function (t) {
test(t, encodings.bool, [true, false])
})
tape('int32', function (t) {
test(t, encodings.int32, [-1, 0, 42, 4242424])
})
tape('int64', function (t) {
test(t, encodings.int64, [-1, 0, 1, 24242424244])
})
tape('sint64', function (t) {
test(t, encodings.sint64, [-14, 0, 144, 4203595524])
})
tape('uint64', function (t) {
test(t, encodings.uint64, [1, 0, 144, 424444, 4203595524])
})
tape('fixed64 (node style)', function (t) {
test(
t,
encodings.fixed64,
[Buffer.from([0, 0, 0, 0, 0, 0, 0, 1])],
[allocBuffer]
)
})
tape('fixed64 (browser style)', function (t) {
test(
t,
encodings.fixed64,
[new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1])],
[allocUint8Array]
)
})
tape('double', function (t) {
test(t, encodings.double, [0, 2, 0.5, 0.4])
})
tape('float', function (t) {
test(t, encodings.float, [0, 2, 0.5])
})
tape('fixed32', function (t) {
test(t, encodings.fixed32, [4, 0, 10000])
})
tape('sfixed32', function (t) {
test(t, encodings.sfixed32, [-100, 4, 0, 142425])
})
function test (t, enc, vals, allocFunctions = [allocBuffer, allocUint8Array]) {
if (!Array.isArray(vals)) vals = [vals]
for (const allocFunction of allocFunctions) {
for (const val of vals) {
let buf = allocFunction(enc.encodingLength(val))
enc.encode(val, buf, 0)
t.same(enc.encode.bytes, buf.length)
t.same(enc.encodingLength(val), buf.length)
t.same(enc.decode(buf, 0), val)
t.same(enc.decode.bytes, buf.length)
const anotherBuf = allocFunction(enc.encodingLength(val) + 1000)
buf = enc.encode(val, anotherBuf, 10)
t.same(buf, anotherBuf)
t.ok(enc.encode.bytes < anotherBuf.length)
t.same(enc.decode(buf, 10, 10 + enc.encodingLength(val)), val)
t.ok(enc.decode.bytes < anotherBuf.length)
}
}
t.end()
}
tape('test browser-style buffer', function (t) {
const enc = encodings.string
const val = 'value'
let buf = new Uint8Array(enc.encodingLength(val))
enc.encode(val, buf, 0)
// First elem is the length (5). Others are the encoded 'value'
const expectedEncodedVal = new Uint8Array([5, 118, 97, 108, 117, 101])
t.same(buf, expectedEncodedVal)
t.same(enc.encode.bytes, buf.length)
t.same(enc.encodingLength(val), buf.length)
t.same(enc.decode(buf, 0), val)
t.same(enc.decode.bytes, buf.length)
const anotherBuf = new Uint8Array(enc.encodingLength(val) + 1000)
buf = enc.encode(val, anotherBuf, 10)
t.same(buf, anotherBuf)
t.ok(enc.encode.bytes < anotherBuf.length)
t.same(enc.decode(buf, 10, 10 + enc.encodingLength(val)), val)
t.ok(enc.decode.bytes < anotherBuf.length)
t.end()
})
function allocBuffer (length) {
// Node style
return Buffer.alloc(length)
}
function allocUint8Array (length) {
// Browser style
return new Uint8Array(length)
}