Skip to content

Commit 4ffd5dd

Browse files
authored
Merge pull request #189 from ariel-anieli/ec-gb-trees-tests
Moved `ec_gb_trees` tests into separate file
2 parents 2011988 + a571299 commit 4ffd5dd

File tree

2 files changed

+67
-76
lines changed

2 files changed

+67
-76
lines changed

src/ec_gb_trees.erl

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -135,79 +135,3 @@ from_list(List) when is_list(List) ->
135135
-spec keys(gb_trees:tree(K,_V)) -> [ec_dictionary:key(K)].
136136
keys(Data) ->
137137
gb_trees:keys(Data).
138-
139-
%%%===================================================================
140-
%%% Tests
141-
%%%===================================================================
142-
143-
144-
-ifdef(TEST).
145-
-include_lib("eunit/include/eunit.hrl").
146-
147-
%% For me unit testing initially is about covering the obvious case. A
148-
%% check to make sure that what you expect the tested functionality to
149-
%% do, it actually does. As time goes on and people detect bugs you
150-
%% add tests for those specific problems to the unit test suit.
151-
%%
152-
%% However, when getting started you can only test your basic
153-
%% expectations. So here are the expectations I have for the add
154-
%% functionality.
155-
%%
156-
%% 1) I can put arbitrary terms into the dictionary as keys
157-
%% 2) I can put arbitrary terms into the dictionary as values
158-
%% 3) When I put a value in the dictionary by a key, I can retrieve
159-
%% that same value
160-
%% 4) When I put a different value in the dictionary by key it does
161-
%% not change other key value pairs.
162-
%% 5) When I update a value the new value in available by the new key
163-
%% 6) When a value does not exist a not found exception is created
164-
165-
add_test() ->
166-
Dict0 = ec_dictionary:new(ec_gb_trees),
167-
168-
Key1 = foo,
169-
Key2 = [1, 3],
170-
Key3 = {"super"},
171-
Key4 = <<"fabulous">>,
172-
Key5 = {"Sona", 2, <<"Zuper">>},
173-
174-
Value1 = Key5,
175-
Value2 = Key4,
176-
Value3 = Key2,
177-
Value4 = Key3,
178-
Value5 = Key1,
179-
180-
Dict01 = ec_dictionary:add(Key1, Value1, Dict0),
181-
Dict02 = ec_dictionary:add(Key3, Value3,
182-
ec_dictionary:add(Key2, Value2,
183-
Dict01)),
184-
Dict1 =
185-
ec_dictionary:add(Key5, Value5,
186-
ec_dictionary:add(Key4, Value4,
187-
Dict02)),
188-
189-
?assertMatch(Value1, ec_dictionary:get(Key1, Dict1)),
190-
?assertMatch(Value2, ec_dictionary:get(Key2, Dict1)),
191-
?assertMatch(Value3, ec_dictionary:get(Key3, Dict1)),
192-
?assertMatch(Value4, ec_dictionary:get(Key4, Dict1)),
193-
?assertMatch(Value5, ec_dictionary:get(Key5, Dict1)),
194-
195-
196-
Dict2 = ec_dictionary:add(Key3, Value5,
197-
ec_dictionary:add(Key2, Value4, Dict1)),
198-
199-
200-
?assertMatch(Value1, ec_dictionary:get(Key1, Dict2)),
201-
?assertMatch(Value4, ec_dictionary:get(Key2, Dict2)),
202-
?assertMatch(Value5, ec_dictionary:get(Key3, Dict2)),
203-
?assertMatch(Value4, ec_dictionary:get(Key4, Dict2)),
204-
?assertMatch(Value5, ec_dictionary:get(Key5, Dict2)),
205-
206-
207-
?assertThrow(not_found, ec_dictionary:get(should_blow_up, Dict2)),
208-
?assertThrow(not_found, ec_dictionary:get("This should blow up too",
209-
Dict2)).
210-
211-
212-
213-
-endif.

test/ec_gb_trees_tests.erl

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
%%% @copyright 2024 Erlware, LLC.
2+
-module(ec_gb_trees_tests).
3+
-include_lib("eunit/include/eunit.hrl").
4+
5+
%% For me unit testing initially is about covering the obvious case. A
6+
%% check to make sure that what you expect the tested functionality to
7+
%% do, it actually does. As time goes on and people detect bugs you
8+
%% add tests for those specific problems to the unit test suit.
9+
%%
10+
%% However, when getting started you can only test your basic
11+
%% expectations. So here are the expectations I have for the add
12+
%% functionality.
13+
%%
14+
%% 1) I can put arbitrary terms into the dictionary as keys
15+
%% 2) I can put arbitrary terms into the dictionary as values
16+
%% 3) When I put a value in the dictionary by a key, I can retrieve
17+
%% that same value
18+
%% 4) When I put a different value in the dictionary by key it does
19+
%% not change other key value pairs.
20+
%% 5) When I update a value the new value in available by the new key
21+
%% 6) When a value does not exist a not found exception is created
22+
23+
add_test() ->
24+
Dict0 = ec_dictionary:new(ec_gb_trees),
25+
26+
Key1 = foo,
27+
Key2 = [1, 3],
28+
Key3 = {"super"},
29+
Key4 = <<"fabulous">>,
30+
Key5 = {"Sona", 2, <<"Zuper">>},
31+
32+
Value1 = Key5,
33+
Value2 = Key4,
34+
Value3 = Key2,
35+
Value4 = Key3,
36+
Value5 = Key1,
37+
38+
Dict01 = ec_dictionary:add(Key1, Value1, Dict0),
39+
Dict02 = ec_dictionary:add(Key3, Value3,
40+
ec_dictionary:add(Key2, Value2,
41+
Dict01)),
42+
Dict1 =
43+
ec_dictionary:add(Key5, Value5,
44+
ec_dictionary:add(Key4, Value4,
45+
Dict02)),
46+
47+
?assertMatch(Value1, ec_dictionary:get(Key1, Dict1)),
48+
?assertMatch(Value2, ec_dictionary:get(Key2, Dict1)),
49+
?assertMatch(Value3, ec_dictionary:get(Key3, Dict1)),
50+
?assertMatch(Value4, ec_dictionary:get(Key4, Dict1)),
51+
?assertMatch(Value5, ec_dictionary:get(Key5, Dict1)),
52+
53+
54+
Dict2 = ec_dictionary:add(Key3, Value5,
55+
ec_dictionary:add(Key2, Value4, Dict1)),
56+
57+
58+
?assertMatch(Value1, ec_dictionary:get(Key1, Dict2)),
59+
?assertMatch(Value4, ec_dictionary:get(Key2, Dict2)),
60+
?assertMatch(Value5, ec_dictionary:get(Key3, Dict2)),
61+
?assertMatch(Value4, ec_dictionary:get(Key4, Dict2)),
62+
?assertMatch(Value5, ec_dictionary:get(Key5, Dict2)),
63+
64+
65+
?assertThrow(not_found, ec_dictionary:get(should_blow_up, Dict2)),
66+
?assertThrow(not_found, ec_dictionary:get("This should blow up too",
67+
Dict2)).

0 commit comments

Comments
 (0)