aboutsummaryrefslogtreecommitdiffstats
path: root/lib/debugger/test/int_eval_SUITE.erl
blob: 2a1ee5a9112b8300a07eb0b3a0425f710ef1cded (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1999-2010. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%

%%
-module(int_eval_SUITE).

%% Purpose: Deeper test of the evaluator.

-export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2,init_per_testcase/2, end_per_testcase/2,
	 bifs_outside_erlang/1, spawning/1, applying/1,
	 catch_and_throw/1, external_call/1, test_module_info/1,
	 apply_interpreted_fun/1, apply_uninterpreted_fun/1,
	 interpreted_exit/1, otp_8310/1]).

%% Helpers.
-export([applier/3]).

-define(IM, my_int_eval_module).

-include_lib("test_server/include/test_server.hrl").

suite() -> [{suite_callbacks,[ts_install_scb]}].

all() -> 
[bifs_outside_erlang, spawning, applying,
 catch_and_throw, external_call, test_module_info,
 apply_interpreted_fun, apply_uninterpreted_fun,
 interpreted_exit, otp_8310].

groups() -> 
    [].

init_per_group(_GroupName, Config) ->
	Config.

end_per_group(_GroupName, Config) ->
	Config.


init_per_testcase(_Case, Config) ->
    ?line DataDir = ?config(data_dir, Config),
    ?line {module,?IM} = int:i(filename:join(DataDir, ?IM)),
    ?line ok = io:format("Interpreted modules: ~p",[int:interpreted()]),
    {ok, Dog} = timer:apply_after(timer:minutes(1),
				  erlang, exit, [self(), kill]),
    [{watchdog,Dog}|Config].

end_per_testcase(_Case, Config) ->
    ok = io:format("Interpreted modules: ~p", [int:interpreted()]),
    Dog = ?config(watchdog, Config),
    timer:cancel(Dog),
    ok.

bifs_outside_erlang(doc) ->
    "Test that BIFs outside the erlang module are correctly evaluated.";
bifs_outside_erlang(suite) ->
    [];
bifs_outside_erlang(Config) when is_list(Config) ->
    Fun = fun() ->
		  Id = ?IM:ets_new(),
		  Self = self(),
		  ok = io:format("Self: ~p", [Self]),
		  Info = ets:info(Id),
		  Self = proplists:get_value(owner, Info),
		  ?IM:ets_delete(Id),
		  ok
	  end,
    ?line ok = spawn_eval(Fun),
    ok.

spawning(doc) ->
    "Try evalutate spawn_link/3.";
spawning(suite) ->
    [];
spawning(Config) when is_list(Config) ->
    ?line ok = spawn_eval(fun() -> ?IM:spawn_test() end).

applying(doc) ->
    "Try various sorts of applies.";
applying(suite) ->
    [];
applying(Config) when is_list(Config) ->
    Fun = fun({number,X}, {number,Y}) -> X+Y end,
    ?line ok = spawn_eval(fun() -> ?IM:apply_test(Fun) end).

catch_and_throw(doc) ->
    "Test catch and throw/1.";
catch_and_throw(suite) ->
    [];
catch_and_throw(Config) when is_list(Config) ->
    {a,ball} = spawn_eval(fun() -> ok = ?IM:catch_a_ball(),
				   catch ?IM:throw_a_ball() end),

    %% Throw and catch without any extra outer catch.

    ?line process_flag(trap_exit, true),
    ?line Pid1 = spawn_link(fun() -> exit(?IM:catch_a_ball()) end),
    receive
	{'EXIT',Pid1,ok} -> ok;
	{'EXIT',Pid1,Bad1} -> ?line ?t:fail({bad_message,Bad1})
    after 5000 ->
	    ?line ?t:fail(timeout)
    end,


    %% Throw without catch.

    ?line Pid2 = spawn_link(fun() -> ?IM:throw_a_ball() end),
    receive
	{'EXIT',Pid2,{{nocatch,{a,ball}},[_|_]}} -> ok;
	{'EXIT',Pid2,Bad2} -> ?line ?t:fail({bad_message,Bad2})
    after 5000 ->
	    ?line ?t:fail(timeout)
    end,

    ?line ok = ?IM:more_catch(fun(_) -> ?IM:exit_me() end),
    ?line ok = ?IM:more_catch(fun(_) -> exit({unint, exit}) end),
    ?line {a, ball} = ?IM:more_catch(fun(_) -> ?IM:throw_a_ball() end),
    ?line {b, ball} = ?IM:more_catch(fun(_) -> throw({b,ball}) end),

    ExitInt = {'EXIT',{int,exit}},
    ExitU   = {'EXIT',{unint,exit}},

    ?line ExitInt = (catch ?IM:more_nocatch(fun(_) -> ?IM:exit_me() end)),
    ?line ExitU   = (catch ?IM:more_nocatch(fun(_) -> exit({unint, exit}) end)),
    ?line {a, ball} = (catch {error, ?IM:more_nocatch(fun(_) -> ?IM:throw_a_ball() end)}),
    ?line {b, ball} = (catch {error, ?IM:more_nocatch(fun(_) -> throw({b,ball}) end)}),
    ok.

external_call(doc) ->
    "Test external calls.";
external_call(suite) ->
    [];
external_call(Config) when is_list(Config) ->
    ?line ok = spawn_eval(fun() -> ?IM:external_call_test({some,stupid,data}) end).

test_module_info(doc) ->
    "Test the module_info/0,1 functions.";
test_module_info(suite) ->
    [];
test_module_info(Config) when is_list(Config) ->
    ?line ModInfo = ?IM:module_info(),
    ?line {value,{exports,Exp}} = lists:keysearch(exports, 1, ModInfo),
    ?line {value,{attributes,Attr}} = lists:keysearch(attributes, 1, ModInfo),
    ?line Exp = ?IM:module_info(exports),
    ?line Attr = ?IM:module_info(attributes),
    ?line {value,{stupid_attribute,[{a,b}]}} =
	lists:keysearch(stupid_attribute, 1, Attr),

    %% Check exports using a list comprehension in the module itself.

    ?line ok = ?IM:check_exports(Exp),

    %% Call module_info/0,1 from the module itself.

    ?line ok = ?IM:check_module_info(ModInfo, Exp),

    ok.

apply_interpreted_fun(doc) ->
    "Apply a fun defined in interpreted code.";
apply_interpreted_fun(suite) -> [];
apply_interpreted_fun(Config) when is_list(Config) ->

    %% Called from uninterpreted code
    ?line F1 = spawn_eval(fun() -> ?IM:give_me_a_fun_0() end),
    ?line perfectly_alright = spawn_eval(fun() -> F1() end),
    ?line ATerm = {a,term},
    ?line F2 = spawn_eval(fun() -> ?IM:give_me_a_fun_0(ATerm) end),
    ?line {ok,ATerm} = spawn_eval(fun() -> F2() end),

    %% Called from uninterpreted code, badarity
    ?line {'EXIT',{{badarity,{F1,[snape]}},[{?MODULE,_,_}|_]}} =
	spawn_eval(fun() -> F1(snape) end),

    %% Called from uninterpreted code, error in fun
    ?line F3 = spawn_eval(fun() -> ?IM:give_me_a_bad_fun() end),
    ?line {'EXIT',{snape,[{?IM,_FunName,_}|_]}} =
	spawn_eval(fun() -> F3(snape) end),

    %% Called from within interpreted code
    ?line perfectly_alright = spawn_eval(fun() -> ?IM:do_apply(F1) end),

    %% Called from within interpreted code, badarity
    ?line {'EXIT',{{badarity,{F1,[snape]}},[{?IM,do_apply,_}|_]}} =
	spawn_eval(fun() -> ?IM:do_apply(F1, snape) end),

    %% Called from within interpreted code, error in fun
    ?line {'EXIT',{snape,[{?IM,_FunName,_}|_]}} =
	spawn_eval(fun() -> ?IM:do_apply(F3, snape) end),

    %% Try some more complex funs.
    ?line F4 = ?IM:give_me_a_fun_1(14, 42),
    ?line {false,yes,yeah,false} =
	F4({{1,nope},{14,yes},{42,yeah},{100,forget_it}}),
    ?line [this_is_ok,me_too] =
	F4([{-24,no_way},{15,this_is_ok},{1333,forget_me},{37,me_too}]),

    %% OTP-5837
    %% Try fun with guard containing variable bound in environment
    ?line [yes,no,no,no] = ?IM:otp_5837(1),

    ok.

apply_uninterpreted_fun(doc) ->
    "Apply a fun defined outside interpreted code.";
apply_uninterpreted_fun(suite) -> [];
apply_uninterpreted_fun(Config) when is_list(Config) ->

    ?line F1 = fun(snape) ->
		       erlang:error(snape);
		  (_Arg) ->
		       perfectly_alright
	       end,

    %% Ok
    ?line perfectly_alright =
	spawn_eval(fun() -> ?IM:do_apply(F1, any_arg) end),

    %% Badarity (evaluated in dbg_debugged, which calls erlang:apply/2)
    ?line {'EXIT',{{badarity,{F1,[]}},[{erlang,apply,_}|_]}} =
	spawn_eval(fun() -> ?IM:do_apply(F1) end),

    %% Error in fun
    ?line {'EXIT',{snape,[{?MODULE,_FunName,_}|_]}} =
	spawn_eval(fun() -> ?IM:do_apply(F1, snape) end),

    ok.

%%
%% Try executing an interpreted exit/1 call.
%%

interpreted_exit(Config) when is_list(Config) ->
    ?line process_flag(trap_exit, true),
    ?line Reason = make_ref(),
    ?line Pid = spawn_link(fun() -> ?IM:please_call_exit(Reason) end),
    ?line receive
	      {'EXIT',Pid,Reason} ->
		  ok;
	      {'EXIT',Pid,BadReason} ->
		  ?line ?t:fail({bad_message,BadReason})
	  after 10000 ->
		  ?line ?t:fail(timeout)
	  end,
    ok.

otp_8310(doc) ->
    "OTP-8310. Bugfixes lc/bc and andalso/orelse.";
otp_8310(Config) when is_list(Config) ->
    ?line ok = ?IM:otp_8310(),
    ok.

applier(M, F, A) ->
    Res = apply(M, F, A),
    io:format("~p:~p(~p) => ~p\n", [M,F,A,Res]),
    Res.

%%
%% Evaluate in another process, to prevent the test_case process to become
%% interpreted.
%%

spawn_eval(Fun) ->
    Self = self(),
    spawn_link(fun() -> Self ! (catch Fun()) end),
    receive
	Result ->
	    Result
    end.