Skip to content

Commit

Permalink
Add proper testing on primitive types (#94)
Browse files Browse the repository at this point in the history
* Add property based test on primitive msgpack types
  • Loading branch information
kuenishi committed Sep 17, 2023
1 parent 6f98251 commit 12790ba
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 40 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
10 changes: 10 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@
{erl_first_files, [
"src/msgpack_ext.erl"
]}.

{project_plugins, [rebar3_proper]}.

{profiles,
[{test, [
{erl_opts, [nowarn_export_all]},
{deps, [proper]}
]
}]
}.
77 changes: 77 additions & 0 deletions test/prop_msgpack.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
-module(prop_msgpack).
-include_lib("proper/include/proper.hrl").
-include("msgpack.hrl").


%%% 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 %%%
pack_and_unpack(Obj) ->
Bin = msgpack:pack(Obj),
{ok, Obj} = msgpack:unpack(Bin),
is_binary(Bin).

%%% Generators %%%
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 12790ba

Please sign in to comment.