1# test if rand() gives true alea
2message(STATUS "Testing if rand() gives true alea")
3try_run(test-rand_runs test-rand_compiles
4        ${PROJECT_BINARY_DIR}/config
5        ${PROJECT_SOURCE_DIR}/config/test-rand.c)
6if(test-rand_runs EQUAL 0)
7   set(HAVE_RAND_BUG 0)
8   message(STATUS "Testing if rand() gives true alea -- Probably")
9else()
10   set(HAVE_RAND_BUG 1)
11   message(STATUS "Testing if rand() gives true alea -- Buggy")
12endif()
13
14message(STATUS "Testing if srand() yields a deterministic sequence")
15include(CheckCSourceRuns)
16CHECK_C_SOURCE_RUNS("
17    #include <stdlib.h>
18    int main()
19    {
20        srand(1);
21        unsigned long a = rand();
22        srand(1);
23        unsigned long b = rand();
24        return a == b ? EXIT_SUCCESS : EXIT_FAILURE;
25    }
26" SRAND_CHECK)
27
28if (SRAND_CHECK_EXITCODE EQUAL 0)
29    message(STATUS "Testing if srand() yields a deterministic sequence -- Yes")
30    set(HAVE_USUAL_SRAND_DETERMINISTIC_BEHAVIOR 1)
31else()
32    # From "man srand" in openbsd:
33    #
34    #   Standards insist that this interface return deterministic results.
35    #   Unsafe usage is very common, so OpenBSD changed the subsystem to return
36    #   non-deterministic results by default.
37    #
38    message(STATUS "Testing if srand() yields a deterministic sequence -- No")
39    set(HAVE_USUAL_SRAND_DETERMINISTIC_BEHAVIOR 0)
40    CHECK_FUNCTION_EXISTS(srand_deterministic	HAVE_SRAND_DETERMINISTIC)
41    if(NOT HAVE_SRAND_DETERMINISTIC)
42        message(WARNING "tests in cado-nfs will not be reproducible because you do not have deterministic rand()")
43    endif()
44endif()
45