1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2015-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19
20%% Test specific code snippets that has crashed the compiler in the past.
21-module(regressions_SUITE).
22-include_lib("common_test/include/ct.hrl").
23
24-export([all/0,groups/0,init_per_testcase/2,end_per_testcase/2,
25         init_per_group/2,end_per_group/2,
26	 init_per_suite/1,end_per_suite/1,
27         suite/0]).
28-export([maps/1]).
29
30groups() ->
31    [{p,test_lib:parallel(),
32      [maps]}].
33
34init_per_suite(Config) ->
35    test_lib:recompile(?MODULE),
36    Config.
37
38end_per_suite(_Config) ->
39    ok.
40
41init_per_group(_GroupName, Config) ->
42    Config.
43
44end_per_group(_GroupName, Config) ->
45    Config.
46
47init_per_testcase(_Case, Config) ->
48    Config.
49
50end_per_testcase(_Case, _Config) ->
51    ok.
52
53suite() ->
54    [{ct_hooks,[ts_install_cth]},
55     {timetrap,{minutes,2}}].
56
57all() ->
58    [{group,p}].
59
60%%% test cases
61
62maps(Config) when is_list(Config) ->
63    Ts = [{beam_bool_get_elements,
64           <<"century(#{ron := operator}, _century) ->
65                  if 0.0; _century, _century, _century -> _century end.
66           ">>},
67          {empty_map_clauses,
68           <<"politics(#{}, researchers) -> concerned;
69              politics(#{[] := _}, workers) -> dot;
70              politics(#{[] := ct}, counsel) -> calls.
71             ">>},
72          {empty_map_clauses_variable,
73           <<"georgia(#{a := effectively}, ratio, is, eventually) -> teens;
74              georgia(#{a := government}, knowledge, poker, partly) -> signed;
75              georgia(#{}, recording, bring, vital) -> divided;
76              georgia(#{0 := 0}, articles, brought, #{true := true, a := There}) -> There.
77             ">>}],
78    ok = run(Config, Ts),
79    ok.
80
81%% aux
82
83run(Config, Tests) ->
84    F = fun({N,P}) ->
85                io:format("Compiling test for: ~w~n", [N]),
86                case catch run_test(Config, P) of
87                    {'EXIT', Reason} ->
88                        io:format("~nTest ~p failed.~nReason: ~p~n",
89				  [N, Reason]),
90                        fail();
91                    _ -> ok
92                end
93        end,
94    lists:foreach(F, Tests).
95
96
97run_test(Conf, Test0) ->
98    Module = "regressions_"++test_lib:uniq(),
99    Filename = Module ++ ".erl",
100    DataDir = proplists:get_value(priv_dir, Conf),
101    Test = ["-module(", Module, "). ", Test0],
102    File = filename:join(DataDir, Filename),
103    Def = [binary,export_all,return],
104    Opts = [ Opt ++ Def ||
105             Opt <- [ [no_postopt],
106                      [no_copt],
107                      [no_postopt,no_copt],
108                      [inline],
109                      [inline,no_postopt],
110                      []
111                    ]],
112    ok = file:write_file(File, Test),
113    lists:foreach(fun(Opt) ->
114                          io:format("  - compiling with ~p~n", [Opt]),
115                          {ok,_M,_Bin,_} = compile:file(File,Opt)
116                  end, Opts),
117    file:delete(File),
118    ok.
119
120fail() ->
121    ct:fail(failed).
122