From 4152157e10e207ce18a14200bcc8988911a6ed8a Mon Sep 17 00:00:00 2001 From: Matt Joyce Date: Wed, 8 Jun 2022 11:02:36 +0200 Subject: Newlib01: Add tests for rand() and lrand48() Check that the state of rand() and lrand48() is thread-specific, that they are properly initialized, and return the expected sequence of pseudo-random numbers for default seed values. --- testsuites/libtests/newlib01/init.c | 50 +++++++++++++++++++++++++++++++ testsuites/libtests/newlib01/newlib01.doc | 3 ++ 2 files changed, 53 insertions(+) diff --git a/testsuites/libtests/newlib01/init.c b/testsuites/libtests/newlib01/init.c index 8975a72fad..0bb3c4628b 100644 --- a/testsuites/libtests/newlib01/init.c +++ b/testsuites/libtests/newlib01/init.c @@ -82,6 +82,50 @@ static void wait(void) rtems_test_assert(sc == RTEMS_SUCCESSFUL); } +/* + * Check that rand() is properly initialized and returns the expected sequence + * for default seed values. A call to rand() without any previous call to + * srand() generates the same sequence as when srand() is first called with a + * seed value of 1. + */ +static void test_rand(void) +{ + int rv; + + rv = rand(); + rtems_test_assert(rv == 1481765933); + rv = rand(); + rtems_test_assert(rv == 1085377743); + rv = rand(); + rtems_test_assert(rv == 1270216262); + + srand(1); + rv = rand(); + rtems_test_assert(rv == 1481765933); + rv = rand(); + rtems_test_assert(rv == 1085377743); + rv = rand(); + rtems_test_assert(rv == 1270216262); +} + +/* + * Check that lrand48() is properly initialized and returns the expected + * sequence for default seed values. A call to lrand48() without any previous + * call to srand48() uses default constant initializer values set in the _seed + * member of struct _rand48. + */ +static void test_lrand48(void) +{ + long rv; + + rv = lrand48(); + rtems_test_assert(rv == 851401618); + rv = lrand48(); + rtems_test_assert(rv == 1804928587); + rv = lrand48(); + rtems_test_assert(rv == 758783491); +} + static void stdio_file_worker(rtems_task_argument arg) { test_context *ctx = &test_instance; @@ -90,6 +134,9 @@ static void stdio_file_worker(rtems_task_argument arg) char buf[1] = { 'x' }; size_t n; + test_rand(); + test_lrand48(); + rtems_test_assert(reent->__cleanup == NULL); output = stdout = fopen(&stdio_file_path[0], "r+"); @@ -454,6 +501,9 @@ static void Init(rtems_task_argument arg) int rv; TEST_BEGIN(); + test_rand(); + test_lrand48(); + ctx->main_task_id = rtems_task_self(); /* Fill dynamic file pool in Newlib */ diff --git a/testsuites/libtests/newlib01/newlib01.doc b/testsuites/libtests/newlib01/newlib01.doc index fbda12aa98..dc16584418 100644 --- a/testsuites/libtests/newlib01/newlib01.doc +++ b/testsuites/libtests/newlib01/newlib01.doc @@ -19,3 +19,6 @@ concepts: - Check that exit procedures provide proper resource cleanup. Ensure that a file opened from a worker task--but that is not assigned to a stdio stream--is not closed during thread termination. + - Check that the state of random number generators is thread-specific, they + are properly initialized, and return the expected sequence for default + seed values. -- cgit v1.2.3