Skip to content

Commit 6093a74

Browse files
committed
tests: add tests for is_function/2
Test both `is_function/2` guard opcode and BIF. Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent ee65bbd commit 6093a74

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

tests/erlang_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ compile_erlang(test_funs8)
194194
compile_erlang(test_funs9)
195195
compile_erlang(test_funs10)
196196
compile_erlang(test_funs11)
197+
compile_erlang(test_funs12)
197198

198199
compile_erlang(test_make_fun3)
199200

@@ -668,6 +669,7 @@ add_custom_target(erlang_test_modules DEPENDS
668669
test_funs9.beam
669670
test_funs10.beam
670671
test_funs11.beam
672+
test_funs12.beam
671673

672674
test_make_fun3.beam
673675

tests/erlang_tests/test_funs12.erl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2025 Davide Bettio <davide@uninstall.it>
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+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
%
20+
21+
-module(test_funs12).
22+
-export([start/0, check_guard/3, check_bool/3, discard/1, get_fun/0]).
23+
24+
start() ->
25+
CheckGuardFun = fun ?MODULE:check_guard/3,
26+
SelfFun = fun erlang:self/0,
27+
{A, F} = ?MODULE:get_fun(),
28+
true = ?MODULE:check_guard(3, CheckGuardFun, 3),
29+
false = ?MODULE:check_guard([], CheckGuardFun, []),
30+
true = ?MODULE:check_bool(SelfFun, F, 1),
31+
false = ?MODULE:check_bool([], {}, 1),
32+
ok = expect_error(fun() -> ?MODULE:check_bool({}, [], not_integer) end, error, badarg),
33+
ok = expect_error(fun() -> ?MODULE:check_bool(SelfFun, F, not_integer) end, error, badarg),
34+
ok = expect_error(fun() -> ?MODULE:check_bool(SelfFun, F, -20) end, error, badarg),
35+
1 = ?MODULE:check_guard(A, F, A),
36+
?MODULE:check_guard(0, SelfFun, 0).
37+
38+
check_guard(A, B, A) when A > 1 andalso is_function(B, A) ->
39+
discard(B);
40+
check_guard(A, B, A) when is_function(B, A) ->
41+
A;
42+
check_guard(A, _B, A) when A < 0 ->
43+
error;
44+
check_guard(_A, _B, _A) ->
45+
false.
46+
47+
discard(_X) ->
48+
true.
49+
50+
check_bool(A, B, C) ->
51+
is_function(A, C) or is_function(B, C).
52+
53+
get_fun() ->
54+
{1, fun(X) -> X + 1 end}.
55+
56+
expect_error(Fun, A, B) ->
57+
try Fun() of
58+
Result -> {error, Result}
59+
catch
60+
A:B -> ok
61+
end.

tests/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ struct Test tests[] = {
233233
TEST_CASE_EXPECTED(test_funs9, 3555),
234234
TEST_CASE_EXPECTED(test_funs10, 6817),
235235
TEST_CASE_EXPECTED(test_funs11, 817),
236+
TEST_CASE(test_funs12),
236237
TEST_CASE(test_make_fun3),
237238
TEST_CASE(fun_call_bif),
238239

0 commit comments

Comments
 (0)