|
| 1 | +%% |
| 2 | +%% MessagePack for Erlang |
| 3 | +%% |
| 4 | +%% Copyright (C) 2009-2014 UENISHI Kota |
| 5 | +%% |
| 6 | +%% Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +%% you may not use this file except in compliance with the License. |
| 8 | +%% You may obtain a copy of the License at |
| 9 | +%% |
| 10 | +%% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +%% |
| 12 | +%% Unless required by applicable law or agreed to in writing, software |
| 13 | +%% distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +%% See the License for the specific language governing permissions and |
| 16 | +%% limitations under the License. |
| 17 | +%% |
| 18 | + |
| 19 | +-module(msgpack_eqc). |
| 20 | + |
| 21 | + |
| 22 | +-ifdef(TEST). |
| 23 | +-ifdef(EQC). |
| 24 | + |
| 25 | +-compile(export_all). |
| 26 | +-include_lib("eqc/include/eqc.hrl"). |
| 27 | +-include_lib("eunit/include/eunit.hrl"). |
| 28 | + |
| 29 | +-define(NUMTESTS, 16). |
| 30 | +-define(QC_OUT(P), |
| 31 | + eqc:on_output(fun(Str, Args) -> |
| 32 | + io:format(user, Str, Args) end, P)). |
| 33 | +-define(_assertProp(S), |
| 34 | + {timeout, ?NUMTESTS * 10, |
| 35 | + ?_assert(quickcheck(numtests(?NUMTESTS, ?QC_OUT(S))))}). |
| 36 | + |
| 37 | +eqc_test_() -> |
| 38 | + {inparallel, |
| 39 | + [ |
| 40 | + ?_assertProp(prop_msgpack()), |
| 41 | + ?_assertProp(prop_msgpack([{format, jiffy}])), |
| 42 | + ?_assertProp(prop_msgpack([{format, jsx}])) |
| 43 | + ]}. |
| 44 | + |
| 45 | +prop_msgpack() -> |
| 46 | + ?FORALL(Obj, msgpack_object(), |
| 47 | + begin |
| 48 | + {ok, Obj} =:= msgpack:unpack(msgpack:pack(Obj)) |
| 49 | + end). |
| 50 | + |
| 51 | +prop_msgpack(Options) -> |
| 52 | + ?FORALL(Obj, msgpack_object(), |
| 53 | + begin |
| 54 | + {ok, Obj} =:= msgpack:unpack(msgpack:pack(Obj, Options), Options) |
| 55 | + end). |
| 56 | + |
| 57 | +msgpack_object() -> |
| 58 | + oneof(container_types() ++ primitive_types()). |
| 59 | + |
| 60 | +container_types() -> |
| 61 | + [ fix_array(), array16() ]. |
| 62 | +%% TODO: add map |
| 63 | + |
| 64 | +primitive_types() -> |
| 65 | + [null(), |
| 66 | + positive_fixnum(), negative_fixnum(), |
| 67 | + int8(), int16(), int32(), int64(), |
| 68 | + uint8(), uint16(), uint32(), uint64(), |
| 69 | + eqc_gen:real(), eqc_gen:bool(), |
| 70 | + fix_raw(), raw16(), raw32() |
| 71 | + ]. |
| 72 | + %% fix_raw(), raw16(), raw32()]). |
| 73 | + |
| 74 | +positive_fixnum() -> choose(0, 127). |
| 75 | +negative_fixnum() -> choose(-32, -1). |
| 76 | + |
| 77 | +int8() -> choose(-16#80, 16#7F). |
| 78 | +int16() -> oneof([choose(-16#8000, -16#81), |
| 79 | + choose(16#80, 16#7FFF)]). |
| 80 | +int32() -> oneof([choose(-16#80000000, -16#8001), |
| 81 | + choose(16#10000, 16#7FFFFFFF)]). |
| 82 | +int64() -> oneof([choose(-16#8000000000000000, -16#80000001), |
| 83 | + choose(16#100000000, 16#7FFFFFFFFFFFFFFF)]). |
| 84 | + |
| 85 | +uint8() -> choose(0, 16#FF). |
| 86 | +uint16() -> choose(16#100, 16#FFFF). |
| 87 | +uint32() -> choose(16#10000, 16#FFFFFFFF). |
| 88 | +uint64() -> choose(16#100000000, 16#FFFFFFFFFFFFFFFF). |
| 89 | + |
| 90 | +null() -> null. |
| 91 | + |
| 92 | +fix_raw() -> |
| 93 | + ?LET(Integer, choose(0, 31), |
| 94 | + ?LET(Binary, binary(Integer), Binary)). |
| 95 | + |
| 96 | +raw16() -> |
| 97 | + ?LET(Integer, uint16(), |
| 98 | + ?LET(Binary, binary(Integer), Binary)). |
| 99 | + |
| 100 | +raw32() -> |
| 101 | + ?LET(Binary, binary(65537), Binary). |
| 102 | + |
| 103 | +fix_array() -> |
| 104 | + eqc_gen:resize(16, eqc_gen:list(oneof(primitive_types()))). |
| 105 | + |
| 106 | +array16() -> |
| 107 | + eqc_gen:resize(128, eqc_gen:list(oneof(primitive_types()))). |
| 108 | + |
| 109 | +-endif. |
| 110 | +-endif. |
0 commit comments