| 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_get/1 |
| 16 | , t_pop/1 |
| 17 | , t_fold/1 |
| 18 | , t_dictionary_specs/1 |
| 19 | ]). |
| 20 | |
| 21 | |
| 22 | -define(DICT_MODULE , dict_module). |
| 23 | -define(DICT_MODULE_KV_LIST , hope_kv_list). |
| 24 | |
| 25 | |
| 26 | %% ============================================================================ |
| 27 | %% Common Test callbacks |
| 28 | %% ============================================================================ |
| 29 | |
| 30 | all() -> |
| 31 | [{group, ?DICT_MODULE_KV_LIST}]. |
| 32 | |
| 33 | groups() -> |
| 34 | Tests = |
| 35 | [ t_set_new |
| 36 | , t_set_existing |
| 37 | , t_get |
| 38 | , t_pop |
| 39 | , t_fold |
| 40 | , t_dictionary_specs |
| 41 | ], |
| 42 | Properties = [parallel], |
| 43 | [{?DICT_MODULE_KV_LIST, Properties, Tests}]. |
| 44 | |
| 45 | init_per_group(DictModule, Cfg) -> |
| 46 | hope_kv_list:set(Cfg, ?DICT_MODULE, DictModule). |
| 47 | |
| 48 | end_per_group(_DictModule, _Cfg) -> |
| 49 | ok. |
| 50 | |
| 51 | |
| 52 | %% ============================================================================= |
| 53 | %% Test cases |
| 54 | %% ============================================================================= |
| 55 | |
| 56 | t_get(Cfg) -> |
| 57 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), |
| 58 | K1 = k1, |
| 59 | K2 = k2, |
| 60 | V1 = v1, |
| 61 | V2 = v2, |
| 62 | D = DictModule:set(DictModule:empty(), K1, V1), |
| 63 | {some, V1} = DictModule:get(D, K1), |
| 64 | V1 = DictModule:get(D, K1, V2), |
| 65 | none = DictModule:get(D, K2), |
| 66 | V2 = DictModule:get(D, K2, V2), |
| 67 | default = DictModule:get(D, K1, default, fun (X) -> X =:= foo end), |
| 68 | V1 = DictModule:get(D, K1, default, fun (X) -> X =:= V1 end). |
| 69 | |
| 70 | t_set_new(Cfg) -> |
| 71 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), |
| 72 | Key = key, |
| 73 | ValExpected = bar, |
| 74 | ListInitial = DictModule:empty(), |
| 75 | ListResulting = DictModule:set(ListInitial, Key, ValExpected), |
| 76 | {some, ValResulting} = DictModule:get(ListResulting, Key), |
| 77 | ValResulting = ValExpected. |
| 78 | |
| 79 | t_set_existing(Cfg) -> |
| 80 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), |
| 81 | Key = key, |
| 82 | ValInitial = foo, |
| 83 | ValExpected = bar, |
| 84 | ListInitial = [{donald, duck}, {Key, ValInitial}], |
| 85 | ListResulting = DictModule:set(ListInitial, Key, ValExpected), |
| 86 | {some, ValResulting} = DictModule:get(ListResulting, Key), |
| 87 | ValResulting = ValExpected. |
| 88 | |
| 89 | t_pop(Cfg) -> |
| 90 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), |
| 91 | KVList = [{a, 1}, {b, 2}, {c, 3}], |
| 92 | Dict1 = DictModule:of_kv_list(KVList), |
| 93 | {{some, 1} , Dict2} = DictModule:pop(Dict1, a), |
| 94 | {none , Dict3} = DictModule:pop(Dict2, a), |
| 95 | {{some, 2} , Dict4} = DictModule:pop(Dict3, b), |
| 96 | {none , Dict5} = DictModule:pop(Dict4, b), |
| 97 | {{some, 3} , Dict6} = DictModule:pop(Dict5, c), |
| 98 | {none , Dict7} = DictModule:pop(Dict6, c), |
| 99 | [] = DictModule:to_kv_list(Dict7). |
| 100 | |
| 101 | t_fold(Cfg) -> |
| 102 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), |
| 103 | KVList = [{a, 1}, {a, 5}, {b, 3}, {c, 4}, {c, 4}], |
| 104 | Dict = DictModule:of_kv_list(KVList), |
| 105 | 17 = DictModule:fold(Dict, fun (_K, V, Acc) -> V + Acc end, 0). |
| 106 | |
| 107 | t_dictionary_specs(Cfg) -> |
| 108 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), |
| 109 | [] = proper:check_specs(DictModule). |