Commit | Line | Data |
---|---|---|
8dad2faf SK |
1 | -module(hope_list_SUITE). |
2 | ||
fd66fd0c | 3 | -include_lib("proper/include/proper_common.hrl"). |
f4780d18 | 4 | |
8dad2faf SK |
5 | %% Callbacks |
6 | -export( | |
7 | [ all/0 | |
8 | , groups/0 | |
9 | ]). | |
10 | ||
11 | %% Test cases | |
12 | -export( | |
2fa2f026 SK |
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 | |
d302b4a3 | 19 | , t_manual_map/1 |
2fa2f026 | 20 | , t_manual_map_result/1 |
d302b4a3 SK |
21 | , t_manual_map_rev/1 |
22 | , t_manual_map_slow/1 | |
fcfc097a | 23 | , t_manual_divide/1 |
8dad2faf SK |
24 | ]). |
25 | ||
26 | ||
27 | -define(GROUP , hope_list). | |
28 | ||
dbe73b30 | 29 | -define(TEST(TestSpec), true = proper:quickcheck(TestSpec)). |
f4780d18 | 30 | |
ea7f00f1 SK |
31 | -define(type, proper_types). |
32 | ||
8dad2faf SK |
33 | |
34 | %% ============================================================================ | |
35 | %% Common Test callbacks | |
36 | %% ============================================================================ | |
37 | ||
38 | all() -> | |
39 | [{group, ?GROUP}]. | |
40 | ||
41 | groups() -> | |
42 | Tests = | |
2fa2f026 SK |
43 | [ t_auto_hope_list_specs |
44 | , t_auto_map | |
45 | , t_auto_map_3 | |
46 | , t_auto_map_rev | |
47 | , t_auto_map_slow | |
48 | , t_auto_unique_preserve_order | |
d302b4a3 | 49 | , t_manual_map |
2fa2f026 | 50 | , t_manual_map_result |
d302b4a3 SK |
51 | , t_manual_map_rev |
52 | , t_manual_map_slow | |
fcfc097a | 53 | , t_manual_divide |
8dad2faf | 54 | ], |
8bbf6f4d | 55 | Properties = [parallel], |
8dad2faf SK |
56 | [{?GROUP, Properties, Tests}]. |
57 | ||
2fa2f026 SK |
58 | %% ============================================================================= |
59 | %% Manual test cases | |
60 | %% ============================================================================= | |
61 | ||
d302b4a3 SK |
62 | t_manual_map(_Cfg) -> |
63 | F = fun (N) -> N + 1 end, | |
64 | Xs = lists:seq(1, 5010), | |
65 | Ys = lists:map(F, Xs), | |
66 | Ys = hope_list:map(Xs, F), | |
67 | [] = hope_list:map([], F). | |
68 | ||
2fa2f026 SK |
69 | t_manual_map_result(_Cfg) -> |
70 | AssertPositive = | |
71 | fun (I) when I > 0 -> {ok, I}; (_) -> {error, negative} end, | |
72 | AllPositives = lists:seq(1, 5), | |
73 | AllNegatives = lists:seq(-5, -1), | |
74 | Mixed = lists:seq(-5, 5), | |
75 | {ok, AllPositives} = hope_list:map_result(AllPositives, AssertPositive), | |
76 | {error, negative} = hope_list:map_result(AllNegatives, AssertPositive), | |
77 | {error, negative} = hope_list:map_result(Mixed, AssertPositive). | |
8dad2faf | 78 | |
d302b4a3 SK |
79 | t_manual_map_rev(_Cfg) -> |
80 | F = fun (N) -> N + 1 end, | |
81 | [4, 3, 2] = hope_list:map_rev([1, 2, 3], F), | |
82 | [] = hope_list:map_rev([], F). | |
83 | ||
84 | t_manual_map_slow(_Cfg) -> | |
85 | F = fun (N) -> N + 1 end, | |
86 | [2, 3, 4] = hope_list:map_slow([1, 2, 3], F), | |
87 | [] = hope_list:map_slow([], F). | |
88 | ||
fcfc097a SK |
89 | t_manual_divide(_Cfg) -> |
90 | try | |
91 | hope_list:divide([a, b, c], -1) | |
92 | catch | |
93 | error:hope_list__divide__size_must_be_a_positive_integer -> ok | |
94 | end, | |
95 | try | |
96 | hope_list:divide([a, b, c], 0) | |
97 | catch | |
98 | error:hope_list__divide__size_must_be_a_positive_integer -> ok | |
99 | end, | |
100 | [[c], [b], [a]] = hope_list:divide([a, b, c], 1), | |
101 | [[c], [b, a]] = hope_list:divide([a, b, c], 2), | |
102 | [[c, b, a]] = hope_list:divide([a, b, c], 3), | |
103 | [[c, b, a]] = hope_list:divide([a, b, c], 4), | |
104 | [[c, b, a]] = hope_list:divide([a, b, c], 5), | |
105 | try | |
106 | hope_list:divide([], 0) | |
107 | catch | |
108 | error:hope_list__divide__size_must_be_a_positive_integer -> ok | |
109 | end, | |
110 | try | |
111 | hope_list:divide([], -1) | |
112 | catch | |
113 | error:hope_list__divide__size_must_be_a_positive_integer -> ok | |
114 | end, | |
115 | [[f, e], [d, c], [b, a]] = hope_list:divide([a, b, c, d, e, f], 2), | |
116 | [[f, e, d], [c, b, a]] = hope_list:divide([a, b, c, d, e, f], 3). | |
117 | ||
8dad2faf | 118 | %% ============================================================================= |
2fa2f026 | 119 | %% Generated test cases |
8dad2faf SK |
120 | %% ============================================================================= |
121 | ||
2fa2f026 | 122 | t_auto_map_rev(_Cfg) -> |
dbe73b30 SK |
123 | ?TEST(?FORALL({L, F}, {type_l(), type_f()}, |
124 | hope_list:map_rev(L, F) == lists:reverse(lists:map(F, L)) | |
125 | )). | |
c66ddf80 | 126 | |
2fa2f026 | 127 | t_auto_map_slow(_Cfg) -> |
dbe73b30 SK |
128 | ?TEST(?FORALL({L, F}, {type_l(), type_f()}, |
129 | hope_list:map_slow(L, F) == lists:map(F, L) | |
130 | )). | |
2a81fbac | 131 | |
2fa2f026 | 132 | t_auto_map(_Cfg) -> |
dbe73b30 SK |
133 | ?TEST(?FORALL({L, F}, {type_l(), type_f()}, |
134 | hope_list:map(L, F) == lists:map(F, L) | |
135 | )). | |
ff793acf | 136 | |
2fa2f026 | 137 | t_auto_map_3(_Cfg) -> |
dbe73b30 SK |
138 | ?TEST(?FORALL({L, F, N}, {type_l(), type_f(), ?type:non_neg_integer()}, |
139 | hope_list:map(L, F, N) == lists:map(F, L) | |
140 | )). | |
fad0cddc | 141 | |
2fa2f026 | 142 | t_auto_unique_preserve_order(_Cfg) -> |
dbe73b30 SK |
143 | ?TEST(?FORALL(L, ?type:list(), |
144 | begin | |
145 | Duplicates = L -- lists:usort(L), | |
347e63e6 SK |
146 | UniquesInOrderA = lists:reverse(lists:reverse(L) -- Duplicates), |
147 | UniquesInOrderB = hope_list:unique_preserve_order(L), | |
148 | UniquesInOrderA == UniquesInOrderB | |
dbe73b30 | 149 | end)). |
f4780d18 | 150 | |
2fa2f026 | 151 | t_auto_hope_list_specs(_Cfg) -> |
8bbf6f4d | 152 | [] = proper:check_specs(hope_list). |
54ab0c82 | 153 | |
cf0f905c | 154 | %% ============================================================================ |
dbe73b30 | 155 | %% Common types |
cf0f905c SK |
156 | %% ============================================================================ |
157 | ||
cf0f905c SK |
158 | type_l() -> |
159 | ?type:list(?type:integer()). | |
160 | ||
161 | type_f() -> | |
162 | ?type:function([?type:integer()], ?type:term()). |