aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/test/basic_SUITE_data/basic_tail_rec.erl
blob: 0124f13df69346ddcbeb18eeec30c6360db261b0 (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
%%% -*- erlang-indent-level: 2 -*-
%%%-------------------------------------------------------------------
%%% Author: Kostis Sagonas
%%%
%%% Contains tests that check that tail recursion optimization occurs.
%%%-------------------------------------------------------------------
-module(basic_tail_rec).

-export([test/0]).
-export([app0/2]).  %% used in an apply/3 call

test() ->
  ok = test_app_tail(),
  ok.

%%--------------------------------------------------------------------
%% Written by Mikael Pettersson: check that apply is tail recursive.

%% Increased the following quantity from 20 to 30 so that the test
%% remains valid even with the naive register allocator.  - Kostis
-define(SIZE_INCREASE, 30).

test_app_tail() ->
  Inc = start(400),
  %% io:format("Inc ~w\n", [Inc]),
  case Inc > ?SIZE_INCREASE of
    true ->
      {error, "apply/3 is not tail recursive in native code"};
    false ->
      ok
  end.

start(N) ->
  app0(N, hipe_bifs:nstack_used_size()).

app0(0, Size0) ->
  hipe_bifs:nstack_used_size() - Size0;
app0(N, Size) ->
  apply(?MODULE, app0, [N-1, Size]).