Support NaN and Infinity, closes #72

This commit is contained in:
Yuki Ito 2016-12-05 19:38:01 +09:00
parent f4639fb968
commit 3616fd6596
2 changed files with 23 additions and 0 deletions

View File

@ -47,8 +47,20 @@ unpack_stream(<<16#C6, L:32/big-unsigned-integer-unit:1, V:L/binary, Rest/binary
%% Floats
unpack_stream(<<16#CA, V:32/float-unit:1, Rest/binary>>, _) ->
{V, Rest};
unpack_stream(<<16#CA, 0:1, 16#FF:8, 0:23, Rest/binary>>, _) ->
{positive_infinity, Rest};
unpack_stream(<<16#CA, 1:1, 16#FF:8, 0:23 , Rest/binary>>, _) ->
{negative_infinity, Rest};
unpack_stream(<<16#CA, _:1, 16#FF:8, _:23 , Rest/binary>>, _) ->
{nan, Rest};
unpack_stream(<<16#CB, V:64/float-unit:1, Rest/binary>>, _) ->
{V, Rest};
unpack_stream(<<16#CB, 0:1, 2#11111111111:11, 0:52, Rest/binary>>, _) ->
{positive_infinity, Rest};
unpack_stream(<<16#CB, 1:1, 2#11111111111:11, 0:52 , Rest/binary>>, _) ->
{negative_infinity, Rest};
unpack_stream(<<16#CB, _:1, 2#11111111111:11, _:52 , Rest/binary>>, _) ->
{nan, Rest};
%% Unsigned integers
unpack_stream(<<16#CC, V:8/unsigned-integer, Rest/binary>>, _) ->

View File

@ -625,3 +625,14 @@ unpack_str_validation_test_() ->
msgpack:unpack(binary_to_list(InvalidStr),
[{spec,new},{unpack_str,from_list},{validate_string,true}]))]}
].
float_unpacking_test_() ->
%% float 32
[?_assertEqual({ok, nan}, msgpack:unpack(<<16#CA, 16#FF, 16#FF, 16#FF, 16#FF>>)),
?_assertEqual({ok, positive_infinity}, msgpack:unpack(<<16#CA, 16#7F, 16#80, 16#00, 16#00>>)),
?_assertEqual({ok, negative_infinity}, msgpack:unpack(<<16#CA, 16#FF, 16#80, 16#00, 16#00>>)),
%% float 64
?_assertEqual({ok, nan}, msgpack:unpack(<<16#CB, 16#FF, 16#FF, 16#FF, 16#FF, 16#FF, 16#FF, 16#FF, 16#FF>>)),
?_assertEqual({ok, positive_infinity}, msgpack:unpack(<<16#CB, 16#7F, 16#F0, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00>>)),
?_assertEqual({ok, negative_infinity}, msgpack:unpack(<<16#CB, 16#FF, 16#F0, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00>>))].