File 7581-Mend-and-extend-random_code_SUITE.patch of Package erlang
From 6fbf3aff0f8e9650722813435a56f411e4d50862 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Mon, 25 Apr 2022 15:13:51 +0200
Subject: [PATCH] Mend and extend random_code_SUITE
The test case `random_code_SUITE:compile/1` that compiled randomly
generated code could never actually fail.
Extend the test case to test each generated code sample with different
compiler options and also make it possible to pass in the number
of tests to run in the environment variable `ERL_RANDOM_CODE_NUMTESTS`.
---
.../test/property_test/compile_prop.erl | 70 ++++++++++++++-----
lib/compiler/test/random_code_SUITE.erl | 26 ++++---
2 files changed, 71 insertions(+), 25 deletions(-)
diff --git a/lib/compiler/test/property_test/compile_prop.erl b/lib/compiler/test/property_test/compile_prop.erl
index 75ff38285d..bef6529569 100644
--- a/lib/compiler/test/property_test/compile_prop.erl
+++ b/lib/compiler/test/property_test/compile_prop.erl
@@ -36,24 +36,60 @@
-import(lists, [duplicate/2,foldl/3]).
--define(REPETITIONS, 1000).
-
compile() ->
- numtests(?REPETITIONS, compile_1()).
-
-compile_1() ->
Opts = [{resize,true}],
?FORALL(Abstr, proper_erlang_abstract_code:module(Opts),
- ?WHENFAIL(
- begin
- io:format("~ts\n", [[erl_pp:form(F) || F <- Abstr]]),
- compile(Abstr, [binary,report_errors])
- end,
- case compile(Abstr, [binary]) of
- {error, _Es, _Ws} -> false;
- _ -> true
- end)).
-
-compile(Abstr, Opts) ->
- compile:noenv_forms(Abstr, Opts).
+ compile(Abstr)).
+
+compile(Forms) ->
+ compile(Forms, compiler_variants()).
+
+compile(Forms, [Opts|OptsL]) ->
+ case spawn_compile(Forms, [return, binary | Opts]) of
+ {ok,_Mod,Bin,_EsWs} when is_binary(Bin) ->
+ %% Uncomment the following lines to print
+ %% the generated source code.
+ %% io:format("<S>\n~ts\n</S>\n",
+ %% [[erl_pp:form(F) || F <- Forms]]),
+
+ %% Uncomment the following line to print the
+ %% generated abstract code.
+ %% io:format("<abstr>\n~p\n</abstr>\n", [Forms]),
+ compile(Forms, OptsL);
+ Err ->
+ io:format("compile: ~p\n", [Err]),
+ io:format("with options ~p\n", [Opts]),
+ io:format("<S>\n~ts\n</S>\n",
+ [[erl_pp:form(F) || F <- Forms]]),
+ false
+ end;
+compile(_Forms, []) ->
+ true.
+
+spawn_compile(Forms, Options) ->
+ {Pid,Ref} = spawn_monitor(fun() ->
+ exit(compile:noenv_forms(Forms, Options))
+ end),
+ receive
+ {'DOWN',Ref,process,Pid,Ret} ->
+ Ret
+ after 600_000 ->
+ timeout
+ end.
+
+compiler_variants() ->
+ [
+ [ssalint,clint0,clint],
+ [r22,ssalint],
+ [no_type_opt,ssalint],
+ [no_module_opt,ssalint],
+ [no_copt,ssalint,clint0],
+ [no_copt,no_bool_opt,no_share_opt,no_bsm_opt,no_fun_opt,
+ no_ssa_opt,no_recv_opt,no_postopt,ssalint,clint0],
+ [no_bool_opt,no_share_opt,no_bsm_opt,no_fun_opt,no_ssa_opt,
+ no_recv_opt,ssalint,clint0,clint],
+ [no_copt,no_bool_opt,no_share_opt,no_bsm_opt,no_fun_opt,
+ no_ssa_opt,no_recv_opt,ssalint,clint0]
+ ].
+
-endif.
diff --git a/lib/compiler/test/random_code_SUITE.erl b/lib/compiler/test/random_code_SUITE.erl
index 04f6cbc1d2..0f68635e84 100644
--- a/lib/compiler/test/random_code_SUITE.erl
+++ b/lib/compiler/test/random_code_SUITE.erl
@@ -20,20 +20,18 @@
-module(random_code_SUITE).
--export([all/0, suite/0, groups/0,
+-export([all/0, suite/0,
init_per_suite/1, end_per_suite/1]).
-export([compile/1]).
+-define(NUMTESTS, 1000).
+
suite() ->
[{ct_hooks,[ts_install_cth]}].
all() ->
- [{group,property_tests}].
-
-groups() ->
- [{property_tests,[parallel],
- [compile]}].
+ [compile].
init_per_suite(Config0) ->
case ct_property_test:init_per_suite(Config0) of
@@ -52,6 +50,18 @@ init_per_suite(Config0) ->
end_per_suite(Config) ->
Config.
-compile(Config) ->
- true = ct_property_test:quickcheck(compile_prop:compile(), Config),
+compile(_Config) ->
+ NumTests = case os:getenv("ERL_RANDOM_CODE_NUMTESTS") of
+ false ->
+ ?NUMTESTS;
+ NumTests0 ->
+ list_to_integer(NumTests0)
+ end,
+
+ %% Conservatively assume that we can run 10 tests each second.
+ TimeTrap = {seconds,60_000 + (NumTests+99) div 100},
+ ct:timetrap(TimeTrap),
+ io:format("~p tests\n", [NumTests]),
+ true = proper:quickcheck(compile_prop:compile(),
+ [quiet,{numtests,NumTests}]),
ok.
--
2.34.1