Permalink
Browse files

Improve eunit test coverage

  • Loading branch information...
jtuple authored and rzezeski committed Aug 5, 2011
1 parent 6f1af68 commit 926c5fa302403dabde05b26269292443d3f81c11
Showing with 173 additions and 2 deletions.
  1. +44 −0 src/bloom.erl
  2. +23 −2 src/json_pp.erl
  3. +28 −0 src/riak_core_gossip.erl
  4. +78 −0 src/riak_core_priority_queue.erl
View
@@ -187,3 +187,47 @@ bitarray_get(I, A) ->
V = array:get(AI, A),
V band (1 bsl (I rem ?W)) =/= 0.
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+simple_shuffle(L, N) ->
+ lists:sublist(simple_shuffle(L), 1, N).
+simple_shuffle(L) ->
+ N = 1000 * length(L),
+ L2 = [{random:uniform(N), E} || E <- L],
+ {_, L3} = lists:unzip(lists:keysort(1, L2)),
+ L3.
+
+fixed_case(Bloom, Size, FalseRate) ->
+ ?assert(bloom:capacity(Bloom) > Size),
+ ?assertEqual(0, bloom:size(Bloom)),
+ RandomList = simple_shuffle(lists:seq(1,100*Size), Size),
+ [?assertEqual(false, bloom:is_element(E, Bloom)) || E <- RandomList],
+ Bloom2 =
+ lists:foldl(fun(E, Bloom0) ->
+ bloom:add_element(E, Bloom0)
+ end, Bloom, RandomList),
+ [?assertEqual(true, bloom:is_element(E, Bloom2)) || E <- RandomList],
+
+ ?assert(bloom:size(Bloom2) > ((1-FalseRate)*Size)),
+ ok.
+
+scalable_case(Bloom, Size, FalseRate) ->
+ ?assertEqual(infinity, bloom:capacity(Bloom)),
+ ?assertEqual(0, bloom:size(Bloom)),
+ RandomList = simple_shuffle(lists:seq(1,100*Size), 10*Size),
+ [?assertEqual(false, bloom:is_element(E, Bloom)) || E <- RandomList],
+ Bloom2 =
+ lists:foldl(fun(E, Bloom0) ->
+ bloom:add_element(E, Bloom0)
+ end, Bloom, RandomList),
+ [?assertEqual(true, bloom:is_element(E, Bloom2)) || E <- RandomList],
+ ?assert(bloom:size(Bloom2) > ((1-FalseRate)*Size)),
+ ok.
+
+bloom_test() ->
+ fixed_case(bloom(5000), 5000, 0.001),
+ scalable_case(sbf(1000, 0.2), 1000, 0.2),
+ ok.
+
+-endif.
View
@@ -65,5 +65,26 @@ listify(IoList) -> binary_to_list(list_to_binary(IoList)).
test() ->
J1 = listify(mochijson:encode(test_data())),
io:format("~s~n", [listify(print(J1))]).
-
-
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+basic_test() ->
+ J1 = listify(mochijson:encode(test_data())),
+ L1 =
+ "{\n"
+ " \"foo\": true,\n"
+ " \"bar\": false,\n"
+ " \"baz\": [\n"
+ " 1,\n"
+ " 2,\n"
+ " 3,\n"
+ " 4\n"
+ " ],\n"
+ " \"fiz:f\": null,\n"
+ " \"fozzer\\\"\": 5\n"
+ "}",
+ ?assertEqual(L1, listify(print(J1))),
+ ok.
+
+-endif.
View
@@ -501,3 +501,31 @@ attempt_simple_transfer(Seed, Ring, [{_, N}|Rest], TargetN, Exit, Idx, Last) ->
lists:keyreplace(N, 1, Last, {N, Idx}));
attempt_simple_transfer(_, Ring, [], _, _, _, _) ->
{ok, Ring}.
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+owners(Ring) ->
+ lists:usort([Owner || {_, Owner} <- riak_core_ring:all_owners(Ring)]).
+
+removal_test() ->
+ application:set_env(riak_core, target_n_val, 3),
+
+ Ring1 = riak_core_ring:fresh(64, n1),
+ ?assertEqual([n1], owners(Ring1)),
+
+ {_, Indices} = lists:foldl(fun({Idx, _}, {true, L}) ->
+ {false, [Idx|L]};
+ ({_, _}, {false, L}) ->
+ {true, L}
+ end, {true, []}, riak_core_ring:all_owners(Ring1)),
+ Ring2 = lists:foldl(fun(Idx, Ring) ->
+ riak_core_ring:transfer_node(Idx, n2, Ring)
+ end, Ring1, Indices),
+ ?assertEqual([n1, n2], owners(Ring2)),
+
+ Ring3 = reassign_indices(Ring2, n2),
+ ?assertEqual([n1], owners(Ring3)),
+ ok.
+
+-endif.
@@ -219,3 +219,81 @@ r2f([]) -> {queue, [], []};
r2f([_] = R) -> {queue, [], R};
r2f([X,Y]) -> {queue, [X], [Y]};
r2f([X,Y|R]) -> {queue, [X,Y], lists:reverse(R, [])}.
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+simple_case(Order) ->
+ Queue = priority_queue:new(),
+ ?assertEqual(true, priority_queue:is_queue(Queue)),
+ ?assertEqual(true, priority_queue:is_empty(Queue)),
+ ?assertEqual(0, priority_queue:len(Queue)),
+ ?assertEqual([], priority_queue:to_list(Queue)),
+ case Order of
+ forward ->
+ Queue2 = priority_queue:in(low, Queue),
+ Queue3 = priority_queue:in(mid, 500, Queue2),
+ Queue4 = priority_queue:in(high, 1000, Queue3);
+ reverse ->
+ Queue2 = priority_queue:in(high, 1000, Queue),
+ Queue3 = priority_queue:in(mid, 500, Queue2),
+ Queue4 = priority_queue:in(low, Queue3);
+ mixed ->
+ Queue2 = priority_queue:in(high, 1000, Queue),
+ Queue3 = priority_queue:in(low, Queue2),
+ Queue4 = priority_queue:in(mid, 500, Queue3)
+ end,
+ ?assertEqual(false, priority_queue:is_empty(Queue4)),
+ ?assertEqual(3, priority_queue:len(Queue4)),
+ ?assertMatch({{value, high}, _}, priority_queue:out(Queue4)),
+ {{value, high}, Queue5} = priority_queue:out(Queue4),
+ ?assertMatch({{value, mid}, _}, priority_queue:out(Queue5)),
+ {{value, mid}, Queue6} = priority_queue:out(Queue5),
+ ?assertMatch({{value, low}, _}, priority_queue:out(Queue6)),
+ {{value, low}, Queue7} = priority_queue:out(Queue6),
+ ?assertEqual(0, priority_queue:len(Queue7)),
+
+ ?assertEqual(true, priority_queue:is_queue(Queue2)),
+ ?assertEqual(true, priority_queue:is_queue(Queue3)),
+ ?assertEqual(true, priority_queue:is_queue(Queue4)),
+ ?assertEqual(false, priority_queue:is_queue([])),
+ ok.
+
+merge_case() ->
+ QueueA1 = priority_queue:new(),
+ QueueA2 = priority_queue:in(1, QueueA1),
+ QueueA3 = priority_queue:in(3, QueueA2),
+ QueueA4 = priority_queue:in(5, QueueA3),
+
+ QueueB1 = priority_queue:new(),
+ QueueB2 = priority_queue:in(2, QueueB1),
+ QueueB3 = priority_queue:in(4, QueueB2),
+ QueueB4 = priority_queue:in(6, QueueB3),
+
+ Merged1 = priority_queue:join(QueueA4, QueueB4),
+ ?assertEqual([{0,1},{0,3},{0,5},{0,2},{0,4},{0,6}],
+ priority_queue:to_list(Merged1)),
+
+ QueueC1 = priority_queue:new(),
+ QueueC2 = priority_queue:in(1, 10, QueueC1),
+ QueueC3 = priority_queue:in(3, 30, QueueC2),
+ QueueC4 = priority_queue:in(5, 50, QueueC3),
+
+ QueueD1 = priority_queue:new(),
+ QueueD2 = priority_queue:in(2, 20, QueueD1),
+ QueueD3 = priority_queue:in(4, 40, QueueD2),
+ QueueD4 = priority_queue:in(6, 60, QueueD3),
+
+ Merged2 = priority_queue:join(QueueC4, QueueD4),
+ ?assertEqual([{60,6},{50,5},{40,4},{30,3},{20,2},{10,1}],
+ priority_queue:to_list(Merged2)),
+ ok.
+
+basic_test() ->
+ simple_case(forward),
+ simple_case(reverse),
+ simple_case(mixed),
+ merge_case(),
+ ok.
+
+-endif.

0 comments on commit 926c5fa

Please sign in to comment.