aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/test/escript_SUITE_data/tail_rec
blob: 2ef64e1239c4cecbcd52229daf6c6b27ffb2a4d2 (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
#!/usr/bin/env escript
%% -*- erlang -*-
-mode(interpret).

tail_rec(PrevSize, N) ->
    {_, Size} = process_info(self(), stack_size),
    if
	N =< 0 ->
	    ok;
	PrevSize =:= undefined ->
	    tail_rec(Size, N - 1);
	PrevSize =:= Size ->
	    tail_rec(Size, N - 1);
	true ->
	    io:format("Not tail recursive (~p): Stack size ~p should be ~p\n",
		      [N, Size, PrevSize]),
	    tail_rec(Size, N - 1)
    end.

main([Repetitions]) ->
    tail_rec(undefined, list_to_integer(Repetitions)),
    io:format("ok\n", []);
main(_) ->
    io:format("Usage: ~s Repetitions\n", [escript:script_name()]),
    init:stop(1).