Skip to content

Commit 448a41e

Browse files
committed
Add tests on pack_str and unpack_str options
1 parent d7d07af commit 448a41e

3 files changed

Lines changed: 117 additions & 35 deletions

File tree

src/msgpack.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ parse_options([{unpack_str, As}|T], Opt0) when As =:= as_binary orelse As =:= as
165165
parse_options([{pack_str, From}|T], Opt)
166166
when From =:= from_binary orelse From =:= from_list orelse From =:= none ->
167167
%% TODO Choose function here
168-
parse_options(T, Opt);
168+
parse_options(T, Opt?OPTION{pack_str=From});
169169

170170
parse_options([{map_format,Type}|T], Opt0)
171171
when Type =:= jsx; Type =:= jiffy; Type =:= map ->

src/msgpack_packer.erl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,14 @@ pack_uint(N) ->
153153
throw({badarg, N}).
154154

155155

156+
%% @doc float : erlang's float is always IEEE 754 64bit format. Thus it
157+
%% never generates << 16#CA:8, F:32/big-float-unit:1 >>.
156158
-spec pack_double(float()) -> binary().
157-
%% float : erlang's float is always IEEE 754 64bit format.
158-
%% pack_float(F) when is_float(F)->
159-
%% << 16#CA:8, F:32/big-float-unit:1 >>.
160-
%% pack_double(F).
161-
%% double
162159
pack_double(F) ->
163160
<< 16#CB:8, F:64/big-float-unit:1 >>.
164161

162+
%% @doc raw bytes in old spec
165163
-spec pack_raw(binary()) -> binary().
166-
%% raw bytes in old spec
167164
pack_raw(Bin) ->
168165
case byte_size(Bin) of
169166
Len when Len < 32->
@@ -180,7 +177,7 @@ pack_raw(Bin) ->
180177
%% raw bytes in new spec
181178
pack_raw2(Bin) ->
182179
case byte_size(Bin) of
183-
Len when Len < 32->
180+
Len when Len < 256->
184181
<< 16#C4:8, Len:8/big-unsigned-integer-unit:1, Bin/binary>>;
185182
Len when Len < 16#10000 -> % 65536
186183
<< 16#C5:8, Len:16/big-unsigned-integer-unit:1, Bin/binary >>;

test/msgpack_tests.erl

Lines changed: 112 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -470,27 +470,39 @@ benchmark_p0_test_() ->
470470
fun erlang:term_to_binary/1,
471471
fun erlang:binary_to_term/1))}].
472472

473-
new_options_test_() ->
473+
%% New options test
474+
old_spec_test_() ->
474475
OldSpecOpt = [{spec, old}],
475476
[
476477
{"old spec",
477478
[?_assertEqual(<<161,1>>, msgpack:pack(<<1>>, OldSpecOpt)),
478479
?_assertEqual(<<162,1,2>>, msgpack:pack(<<1,2>>, OldSpecOpt)),
479480
?_assertMatch(<<191, _:31/binary >>,
480-
msgpack:pack(binary:copy(<<1>>, 31), OldSpecOpt)),
481+
msgpack:pack(binary_a(31), OldSpecOpt)),
481482
?_assertMatch(<<218, 0, 32, _:32/binary >>,
482-
msgpack:pack(binary:copy(<<1>>, 32), OldSpecOpt)),
483+
msgpack:pack(binary_a(32), OldSpecOpt)),
483484
?_assertMatch(<<218, 255, 255, _:65535/binary >>,
484-
msgpack:pack(binary:copy(<<1>>, 65535), OldSpecOpt)),
485+
msgpack:pack(binary_a(65535), OldSpecOpt)),
485486
?_assertMatch(<<219, 0, 1, 0, 0, _:65536/binary >>,
486-
msgpack:pack(binary:copy(<<1>>, 65536), OldSpecOpt))
487-
]},
487+
msgpack:pack(binary_a(65536), OldSpecOpt))
488+
]}
488489
%% {"Decoding new spec binary with old spec",
489490
%% [?_assertEqual({error, {badarg, {new_spec, Code}}},
490491
%% msgpack:unpack(<<Code, 0, 0, 0, 42>>, OldSpecOpt))
491492
%% || Code <- [16#D4, 16#D5, 16#D6, 16#D7, 16#D8, 16#C7, 16#C8, 16#C9,
492-
%% 16#C4, 16#C5, 16#C6] ]},
493-
{"allow_atom none/pack",
493+
%% 16#C4, 16#C5, 16#C6] ]}
494+
].
495+
496+
list_a(Len) ->
497+
[$a||_<-lists:seq(1,Len)].
498+
list_minus_one(Len) ->
499+
[-1||_<-lists:seq(1,Len)].
500+
501+
binary_a(Len) ->
502+
binary:copy(<<$a>>, Len).
503+
504+
new_spec_pack_test_() ->
505+
[{"allow_atom none/pack",
494506
[?_assertEqual(<<196,4,97,116,111,109>>,
495507
msgpack:pack(atom, [{allow_atom, pack}])),
496508
?_assertEqual({error, {badarg, atom}},
@@ -503,27 +515,100 @@ new_options_test_() ->
503515
[?_assertEqual(<<196,4,97,116,111,109>>,
504516
msgpack:pack(atom, [{known_atoms, [atom]},
505517
{allow_atom, none}]))]},
506-
{"pack_str, on binary()",
507-
[?_assertEqual(<<196,3,97,97,97>>,
508-
msgpack:pack(<<"aaa">>, [{spec,new},{pack_str,from_list}])),
509-
%% ?_assertEqual(<<16#D9,3,97,97,97>>, Not passing
510-
%% msgpack:pack(<<"aaa">>, [{spec,new},{pack_str,from_binary}])),
511-
?_assertEqual(<<196,3,97,97,97>>,
512-
msgpack:pack(<<"aaa">>, [{spec,new},{pack_str,none}]))
518+
{"pack_str, on binary(), from_list and none",
519+
[[?_assertEqual(<<16#C4, 3, 97,97,97>>,
520+
msgpack:pack(binary_a(3), [{spec,new},{pack_str,PackStr}])),
521+
?_assertMatch(<<16#C4, 255, _:255/binary>>,
522+
msgpack:pack(binary_a(255), [{spec,new},{pack_str,PackStr}])),
523+
?_assertMatch(<<16#C5, 1, 0, _:256/binary>>,
524+
msgpack:pack(binary_a(256), [{spec,new},{pack_str,PackStr}])),
525+
?_assertMatch(<<16#C5, 255,255, _:65535/binary>>,
526+
msgpack:pack(binary_a(65535), [{spec,new},{pack_str,PackStr}])),
527+
?_assertMatch(<<16#C6, 0, 1, 0, 0, _:65536/binary>>,
528+
msgpack:pack(binary_a(65536), [{spec,new},{pack_str,PackStr}]))]
529+
|| PackStr <- [from_list, none]]},
530+
{"pack_str on binary(), from_binary",
531+
[?_assertMatch(<<2#101:3, 31:5, _:31/binary>>,
532+
msgpack:pack(binary_a(31), [{spec,new},{pack_str,from_binary}])),
533+
?_assertMatch(<<16#D9,32,_:32/binary>>,
534+
msgpack:pack(binary_a(32), [{spec,new},{pack_str,from_binary}])),
535+
?_assertMatch(<<16#D9,255,_:255/binary>>,
536+
msgpack:pack(binary_a(255), [{spec,new},{pack_str,from_binary}])),
537+
?_assertMatch(<<16#DA,1,0,_:256/binary>>,
538+
msgpack:pack(binary_a(256), [{spec,new},{pack_str,from_binary}])),
539+
?_assertMatch(<<16#DA,255,255,_:65535/binary>>,
540+
msgpack:pack(binary_a(65535), [{spec,new},{pack_str,from_binary}])),
541+
?_assertMatch(<<16#DB,0,1,0,0,_:65536/binary>>,
542+
msgpack:pack(binary_a(65536), [{spec,new},{pack_str,from_binary}]))
513543
]},
514-
{"pack_str, on string()",
515-
%% [?_assertEqual(<<196,3,97,97,97>>,
516-
%% msgpack:pack("aaa", [{spec,new},{pack_str,from_list}])),
517-
%% ?_assertEqual(<<16#D9,3,97,97,97>>, Not passing
518-
%% msgpack:pack("aaa", [{spec,new},{pack_str,from_binary}])),
519-
%% ?_assertEqual(<<196,3,97,97,97>>,
520-
%% msgpack:pack("aaa", [{spec,new},{pack_str,none}]))
521-
[]},
522-
{"unpack_str, as_binary",
544+
{"pack_str, on string(), from_list, from_binary, none",
545+
%% from_list => str
546+
[?_assertEqual(<<2#101:3, 3:5, 97,97,97>>,
547+
msgpack:pack("aaa", [{spec,new},{pack_str,from_list}])),
548+
?_assertMatch(<<16#D9, 32, _:32/binary>>,
549+
msgpack:pack(list_a(32), [{spec,new},{pack_str,from_list}])),
550+
?_assertMatch(<<16#DA, 1, 0, _:256/binary>>,
551+
msgpack:pack(list_a(256), [{spec,new},{pack_str,from_list}])),
552+
?_assertMatch(<<16#DB, 0, 1, 0, 0, _:65536/binary>>,
553+
msgpack:pack(list_a(65536), [{spec,new},{pack_str,from_list}])),
554+
%% string() from_binary/none => array of int
555+
[[?_assertEqual(<<2#1001:4, 3:4, "aaa">>,
556+
msgpack:pack("aaa", [{spec,new},{pack_str,PackStr}])),
557+
?_assertMatch(<<16#DC, 1, 0, _:256/binary>>,
558+
msgpack:pack(list_a(256), [{spec,new},{pack_str,PackStr}])),
559+
?_assertMatch(<<16#DD, 0, 1, 0, 0, _:65536/binary>>,
560+
msgpack:pack(list_a(65536), [{spec,new},{pack_str,PackStr}]))]
561+
|| PackStr <- [from_binary, none]]
562+
]},
563+
{"pack_str, on list(), from_list, from_binary, none",
564+
[[?_assertEqual(<<2#1001:4, 3:4, 255,255,255>>, %% 1001XXXX, up to 15 elements
565+
msgpack:pack(list_minus_one(3), [{spec,new},{pack_str,PackStr}])),
566+
?_assertMatch(<<16#DC, 1, 0, _:256/binary>>, %% 0xDC, two bytes, N objects
567+
msgpack:pack(list_minus_one(256), [{spec,new},{pack_str,PackStr}])),
568+
?_assertMatch(<<16#DD, 0, 1, 0, 0, _:65536/binary>>, %% 0xDD, four bytes, N objects
569+
msgpack:pack(list_minus_one(65536), [{spec,new},{pack_str,PackStr}]))]
570+
|| PackStr <- [from_list, from_binary, none]]
571+
}].
572+
573+
new_spec_unpack_test_() ->
574+
[{"unpack_str, on bin",
523575
[
576+
%% mode as_binary as_list
577+
%% -----------+------------+-------
578+
%% bin binary() binary()
579+
[?_assertEqual({ok, <<"aaa">>},
580+
msgpack:unpack(<<16#C4, 3, "aaa">>, [{spec,new},{unpack_str,UnpackStr}])),
581+
?_assertEqual({ok, binary_a(256)},
582+
msgpack:unpack(<<16#C5, 1,0, (binary_a(256))/binary>>, [{spec,new},{unpack_str,UnpackStr}])),
583+
?_assertEqual({ok, binary_a(65536)},
584+
msgpack:unpack(<<16#C6, 0,1,0,0, (binary_a(65536))/binary>>, [{spec,new},{unpack_str,UnpackStr}]))]
585+
|| UnpackStr <- [as_binary, as_list]
524586
]},
525-
{"unpack_str, as_list", []},
526-
{"validate_string, false",
587+
{"unpack_str, on str",
588+
%% str binary() string()
589+
[ %% as_binary
590+
?_assertEqual({ok, <<"aaa">>},
591+
msgpack:unpack(<<2#101:3, 3:5, 97,97,97>>, [{spec,new},{unpack_str,as_binary}])),
592+
?_assertEqual({ok, binary_a(32)},
593+
msgpack:unpack(<<16#D9, 32, (binary_a(32))/binary>>, [{spec,new},{unpack_str,as_binary}])),
594+
?_assertEqual({ok, binary_a(256)},
595+
msgpack:unpack(<<16#DA, 1,0, (binary_a(256))/binary>>, [{spec,new},{unpack_str,as_binary}])),
596+
?_assertEqual({ok, binary_a(65536)},
597+
msgpack:unpack(<<16#DB, 0,1,0,0, (binary_a(65536))/binary>>, [{spec,new},{unpack_str,as_binary}])),
598+
%% as_list => string
599+
?_assertEqual({ok, "aaa"},
600+
msgpack:unpack(<<2#101:3, 3:5, 97,97,97>>, [{spec,new},{unpack_str,as_list}])),
601+
?_assertEqual({ok, list_a(32)},
602+
msgpack:unpack(<<16#D9, 32, (binary_a(32))/binary>>, [{spec,new},{unpack_str,as_list}])),
603+
?_assertEqual({ok, list_a(256)},
604+
msgpack:unpack(<<16#DA, 1,0, (binary_a(256))/binary>>, [{spec,new},{unpack_str,as_list}])),
605+
?_assertEqual({ok, list_a(65536)},
606+
msgpack:unpack(<<16#DB, 0,1,0,0, (binary_a(65536))/binary>>, [{spec,new},{unpack_str,as_list}]))
607+
]}].
608+
609+
unpack_str_validation_test_() ->
610+
[{"validate_string false, on unpacking",
527611
[]},
528-
{"validate_string, true", []}
612+
{"validate_string true, on unpacking",
613+
[]}
529614
].

0 commit comments

Comments
 (0)