|
| 1 | +% |
| 2 | +% This file is part of AtomVM. |
| 3 | +% |
| 4 | +% Copyright 2024 Paul Guyot <pguyot@kallisys.net> |
| 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_size). |
| 22 | + |
| 23 | +-export([start/0, test_size/2, test_size_guard/2]). |
| 24 | + |
| 25 | +start() -> |
| 26 | + gt = ?MODULE:test_size_guard({1, 2, 3}, 4), |
| 27 | + lt = ?MODULE:test_size_guard({1, 2, 3}, 2), |
| 28 | + eq = ?MODULE:test_size_guard({1, 2, 3}, 3), |
| 29 | + gt = ?MODULE:test_size_guard(<<1, 2, 3>>, 4), |
| 30 | + lt = ?MODULE:test_size_guard(<<1, 2, 3>>, 2), |
| 31 | + eq = ?MODULE:test_size_guard(<<1, 2, 3>>, 3), |
| 32 | + gt = ?MODULE:test_size_guard(<<3, 14, 1, 2, 3>>, 4), |
| 33 | + lt = ?MODULE:test_size_guard(<<3, 14, 1, 2, 3>>, 2), |
| 34 | + eq = ?MODULE:test_size_guard(<<3, 14, 1, 2, 3>>, 3), |
| 35 | + false = ?MODULE:test_size_guard(#{a => 1, b => 2, c => 3}, 4), |
| 36 | + false = ?MODULE:test_size_guard([1, 2, 3], 2), |
| 37 | + |
| 38 | + gt = ?MODULE:test_size({1, 2, 3}, 4), |
| 39 | + lt = ?MODULE:test_size({1, 2, 3}, 2), |
| 40 | + eq = ?MODULE:test_size({1, 2, 3}, 3), |
| 41 | + gt = ?MODULE:test_size(<<1, 2, 3>>, 4), |
| 42 | + lt = ?MODULE:test_size(<<1, 2, 3>>, 2), |
| 43 | + eq = ?MODULE:test_size(<<1, 2, 3>>, 3), |
| 44 | + gt = ?MODULE:test_size(<<3, 14, 1, 2, 3>>, 4), |
| 45 | + lt = ?MODULE:test_size(<<3, 14, 1, 2, 3>>, 2), |
| 46 | + eq = ?MODULE:test_size(<<3, 14, 1, 2, 3>>, 3), |
| 47 | + ok = |
| 48 | + try |
| 49 | + ?MODULE:test_size([1, 2, 3], 2), |
| 50 | + fail |
| 51 | + catch |
| 52 | + error:badarg -> |
| 53 | + ok |
| 54 | + end, |
| 55 | + ok = |
| 56 | + try |
| 57 | + ?MODULE:test_size(#{a => 1, b => 2, c => 3}, 4), |
| 58 | + fail |
| 59 | + catch |
| 60 | + error:badarg -> |
| 61 | + ok |
| 62 | + end, |
| 63 | + 0. |
| 64 | + |
| 65 | +% OTP-27 encodes this as a call to byte_size/1, passing it the matching state |
| 66 | +% OTP-21 encodes this as a call to size/1, but encodes bs_context_to_binary |
| 67 | +% first. |
| 68 | +test_size_guard(<<3, 14, Elem/binary>>, S) when size(Elem) < S -> gt; |
| 69 | +test_size_guard(<<3, 14, Elem/binary>>, S) when size(Elem) > S -> lt; |
| 70 | +test_size_guard(<<3, 14, Elem/binary>>, S) when size(Elem) =:= S -> eq; |
| 71 | +test_size_guard(Elem, S) when size(Elem) < S -> gt; |
| 72 | +test_size_guard(Elem, S) when size(Elem) > S -> lt; |
| 73 | +test_size_guard(Elem, S) when size(Elem) =:= S -> eq; |
| 74 | +test_size_guard(_Elem, _S) -> false. |
| 75 | + |
| 76 | +test_size(<<3, 14, Elem/binary>>, S) -> |
| 77 | + Size = size(Elem), |
| 78 | + if |
| 79 | + Size < S -> gt; |
| 80 | + Size > S -> lt; |
| 81 | + true -> eq |
| 82 | + end; |
| 83 | +test_size(Elem, S) -> |
| 84 | + Size = size(Elem), |
| 85 | + if |
| 86 | + Size < S -> gt; |
| 87 | + Size > S -> lt; |
| 88 | + true -> eq |
| 89 | + end. |
0 commit comments