Permalink
Browse files

Added int/1, int/2 generators. Passed unicode tests.

  • Loading branch information...
1 parent 2cde31e commit 36b87845f2bb5bae4b521d45146b27d3806e8bba @arcusfelis committed Dec 29, 2012
Showing with 32 additions and 13 deletions.
  1. +1 −1 include/triq.hrl
  2. +28 −10 src/triq_dom.erl
  3. +3 −2 test/triq_unicode_tests.erl
View
@@ -58,7 +58,7 @@
%%
%% 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,
unicode_char/0, unicode_string/0, unicode_string/1, unicode_binary/0,
unicode_binary/1, unicode_binary/2, unicode_characters/0, unicode_characters/1,
View
@@ -104,7 +104,7 @@
%% 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]).
-export([unicode_char/0,
@@ -138,14 +138,15 @@ error_shrink(#?DOM{kind=Kind},_) -> erlang:error({shrink,Kind}).
%% the structure of the picked value.
%% @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);
%%
%% A tuple is generated by generating each element
%%
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),
{list_to_tuple(DomList), list_to_tuple(List)};
@@ -154,7 +155,8 @@ pick(T,SampleSize) when is_tuple(T) ->
%% each head
%%
pick([], _) -> {[],[]};
-pick([H|T], SampleSize) ->
+pick([H|T], SampleSize)
+ when SampleSize > 0, is_integer(SampleSize) ->
{HDom,HVal} = pick(H,SampleSize),
{TDom,TVal} = pick(T,SampleSize),
{[HDom|TDom], [HVal|TVal]};
@@ -556,7 +558,6 @@ tuple_shrink(#?DOM{kind={shrinkable_tuple, TupleDom}, empty_ok=EmptyOK}, Tuple)
%% @doc The domain of integers.
%% @spec int() -> domain(integer())
-%% TODO: sized int().
-spec int() -> domrec(integer()).
int() ->
#?DOM{kind=int,
@@ -569,10 +570,26 @@ int() ->
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()).
byte() ->
- int().
+ int(0, 255).
%% @doc The domain of floats.
%% @spec real() -> domain(float())
@@ -1231,7 +1248,7 @@ domain(Name,PickFun,ShrinkFun) ->
foldn(_,Acc,0) -> Acc;
-foldn(Fun,Acc,Count) ->
+foldn(Fun,Acc,Count) when Count > 0 ->
foldn(Fun, Fun(Acc), Count-1).
%% remove the RemIdx'th element of List [1-indexed]
@@ -1351,7 +1368,8 @@ unicode_binary(Size, 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
any ->
case EmptyOK of
@@ -1409,5 +1427,5 @@ unicode_characters1(0, _Encoding) ->
unicode_characters1(Size, Encoding) ->
Chars = ?LAZY(resize(Size, unicode_characters(Encoding))),
%% TODO: Unicode characters can be of type `maybe_improper_list()'.
- frequency([{10,unicode_char()}, {1, Chars}]).
+ list(frequency([{10,unicode_char()}, {1, Chars}])).
@@ -8,7 +8,8 @@
-include_lib("triq/include/triq.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.
%% ------------------------------------------------------------------
@@ -75,4 +76,4 @@ run_property_testing_case() ->
erlang:group_leader(whereis(user), self()),
Res = triq:module(?MODULE),
erlang:group_leader(EunitLeader, self()),
- ?assertEqual([], Res).
+ ?assert(Res).

0 comments on commit 36b8784

Please sign in to comment.