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