f6a3e387539ed06f5787a64c9cc7613191f23765
[hope.git] / test / hope_dictionary_SUITE.erl
1 -module(hope_dictionary_SUITE).
2
3 %% Callbacks
4 -export(
5 [ all/0
6 , groups/0
7 , init_per_group/2
8 , end_per_group/2
9 ]).
10
11 %% Test cases
12 -export(
13 [ t_set_new/1
14 , t_set_existing/1
15 , t_pop/1
16 , t_fold/1
17 ]).
18
19
20 -define(DICT_MODULE , dict_module).
21 -define(DICT_MODULE_KV_LIST , hope_kv_list).
22
23
24 %% ============================================================================
25 %% Common Test callbacks
26 %% ============================================================================
27
28 all() ->
29 [{group, ?DICT_MODULE_KV_LIST}].
30
31 groups() ->
32 Tests =
33 [ t_set_new
34 , t_set_existing
35 , t_pop
36 , t_fold
37 ],
38 Properties = [],
39 [{?DICT_MODULE_KV_LIST, Properties, Tests}].
40
41 init_per_group(DictModule, Cfg) ->
42 hope_kv_list:set(Cfg, ?DICT_MODULE, DictModule).
43
44 end_per_group(_DictModule, _Cfg) ->
45 ok.
46
47
48 %% =============================================================================
49 %% Test cases
50 %% =============================================================================
51
52 t_set_new(Cfg) ->
53 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
54 Key = key,
55 ValExpected = bar,
56 ListInitial = DictModule:empty(),
57 ListResulting = DictModule:set(ListInitial, Key, ValExpected),
58 {some, ValResulting} = DictModule:get(ListResulting, Key),
59 ValResulting = ValExpected.
60
61 t_set_existing(Cfg) ->
62 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
63 Key = key,
64 ValInitial = foo,
65 ValExpected = bar,
66 ListInitial = [{donald, duck}, {Key, ValInitial}],
67 ListResulting = DictModule:set(ListInitial, Key, ValExpected),
68 {some, ValResulting} = DictModule:get(ListResulting, Key),
69 ValResulting = ValExpected.
70
71 t_pop(Cfg) ->
72 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
73 KVList = [{a, 1}, {b, 2}, {c, 3}],
74 Dict1 = DictModule:of_kv_list(KVList),
75 {{some, 1} , Dict2} = DictModule:pop(Dict1, a),
76 {none , Dict3} = DictModule:pop(Dict2, a),
77 {{some, 2} , Dict4} = DictModule:pop(Dict3, b),
78 {none , Dict5} = DictModule:pop(Dict4, b),
79 {{some, 3} , Dict6} = DictModule:pop(Dict5, c),
80 {none , Dict7} = DictModule:pop(Dict6, c),
81 [] = DictModule:to_kv_list(Dict7).
82
83 t_fold(Cfg) ->
84 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
85 KVList = [{a, 1}, {a, 5}, {b, 3}, {c, 4}, {c, 4}],
86 Dict = DictModule:of_kv_list(KVList),
87 17 = DictModule:fold(Dict, fun (_K, V, Acc) -> V + Acc end, 0).
This page took 0.046654 seconds and 3 git commands to generate.