Commit | Line | Data |
---|---|---|
f399d3fc | 1 | -module(hope_dictionary_SUITE). |
56b05e81 SK |
2 | |
3 | %% Callbacks | |
4 | -export( | |
5 | [ all/0 | |
6 | , groups/0 | |
5e835f3e SK |
7 | , init_per_group/2 |
8 | , end_per_group/2 | |
56b05e81 SK |
9 | ]). |
10 | ||
11 | %% Test cases | |
12 | -export( | |
39273fbe SK |
13 | [ t_set_new/1 |
14 | , t_set_existing/1 | |
870172d6 | 15 | , t_get/1 |
70cf8e86 | 16 | , t_pop/1 |
e75cf601 | 17 | , t_fold/1 |
8bbf6f4d | 18 | , t_dictionary_specs/1 |
67535be2 | 19 | , t_has_key/1 |
56b05e81 SK |
20 | ]). |
21 | ||
22 | ||
5e835f3e SK |
23 | -define(DICT_MODULE , dict_module). |
24 | -define(DICT_MODULE_KV_LIST , hope_kv_list). | |
56b05e81 SK |
25 | |
26 | ||
27 | %% ============================================================================ | |
28 | %% Common Test callbacks | |
29 | %% ============================================================================ | |
30 | ||
31 | all() -> | |
5e835f3e | 32 | [{group, ?DICT_MODULE_KV_LIST}]. |
56b05e81 SK |
33 | |
34 | groups() -> | |
35 | Tests = | |
39273fbe SK |
36 | [ t_set_new |
37 | , t_set_existing | |
870172d6 | 38 | , t_get |
70cf8e86 | 39 | , t_pop |
e75cf601 | 40 | , t_fold |
d2bddc7b SK |
41 | |
42 | % TODO: Find-out why t_dictionary_specs failes with latest proper HEAD: | |
43 | % | |
44 | % Testing hope_kv_list:to_kv_list/1 | |
45 | % Error: The typeserver encountered an error: {unbound_var,'K'}. | |
46 | % *** CT Error Notification 2015-09-26 13:46:38.684 *** | |
47 | % hope_dictionary_SUITE:t_dictionary_specs failed on line 111 | |
48 | % Reason: {badmatch,[{hope_kv_list,of_kv_list,1}]} | |
49 | % | |
50 | %, t_dictionary_specs | |
51 | ||
67535be2 | 52 | , t_has_key |
56b05e81 | 53 | ], |
8bbf6f4d | 54 | Properties = [parallel], |
5e835f3e SK |
55 | [{?DICT_MODULE_KV_LIST, Properties, Tests}]. |
56 | ||
57 | init_per_group(DictModule, Cfg) -> | |
58 | hope_kv_list:set(Cfg, ?DICT_MODULE, DictModule). | |
59 | ||
60 | end_per_group(_DictModule, _Cfg) -> | |
61 | ok. | |
56b05e81 SK |
62 | |
63 | ||
64 | %% ============================================================================= | |
65 | %% Test cases | |
66 | %% ============================================================================= | |
39273fbe | 67 | |
870172d6 SK |
68 | t_get(Cfg) -> |
69 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), | |
70 | K1 = k1, | |
71 | K2 = k2, | |
72 | V1 = v1, | |
73 | V2 = v2, | |
74 | D = DictModule:set(DictModule:empty(), K1, V1), | |
75 | {some, V1} = DictModule:get(D, K1), | |
76 | V1 = DictModule:get(D, K1, V2), | |
77 | none = DictModule:get(D, K2), | |
e0fbc1da SK |
78 | V2 = DictModule:get(D, K2, V2), |
79 | default = DictModule:get(D, K1, default, fun (X) -> X =:= foo end), | |
80 | V1 = DictModule:get(D, K1, default, fun (X) -> X =:= V1 end). | |
870172d6 | 81 | |
5e835f3e SK |
82 | t_set_new(Cfg) -> |
83 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), | |
39273fbe SK |
84 | Key = key, |
85 | ValExpected = bar, | |
5e835f3e SK |
86 | ListInitial = DictModule:empty(), |
87 | ListResulting = DictModule:set(ListInitial, Key, ValExpected), | |
88 | {some, ValResulting} = DictModule:get(ListResulting, Key), | |
39273fbe SK |
89 | ValResulting = ValExpected. |
90 | ||
5e835f3e SK |
91 | t_set_existing(Cfg) -> |
92 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), | |
39273fbe SK |
93 | Key = key, |
94 | ValInitial = foo, | |
95 | ValExpected = bar, | |
96 | ListInitial = [{donald, duck}, {Key, ValInitial}], | |
5e835f3e SK |
97 | ListResulting = DictModule:set(ListInitial, Key, ValExpected), |
98 | {some, ValResulting} = DictModule:get(ListResulting, Key), | |
39273fbe | 99 | ValResulting = ValExpected. |
70cf8e86 | 100 | |
5e835f3e SK |
101 | t_pop(Cfg) -> |
102 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), | |
70cf8e86 | 103 | KVList = [{a, 1}, {b, 2}, {c, 3}], |
5e835f3e SK |
104 | Dict1 = DictModule:of_kv_list(KVList), |
105 | {{some, 1} , Dict2} = DictModule:pop(Dict1, a), | |
106 | {none , Dict3} = DictModule:pop(Dict2, a), | |
107 | {{some, 2} , Dict4} = DictModule:pop(Dict3, b), | |
108 | {none , Dict5} = DictModule:pop(Dict4, b), | |
109 | {{some, 3} , Dict6} = DictModule:pop(Dict5, c), | |
110 | {none , Dict7} = DictModule:pop(Dict6, c), | |
111 | [] = DictModule:to_kv_list(Dict7). | |
e75cf601 SK |
112 | |
113 | t_fold(Cfg) -> | |
114 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), | |
115 | KVList = [{a, 1}, {a, 5}, {b, 3}, {c, 4}, {c, 4}], | |
116 | Dict = DictModule:of_kv_list(KVList), | |
117 | 17 = DictModule:fold(Dict, fun (_K, V, Acc) -> V + Acc end, 0). | |
8bbf6f4d PO |
118 | |
119 | t_dictionary_specs(Cfg) -> | |
120 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), | |
121 | [] = proper:check_specs(DictModule). | |
67535be2 SK |
122 | |
123 | t_has_key(Cfg) -> | |
124 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), | |
125 | D = DictModule:of_kv_list([{a, 1}, {b, 2}, {c, 3}]), | |
126 | true = DictModule:has_key(D, a), | |
127 | true = DictModule:has_key(D, b), | |
128 | true = DictModule:has_key(D, c), | |
129 | false = DictModule:has_key(D, d), | |
130 | false = DictModule:has_key(D, e), | |
131 | false = DictModule:has_key(D, f). |