|
| 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. |
0 commit comments