|
2
|
1 //================================================================= |
|
|
2 // |
|
|
3 // rand4.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,1999 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //================================================================= |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): rosalia |
|
|
33 // Contributors: rosalia, jlarmour |
|
|
34 // Date: 1999-03-19 |
|
|
35 // Description: Contains testcode for C library rand() function. This tests |
|
|
36 // that random numbers do not have periodicity in the lower |
|
|
37 // bit |
|
|
38 // |
|
|
39 // |
|
|
40 //####DESCRIPTIONEND#### |
|
|
41 |
|
|
42 // CONFIGURATION |
|
|
43 |
|
|
44 #include <pkgconf/libc.h> // C library configuration |
|
|
45 |
|
|
46 // INCLUDES |
|
|
47 |
|
|
48 #include <stdlib.h> |
|
|
49 #include <cyg/infra/testcase.h> |
|
|
50 #include <sys/cstartup.h> // C library initialisation |
|
|
51 |
|
|
52 // CONSTANTS |
|
|
53 |
|
|
54 #define TEST_LENGTH 100000 // how many samples to take |
|
|
55 |
|
|
56 // DEFINES |
|
|
57 |
|
|
58 #define TEST_VALID (!defined(CYGIMP_LIBC_RAND_SIMPLEST)) |
|
|
59 |
|
|
60 // FUNCTIONS |
|
|
61 |
|
|
62 |
|
|
63 externC void |
|
|
64 cyg_package_start( void ) |
|
|
65 { |
|
|
66 cyg_iso_c_start(); |
|
|
67 } // cyg_package_start() |
|
|
68 |
|
|
69 int |
|
|
70 main(int argc, char *argv[]) |
|
|
71 { |
|
|
72 #if TEST_VALID |
|
|
73 int i; |
|
|
74 int r, prev, prevprev; |
|
|
75 int how_many_periodics = 0; |
|
|
76 #endif |
|
|
77 |
|
|
78 CYG_TEST_INIT(); |
|
|
79 |
|
|
80 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " |
|
|
81 "rand() function"); |
|
|
82 |
|
|
83 CYG_TEST_INFO("This test tests the distribution of random numbers and"); |
|
|
84 CYG_TEST_INFO("may take some time"); |
|
|
85 |
|
|
86 #if TEST_VALID |
|
|
87 r = rand() % 2; |
|
|
88 prev = r; |
|
|
89 r = rand() % 2; |
|
|
90 for (i = 0; i < TEST_LENGTH; ++i) { |
|
|
91 prevprev = prev; |
|
|
92 prev = r; |
|
|
93 r = rand() % 2; |
|
|
94 if (r == prevprev) { |
|
|
95 ++how_many_periodics; |
|
|
96 } |
|
|
97 if (how_many_periodics > (2*TEST_LENGTH)/3) { |
|
|
98 break; |
|
|
99 } |
|
|
100 } |
|
|
101 |
|
|
102 CYG_TEST_PASS_FAIL( (how_many_periodics <= (2*TEST_LENGTH)/3), |
|
|
103 "periodicity of rand() in lowest bit"); |
|
|
104 |
|
|
105 #else |
|
|
106 |
|
|
107 // FIXME: should be an _expected_ fail i.e. XFAIL |
|
|
108 CYG_TEST_NA("Chosen rand algorithm is known to fail this test"); |
|
|
109 |
|
|
110 #endif |
|
|
111 |
|
|
112 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for " |
|
|
113 "C library rand() function"); |
|
|
114 } // main() |
|
|
115 |
|
|
116 |
|
|
117 // EOF rand4.c |