-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_array_map_unpack.m
117 lines (109 loc) · 4.21 KB
/
test_array_map_unpack.m
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
nil = uint8(hex2dec('c0'));
fixmap = hex2dec('80'); % (up to 0x8f, e.g. fixmap+11)
%% array of 2.5, 3.5, 2.4e8
packed = uint8([147 ...
203 ...
064 004 000 000 000 000 000 000 ...
203 ...
064 012 000 000 000 000 000 000 ...
203 ...
065 172 156 056 000 000 000 000 ]);
unpacked = msgpack('unpack', packed);
expected = [2.5, 3.5, 2.4e8];
assert(numel(expected) == numel(unpacked));
assert(strcmp(class(expected), class(unpacked)));
for i = 1:numel(expected)
assert(strcmp(class(expected(i)), class(unpacked(i))), 'Wrong class for %d', i);
assert(expected(i) == unpacked(i), 'Wrong value for %d', i);
end
%% array [2.5, false, 2.4e8]
packed = uint8([147 ...
203 ...
064 004 000 000 000 000 000 000 ...
194 ...
203 ...
065 172 156 056 000 000 000 000 ]);
unpacked = msgpack('unpack', packed);
expected = {2.5, false, 2.4e8};
assert(numel(expected) == numel(unpacked));
assert(strcmp(class(expected), class(unpacked)));
for i = 1:numel(expected)
assert(strcmp(class(expected{i}), class(unpacked{i})), 'Wrong class for %d', i);
assert(expected{i} == unpacked{i}, 'Wrong value for %d', i);
end
%% array [2.5, nil, 2.4e8]
msgpack('reset_flags');
msgpack('set_flags +unpack_nil_NaN -unpack_nil_array_skip')
packed = uint8([147 ...
203 ...
064 004 000 000 000 000 000 000 ...
nil ...
203 ...
065 172 156 056 000 000 000 000 ]);
unpacked = msgpack('unpack', packed);
expected = [2.5, NaN, 2.4e8];
assert(numel(expected) == numel(unpacked), 'Different number of elements');
assert(strcmp(class(expected), class(unpacked)), 'Different class');
for i = 1:numel(expected)
assert(strcmp(class(expected(i)), class(unpacked(i))), 'Wrong class for %d', i);
if isnan(expected(i))
assert(isnan(unpacked(i)), 'Should be NaN');
else
assert(expected(i) == unpacked(i), 'Wrong value for %d', i);
end
end
%% skip nil
msgpack('set_flags +unpack_nil_array_skip');
unpacked = msgpack('unpack', packed);
expected = [2.5, 2.4e8];
assert(numel(expected) == numel(unpacked));
assert(strcmp(class(expected), class(unpacked)));
for i = 1:numel(expected)
assert(strcmp(class(expected(i)), class(unpacked(i))), 'Wrong class for %d', i);
assert(expected(i) == unpacked(i), 'Wrong value for %d', i);
end
%% all nil
msgpack('set_flags -unpack_nil_array_skip')
packed = [147, nil, nil, nil];
unpacked = msgpack('unpack', packed);
for i = 1:numel(unpacked)
assert(isnan(unpacked(i)), "Should be NaN");
end
%% map with nil
% map with 2 elements, x: nil, y: {}
packed = uint8([fixmap+2, 161, uint8('x'), nil, 161, uint8('y'), hex2dec('90')]);
unpacked = msgpack('unpack +unpack_nil_NaN', packed);
assert(isstruct(unpacked));
assert(isnan(unpacked.x));
%% map with non-str keys unpacked to cell array
msgpack('set_flags +unpack_narrow');
warning('off', 'msgpack:non_str_map_keys');
% map with 2 elements, uint(3): double(3), y: int(-3)
packed = uint8([fixmap+2, ...
3, 203, 64, 8, 0, 0, 0, 0, 0, 0, ...
161, uint8('y'), 253]);
unpacked = msgpack('unpack', packed);
assert(iscell(unpacked), 'Should be cell');
assert(all(size(unpacked) == [2, 2]), 'Wrong size');
expected = {uint8(3), 3.0; 'y', int8(-3)};
%% map as cell array
msgpack('set_flags +unpack_map_as_cells');
%map with 3 elements, all str keys: 'ab': uint(5), 'cd': double(32.8), 'def': int(-3)
packed = uint8([fixmap+3, ...
162, uint8('ab'), 5, ...
162, uint8('cd'), 203, 64, 64, 102, 102, 102, 102, 102, 102, ...
163, uint8('def'), 253]);
unpacked = msgpack('unpack', packed);
assert(iscell(unpacked));
assert(all(size(unpacked) == [2,3]), 'Wrong cell array size.');
expected = {'ab', 'cd', 'def'; uint8(5), 32.8, int8(-3)};
for i=1:numel(expected)
assert(strcmp(class(expected{i}), class(unpacked{i})), 'Wrong class for elementd %d', i);
if ischar(expected{i})
assert(strcmp(expected{i}, unpacked{i}), 'Mismatch for element %d', i);
else
assert(expected{i} == unpacked{i}, 'Mismatch for element %d', i);
end
end
%% all passed
disp('All tests passed.');