Add proper tests for base62 (#2761)

* Add proper tests for base62

* Delete useless comment
This commit is contained in:
Gilbert 2019-08-07 09:24:03 +08:00 committed by GitHub
parent 3230c25b56
commit 96b341fde5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 17 deletions

View File

@ -11,6 +11,7 @@ script:
- make xref
- make eunit
- make ct
- make proper
- make cover
after_success:

View File

@ -14,6 +14,13 @@ RUN_NODE_NAME = emqxdebug@127.0.0.1
.PHONY: all
all: compile
.PHONY: tests
tests: eunit ct proper
.PHONY: proper
proper:
@rebar3 proper
.PHONY: run
run: run_setup unlock
@rebar3 as test get-deps

View File

@ -21,15 +21,15 @@
{cover_opts, [verbose]}.
{cover_export_enabled, true}.
{plugins, [coveralls]}.
{plugins, [coveralls,
rebar3_proper]}.
{erl_first_files, ["src/emqx_logger.erl"]}.
{profiles,
[{test,
[{deps,
[{meck, "0.8.13"}, % hex
{bbmustache, "1.7.0"}, % hex
[{bbmustache, "1.7.0"}, % hex
{emqtt, {git, "https://github.com/emqx/emqtt", {tag, "v1.0.1"}}},
{emqx_ct_helpers, {git, "https://github.com/emqx/emqx-ct-helpers", {branch, "develop"}}}
]}

View File

@ -18,9 +18,7 @@
%% APIs
-export([ encode/1
, encode/2
, decode/1
, decode/2
]).
%%--------------------------------------------------------------------
@ -32,7 +30,7 @@
encode(I) when is_integer(I) ->
encode(integer_to_binary(I));
encode(S) when is_list(S)->
encode(list_to_binary(S));
encode(unicode:characters_to_binary(S));
encode(B) when is_binary(B) ->
encode(B, <<>>).
@ -40,8 +38,6 @@ encode(B) when is_binary(B) ->
%% binary_to_list(encode(D)).
%% @doc Decode base62 binary to origin data binary
decode(L) when is_list(L) ->
decode(list_to_binary(L));
decode(B) when is_binary(B) ->
decode(B, <<>>).
@ -49,8 +45,6 @@ decode(B) when is_binary(B) ->
%% Interval Functions
%%--------------------------------------------------------------------
encode(D, string) ->
binary_to_list(encode(D));
encode(<<Index1:6, Index2:6, Index3:6, Index4:6, Rest/binary>>, Acc) ->
CharList = [encode_char(Index1), encode_char(Index2), encode_char(Index3), encode_char(Index4)],
NewAcc = <<Acc/binary,(iolist_to_binary(CharList))/binary>>,
@ -66,10 +60,6 @@ encode(<<Index1:6, Index2:2>>, Acc) ->
encode(<<>>, Acc) ->
Acc.
decode(D, integer) ->
binary_to_integer(decode(D));
decode(D, string) ->
binary_to_list(decode(D));
decode(<<Head:8, Rest/binary>>, Acc)
when bit_size(Rest) >= 8->
case Head == $9 of
@ -114,4 +104,3 @@ decode_char(I) when I >= $A andalso I =< $Z->
decode_char(9, I) ->
I + 61 - $A.

View File

@ -138,6 +138,5 @@ to_base62(<<I:128>>) ->
emqx_base62:encode(I).
from_base62(S) ->
I = emqx_base62:decode(S, integer),
I = binary_to_integer( emqx_base62:decode(S)),
<<I:128>>.

51
test/prop_base62.erl Normal file
View File

@ -0,0 +1,51 @@
-module(prop_base62).
-include_lib("proper/include/proper.hrl").
%%%%%%%%%%%%%%%%%%
%%% Properties %%%
%%%%%%%%%%%%%%%%%%
prop_symmetric() ->
?FORALL(Data, raw_data(),
begin
Encoded = emqx_base62:encode(Data),
to_binary(Data) =:= emqx_base62:decode(Encoded)
end).
prop_size() ->
?FORALL(Data, binary(),
begin
Encoded = emqx_base62:encode(Data),
base62_size(Data, Encoded)
end).
%%%%%%%%%%%%%%%
%%% Helpers %%%
%%%%%%%%%%%%%%%
to_binary(Data) when is_list(Data) ->
unicode:characters_to_binary(Data);
to_binary(Data) when is_integer(Data) ->
integer_to_binary(Data);
to_binary(Data) when is_binary(Data) ->
Data.
base62_size(Data, Encoded) ->
DataSize = erlang:size(Data),
EncodedSize = erlang:size(Encoded),
case (DataSize * 8 rem 6) of
0 ->
%% Due to the particularity of base 62, 3 bytes data maybe encoded
%% as 4 bytes data or 5 bytes data, the encode size maybe in the
%% range between DataSize*4/3 and DataSize*8/3
RangeStart = DataSize div 3 * 4,
RangeEnd = DataSize div 3 * 8,
EncodedSize >= RangeStart andalso EncodedSize =< RangeEnd;
_Rem ->
RangeStart = DataSize * 8 div 6 + 1,
RangeEnd = DataSize * 8 div 6 * 2 + 1,
EncodedSize >= RangeStart andalso EncodedSize =< RangeEnd
end.
%%%%%%%%%%%%%%%%%%
%%% Generators %%%
%%%%%%%%%%%%%%%%%%
raw_data() -> oneof([integer(), string(), binary()]).