|
0
|
1 //================================================================= |
|
|
2 // |
|
|
3 // srand.c |
|
|
4 // |
|
|
5 // Testcase for C library srand() |
|
|
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 srand() function |
|
|
36 // |
|
|
37 // |
|
|
38 //####DESCRIPTIONEND#### |
|
|
39 |
|
|
40 // Declarations for test system: |
|
|
41 // |
|
|
42 // TESTCASE_TYPE=CYG_TEST_MODULE |
|
|
43 |
|
|
44 // CONFIGURATION |
|
|
45 |
|
|
46 #include <pkgconf/libc.h> // Configuration header |
|
|
47 |
|
|
48 // INCLUDES |
|
|
49 |
|
|
50 #include <stdlib.h> |
|
|
51 #include <cyg/infra/testcase.h> |
|
|
52 #include <sys/cstartup.h> // C library initialisation |
|
|
53 |
|
|
54 |
|
|
55 // HOW TO START TESTS |
|
|
56 |
|
|
57 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_RAND) |
|
|
58 |
|
|
59 # define START_TEST( test ) test(0) |
|
|
60 |
|
|
61 #else |
|
|
62 |
|
|
63 # define START_TEST( test ) CYG_EMPTY_STATEMENT |
|
|
64 |
|
|
65 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_RAND) |
|
|
66 |
|
|
67 |
|
|
68 // CONSTANTS |
|
|
69 |
|
|
70 // Max cross correlation value of two sequences of |
|
|
71 // different seed |
|
|
72 #define X_CORREL 10 |
|
|
73 |
|
|
74 // size of arrays to use for testing |
|
|
75 #define TEST_SIZE 1024 |
|
|
76 |
|
|
77 |
|
|
78 // FUNCTIONS |
|
|
79 |
|
|
80 externC void |
|
|
81 cyg_package_start( void ) |
|
|
82 { |
|
|
83 #ifdef CYGPKG_LIBC |
|
|
84 cyg_iso_c_start(); |
|
|
85 #else |
|
|
86 (void)main(0, NULL); |
|
|
87 #endif |
|
|
88 } // cyg_package_start() |
|
|
89 |
|
|
90 |
|
|
91 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_RAND) |
|
|
92 |
|
|
93 static void |
|
|
94 test( CYG_ADDRWORD data ) |
|
|
95 { |
|
|
96 int ctr; |
|
|
97 static int array_1[TEST_SIZE]; |
|
|
98 static int array_2[TEST_SIZE]; |
|
|
99 static int array_3[TEST_SIZE]; |
|
|
100 int hits; |
|
|
101 int fail; |
|
|
102 |
|
|
103 |
|
|
104 srand(3); |
|
|
105 for (ctr=0; ctr<TEST_SIZE; ++ctr) |
|
|
106 array_1[ctr] = rand(); |
|
|
107 |
|
|
108 srand(9); |
|
|
109 for (ctr=0; ctr<TEST_SIZE; ++ctr) |
|
|
110 array_2[ctr] = rand(); |
|
|
111 |
|
|
112 srand(3); |
|
|
113 for (ctr=0; ctr<TEST_SIZE; ++ctr) |
|
|
114 array_3[ctr] = rand(); |
|
|
115 |
|
|
116 // Make sure arrays 1 and 3 are the same |
|
|
117 fail = 0; |
|
|
118 for (ctr=0; ctr<TEST_SIZE; ++ctr) |
|
|
119 { |
|
|
120 if (array_1[ctr] != array_3[ctr]) |
|
|
121 ++fail; |
|
|
122 } // for |
|
|
123 |
|
|
124 CYG_TEST_PASS_FAIL( fail == 0, "resetting the seed to the same value"); |
|
|
125 |
|
|
126 // Check sequences of different seeds are different |
|
|
127 hits = 0; |
|
|
128 for (ctr=0; ctr<TEST_SIZE; ++ctr) |
|
|
129 { |
|
|
130 if (array_1[ctr] == array_2[ctr]) |
|
|
131 ++hits; |
|
|
132 } // for |
|
|
133 |
|
|
134 CYG_TEST_PASS_FAIL(hits < X_CORREL, |
|
|
135 "random sequence for different seeds is different"); |
|
|
136 |
|
|
137 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library " |
|
|
138 "srand() function"); |
|
|
139 } // test() |
|
|
140 |
|
|
141 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_RAND) |
|
|
142 |
|
|
143 |
|
|
144 int |
|
|
145 main(int argc, char *argv[]) |
|
|
146 { |
|
|
147 CYG_TEST_INIT(); |
|
|
148 |
|
|
149 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " |
|
|
150 "srand() function"); |
|
|
151 |
|
|
152 START_TEST( test ); |
|
|
153 |
|
|
154 CYG_TEST_PASS_FINISH("Testing is not applicable to this configuration"); |
|
|
155 } // main() |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 // EOF srand.c |