01-spectest.lua 9.45 KB
Newer Older
François Perrad's avatar
François Perrad committed
1
2
3
4
#! /usr/bin/lua

local unpack = table.unpack or unpack

François Perrad's avatar
François Perrad committed
5
require 'Test.Assertion'
François Perrad's avatar
François Perrad committed
6
7
8
9
10
11
12
13
14
15
16

local mp = require 'MessagePack'

local data = {
    false,              "false",
    true,               "true",
    nil,                "nil",
    0,                  "0 Positive FixNum",
    0,                  "0 uint8",
    0,                  "0 uint16",
    0,                  "0 uint32",
François Perrad's avatar
François Perrad committed
17
18
19
    0,                  "0 int8",
    0,                  "0 int16",
    0,                  "0 int32",
François Perrad's avatar
François Perrad committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    -1,                 "-1 Negative FixNum",
    -1,                 "-1 int8",
    -1,                 "-1 int16",
    -1,                 "-1 int32",
    127,                "127 Positive FixNum",
    127,                "127 uint8",
    255,                "255 uint16",
    65535,              "65535 uint32",
    -32,                "-32 Negative FixNum",
    -32,                "-32 int8",
    -128,               "-128 int16",
    -32768,             "-32768 int32",
    0.0,                "0.0 float",
    -0.0,               "-0.0 float",
François Perrad's avatar
François Perrad committed
34
    "a",                "\"a\" FixStr",
François Perrad's avatar
François Perrad committed
35
    "a",                "\"a\" str 8",
François Perrad's avatar
François Perrad committed
36
37
38
    "a",                "\"a\" str 16",
    "a",                "\"a\" str 32",
    "",                 "\"\" FixStr",
François Perrad's avatar
François Perrad committed
39
    "",                 "\"\" str 8",
François Perrad's avatar
François Perrad committed
40
41
    "",                 "\"\" str 16",
    "",                 "\"\" str 32",
François Perrad's avatar
François Perrad committed
42
43
44
45
46
47
    "a",                "\"a\" bin 8",
    "a",                "\"a\" bin 16",
    "a",                "\"a\" bin 32",
    "",                 "\"\" bin 8",
    "",                 "\"\" bin 16",
    "",                 "\"\" bin 32",
François Perrad's avatar
François Perrad committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    { 0 },              "[0] FixArray",
    { 0 },              "[0] array 16",
    { 0 },              "[0] array 32",
    {},                 "[] FixArray",
    {},                 "[] array 16",
    {},                 "[] array 32",
    {},                 "{} FixMap",
    {},                 "{} map 16",
    {},                 "{} map 32",
    { a=97 },           "{\"a\"=>97} FixMap",
    { a=97},            "{\"a\"=>97} map 16",
    { a=97 },           "{\"a\"=>97} map 32",
    { {} },             "[[]]",
    { {"a"} },          "[[\"a\"]]",
François Perrad's avatar
François Perrad committed
62
63
64
65
66
67
68
69
70

    nil,                "fixext 1",
    nil,                "fixext 2",
    nil,                "fixext 4",
    nil,                "fixext 8",
    nil,                "fixext 16",
    nil,                "ext 8",
    nil,                "ext 16",
    nil,                "ext 32",
François Perrad's avatar
François Perrad committed
71
}
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
if not mp.small_lua then
    for i, v in ipairs({
    0,                  "0 uint64",
    0,                  "0 int64",
    -1,                 "-1 int64",
    4294967295,         "4294967295 uint64",
    -2147483648,        "-2147483648 int64",
    0.0,                "0.0 double",
    -0.0,               "-0.0 double",
    1.0,                "1.0 double",
    -1.0,               "-1.0 double",
}) do
        data[60*2+i] = v
    end
end
François Perrad's avatar
François Perrad committed
87

François Perrad's avatar
François Perrad committed
88
plan(9 * (mp.small_lua and 60 or 69))
François Perrad's avatar
François Perrad committed
89

François Perrad's avatar
https    
François Perrad committed
90
-- see https://github.com/msgpack/msgpack/blob/master/test/cases_gen.rb
François Perrad's avatar
François Perrad committed
91
92
93
94
95
96
97
98
local source = [===[
c2                              # false
c3                              # true
c0                              # nil
00                              # 0 Positive FixNum
cc 00                           # 0 uint8
cd 00 00                        # 0 uint16
ce 00 00 00 00                  # 0 uint32
François Perrad's avatar
François Perrad committed
99
100
101
d0 00                           # 0 int8
d1 00 00                        # 0 int16
d2 00 00 00 00                  # 0 int32
François Perrad's avatar
François Perrad committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
ff                              # -1 Negative FixNum
d0 ff                           # -1 int8
d1 ff ff                        # -1 int16
d2 ff ff ff ff                  # -1 int32
7f                              # 127 Positive FixNum
cc 7f                           # 127 uint8
cd 00 ff                        # 255 uint16
ce 00 00 ff ff                  # 65535 uint32
e0                              # -32 Negative FixNum
d0 e0                           # -32 int8
d1 ff 80                        # -128 int16
d2 ff ff 80 00                  # -32768 int32
ca 00 00 00 00                  # 0.0 float
ca 80 00 00 00                  # -0.0 float
François Perrad's avatar
François Perrad committed
116
a1 61                           # "a" FixStr
François Perrad's avatar
François Perrad committed
117
d9 01 61                        # "a" str 8
François Perrad's avatar
François Perrad committed
118
119
120
da 00 01 61                     # "a" str 16
db 00 00 00 01 61               # "a" str 32
a0                              # "" FixStr
François Perrad's avatar
François Perrad committed
121
d9 00                           # "" str 8
François Perrad's avatar
François Perrad committed
122
123
da 00 00                        # "" str 16
db 00 00 00 00                  # "" str 32
François Perrad's avatar
François Perrad committed
124
125
126
127
128
129
c4 01 61                        # "a" bin 8
c5 00 01 61                     # "a" bin 16
c6 00 00 00 01 61               # "a" bin 32
c4 00                           # "" bin 8
c5 00 00                        # "" bin 16
c6 00 00 00 00                  # "" bin 32
François Perrad's avatar
François Perrad committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
91 00                           # [0] FixArray
dc 00 01 00                     # [0] array 16
dd 00 00 00 01 00               # [0] array 32
90                              # [] FixArray
dc 00 00                        # [] array 16
dd 00 00 00 00                  # [] array 32
80                              # {} FixMap
de 00 00                        # {} map 16
df 00 00 00 00                  # {} map 32
81 a1 61 61                     # {"a"=>97} FixMap
de 00 01 a1 61 61               # {"a"=>97} map 16
df 00 00 00 01 a1 61 61         # {"a"=>97} map 32
91 90                           # [[]]
91 91 a1 61                     # [["a"]]
François Perrad's avatar
François Perrad committed
144
145
146
147
148
149
150
151
152

d4 01 01                        # fixext 1
d5 02 02 01                     # fixext 2
d6 04 04 03 02 01               # fixext 4
d7 08 08 07 06 05 04 03 02 01   # fixext 8
d8 16 10 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01   # fixext 16
c7 01 08 61                     # ext 8
c8 00 01 16 61                  # ext 16
c9 00 00 00 01 32 61            # ext 32
François Perrad's avatar
François Perrad committed
153
]===]
154
155
156
157
158
159
160
161
162
163
164
165
166
if not mp.small_lua then
    source = source .. [===[
cf 00 00 00 00 00 00 00 00      # 0 uint64
d3 00 00 00 00 00 00 00 00      # 0 int64
d3 ff ff ff ff ff ff ff ff      # -1 int64
cf 00 00 00 00 ff ff ff ff      # 4294967295 uint64
d3 ff ff ff ff 80 00 00 00      # -2147483648 int64
cb 00 00 00 00 00 00 00 00      # 0.0 double
cb 80 00 00 00 00 00 00 00      # -0.0 double
cb 3f f0 00 00 00 00 00 00      # 1.0 double
cb bf f0 00 00 00 00 00 00      # -1.0 double
]===]
end
François Perrad's avatar
François Perrad committed
167
168
169
170
171
172
173
174
175
176
177
178

source = source:gsub('#[^\n]+', '')
local t = {}
for v in source:gmatch'%x%x' do
    t[#t+1] = tonumber(v, 16)
end
local mpac = string.char(unpack(t))


local i = 1
for _, val in mp.unpacker(mpac) do
    if type(val) == 'table' then
François Perrad's avatar
François Perrad committed
179
180
        same(val, data[i], "reference   " .. data[i+1])
        same(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
181
    else
François Perrad's avatar
François Perrad committed
182
183
        equals(val, data[i], "reference   " .. data[i+1])
        equals(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
184
185
186
    end
    i = i + 2
end
François Perrad's avatar
François Perrad committed
187
188
189
190

local f = io.open('cases.mpac', 'w')
f:write(mpac)
f:close()
191
192
local r, ltn12 = pcall(require, 'ltn12')        -- from LuaSocket
if not r then
François Perrad's avatar
info    
François Perrad committed
193
    diag "ltn12.source.file emulated"
194
195
196
197
198
    ltn12 = { source = {} }

    function ltn12.source.file (handle)
        if handle then
            return function ()
François Perrad's avatar
François Perrad committed
199
                local chunk = handle:read(1)
200
201
202
203
204
205
206
207
208
209
210
                if not chunk then
                    handle:close()
                end
                return chunk
            end
        else return function ()
                return nil, "unable to open file"
            end
        end
    end
end
François Perrad's avatar
François Perrad committed
211
212
i = 1
f = io.open('cases.mpac', 'r')
213
214
local s = ltn12.source.file(f)
for _, val in mp.unpacker(s) do
François Perrad's avatar
François Perrad committed
215
    if type(val) == 'table' then
François Perrad's avatar
François Perrad committed
216
        same(val, data[i], "reference   " .. data[i+1])
François Perrad's avatar
François Perrad committed
217
    else
François Perrad's avatar
François Perrad committed
218
        equals(val, data[i], "reference   " .. data[i+1])
François Perrad's avatar
François Perrad committed
219
220
221
222
    end
    i = i + 2
end
os.remove 'cases.mpac'  -- clean up
François Perrad's avatar
François Perrad committed
223

François Perrad's avatar
François Perrad committed
224
225
diag("set_string'string'")
mp.set_string'string'
François Perrad's avatar
François Perrad committed
226
i = 1
François Perrad's avatar
François Perrad committed
227
228
for _, val in mp.unpacker(mpac) do
    if type(val) == 'table' then
François Perrad's avatar
François Perrad committed
229
        same(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
230
    else
François Perrad's avatar
François Perrad committed
231
        equals(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
232
233
234
235
    end
    i = i + 2
end

François Perrad's avatar
François Perrad committed
236
237
diag("set_string'binary'")
mp.set_string'binary'
François Perrad's avatar
François Perrad committed
238
i = 1
François Perrad's avatar
François Perrad committed
239
240
for _, val in mp.unpacker(mpac) do
    if type(val) == 'table' then
François Perrad's avatar
François Perrad committed
241
        same(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
242
    else
François Perrad's avatar
François Perrad committed
243
        equals(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
244
245
246
    end
    i = i + 2
end
François Perrad's avatar
François Perrad committed
247
mp.set_string'string_compat'
François Perrad's avatar
François Perrad committed
248

249
250
diag("set_integer'signed'")
mp.set_integer'signed'
François Perrad's avatar
François Perrad committed
251
i = 1
François Perrad's avatar
François Perrad committed
252
253
for _, val in mp.unpacker(mpac) do
    if type(val) == 'table' then
François Perrad's avatar
François Perrad committed
254
        same(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
255
    else
François Perrad's avatar
François Perrad committed
256
        equals(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
257
258
259
    end
    i = i + 2
end
260
mp.set_integer'unsigned'
François Perrad's avatar
François Perrad committed
261

262
263
diag("set_array'with_hole'")
mp.set_array'with_hole'
François Perrad's avatar
François Perrad committed
264
i = 1
François Perrad's avatar
François Perrad committed
265
266
for _, val in mp.unpacker(mpac) do
    if type(val) == 'table' then
François Perrad's avatar
François Perrad committed
267
        same(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
268
    else
François Perrad's avatar
François Perrad committed
269
        equals(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
270
271
272
273
    end
    i = i + 2
end

274
275
diag("set_array'always_as_map'")
mp.set_array'always_as_map'
François Perrad's avatar
François Perrad committed
276
i = 1
François Perrad's avatar
François Perrad committed
277
278
for _, val in mp.unpacker(mpac) do
    if type(val) == 'table' then
François Perrad's avatar
François Perrad committed
279
        same(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
280
    else
François Perrad's avatar
François Perrad committed
281
        equals(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
François Perrad's avatar
François Perrad committed
282
283
284
    end
    i = i + 2
end
285
mp.set_array'without_hole'
286

287
288
diag("set_number'float'")
mp.set_number'float'
François Perrad's avatar
François Perrad committed
289
i = 1
290
291
for _, val in mp.unpacker(mpac) do
    if type(val) == 'table' then
François Perrad's avatar
François Perrad committed
292
        same(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
293
    else
François Perrad's avatar
François Perrad committed
294
        equals(mp.unpack(mp.pack(data[i])), data[i], "unpack/pack " .. data[i+1])
295
296
297
    end
    i = i + 2
end