comparison packages/language/c/libc/time/current/tests/clock.c @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children 0ae0bc38e387
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //=================================================================
2 //
3 // clock.c
4 //
5 // Testcase for C library clock()
6 //
7 //=================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (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://www.redhat.com/
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 Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //=================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): ctarpy, jlarmour
35 // Contributors:
36 // Date: 1999-03-05
37 // Description: Contains testcode for C library clock() function
38 //
39 //
40 //####DESCRIPTIONEND####
41
42 // CONFIGURATION
43
44 #include <pkgconf/libc_time.h> // Configuration header
45
46 // INCLUDES
47
48 #include <time.h>
49 #include <cyg/infra/testcase.h>
50
51 #define RUN_TEST 1
52
53 // This test is bound to fail on Linux -- we don't have exclusive access
54 // to the CPU.
55 #if defined(CYGPKG_HAL_I386_LINUX)
56 #undef RUN_TEST
57 #define RUN_TEST 0
58 #endif
59
60
61 // HOW TO START TESTS
62
63 #if RUN_TEST
64
65 #include <cyg/hal/hal_cache.h>
66 #include <cyg/hal/hal_intr.h>
67
68 # define START_TEST( test ) test(0)
69
70 #else
71
72 # define START_TEST( test ) CYG_EMPTY_STATEMENT
73
74 #endif
75
76
77 // CONSTANTS
78
79 // This defines how many loops before we decide that
80 // the clock doesnt work
81 #define MAX_TIMEOUT 1000000
82
83 // Percentage error before we declare fail: range 0 - 100
84 #define TOLERANCE 40
85
86
87 // FUNCTIONS
88
89 #if RUN_TEST
90
91 static int
92 my_abs(int i)
93 {
94 return (i < 0) ? -i : i;
95 } // my_abs()
96
97
98 static void
99 test( CYG_ADDRWORD data )
100 {
101 unsigned long ctr, ctr2, err;
102 clock_t clock_init;
103 clock_t clock_first=0, clock_second=0, clock_third=0;
104
105 // First disable the caches - they may affect the timing loops
106 // below - especially if tracing or assertions are enabled, causing
107 // the elapsed time during the clock() call to vary.
108 {
109 register CYG_INTERRUPT_STATE oldints;
110
111 HAL_DISABLE_INTERRUPTS(oldints);
112 HAL_DCACHE_SYNC();
113 HAL_ICACHE_DISABLE();
114 HAL_DCACHE_DISABLE();
115 HAL_DCACHE_SYNC();
116 HAL_ICACHE_INVALIDATE_ALL();
117 HAL_DCACHE_INVALIDATE_ALL();
118 HAL_RESTORE_INTERRUPTS(oldints);
119 }
120
121 // This waits for a clock tick, to ensure that we are at the
122 // start of a clock period. Then sit in a tight loop to get
123 // the clock period. Repeat this, and make sure that it the
124 // two timed periods are acceptably close.
125
126 err = 101;
127 clock_init = clock();
128
129 if (clock_init == (clock_t)-1) // unimplemented is just as valid
130 {
131 CYG_TEST_PASS_FINISH( "clock() returns -1, meaning unimplemented");
132 } // if
133
134 for (ctr = 0; ctr<MAX_TIMEOUT; ctr++)
135 {
136 if ((clock_first=clock()) > clock_init)
137 break; // Hit the next clock pulse
138 }
139 if (ctr < MAX_TIMEOUT)
140 {
141 // We hit a clock pulse, rather than a timeout
142 // Now we are at the beginning of a pulse, so time until the
143 // next one in order to get a measure of the length.
144 for (ctr = 0; ctr<MAX_TIMEOUT; ctr++)
145 {
146 if ((clock_second=clock()) > clock_first)
147 break; // Hit the next clock pulse
148 } // for
149 if (ctr < MAX_TIMEOUT)
150 {
151 // We hit a clock pulse, rather than a timeout. ctr contains
152 // the number of loops/tick. Measure this again, and see if
153 // the two results are acceptably close.
154 for (ctr2 = 0; ctr2<MAX_TIMEOUT; ctr2++)
155 {
156 if ((clock_third=clock()) > clock_second)
157 break; // Hit the next clock pulse
158 } // for
159 if (ctr2 < MAX_TIMEOUT)
160 {
161 err = (100 * my_abs(ctr-ctr2)) / ctr;
162 } // if (ctr < MAX_TIMEOUT)
163 else
164 {
165 CYG_TEST_FAIL_FINISH("No change in clock state!");
166 } // else (ctr < MAX_TIMEOUT)
167 } // if (ctr < MAX_TIMEOUT)
168 else
169 {
170 CYG_TEST_FAIL_FINISH("No change in clock state!");
171 } // else (ctr < MAX_TIMEOUT)
172
173 } // if (ctr < MAX_TIMEOUT)
174 else
175 {
176 CYG_TEST_FAIL_FINISH("No change in clock state!");
177 } // else (ctr < MAX_TIMEOUT)
178
179 CYG_TEST_PASS_FAIL(err < TOLERANCE, "clock() stability");
180
181 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
182 "clock() function");
183 } // test()
184 #endif
185
186 int
187 main(int argc, char *argv[])
188 {
189 CYG_TEST_INIT();
190
191 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
192 "clock() function");
193
194 START_TEST( test );
195
196 CYG_TEST_NA("Testing is not applicable to this configuration");
197
198 } // main()
199
200 // EOF clock.c