summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/rand_srand/test1/test1.c
blob: 34154cb6d234c4fc8d34392cf9eb23599d0d5203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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;
}