From a5712997ef0950add2f0a5f961403a28fa27f83c Mon Sep 17 00:00:00 2001 From: Ariel Otilibili Date: Sat, 23 Nov 2024 13:03:14 +0100 Subject: [PATCH] Moved `ec_gb_trees` tests into separate file Part of #179. Signed-off-by: Ariel Otilibili --- src/ec_gb_trees.erl | 76 -------------------------------------- test/ec_gb_trees_tests.erl | 67 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 76 deletions(-) create mode 100644 test/ec_gb_trees_tests.erl diff --git a/src/ec_gb_trees.erl b/src/ec_gb_trees.erl index 7985fab..cde3f1b 100644 --- a/src/ec_gb_trees.erl +++ b/src/ec_gb_trees.erl @@ -135,79 +135,3 @@ from_list(List) when is_list(List) -> -spec keys(gb_trees:tree(K,_V)) -> [ec_dictionary:key(K)]. keys(Data) -> gb_trees:keys(Data). - -%%%=================================================================== -%%% Tests -%%%=================================================================== - - --ifdef(TEST). --include_lib("eunit/include/eunit.hrl"). - -%% For me unit testing initially is about covering the obvious case. A -%% check to make sure that what you expect the tested functionality to -%% do, it actually does. As time goes on and people detect bugs you -%% add tests for those specific problems to the unit test suit. -%% -%% However, when getting started you can only test your basic -%% expectations. So here are the expectations I have for the add -%% functionality. -%% -%% 1) I can put arbitrary terms into the dictionary as keys -%% 2) I can put arbitrary terms into the dictionary as values -%% 3) When I put a value in the dictionary by a key, I can retrieve -%% that same value -%% 4) When I put a different value in the dictionary by key it does -%% not change other key value pairs. -%% 5) When I update a value the new value in available by the new key -%% 6) When a value does not exist a not found exception is created - -add_test() -> - Dict0 = ec_dictionary:new(ec_gb_trees), - - Key1 = foo, - Key2 = [1, 3], - Key3 = {"super"}, - Key4 = <<"fabulous">>, - Key5 = {"Sona", 2, <<"Zuper">>}, - - Value1 = Key5, - Value2 = Key4, - Value3 = Key2, - Value4 = Key3, - Value5 = Key1, - - Dict01 = ec_dictionary:add(Key1, Value1, Dict0), - Dict02 = ec_dictionary:add(Key3, Value3, - ec_dictionary:add(Key2, Value2, - Dict01)), - Dict1 = - ec_dictionary:add(Key5, Value5, - ec_dictionary:add(Key4, Value4, - Dict02)), - - ?assertMatch(Value1, ec_dictionary:get(Key1, Dict1)), - ?assertMatch(Value2, ec_dictionary:get(Key2, Dict1)), - ?assertMatch(Value3, ec_dictionary:get(Key3, Dict1)), - ?assertMatch(Value4, ec_dictionary:get(Key4, Dict1)), - ?assertMatch(Value5, ec_dictionary:get(Key5, Dict1)), - - - Dict2 = ec_dictionary:add(Key3, Value5, - ec_dictionary:add(Key2, Value4, Dict1)), - - - ?assertMatch(Value1, ec_dictionary:get(Key1, Dict2)), - ?assertMatch(Value4, ec_dictionary:get(Key2, Dict2)), - ?assertMatch(Value5, ec_dictionary:get(Key3, Dict2)), - ?assertMatch(Value4, ec_dictionary:get(Key4, Dict2)), - ?assertMatch(Value5, ec_dictionary:get(Key5, Dict2)), - - - ?assertThrow(not_found, ec_dictionary:get(should_blow_up, Dict2)), - ?assertThrow(not_found, ec_dictionary:get("This should blow up too", - Dict2)). - - - --endif. diff --git a/test/ec_gb_trees_tests.erl b/test/ec_gb_trees_tests.erl new file mode 100644 index 0000000..2c0ee12 --- /dev/null +++ b/test/ec_gb_trees_tests.erl @@ -0,0 +1,67 @@ +%%% @copyright 2024 Erlware, LLC. +-module(ec_gb_trees_tests). +-include_lib("eunit/include/eunit.hrl"). + +%% For me unit testing initially is about covering the obvious case. A +%% check to make sure that what you expect the tested functionality to +%% do, it actually does. As time goes on and people detect bugs you +%% add tests for those specific problems to the unit test suit. +%% +%% However, when getting started you can only test your basic +%% expectations. So here are the expectations I have for the add +%% functionality. +%% +%% 1) I can put arbitrary terms into the dictionary as keys +%% 2) I can put arbitrary terms into the dictionary as values +%% 3) When I put a value in the dictionary by a key, I can retrieve +%% that same value +%% 4) When I put a different value in the dictionary by key it does +%% not change other key value pairs. +%% 5) When I update a value the new value in available by the new key +%% 6) When a value does not exist a not found exception is created + +add_test() -> + Dict0 = ec_dictionary:new(ec_gb_trees), + + Key1 = foo, + Key2 = [1, 3], + Key3 = {"super"}, + Key4 = <<"fabulous">>, + Key5 = {"Sona", 2, <<"Zuper">>}, + + Value1 = Key5, + Value2 = Key4, + Value3 = Key2, + Value4 = Key3, + Value5 = Key1, + + Dict01 = ec_dictionary:add(Key1, Value1, Dict0), + Dict02 = ec_dictionary:add(Key3, Value3, + ec_dictionary:add(Key2, Value2, + Dict01)), + Dict1 = + ec_dictionary:add(Key5, Value5, + ec_dictionary:add(Key4, Value4, + Dict02)), + + ?assertMatch(Value1, ec_dictionary:get(Key1, Dict1)), + ?assertMatch(Value2, ec_dictionary:get(Key2, Dict1)), + ?assertMatch(Value3, ec_dictionary:get(Key3, Dict1)), + ?assertMatch(Value4, ec_dictionary:get(Key4, Dict1)), + ?assertMatch(Value5, ec_dictionary:get(Key5, Dict1)), + + + Dict2 = ec_dictionary:add(Key3, Value5, + ec_dictionary:add(Key2, Value4, Dict1)), + + + ?assertMatch(Value1, ec_dictionary:get(Key1, Dict2)), + ?assertMatch(Value4, ec_dictionary:get(Key2, Dict2)), + ?assertMatch(Value5, ec_dictionary:get(Key3, Dict2)), + ?assertMatch(Value4, ec_dictionary:get(Key4, Dict2)), + ?assertMatch(Value5, ec_dictionary:get(Key5, Dict2)), + + + ?assertThrow(not_found, ec_dictionary:get(should_blow_up, Dict2)), + ?assertThrow(not_found, ec_dictionary:get("This should blow up too", + Dict2)).