|
0
|
1 //================================================================= |
|
|
2 // |
|
|
3 // rand3.c |
|
|
4 // |
|
|
5 // Testcase for C library rand() |
|
|
6 // |
|
|
7 //================================================================= |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //================================================================= |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
2
|
32 // Author(s): ctarpy, jlarmour |
|
|
33 // Contributors: ctarpy, jlarmour |
|
|
34 // Date: 1999-03-23 |
|
0
|
35 // Description: Contains testcode for C library rand() function. This tests |
|
|
36 // that random numbers are distributed well between 0 and |
|
|
37 // RAND_MAX |
|
|
38 // |
|
|
39 // |
|
|
40 //####DESCRIPTIONEND#### |
|
|
41 |
|
|
42 |
|
|
43 // CONFIGURATION |
|
|
44 |
|
|
45 #include <pkgconf/libc.h> // Configuration header |
|
|
46 |
|
|
47 |
|
|
48 // INCLUDES |
|
|
49 |
|
|
50 #include <stdlib.h> |
|
|
51 #include <cyg/infra/testcase.h> |
|
|
52 #include <sys/cstartup.h> // C library initialisation |
|
|
53 |
|
|
54 |
|
2
|
55 // CONSTANTS |
|
0
|
56 |
|
2
|
57 #define NUM_BUCKETS 1000 // how many categories to define |
|
|
58 #define TEST_LENGTH 200000 // how many samples to take - careful |
|
|
59 // when reducing this since it also reduces |
|
|
60 // BUCKET_DIFF_TOLERANCE below. If you reduce |
|
|
61 // it too low, BUCKET_DIFF_TOLERANCE will need |
|
|
62 // a fudge factor |
|
0
|
63 |
|
2
|
64 #define BUCKET_SIZE (RAND_MAX / NUM_BUCKETS) // number space allocated |
|
|
65 // to bucket from 0..RAND_MAX |
|
|
66 #define NUM_PER_BUCKET (TEST_LENGTH/NUM_BUCKETS) // Expected number that went |
|
|
67 // into each bucket at end |
|
0
|
68 |
|
2
|
69 // how much the buckets can vary at the end. |
|
|
70 #define BUCKET_DIFF_TOLERANCE (NUM_PER_BUCKET/4) // allowed to vary 25% |
|
0
|
71 |
|
|
72 |
|
|
73 |
|
|
74 // FUNCTIONS |
|
|
75 |
|
|
76 |
|
|
77 externC void |
|
|
78 cyg_package_start( void ) |
|
|
79 { |
|
|
80 cyg_iso_c_start(); |
|
|
81 } // cyg_package_start() |
|
|
82 |
|
|
83 |
|
2
|
84 static __inline__ int |
|
0
|
85 my_abs(int i) |
|
|
86 { |
|
|
87 return (i < 0) ? -i : i; |
|
|
88 } // my_abs() |
|
|
89 |
|
|
90 int |
|
|
91 main(int argc, char *argv[]) |
|
|
92 { |
|
2
|
93 // divide the space from 0..RAND_MAX into NUM_BUCKETS categories *BUT* |
|
|
94 // RAND_MAX / NUM_BUCKETS may not divide exactly so we leave space for |
|
|
95 // the bits left over, in case there are any! So we add 1. |
|
|
96 |
|
|
97 static cyg_uint8 rand_bucket[NUM_BUCKETS+1]; |
|
|
98 cyg_ucount32 count; // loop variable |
|
|
99 int r; // temp for rand() variable |
|
|
100 |
|
0
|
101 CYG_TEST_INIT(); |
|
|
102 |
|
|
103 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " |
|
|
104 "rand() function"); |
|
|
105 |
|
|
106 CYG_TEST_INFO("This test tests the distribution of random numbers and"); |
|
|
107 CYG_TEST_INFO("may take some time"); |
|
|
108 |
|
2
|
109 for ( count=0; count < TEST_LENGTH; ++count ) { |
|
|
110 r = rand(); |
|
|
111 ++rand_bucket[ r / BUCKET_SIZE ]; |
|
|
112 if ((count%10000)==0) |
|
|
113 CYG_TEST_STILL_ALIVE(count, "Still testing..."); |
|
|
114 } // for |
|
|
115 |
|
|
116 for ( count=0; count < NUM_BUCKETS; ++count ) { |
|
|
117 cyg_ucount32 diff; |
|
|
118 |
|
|
119 diff = my_abs( rand_bucket[count] - NUM_PER_BUCKET ); |
|
|
120 if ( diff > BUCKET_DIFF_TOLERANCE ) |
|
|
121 break; |
|
|
122 } // for |
|
0
|
123 |
|
2
|
124 // if the previous loop completed, we may want to check the "extra" |
|
|
125 // bucket (see the comment at the top) that may have some bits in if |
|
|
126 // RAND_MAX doesn't split into NUM_BUCKETS evenly. The number of random |
|
|
127 // digits that fell into that bucket would be expected to be proportional |
|
|
128 // to the ratio of the remainder of (RAND_MAX % NUM_BUCKETS) to |
|
|
129 // NUM_BUCKETS. |
|
|
130 if (count == NUM_BUCKETS) { |
|
|
131 cyg_ucount32 rem; |
|
|
132 cyg_ucount32 last_bucket_expected; |
|
|
133 cyg_ucount32 diff; |
|
|
134 |
|
|
135 rem = RAND_MAX % NUM_BUCKETS; |
|
|
136 |
|
|
137 last_bucket_expected = (rem * NUM_PER_BUCKET) / BUCKET_SIZE; |
|
|
138 |
|
|
139 diff = my_abs(last_bucket_expected - rand_bucket[count]); |
|
|
140 CYG_TEST_PASS_FAIL(diff <= BUCKET_DIFF_TOLERANCE, |
|
|
141 "Upper bound fencepost test"); |
|
|
142 } |
|
|
143 CYG_TEST_PASS_FAIL( (count >= NUM_BUCKETS), |
|
|
144 "even distribution of rand()"); |
|
|
145 |
|
|
146 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for " |
|
|
147 "C library rand() function"); |
|
0
|
148 } // main() |
|
|
149 |
|
|
150 |
|
|
151 // EOF rand3.c |