summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Sackman <matthew@lshift.net>2009-08-19 11:18:35 +0100
committerMatthew Sackman <matthew@lshift.net>2009-08-19 11:18:35 +0100
commite22c1e1a03c6a0d521dfa6109a26bb8bd18ce34b (patch)
tree75f0be8b22be8b066efc9451712cf743ae798d16
parente4862945d91dd5fead85a8db6a48c480a31746ac (diff)
downloadrabbitmq-server-e22c1e1a03c6a0d521dfa6109a26bb8bd18ce34b.tar.gz
New branch for bug 21428bug21428
-rw-r--r--src/rabbit_misc.erl20
-rw-r--r--src/rabbit_tests.erl9
2 files changed, 28 insertions, 1 deletions
diff --git a/src/rabbit_misc.erl b/src/rabbit_misc.erl
index 13a2aa05..ed8b4165 100644
--- a/src/rabbit_misc.erl
+++ b/src/rabbit_misc.erl
@@ -53,6 +53,7 @@
-export([append_file/2, ensure_parent_dirs_exist/1]).
-export([format_stderr/2]).
-export([start_applications/1, stop_applications/1]).
+-export([unfold/2, ceil/1]).
-import(mnesia).
-import(lists).
@@ -116,7 +117,9 @@
-spec(format_stderr/2 :: (string(), [any()]) -> 'ok').
-spec(start_applications/1 :: ([atom()]) -> 'ok').
-spec(stop_applications/1 :: ([atom()]) -> 'ok').
-
+-spec(unfold/2 :: (fun ((A) -> ({'true', B, A} | 'false')), A) -> {[B], A}).
+-spec(ceil/1 :: (number()) -> number()).
+
-endif.
%%----------------------------------------------------------------------------
@@ -446,3 +449,18 @@ stop_applications(Apps) ->
cannot_stop_application,
Apps).
+unfold(Fun, Init) ->
+ unfold(Fun, [], Init).
+
+unfold(Fun, Acc, Init) ->
+ case Fun(Init) of
+ {true, E, I} -> unfold(Fun, [E|Acc], I);
+ false -> {Acc, Init}
+ end.
+
+ceil(N) ->
+ T = trunc(N),
+ case N - T of
+ 0 -> N;
+ _ -> 1 + T
+ end.
diff --git a/src/rabbit_tests.erl b/src/rabbit_tests.erl
index e5100ccd..e9a2b812 100644
--- a/src/rabbit_tests.erl
+++ b/src/rabbit_tests.erl
@@ -49,6 +49,7 @@ test_content_prop_roundtrip(Datum, Binary) ->
all_tests() ->
passed = test_priority_queue(),
+ passed = test_unfold(),
passed = test_parsing(),
passed = test_topic_matching(),
passed = test_log_management(),
@@ -116,6 +117,14 @@ test_simple_n_element_queue(N) ->
{true, false, N, ToListRes, Items} = test_priority_queue(Q),
passed.
+test_unfold() ->
+ {[], test} = rabbit_misc:unfold(fun (V) -> false end, test),
+ List = lists:seq(2,20,2),
+ {List, 0} = rabbit_misc:unfold(fun (0) -> false;
+ (N) -> {true, N*2, N-1}
+ end, 10),
+ passed.
+
test_parsing() ->
passed = test_content_properties(),
passed.