1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2016-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-module(beam_jump_SUITE).
21
22-export([all/0,suite/0,groups/0,init_per_suite/1,end_per_suite/1,
23	 init_per_group/2,end_per_group/2,
24	 undefined_label/1,ambiguous_catch_try_state/1,
25         build_tuple/1]).
26
27suite() ->
28    [{ct_hooks,[ts_install_cth]}].
29
30all() ->
31    [{group,p}].
32
33groups() ->
34    [{p,[parallel],
35      [undefined_label,
36       ambiguous_catch_try_state,
37       build_tuple
38      ]}].
39
40init_per_suite(Config) ->
41    test_lib:recompile(?MODULE),
42    Config.
43
44end_per_suite(_Config) ->
45    ok.
46
47init_per_group(_GroupName, Config) ->
48    Config.
49
50end_per_group(_GroupName, Config) ->
51    Config.
52
53undefined_label(_Config) ->
54    {'EXIT',{function_clause,_}} = (catch flights(0, [], [])),
55    ok.
56
57%% Would lose a label when compiled with no_copt.
58
59flights(0, [], []) when [], 0; 0.0, [], false ->
60    clark;
61flights(_, Reproduction, introduction) when false, Reproduction ->
62    responsible.
63
64%% [ERL-209] beam_jump would share 'catch' blocks, causing an
65%% ambiguous_catch_try_state error in beam_validator.
66
67ambiguous_catch_try_state(_Config) ->
68    {{'EXIT',{{case_clause,song},_}},{'EXIT',{{case_clause,song},_}}} =
69	checks(42),
70    ok.
71
72river() -> song.
73
74checks(Wanted) ->
75    %% Must be one line to cause the unsafe optimization.
76    {catch case river() of sheet -> begin +Wanted, if "da" -> Wanted end end end, catch case river() of sheet -> begin + Wanted, if "da" -> Wanted end end end}.
77
78-record(message2, {id, p1}).
79-record(message3, {id, p1, p2}).
80
81build_tuple(_Config) ->
82    {'EXIT',{{badrecord,message3},_}} = (catch do_build_tuple(#message2{})),
83    ok.
84
85do_build_tuple(Message) ->
86    if is_record(Message, message2) ->
87	    Res = {res, rand:uniform(100)},
88	    {Message#message3.id, Res}
89    end.
90