aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/test/basic_SUITE_data/basic_fun.erl
blob: 18ba7fdb3fb120d33557b6a26aa1bcd3742135e5 (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
%%% -*- erlang-indent-level: 2 -*-
%%%-------------------------------------------------------------------
%%% Author: Kostis Sagonas
%%%
%%% Tests for correct handling of funs.
%%%-------------------------------------------------------------------
-module(basic_fun).

-export([test/0]).

-export([dummy_foo/4, add1/1, test_fun03/0]).

test() ->
  ok = test_calls(),
  ok = test_is_function(),
  ok = test_is_function2(),
  ok.

%%--------------------------------------------------------------------
%% Tests function and fun calls.

test_calls() ->
  ok = test_apply_call(?MODULE, dummy_foo),
  ok = test_fun_call(fun dummy_foo/4),
  ok = test_fun_call(fun ?MODULE:dummy_foo/4),
  ok.

test_apply_call(M, F) ->
  M:F(bar, 42, foo, 17).

test_fun_call(Fun) ->
  Fun(bar, 42, foo, 17).

dummy_foo(_, _, foo, _) -> ok.

%%--------------------------------------------------------------------
%% Tests handling of funs out of exported functions and 2-tuple funs.

test_fun03() ->
  MFPair = add1_as_2tuple(),
  4712 = do_call(add1_as_export(), 4711),
  {badfun, MFPair} = try do_call(MFPair, 88) catch error:Err -> Err end,
  true = do_guard(add1_as_export()),
  false = do_guard(MFPair), % 2-tuples do not satisfy is_function/1
  ok.

do_call(F, X) -> F(X).

do_guard(F) when is_function(F) -> true;
do_guard(_) -> false.

add1_as_export() -> fun ?MODULE:add1/1.

add1_as_2tuple() -> {?MODULE, add1}.

add1(X) -> X+1.

%%--------------------------------------------------------------------
%% Tests the is_function guard and BIF.

test_is_function() ->
  Fun = fun (X, foo) -> dummy_foo(X, mnesia_lib, foo, [X]) end,
  ok = test_when_guard(Fun),
  ok = test_if_guard(Fun),
  ok.

test_when_guard(X) when is_function(X) -> ok.

test_if_guard(X) ->
  if is_function(X) -> ok;
     true -> weird
  end.

%%--------------------------------------------------------------------
%% Tests the is_function2 guard and BIF.

test_is_function2() ->
  ok = test_guard(),
  ok = test_guard2(),
  ok = test_call(),
  ok.

test_guard() ->
  zero_fun = test_f2(fun () -> ok end),
  unary_fun = test_f2(fun(X) -> X end),
  binary_fun = test_f2(fun (X, Y) -> {X, Y} end),
  no_fun = test_f2(gazonk),
  ok.

test_f2(Fun) when is_function(Fun, 0) ->
  zero_fun;
test_f2(Fun) when is_function(Fun, 1) ->
  unary_fun;
test_f2(Fun) when is_function(Fun, 2) ->
  binary_fun;
test_f2(_) ->
  no_fun.

test_guard2() ->
  zero_fun = test_f2_n(fun () -> ok end, 0),
  unary_fun = test_f2_n(fun (X) -> X end, 1),
  binary_fun = test_f2_n(fun (X, Y) -> {X, Y} end, 2),
  no_fun = test_f2_n(gazonk, 0),
  ok.

test_f2_n(F, N) when is_function(F, N) ->
  case N of
    0 -> zero_fun;
    1 -> unary_fun;
    2 -> binary_fun
  end;
test_f2_n(_, _) ->
  no_fun.

test_call() ->
  true  = test_fn2(fun (X, Y) -> {X,Y} end, 2),
  false = test_fn2(fun (X, Y) -> {X,Y} end, 3),
  false = test_fn2(gazonk, 2),
  {'EXIT', {badarg, _TR1}} = (catch test_fn2(gazonk, gazonk)),
  {'EXIT', {badarg, _TR2}} = (catch test_fn2(fun (X, Y) -> {X, Y} end, gazonk)),
  ok.

test_fn2(F, N) ->
  is_function(F, N).