X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=test%2Fhope_list_SUITE.erl;h=e1f8375a59ef12fc334118009381584dad261dbf;hb=347e63e68cda29d5fdcf5ffc0d551cc6d7a9bc6d;hp=69674582f2e2b07ab7faf0daef5ea5d474b585ca;hpb=ff793acf03456b6bd4c12ea330a9311f7e94cc77;p=hope.git diff --git a/test/hope_list_SUITE.erl b/test/hope_list_SUITE.erl index 6967458..e1f8375 100644 --- a/test/hope_list_SUITE.erl +++ b/test/hope_list_SUITE.erl @@ -1,6 +1,6 @@ -module(hope_list_SUITE). --include_lib("proper/include/proper.hrl"). +-include_lib("proper/include/proper_common.hrl"). %% Callbacks -export( @@ -10,17 +10,24 @@ %% Test cases -export( - [ t_unique_preserve_order/1 - , t_hope_list_specs/1 - , t_map_rev/1 - , t_map_slow/1 - , t_map/1 + [ t_auto_hope_list_specs/1 + , t_auto_map/1 + , t_auto_map_3/1 + , t_auto_map_rev/1 + , t_auto_map_slow/1 + , t_auto_unique_preserve_order/1 + , t_manual_map/1 + , t_manual_map_result/1 + , t_manual_map_rev/1 + , t_manual_map_slow/1 ]). -define(GROUP , hope_list). --define(PROPTEST(A), true = proper:quickcheck(A())). +-define(TEST(TestSpec), true = proper:quickcheck(TestSpec)). + +-define(type, proper_types). %% ============================================================================ @@ -32,47 +39,93 @@ all() -> groups() -> Tests = - [ t_unique_preserve_order - , t_hope_list_specs - , t_map_rev - , t_map_slow - , t_map + [ t_auto_hope_list_specs + , t_auto_map + , t_auto_map_3 + , t_auto_map_rev + , t_auto_map_slow + , t_auto_unique_preserve_order + , t_manual_map + , t_manual_map_result + , t_manual_map_rev + , t_manual_map_slow ], Properties = [parallel], [{?GROUP, Properties, Tests}]. - %% ============================================================================= -%% Test cases +%% Manual test cases %% ============================================================================= -t_map_rev(_Cfg) -> +t_manual_map(_Cfg) -> + F = fun (N) -> N + 1 end, + Xs = lists:seq(1, 5010), + Ys = lists:map(F, Xs), + Ys = hope_list:map(Xs, F), + [] = hope_list:map([], F). + +t_manual_map_result(_Cfg) -> + AssertPositive = + fun (I) when I > 0 -> {ok, I}; (_) -> {error, negative} end, + AllPositives = lists:seq(1, 5), + AllNegatives = lists:seq(-5, -1), + Mixed = lists:seq(-5, 5), + {ok, AllPositives} = hope_list:map_result(AllPositives, AssertPositive), + {error, negative} = hope_list:map_result(AllNegatives, AssertPositive), + {error, negative} = hope_list:map_result(Mixed, AssertPositive). + +t_manual_map_rev(_Cfg) -> F = fun (N) -> N + 1 end, [4, 3, 2] = hope_list:map_rev([1, 2, 3], F), [] = hope_list:map_rev([], F). -t_map_slow(_Cfg) -> +t_manual_map_slow(_Cfg) -> F = fun (N) -> N + 1 end, [2, 3, 4] = hope_list:map_slow([1, 2, 3], F), [] = hope_list:map_slow([], F). -t_map(_Cfg) -> - F = fun (N) -> N + 1 end, - Xs = lists:seq(1, 5010), - Ys = lists:map(F, Xs), - Ys = hope_list:map(Xs, F), - [] = hope_list:map([], F). +%% ============================================================================= +%% Generated test cases +%% ============================================================================= -t_unique_preserve_order(_Cfg) -> - ?PROPTEST(prop_unique_preserve_order). +t_auto_map_rev(_Cfg) -> + ?TEST(?FORALL({L, F}, {type_l(), type_f()}, + hope_list:map_rev(L, F) == lists:reverse(lists:map(F, L)) + )). + +t_auto_map_slow(_Cfg) -> + ?TEST(?FORALL({L, F}, {type_l(), type_f()}, + hope_list:map_slow(L, F) == lists:map(F, L) + )). + +t_auto_map(_Cfg) -> + ?TEST(?FORALL({L, F}, {type_l(), type_f()}, + hope_list:map(L, F) == lists:map(F, L) + )). + +t_auto_map_3(_Cfg) -> + ?TEST(?FORALL({L, F, N}, {type_l(), type_f(), ?type:non_neg_integer()}, + hope_list:map(L, F, N) == lists:map(F, L) + )). + +t_auto_unique_preserve_order(_Cfg) -> + ?TEST(?FORALL(L, ?type:list(), + begin + Duplicates = L -- lists:usort(L), + UniquesInOrderA = lists:reverse(lists:reverse(L) -- Duplicates), + UniquesInOrderB = hope_list:unique_preserve_order(L), + UniquesInOrderA == UniquesInOrderB + end)). + +t_auto_hope_list_specs(_Cfg) -> + [] = proper:check_specs(hope_list). -prop_unique_preserve_order() -> - ?FORALL(L, list(), - begin - Duplicates = L -- lists:usort(L), - hope_list:unique_preserve_order(L) == - lists:reverse(lists:reverse(L) -- Duplicates) - end). +%% ============================================================================ +%% Common types +%% ============================================================================ -t_hope_list_specs(_) -> - [] = proper:check_specs(hope_list). +type_l() -> + ?type:list(?type:integer()). + +type_f() -> + ?type:function([?type:integer()], ?type:term()).