Skip to content

Commit

Permalink
Add property based test on primitive msgpack types
Browse files Browse the repository at this point in the history
  • Loading branch information
kuenishi committed Sep 17, 2023
1 parent f02c3d5 commit f3fa40d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checkall.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ jobs:
- name: Compile
run: rebar3 compile
- name: Test
run: rebar3 eunit
run: rebar3 do eunit, proper
- name: Check
run: rebar3 do xref, dialyzer
39 changes: 0 additions & 39 deletions eqc/msgpack_eqc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,45 +61,6 @@ container_types() ->
[ fix_array(), array16() ].
%% TODO: add map

primitive_types() ->
[null(),
positive_fixnum(), negative_fixnum(),
int8(), int16(), int32(), int64(),
uint8(), uint16(), uint32(), uint64(),
eqc_gen:real(), eqc_gen:bool(),
fix_raw(), raw16(), raw32()
].
%% fix_raw(), raw16(), raw32()]).

positive_fixnum() -> choose(0, 127).
negative_fixnum() -> choose(-32, -1).

int8() -> choose(-16#80, 16#7F).
int16() -> oneof([choose(-16#8000, -16#81),
choose(16#80, 16#7FFF)]).
int32() -> oneof([choose(-16#80000000, -16#8001),
choose(16#10000, 16#7FFFFFFF)]).
int64() -> oneof([choose(-16#8000000000000000, -16#80000001),
choose(16#100000000, 16#7FFFFFFFFFFFFFFF)]).

uint8() -> choose(0, 16#FF).
uint16() -> choose(16#100, 16#FFFF).
uint32() -> choose(16#10000, 16#FFFFFFFF).
uint64() -> choose(16#100000000, 16#FFFFFFFFFFFFFFFF).

null() -> null.

fix_raw() ->
?LET(Integer, choose(0, 31),
?LET(Binary, binary(Integer), Binary)).

raw16() ->
?LET(Integer, uint16(),
?LET(Binary, binary(Integer), Binary)).

raw32() ->
?LET(Binary, binary(65537), Binary).

fix_array() ->
eqc_gen:resize(16, eqc_gen:list(oneof(primitive_types()))).

Expand Down
106 changes: 70 additions & 36 deletions test/prop_msgpack.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,76 @@
-include_lib("proper/include/proper.hrl").
-include("msgpack.hrl").

%%%%%%%%%%%%%%%%%%
%%% Properties %%%
%%%%%%%%%%%%%%%%%%
prop_test() ->
?FORALL(Type, mytype(),
begin
boolean(Type)
end).

prop_pack() ->
?FORALL(Term, msgpack_term(),
begin
Bin = msgpack:pack(Term),
is_binary(Bin)
end).

%%%%%%%%%%%%%%%

%%% Primitive Properties %%%
prop_uint() ->
?FORALL(
UnsignedInt,
oneof([positive_fixnum(), uint8(), uint16(), uint32(), uint64()]),
pack_and_unpack(UnsignedInt)).

prop_int() ->
?FORALL(
Int,
oneof([positive_fixnum(), negative_fixnum(), int8(), int16(), int32(), int64()]),
pack_and_unpack(Int)).

prop_binary() ->
?FORALL(
Binary,
oneof([fix_raw(), raw16(), raw32()]),
pack_and_unpack(Binary)).

prop_primitive() ->
?FORALL(
PrimObj,
oneof(primitive_types()),
pack_and_unpack(PrimObj)).



%%% Helpers %%%
%%%%%%%%%%%%%%%
boolean(_) -> true.
pack_and_unpack(Obj) ->
Bin = msgpack:pack(Obj),
{ok, Obj} = msgpack:unpack(Bin),
is_binary(Bin).

%%%%%%%%%%%%%%%%%%
%%% Generators %%%
%%%%%%%%%%%%%%%%%%
msgpack_term() ->
frequency(
[
{10, integer()},
{10, float()},
{10, boolean()},
{10, binary()},
{10, utf8()},
{2, ?LAZY(list(msgpack_term()))},
{1, ?LAZY(map(msgpack_term(), msgpack_term()))}
]).


mytype() ->
term().
null() -> null.

positive_fixnum() -> choose(0, 127).
negative_fixnum() -> choose(-32, -1).

int8() -> choose(-16#80, 16#7F).
int16() -> oneof([choose(-16#8000, -16#81),
choose(16#80, 16#7FFF)]).
int32() -> oneof([choose(-16#80000000, -16#8001),
choose(16#10000, 16#7FFFFFFF)]).
int64() -> oneof([choose(-16#8000000000000000, -16#80000001),
choose(16#100000000, 16#7FFFFFFFFFFFFFFF)]).

uint8() -> choose(0, 16#FF).
uint16() -> choose(16#100, 16#FFFF).
uint32() -> choose(16#10000, 16#FFFFFFFF).
uint64() -> choose(16#100000000, 16#FFFFFFFFFFFFFFFF).

fix_raw() ->
?LET(Integer, choose(0, 31),
?LET(Binary, binary(Integer), Binary)).

raw16() ->
?LET(Integer, uint16(),
?LET(Binary, binary(Integer), Binary)).

raw32() ->
?LET(Binary, binary(65537), Binary).

primitive_types() ->
[
null(),
positive_fixnum(), negative_fixnum(),
int8(), int16(), int32(), int64(),
uint8(), uint16(), uint32(), uint64(),
proper_types:float(), proper_types:bool(),
fix_raw(), raw16(), raw32()
].

0 comments on commit f3fa40d

Please sign in to comment.