Skip to content

Commit

Permalink
tests: add test for atomvm:random/0
Browse files Browse the repository at this point in the history
Add test for atomvm:random/0 so implementation can be replaced with a
different implementation.

Signed-off-by: Davide Bettio <davide@uninstall.it>
  • Loading branch information
bettio committed Nov 20, 2023
1 parent f68e0b8 commit 2914ad5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/erlang_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ compile_erlang(test_module_info)
compile_erlang(int64_build_binary)

compile_erlang(test_crypto_strong_rand_bytes)
compile_erlang(test_atomvm_random)

add_custom_target(erlang_test_modules DEPENDS
code_load_files
Expand Down Expand Up @@ -939,4 +940,5 @@ add_custom_target(erlang_test_modules DEPENDS
int64_build_binary.beam

test_crypto_strong_rand_bytes.beam
test_atomvm_random.beam
)
51 changes: 51 additions & 0 deletions tests/erlang_tests/test_atomvm_random.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
%
% This file is part of AtomVM.
%
% Copyright 2023 Davide Bettio <davide@uninstall.it>
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
%

-module(test_atomvm_random).
-export([start/0]).

start() ->
RandomTuple = random_tuple(),
1 = all_integer(RandomTuple),
Test1 = all_equal(RandomTuple),

Bin = atomvm:rand_bytes(4),
RandomTuple2 = binary_to_tuple(Bin),
Test2 = all_equal(RandomTuple2),
Test1 + Test2 * 2.

all_integer({A, B, C, D}) when
is_integer(A) and is_integer(B) and is_integer(C) and is_integer(D)
->
1;
all_integer(_) ->
0.

all_equal({A, B, C, D}) when (A =/= B) and (B =/= C) and (C =/= D) ->
0;
all_equal(_) ->
1.

random_tuple() ->
{atomvm:random(), atomvm:random(), atomvm:random(), atomvm:random()}.

binary_to_tuple(B) ->
L = erlang:binary_to_list(B),
erlang:list_to_tuple(L).
1 change: 1 addition & 0 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ struct Test tests[] = {

#if defined ATOMVM_HAS_MBEDTLS
TEST_CASE(test_crypto_strong_rand_bytes),
TEST_CASE(test_atomvm_random),
#endif

// TEST CRASHES HERE: TEST_CASE(memlimit),
Expand Down

0 comments on commit 2914ad5

Please sign in to comment.