Permalink
Browse files

Add unicode generators, but tests are not passed yet.

  • Loading branch information...
1 parent bf8de0f commit 2cde31e48ecfa00ca67f38c4a77349a1aad40251 @arcusfelis committed Dec 29, 2012
Showing with 91 additions and 7 deletions.
  1. +4 −4 include/triq.hrl
  2. +9 −3 src/triq_dom.erl
  3. +78 −0 test/triq_unicode_tests.erl
View
@@ -58,11 +58,11 @@
%%
%% import domain functions (a.k.a. generators)
%%
--import(?DOMAIN_MODULE, [list/1, tuple/1, int/0, real/0, elements/1, any/0, atom/0,
+-import(?DOMAIN_MODULE, [list/1, tuple/1, int/0, 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_binary/0, unicode_binary/1, unicode_char/0,
- non_empty/1,
- shrink_without_duplicates/1, resize/2, frequency/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,
+ non_empty/1, shrink_without_duplicates/1, resize/2, frequency/1]).
%%
View
@@ -104,7 +104,7 @@
%% generators
--export([list/1, tuple/1, int/0, real/0, sized/1, elements/1, any/0, atom/0, atom/1, choose/2,
+-export([list/1, tuple/1, int/0, 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,
@@ -556,6 +556,7 @@ 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,
@@ -568,6 +569,11 @@ int() ->
end
}.
+%% TODO: write me.
+-spec byte() -> domrec(integer()).
+byte() ->
+ int().
+
%% @doc The domain of floats.
%% @spec real() -> domain(float())
-spec real() -> domrec(float()).
@@ -1315,7 +1321,7 @@ unicode_string(Size) ->
-spec unicode_binary() -> domrec(binary()).
unicode_binary() ->
- unicode_binary(any, undefined).
+ unicode_binary(any, utf8).
%% @doc Generate an unicode binary binary.
@@ -1324,7 +1330,7 @@ unicode_binary() ->
Encoding :: unicode:encoding().
unicode_binary(Size) when is_integer(Size) ->
- unicode_binary(Size, undefined);
+ unicode_binary(Size, utf8);
unicode_binary(Encoding) ->
unicode_binary(any, Encoding).
@@ -0,0 +1,78 @@
+%% @author Uvarov Michael <arcusfelis@gmail.com>
+-module(triq_unicode_tests).
+
+%% ------------------------------------------------------------------
+%% Tests
+%% ------------------------------------------------------------------
+
+-include_lib("triq/include/triq.hrl").
+-include_lib("eunit/include/eunit.hrl").
+
+equals(X, Y) -> X =:= Y.
+
+
+%% ------------------------------------------------------------------
+%% Call test generators
+%% ------------------------------------------------------------------
+
+prop_unicode_char() ->
+ ?FORALL(Char, unicode_char(),
+ begin
+% io:format(user, "~p~n", [Char]),
+ true
+ end).
+
+prop_unicode_binary() ->
+ ?FORALL(Bin, unicode_binary(),
+ begin
+ equals(Bin, unicode:characters_to_binary(
+ unicode:characters_to_list(Bin)))
+ end).
+
+
+%% Check a binary generator with fixed length.
+prop_sized_unicode_binary() ->
+ ?FORALL({Len, Bin}, ?LET(Len, byte(), {Len, unicode_binary(Len)}),
+ equals(Len, length(unicode:characters_to_list(Bin)))).
+
+
+%% Check, that the `characters_to_list/1' does not fail.
+prop_unicode_string() ->
+ ?FORALL(Str, unicode_string(),
+ equals(Str, unicode:characters_to_list(
+ unicode:characters_to_binary(Str)))).
+
+
+prop_unicode_characters() ->
+ ?FORALL(Chars, unicode_characters(),
+ is_binary(unicode:characters_to_binary(Chars))).
+
+
+encoding() ->
+ [unicode, utf8, utf16, {utf16, little}, {utf16, big}, utf32,
+ {utf32, little}, {utf32, big}].
+
+
+prop_unicode_external_characters() ->
+ ?FORALL({Encoding, Chars},
+ oneof([{Encoding, unicode_characters(Encoding)}
+ || Encoding <- encoding()]),
+ begin
+ List = unicode:characters_to_list(Chars, Encoding),
+ is_binary(unicode:characters_to_binary(Chars, Encoding))
+ end).
+
+
+%% -------------------------------------------------------------------
+%% Property Testing
+%% -------------------------------------------------------------------
+
+run_property_testing_test_() ->
+ {timeout, 60, fun run_property_testing_case/0}.
+
+run_property_testing_case() ->
+ EunitLeader = erlang:group_leader(),
+ erlang:group_leader(whereis(user), self()),
+ Res = triq:module(?MODULE),
+ erlang:group_leader(EunitLeader, self()),
+ ?assertEqual([], Res).

0 comments on commit 2cde31e

Please sign in to comment.