| 1 | -module(hope_list_SUITE). |
| 2 | |
| 3 | -include_lib("proper/include/proper_common.hrl"). |
| 4 | |
| 5 | %% Callbacks |
| 6 | -export( |
| 7 | [ all/0 |
| 8 | , groups/0 |
| 9 | ]). |
| 10 | |
| 11 | %% Test cases |
| 12 | -export( |
| 13 | [ t_auto_hope_list_specs/1 |
| 14 | , t_auto_map/1 |
| 15 | , t_auto_map_3/1 |
| 16 | , t_auto_map_rev/1 |
| 17 | , t_auto_map_slow/1 |
| 18 | , t_auto_unique_preserve_order/1 |
| 19 | , t_manual_map/1 |
| 20 | , t_manual_map_result/1 |
| 21 | , t_manual_map_rev/1 |
| 22 | , t_manual_map_slow/1 |
| 23 | ]). |
| 24 | |
| 25 | |
| 26 | -define(GROUP , hope_list). |
| 27 | |
| 28 | -define(TEST(TestSpec), true = proper:quickcheck(TestSpec)). |
| 29 | |
| 30 | -define(type, proper_types). |
| 31 | |
| 32 | |
| 33 | %% ============================================================================ |
| 34 | %% Common Test callbacks |
| 35 | %% ============================================================================ |
| 36 | |
| 37 | all() -> |
| 38 | [{group, ?GROUP}]. |
| 39 | |
| 40 | groups() -> |
| 41 | Tests = |
| 42 | [ t_auto_hope_list_specs |
| 43 | , t_auto_map |
| 44 | , t_auto_map_3 |
| 45 | , t_auto_map_rev |
| 46 | , t_auto_map_slow |
| 47 | , t_auto_unique_preserve_order |
| 48 | , t_manual_map |
| 49 | , t_manual_map_result |
| 50 | , t_manual_map_rev |
| 51 | , t_manual_map_slow |
| 52 | ], |
| 53 | Properties = [parallel], |
| 54 | [{?GROUP, Properties, Tests}]. |
| 55 | |
| 56 | %% ============================================================================= |
| 57 | %% Manual test cases |
| 58 | %% ============================================================================= |
| 59 | |
| 60 | t_manual_map(_Cfg) -> |
| 61 | F = fun (N) -> N + 1 end, |
| 62 | Xs = lists:seq(1, 5010), |
| 63 | Ys = lists:map(F, Xs), |
| 64 | Ys = hope_list:map(Xs, F), |
| 65 | [] = hope_list:map([], F). |
| 66 | |
| 67 | t_manual_map_result(_Cfg) -> |
| 68 | AssertPositive = |
| 69 | fun (I) when I > 0 -> {ok, I}; (_) -> {error, negative} end, |
| 70 | AllPositives = lists:seq(1, 5), |
| 71 | AllNegatives = lists:seq(-5, -1), |
| 72 | Mixed = lists:seq(-5, 5), |
| 73 | {ok, AllPositives} = hope_list:map_result(AllPositives, AssertPositive), |
| 74 | {error, negative} = hope_list:map_result(AllNegatives, AssertPositive), |
| 75 | {error, negative} = hope_list:map_result(Mixed, AssertPositive). |
| 76 | |
| 77 | t_manual_map_rev(_Cfg) -> |
| 78 | F = fun (N) -> N + 1 end, |
| 79 | [4, 3, 2] = hope_list:map_rev([1, 2, 3], F), |
| 80 | [] = hope_list:map_rev([], F). |
| 81 | |
| 82 | t_manual_map_slow(_Cfg) -> |
| 83 | F = fun (N) -> N + 1 end, |
| 84 | [2, 3, 4] = hope_list:map_slow([1, 2, 3], F), |
| 85 | [] = hope_list:map_slow([], F). |
| 86 | |
| 87 | %% ============================================================================= |
| 88 | %% Generated test cases |
| 89 | %% ============================================================================= |
| 90 | |
| 91 | t_auto_map_rev(_Cfg) -> |
| 92 | ?TEST(?FORALL({L, F}, {type_l(), type_f()}, |
| 93 | hope_list:map_rev(L, F) == lists:reverse(lists:map(F, L)) |
| 94 | )). |
| 95 | |
| 96 | t_auto_map_slow(_Cfg) -> |
| 97 | ?TEST(?FORALL({L, F}, {type_l(), type_f()}, |
| 98 | hope_list:map_slow(L, F) == lists:map(F, L) |
| 99 | )). |
| 100 | |
| 101 | t_auto_map(_Cfg) -> |
| 102 | ?TEST(?FORALL({L, F}, {type_l(), type_f()}, |
| 103 | hope_list:map(L, F) == lists:map(F, L) |
| 104 | )). |
| 105 | |
| 106 | t_auto_map_3(_Cfg) -> |
| 107 | ?TEST(?FORALL({L, F, N}, {type_l(), type_f(), ?type:non_neg_integer()}, |
| 108 | hope_list:map(L, F, N) == lists:map(F, L) |
| 109 | )). |
| 110 | |
| 111 | t_auto_unique_preserve_order(_Cfg) -> |
| 112 | ?TEST(?FORALL(L, ?type:list(), |
| 113 | begin |
| 114 | Duplicates = L -- lists:usort(L), |
| 115 | UniquesInOrderA = lists:reverse(lists:reverse(L) -- Duplicates), |
| 116 | UniquesInOrderB = hope_list:unique_preserve_order(L), |
| 117 | UniquesInOrderA == UniquesInOrderB |
| 118 | end)). |
| 119 | |
| 120 | t_auto_hope_list_specs(_Cfg) -> |
| 121 | [] = proper:check_specs(hope_list). |
| 122 | |
| 123 | %% ============================================================================ |
| 124 | %% Common types |
| 125 | %% ============================================================================ |
| 126 | |
| 127 | type_l() -> |
| 128 | ?type:list(?type:integer()). |
| 129 | |
| 130 | type_f() -> |
| 131 | ?type:function([?type:integer()], ?type:term()). |