summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/rand_srand
diff options
context:
space:
mode:
Diffstat (limited to 'src/pal/tests/palsuite/c_runtime/rand_srand')
-rw-r--r--src/pal/tests/palsuite/c_runtime/rand_srand/CMakeLists.txt4
-rw-r--r--src/pal/tests/palsuite/c_runtime/rand_srand/test1/CMakeLists.txt19
-rw-r--r--src/pal/tests/palsuite/c_runtime/rand_srand/test1/test1.c100
-rw-r--r--src/pal/tests/palsuite/c_runtime/rand_srand/test1/testinfo.dat16
4 files changed, 139 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/c_runtime/rand_srand/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/rand_srand/CMakeLists.txt
new file mode 100644
index 0000000000..f6aa0cb2d9
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/rand_srand/CMakeLists.txt
@@ -0,0 +1,4 @@
+cmake_minimum_required(VERSION 2.8.12.2)
+
+add_subdirectory(test1)
+
diff --git a/src/pal/tests/palsuite/c_runtime/rand_srand/test1/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/rand_srand/test1/CMakeLists.txt
new file mode 100644
index 0000000000..939914662a
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/rand_srand/test1/CMakeLists.txt
@@ -0,0 +1,19 @@
+cmake_minimum_required(VERSION 2.8.12.2)
+
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+
+set(SOURCES
+ test1.c
+)
+
+add_executable(paltest_rand_srand_test1
+ ${SOURCES}
+)
+
+add_dependencies(paltest_rand_srand_test1 coreclrpal)
+
+target_link_libraries(paltest_rand_srand_test1
+ pthread
+ m
+ coreclrpal
+)
diff --git a/src/pal/tests/palsuite/c_runtime/rand_srand/test1/test1.c b/src/pal/tests/palsuite/c_runtime/rand_srand/test1/test1.c
new file mode 100644
index 0000000000..34154cb6d2
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/rand_srand/test1/test1.c
@@ -0,0 +1,100 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/*=============================================================================
+**
+** Source: test1.c
+**
+** Purpose: Test to ensure that srand provide random
+** number to rand. Also make sure that rand result from a
+** srand with seed 1 and no call to srand are the same.
+**
+** Dependencies: PAL_Initialize
+** PAL_Terminate
+** Fail
+** srand()
+**
+
+**
+**===========================================================================*/
+
+#include <palsuite.h>
+
+
+int __cdecl main(int argc, char **argv)
+{
+ int RandNumber[10];
+ int TempRandNumber;
+ int i;
+ int SRAND_SEED;
+ int SRAND_REINIT = 1;
+
+ /*
+ * Initialize the PAL and return FAILURE if this fails
+ */
+
+ if (PAL_Initialize(argc, argv))
+ {
+ return FAIL;
+ }
+
+ SRAND_SEED = time(NULL);
+
+ /* does not initialize srand and call rand. */
+ for (i=0; i<10; i++)
+ {
+ /* keep the value in a array */
+ RandNumber[i]=rand();
+ if (RandNumber[i] < 0 || RandNumber[i] > RAND_MAX)
+ {
+ Fail("1) ERROR: random generated an invalid value: %d", RandNumber[i]);
+ }
+ }
+
+
+ /* initialize random generator */
+ srand(SRAND_SEED);
+
+
+ /* choose 10 numbers with a different seed.
+ the numbers should be different than
+ those the previously generated one */
+ for(i = 0; i < 10; i++)
+ {
+ TempRandNumber=rand();
+ if (TempRandNumber < 0 || TempRandNumber > RAND_MAX)
+ {
+ Fail("2) ERROR: random generated an invalid value: %d", TempRandNumber);
+ }
+ }
+
+
+
+ /* renitialize the srand with 1 */
+ srand(SRAND_REINIT);
+
+
+
+ /* choose 10 numbers with seed 1,
+ the number should be the same as those we kept in the array. */
+ for( i = 0; i < 10;i++ )
+ {
+ /* pick the random number*/
+ TempRandNumber=rand();
+ /* test if it is the same number generated in the first sequences*/
+ if(RandNumber[i]!=TempRandNumber)
+ {
+ Fail ("ERROR: rand should return the same value when srand "
+ "is initialized with 1 or not initialized at all");
+ }
+ if (TempRandNumber < 0 || TempRandNumber > RAND_MAX)
+ {
+ Fail("3) ERROR: random generated an invalid value: %d", TempRandNumber);
+ }
+ }
+
+
+ PAL_Terminate();
+ return PASS;
+}
diff --git a/src/pal/tests/palsuite/c_runtime/rand_srand/test1/testinfo.dat b/src/pal/tests/palsuite/c_runtime/rand_srand/test1/testinfo.dat
new file mode 100644
index 0000000000..cf1b42dbf0
--- /dev/null
+++ b/src/pal/tests/palsuite/c_runtime/rand_srand/test1/testinfo.dat
@@ -0,0 +1,16 @@
+# Licensed to the .NET Foundation under one or more agreements.
+# The .NET Foundation licenses this file to you under the MIT license.
+# See the LICENSE file in the project root for more information.
+
+Version = 1.0
+Section = C Runtime
+Function = rand and srand
+Name = Positive Test for rand and srand
+TYPE = DEFAULT
+EXE1 = test1
+Description
+= Call rand without srand and get 10 random number batch 1.
+= call srand with seed 2, get 10 other random number and verify
+= that numbers are different from the batch 1.
+= Set the seed to 1, get 10 other random number and verify
+= that the generated number are the same as batch 1.