Skip to content

Commit

Permalink
Added int/1, int/2 generators. Passed unicode tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
arcusfelis committed Dec 29, 2012
1 parent 2cde31e commit 36b8784
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion include/triq.hrl
Expand Up @@ -58,7 +58,7 @@
%% %%
%% import domain functions (a.k.a. generators) %% import domain functions (a.k.a. generators)
%% %%
-import(?DOMAIN_MODULE, [list/1, tuple/1, int/0, byte/0, real/0, elements/1, any/0, atom/0, -import(?DOMAIN_MODULE, [list/1, tuple/1, int/0, int/1, int/2, byte/0, real/0, elements/1, any/0, atom/0,
choose/2, bool/0, char/0, oneof/1, return/1, vector/2, binary/0, binary/1, choose/2, bool/0, char/0, oneof/1, return/1, vector/2, binary/0, binary/1,
unicode_char/0, unicode_string/0, unicode_string/1, unicode_binary/0, unicode_char/0, unicode_string/0, unicode_string/1, unicode_binary/0,
unicode_binary/1, unicode_binary/2, unicode_characters/0, unicode_characters/1, unicode_binary/1, unicode_binary/2, unicode_characters/0, unicode_characters/1,
Expand Down
38 changes: 28 additions & 10 deletions src/triq_dom.erl
Expand Up @@ -104,7 +104,7 @@




%% generators %% generators
-export([list/1, tuple/1, int/0, byte/0, real/0, sized/1, elements/1, any/0, atom/0, atom/1, choose/2, -export([list/1, tuple/1, int/0, int/1, int/2, byte/0, real/0, sized/1, elements/1, any/0, atom/0, atom/1, choose/2,
oneof/1, frequency/1, bool/0, char/0, return/1, vector/2, binary/1, binary/0, non_empty/1, resize/2]). oneof/1, frequency/1, bool/0, char/0, return/1, vector/2, binary/1, binary/0, non_empty/1, resize/2]).


-export([unicode_char/0, -export([unicode_char/0,
Expand Down Expand Up @@ -138,14 +138,15 @@ error_shrink(#?DOM{kind=Kind},_) -> erlang:error({shrink,Kind}).
%% the structure of the picked value. %% the structure of the picked value.
%% @spec pick(domain(T), pos_integer()) -> {domain(T), T} %% @spec pick(domain(T), pos_integer()) -> {domain(T), T}
%% %%
pick(Dom=#?DOM{pick=PickFun}, SampleSize) -> pick(Dom=#?DOM{pick=PickFun}, SampleSize)
when SampleSize > 0, is_integer(SampleSize) ->
PickFun(Dom,SampleSize); PickFun(Dom,SampleSize);


%% %%
%% A tuple is generated by generating each element %% A tuple is generated by generating each element
%% %%
pick({}=Empty, _) -> {Empty,Empty}; pick({}=Empty, _) -> {Empty,Empty};
pick(T,SampleSize) when is_tuple(T) -> pick(T,SampleSize) when is_tuple(T), SampleSize > 0, is_integer(SampleSize) ->
{DomList,List} = pick(tuple_to_list(T), SampleSize), {DomList,List} = pick(tuple_to_list(T), SampleSize),
{list_to_tuple(DomList), list_to_tuple(List)}; {list_to_tuple(DomList), list_to_tuple(List)};


Expand All @@ -154,7 +155,8 @@ pick(T,SampleSize) when is_tuple(T) ->
%% each head %% each head
%% %%
pick([], _) -> {[],[]}; pick([], _) -> {[],[]};
pick([H|T], SampleSize) -> pick([H|T], SampleSize)
when SampleSize > 0, is_integer(SampleSize) ->
{HDom,HVal} = pick(H,SampleSize), {HDom,HVal} = pick(H,SampleSize),
{TDom,TVal} = pick(T,SampleSize), {TDom,TVal} = pick(T,SampleSize),
{[HDom|TDom], [HVal|TVal]}; {[HDom|TDom], [HVal|TVal]};
Expand Down Expand Up @@ -556,7 +558,6 @@ tuple_shrink(#?DOM{kind={shrinkable_tuple, TupleDom}, empty_ok=EmptyOK}, Tuple)


%% @doc The domain of integers. %% @doc The domain of integers.
%% @spec int() -> domain(integer()) %% @spec int() -> domain(integer())
%% TODO: sized int().
-spec int() -> domrec(integer()). -spec int() -> domrec(integer()).
int() -> int() ->
#?DOM{kind=int, #?DOM{kind=int,
Expand All @@ -569,10 +570,26 @@ int() ->
end end
}. }.


%% TODO: write me.
int(Max) ->
int(0, Max).

int(Min, Max) ->
Diff = Max - Min,
#?DOM{kind=int,
shrink=fun(Dom,Val) when Val>0 -> {Dom,Val-1};
(Dom,Val) when Val<0 -> {Dom,Val+1};
(Dom,0) -> {Dom,0}
end,
pick=fun(Dom,SampleSize) ->
{Dom, random:uniform(max(SampleSize, Diff)) + Min}
end
}.


-spec byte() -> domrec(integer()). -spec byte() -> domrec(integer()).
byte() -> byte() ->
int(). int(0, 255).


%% @doc The domain of floats. %% @doc The domain of floats.
%% @spec real() -> domain(float()) %% @spec real() -> domain(float())
Expand Down Expand Up @@ -1231,7 +1248,7 @@ domain(Name,PickFun,ShrinkFun) ->




foldn(_,Acc,0) -> Acc; foldn(_,Acc,0) -> Acc;
foldn(Fun,Acc,Count) -> foldn(Fun,Acc,Count) when Count > 0 ->
foldn(Fun, Fun(Acc), Count-1). foldn(Fun, Fun(Acc), Count-1).


%% remove the RemIdx'th element of List [1-indexed] %% remove the RemIdx'th element of List [1-indexed]
Expand Down Expand Up @@ -1351,7 +1368,8 @@ unicode_binary(Size, Encoding) ->




unicode_binary_pick(#?DOM{kind=#unicode_binary{size=Size, encoding=Encoding}, unicode_binary_pick(#?DOM{kind=#unicode_binary{size=Size, encoding=Encoding},
empty_ok=EmptyOK}=BinDom, SampleSize) -> empty_ok=EmptyOK}=BinDom, SampleSize)
when SampleSize > 1 ->
Sz = case Size of Sz = case Size of
any -> any ->
case EmptyOK of case EmptyOK of
Expand Down Expand Up @@ -1409,5 +1427,5 @@ unicode_characters1(0, _Encoding) ->
unicode_characters1(Size, Encoding) -> unicode_characters1(Size, Encoding) ->
Chars = ?LAZY(resize(Size, unicode_characters(Encoding))), Chars = ?LAZY(resize(Size, unicode_characters(Encoding))),
%% TODO: Unicode characters can be of type `maybe_improper_list()'. %% TODO: Unicode characters can be of type `maybe_improper_list()'.
frequency([{10,unicode_char()}, {1, Chars}]). list(frequency([{10,unicode_char()}, {1, Chars}])).


5 changes: 3 additions & 2 deletions test/triq_unicode_tests.erl
Expand Up @@ -8,7 +8,8 @@
-include_lib("triq/include/triq.hrl"). -include_lib("triq/include/triq.hrl").
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").


equals(X, Y) -> X =:= Y. equals(X, X) -> true;
equals(X, Y) -> io:format(user, "Are not equal ~p and ~p.", [X,Y]), false.




%% ------------------------------------------------------------------ %% ------------------------------------------------------------------
Expand Down Expand Up @@ -75,4 +76,4 @@ run_property_testing_case() ->
erlang:group_leader(whereis(user), self()), erlang:group_leader(whereis(user), self()),
Res = triq:module(?MODULE), Res = triq:module(?MODULE),
erlang:group_leader(EunitLeader, self()), erlang:group_leader(EunitLeader, self()),
?assertEqual([], Res). ?assert(Res).

0 comments on commit 36b8784

Please sign in to comment.