comparison packages/language/c/libc/current/tests/stdlib/rand3.c @ 0:3111d98ba7b3 ecos-v1_1-release

Initial commit of eCos version 1.1
author jlarmour
date Tue, 11 May 1999 11:16:07 +0000
parents
children 443894e2e912
comparison
equal deleted inserted replaced
-1:000000000000 0:3111d98ba7b3
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
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved.
26 // -------------------------------------------
27 //
28 //####COPYRIGHTEND####
29 //=================================================================
30 //#####DESCRIPTIONBEGIN####
31 //
32 // Author(s): ctarpy@cygnus.co.uk, jlarmour@cygnus.co.uk
33 // Contributors: jlarmour@cygnus.co.uk
34 // Date: 1998/6/3
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 // Declarations for test system:
43 //
44 // TESTCASE_TYPE=CYG_TEST_MODULE
45
46
47 // CONFIGURATION
48
49 #include <pkgconf/libc.h> // Configuration header
50
51
52 // INCLUDES
53
54 #include <stdlib.h>
55 #include <cyg/infra/testcase.h>
56 #include <sys/cstartup.h> // C library initialisation
57
58
59 // HOW TO START TESTS
60
61 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_RAND)
62
63 # define START_TEST( test ) test(0)
64
65 #else
66
67 # define START_TEST( test ) CYG_EMPTY_STATEMENT
68
69 #endif // if defined(CYGPKG_LIBC)
70
71
72 // CONSTANTS
73
74 #define NUM_BUCKETS 1024 // how many categories to define
75 #define TEST_LENGTH 100000 // how many samples to take
76 #define BUCKET_DIFF_TOLERANCE 4 // how much the buckets can vary at the end
77
78 // FUNCTIONS
79
80
81 externC void
82 cyg_package_start( void )
83 {
84 #ifdef CYGPKG_LIBC
85 cyg_iso_c_start();
86 #else
87 (void)main(0, NULL);
88 #endif
89 } // cyg_package_start()
90
91
92 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_RAND)
93
94 static int
95 my_abs(int i)
96 {
97 return (i < 0) ? -i : i;
98 } // my_abs()
99
100 static void
101 test( CYG_ADDRWORD data )
102 {
103 static cyg_uint8 rand_bucket[NUM_BUCKETS]; // divide the space from
104 // 0..RAND_MAX into
105 // NUM_BUCKETS categories
106 cyg_ucount32 count; // loop variable
107 int r; // temp for rand() variable
108 cyg_ucount32 sum; // sum of bucket contents
109 cyg_ucount32 average; // average of bucket contents
110
111 // initialise all buckets to 0 - do it ourselves rather than rely on memset
112 for ( count=0; count < NUM_BUCKETS; ++count )
113 {
114 rand_bucket[ count ] = 0;
115 } // for
116
117 for ( count=0; count < TEST_LENGTH; ++count )
118 {
119 r = rand();
120 ++rand_bucket[ r % NUM_BUCKETS ];
121 } // for
122
123 for ( sum=0, count=0; count < NUM_BUCKETS; ++count )
124 {
125 sum += rand_bucket[ count ];
126 } // for
127
128 average = sum / NUM_BUCKETS;
129
130 for ( count=0; count < NUM_BUCKETS; ++count )
131 {
132 if ( my_abs(rand_bucket[count] - average) > BUCKET_DIFF_TOLERANCE )
133 break;
134 } // for
135
136 CYG_TEST_PASS_FAIL( (count >= NUM_BUCKETS), "even distribution of rand()");
137
138 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
139 "rand() function");
140 } // test()
141
142 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_RAND)
143
144
145 int
146 main(int argc, char *argv[])
147 {
148 CYG_TEST_INIT();
149
150 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
151 "rand() function");
152
153 CYG_TEST_INFO("This test tests the distribution of random numbers and");
154 CYG_TEST_INFO("may take some time");
155
156 START_TEST( test );
157
158 CYG_TEST_PASS_FINISH("Testing is not applicable to this configuration");
159 } // main()
160
161
162 // EOF rand3.c