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