couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject [2/8] Initial implementation
Date Fri, 01 Aug 2014 09:06:15 GMT
http://git-wip-us.apache.org/repos/asf/couchdb-khash/blob/9b44d236/test/gen_term.erl
----------------------------------------------------------------------
diff --git a/test/gen_term.erl b/test/gen_term.erl
new file mode 100644
index 0000000..11a4f96
--- /dev/null
+++ b/test/gen_term.erl
@@ -0,0 +1,131 @@
+%% This file is part of khash released under the MIT license.
+%% See the LICENSE file for more information.
+%% Copyright 2013 Cloudant, Inc <support@cloudant.com>
+
+-module(gen_term).
+
+-export([
+    any/0,
+    any/1,
+
+    gen_atom/1,
+    gen_integer/1,
+    gen_float/1,
+    gen_reference/1,
+    gen_port/1,
+    gen_pid/1,
+    gen_tuple/1,
+    gen_list/1,
+    gen_short_string/1,
+    gen_string/1,
+    gen_binary/1,
+    gen_bitstring/1,
+    gen_bignum/1,
+    gen_function/1
+]).
+
+
+any() ->
+    any(16).
+
+
+any(MaxSize) when MaxSize =< 0 ->
+    Fun = choice(value_types()),
+    ?MODULE:Fun(MaxSize);
+any(MaxSize) ->
+    Fun = choice(all_types()),
+    ?MODULE:Fun(MaxSize).
+
+
+gen_atom(MaxSize) ->
+    list_to_atom(gen_short_string(MaxSize)).
+
+
+gen_integer(_) ->
+    Value = case random:uniform() < 0.5 of
+        true -> random:uniform(127);
+        false -> random:uniform(16#FFFFFFFF)
+    end,
+    case random:uniform() < 0.5 of
+        true -> -1 * Value;
+        false -> Value
+    end.
+
+
+gen_float(_) ->
+    random:uniform() * float(16#FFFFFFFF).
+
+
+gen_reference(_) ->
+    erlang:make_ref().
+
+
+gen_port(_) ->
+    Ports = erlang:ports(),
+    lists:nth(random:uniform(length(Ports)), Ports).
+
+
+gen_pid(_) ->
+    Pids = erlang:processes(),
+    lists:nth(random:uniform(length(Pids)), Pids).
+
+
+gen_tuple(MaxSize) ->
+    list_to_tuple(gen_list(MaxSize)).
+
+
+gen_list(MaxSize) ->
+    Width = random:uniform(MaxSize),
+    [any(MaxSize-Width) || _ <- lists:seq(1, Width)].
+
+
+gen_short_string(_) ->
+    Size = random:uniform(255),
+    [random:uniform(127) || _ <- lists:seq(1, Size)].
+
+
+gen_string(_) ->
+    Size = random:uniform(4096),
+    [random:uniform(127) || _ <- lists:seq(1, Size)].
+
+
+gen_binary(MaxSize) ->
+    list_to_binary(gen_string(MaxSize)).
+
+
+gen_bitstring(MaxSize) ->
+    B = gen_binary(MaxSize),
+    <<2:4/integer, B/binary>>.
+
+
+gen_bignum(_) ->
+    16#FFFFFFFFFFFFFFFF + random:uniform(16#FFFFFFFF).
+
+
+gen_function(_) ->
+    choice(all_types()).
+
+
+choice(Options) ->
+    lists:nth(random:uniform(length(Options)), Options).
+
+
+value_types() ->
+    [
+        gen_atom,
+        gen_integer,
+        gen_float,
+        gen_reference,
+        gen_port,
+        gen_pid,
+        gen_short_string,
+        gen_string,
+        gen_binary,
+        gen_bitstring,
+        gen_bignum,
+        gen_function
+    ].
+
+
+all_types() ->
+    value_types() ++ [gen_tuple, gen_list].

http://git-wip-us.apache.org/repos/asf/couchdb-khash/blob/9b44d236/test/util.erl
----------------------------------------------------------------------
diff --git a/test/util.erl b/test/util.erl
new file mode 100644
index 0000000..4f0bbf6
--- /dev/null
+++ b/test/util.erl
@@ -0,0 +1,25 @@
+%% This file is part of khash released under the MIT license.
+%% See the LICENSE file for more information.
+%% Copyright 2013 Cloudant, Inc <support@cloudant.com>
+
+-module(util).
+-compile(export_all).
+
+
+init_code_path() ->
+    code:add_pathz("ebin").
+
+
+run(Plan, Fun) ->
+    init_code_path(),
+    etap:plan(Plan),
+    case (catch Fun()) of
+        ok ->
+            etap:end_tests();
+        Other ->
+            etap:diag(io_lib:format("Test died abnormally:~n~p", [Other])),
+            timer:sleep(500),
+            etap:bail(Other)
+    end,
+    ok.
+


Mime
View raw message