Permalink
Browse files

some cstree tests

  • Loading branch information...
1 parent 562f72f commit 00987f14ecb53725621b5f8a722c1da85b77ee6a @etrepum committed Jun 20, 2012
Showing with 74 additions and 7 deletions.
  1. +1 −7 src/cstree.erl
  2. +73 −0 test/test_cstree.erl
View
@@ -8,7 +8,7 @@
TotalCount :: non_neg_integer()}).
-type(cstree(T) :: empty | cstree_node(T)).
--export([new/0, increment/2, increment/3, total/1, count/1,
+-export([new/0, increment/2, increment/3, total/1,
from_list/1, to_list/1, seek/2]).
-define(E, empty).
@@ -44,12 +44,6 @@ total({_Left, _Right, _Elem, _ElemCount, Total}) ->
total(?E) ->
0.
--spec count(cstree(_)) -> non_neg_integer().
-count({_Left, _Right, _Elem, ElemCount, _Total}) ->
- ElemCount;
-count(?E) ->
- 0.
-
-spec seek(non_neg_integer(), cstree_node(T)) -> T.
seek(N, {L, R, X, XC, _XTC}) ->
case N - total(L) of
View
@@ -0,0 +1,73 @@
+-module(test_cstree).
+-include_lib("eunit/include/eunit.hrl").
+
+to_list_test() ->
+ ?assertEqual(
+ [{a, 1}, {b, 2}],
+ lists:sort(
+ cstree:to_list(lists:foldl(fun cstree:increment/2,
+ cstree:new(),
+ [{b, 1}, {a, 1}, {b, 1}])))),
+ ok.
+
+from_list_test() ->
+ ?assertEqual(
+ [{a, 2}, {b, 1}],
+ lists:sort(cstree:to_list(cstree:from_list([{b, 1}, {a, 2}])))),
+ ?assertEqual(
+ [{a, 2}, {b, 1}],
+ lists:sort(cstree:to_list(cstree:from_list([{a, 2}, {b, 1}])))),
+ ?assertEqual(
+ [{a, 2}, {b, 1}, {c, 4}, {d, 1}, {e, 6}],
+ lists:sort(
+ cstree:to_list(
+ cstree:from_list(
+ [{a, 2}, {b, 1}, {c, 4}, {d, 1}, {e, 6}])))),
+ ok.
+
+total_test() ->
+ ?assertEqual(
+ 3,
+ cstree:total(cstree:from_list([{a, 2}, {b, 1}]))),
+ ok.
+
+inc_test() ->
+ ?assertEqual(
+ [{a, 1}, {b, 1}],
+ lists:sort(
+ cstree:to_list(
+ lists:foldl(fun cstree:increment/2,
+ cstree:new(),
+ [{a, 1}, {b, 1}])))),
+ ?assertEqual(
+ [{a, 2}, {b, 1}],
+ lists:sort(
+ cstree:to_list(
+ lists:foldl(fun cstree:increment/2,
+ cstree:new(),
+ [{a, 1}, {b, 1}, {a, 1}])))),
+ ?assertEqual(
+ [{a, 2}, {b, 1}, {c, 4}],
+ lists:sort(
+ cstree:to_list(
+ lists:foldl(fun cstree:increment/2,
+ cstree:new(),
+ [{a, 1}, {b, 1}, {a, 1}, {c, 1}, {c, 1},
+ {c, 1}, {c, 1}])))),
+ ?assertEqual(
+ [{a, 6}, {b, 5}, {c, 4}],
+ lists:sort(
+ cstree:to_list(
+ lists:foldl(fun cstree:increment/2,
+ cstree:new(),
+ [{a, 1}, {b, 1}, {a, 1}, {c, 1}, {c, 1},
+ {c, 1}, {c, 1}, {b, 1}, {b, 1}, {b, 1},
+ {b, 1}, {a, 1}, {a, 1}, {a, 1}, {a, 1}])))),
+ ok.
+
+seek_test() ->
+ S = cstree:from_list([{c, 4}, {a, 2}, {b, 1}]),
+ ?assertEqual(
+ lists:sort([c, c, c, c, a, a, b]),
+ lists:sort([cstree:seek(N, S) || N <- lists:seq(0, 6)])),
+ ok.

0 comments on commit 00987f1

Please sign in to comment.