OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [language/] [c/] [libc/] [stdlib/] [v2_0/] [tests/] [rand3.c] - Blame information for rev 249

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//=================================================================
2
//
3
//        rand3.c
4
//
5
//        Testcase for C library rand()
6
//
7
//=================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//=================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):     jlarmour
44
// Contributors:  
45
// Date:          2000-04-30
46
// Description:   Contains testcode for C library rand() function. This tests
47
//                that random numbers are distributed well between 0 and
48
//                RAND_MAX
49
//
50
//
51
//####DESCRIPTIONEND####
52
 
53
// INCLUDES
54
 
55
#include <stdlib.h>
56
#include <cyg/infra/testcase.h>
57
 
58
 
59
// CONSTANTS
60
 
61
#define NUM_BUCKETS 1000     // how many categories to define
62
#define TEST_LENGTH 200000   // how many samples to take - careful
63
                             // when reducing this since it also reduces
64
                             // BUCKET_DIFF_TOLERANCE below. If you reduce
65
                             // it too low, BUCKET_DIFF_TOLERANCE will need
66
                             // a fudge factor
67
 
68
#define BUCKET_SIZE    (RAND_MAX / NUM_BUCKETS)  // number space allocated
69
                                                 // to bucket from 0..RAND_MAX
70
#define NUM_PER_BUCKET (TEST_LENGTH/NUM_BUCKETS) // Expected number that went
71
                                                 // into each bucket at end
72
 
73
// how much the buckets can vary at the end.
74
#define BUCKET_DIFF_TOLERANCE (NUM_PER_BUCKET/4) // allowed to vary 25%
75
 
76
 
77
 
78
// FUNCTIONS
79
 
80
static __inline__ int
81
my_abs(int i)
82
{
83
    return (i < 0) ? -i : i;
84
} // my_abs()
85
 
86
int
87
main(int argc, char *argv[])
88
{
89
    // divide the space from 0..RAND_MAX into NUM_BUCKETS categories *BUT*
90
    // RAND_MAX / NUM_BUCKETS may not divide exactly so we leave space for
91
    // the bits left over, in case there are any! So we add 1.
92
 
93
    static cyg_uint8 rand_bucket[NUM_BUCKETS+1];
94
    cyg_ucount32 count;                        // loop variable
95
    int r;                                     // temp for rand() variable
96
 
97
    CYG_TEST_INIT();
98
 
99
    CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
100
                  "rand() function");
101
 
102
    CYG_TEST_INFO("This test tests the distribution of random numbers and");
103
    CYG_TEST_INFO("may take some time");
104
 
105
    for ( count=0; count < TEST_LENGTH; ++count ) {
106
        r = rand();
107
        ++rand_bucket[ r / BUCKET_SIZE ];
108
        if ((count%10000)==0)
109
            CYG_TEST_STILL_ALIVE(count, "Still testing...");
110
    } // for
111
 
112
    for ( count=0; count < NUM_BUCKETS; ++count ) {
113
        cyg_ucount32 diff;
114
 
115
        diff = my_abs( rand_bucket[count] - NUM_PER_BUCKET );
116
        if ( diff > BUCKET_DIFF_TOLERANCE )
117
            break;
118
    } // for
119
 
120
    // if the previous loop completed, we may want to check the "extra"
121
    // bucket (see the comment at the top) that may have some bits in if
122
    // RAND_MAX doesn't split into NUM_BUCKETS evenly. The number of random
123
    // digits that fell into that bucket would be expected to be proportional
124
    // to the ratio of the remainder of (RAND_MAX % NUM_BUCKETS) to
125
    // NUM_BUCKETS. 
126
    if (count == NUM_BUCKETS) {
127
        cyg_ucount32 rem;
128
        cyg_ucount32 last_bucket_expected;
129
        cyg_ucount32 diff;
130
 
131
        rem = RAND_MAX % NUM_BUCKETS;
132
 
133
        last_bucket_expected = (rem * NUM_PER_BUCKET) / BUCKET_SIZE;
134
 
135
        diff = my_abs(last_bucket_expected - rand_bucket[count]);
136
        CYG_TEST_PASS_FAIL(diff <= BUCKET_DIFF_TOLERANCE,
137
                           "Upper bound fencepost test");
138
    }
139
    CYG_TEST_PASS_FAIL( (count >= NUM_BUCKETS),
140
                        "even distribution of rand()");
141
 
142
    CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for "
143
                    "C library rand() function");
144
} // main()
145
 
146
 
147
// EOF rand3.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.