Implement (and test) hope_list:divide/2
[hope.git] / test / hope_list_SUITE.erl
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 , t_manual_divide/1
24 ]).
25
26
27 -define(GROUP , hope_list).
28
29 -define(TEST(TestSpec), true = proper:quickcheck(TestSpec)).
30
31 -define(type, proper_types).
32
33
34 %% ============================================================================
35 %% Common Test callbacks
36 %% ============================================================================
37
38 all() ->
39 [{group, ?GROUP}].
40
41 groups() ->
42 Tests =
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
49 , t_manual_map
50 , t_manual_map_result
51 , t_manual_map_rev
52 , t_manual_map_slow
53 , t_manual_divide
54 ],
55 Properties = [parallel],
56 [{?GROUP, Properties, Tests}].
57
58 %% =============================================================================
59 %% Manual test cases
60 %% =============================================================================
61
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
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).
78
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
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
118 %% =============================================================================
119 %% Generated test cases
120 %% =============================================================================
121
122 t_auto_map_rev(_Cfg) ->
123 ?TEST(?FORALL({L, F}, {type_l(), type_f()},
124 hope_list:map_rev(L, F) == lists:reverse(lists:map(F, L))
125 )).
126
127 t_auto_map_slow(_Cfg) ->
128 ?TEST(?FORALL({L, F}, {type_l(), type_f()},
129 hope_list:map_slow(L, F) == lists:map(F, L)
130 )).
131
132 t_auto_map(_Cfg) ->
133 ?TEST(?FORALL({L, F}, {type_l(), type_f()},
134 hope_list:map(L, F) == lists:map(F, L)
135 )).
136
137 t_auto_map_3(_Cfg) ->
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 )).
141
142 t_auto_unique_preserve_order(_Cfg) ->
143 ?TEST(?FORALL(L, ?type:list(),
144 begin
145 Duplicates = L -- lists:usort(L),
146 UniquesInOrderA = lists:reverse(lists:reverse(L) -- Duplicates),
147 UniquesInOrderB = hope_list:unique_preserve_order(L),
148 UniquesInOrderA == UniquesInOrderB
149 end)).
150
151 t_auto_hope_list_specs(_Cfg) ->
152 [] = proper:check_specs(hope_list).
153
154 %% ============================================================================
155 %% Common types
156 %% ============================================================================
157
158 type_l() ->
159 ?type:list(?type:integer()).
160
161 type_f() ->
162 ?type:function([?type:integer()], ?type:term()).
This page took 0.066518 seconds and 4 git commands to generate.