summaryrefslogtreecommitdiffstats
path: root/test/timer.c
blob: e992cbaa7345a8d2ad0e33e82387841a383c5d50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Purpose:     Tests the operation of the platform-independent timers.
 */

#include "h5test.h"

/*-------------------------------------------------------------------------
 * Function:    test_time_formatting
 *
 * Purpose:     Tests time string creation.
 *
 * Return:      Success:        0
 *              Failure:        -1
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_time_formatting(void)
{
    char *s = NULL;

    TESTING("Time string formats");

    /*      < 0,            N/A             */
    s = H5_timer_get_time_string(-1.0);
    if (NULL == s || strcmp(s, "N/A") != 0)
        TEST_ERROR;
    free(s);

    /*      0               0               */
    s = H5_timer_get_time_string(0.0);
    if (NULL == s || strcmp(s, "0.0 s") != 0)
        TEST_ERROR;
    free(s);

    /*      < 1 us          nanoseconds     */
    s = H5_timer_get_time_string(123.0E-9);
    if (NULL == s || strcmp(s, "123 ns") != 0)
        TEST_ERROR;
    free(s);

    /*      < 1 ms          microseconds    */
    s = H5_timer_get_time_string(23.456E-6);
    if (NULL == s || strcmp(s, "23.5 us") != 0)
        TEST_ERROR;
    free(s);

    /*      < 1 s           milliseconds    */
    s = H5_timer_get_time_string(4.56789E-3);
    if (NULL == s || strcmp(s, "4.6 ms") != 0)
        TEST_ERROR;
    free(s);

    /*      < 1 min         seconds         */
    s = H5_timer_get_time_string(3.14);
    if (NULL == s || strcmp(s, "3.14 s") != 0)
        TEST_ERROR;
    free(s);

    /*      < 1 hr          mins, secs      */
    s = H5_timer_get_time_string(2521.0);
    if (NULL == s || strcmp(s, "42 m 1 s") != 0)
        TEST_ERROR;
    free(s);

    /*      < 1 d           hrs, mins, secs */
    s = H5_timer_get_time_string(9756.0);
    if (NULL == s || strcmp(s, "2 h 42 m 36 s") != 0)
        TEST_ERROR;
    free(s);

    /*      > 1 d            days, hrs, mins, secs */
    s = H5_timer_get_time_string(280802.0);
    if (NULL == s || strcmp(s, "3 d 6 h 0 m 2 s") != 0)
        TEST_ERROR;
    free(s);

    PASSED();
    return 0;

error:
    if (s)
        free(s);
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:    test_timer_system_user
 *
 * Purpose:     Tests the ability to get system and user times from the
 *              timers.
 *              Some platforms may require special code to get system and
 *              user times.  If we do not support that particular platform
 *              dependent functionality, this test is skipped.
 *
 * Return:      Success:        0
 *              Failure:        -1
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_timer_system_user(void)
{
    int           i;
    char         *buf = NULL;
    H5_timer_t    timer;
    H5_timevals_t times;
    herr_t        err;

    TESTING("system/user times");

    err = H5_timer_init(&timer);
    if (err < 0)
        TEST_ERROR;

    err = H5_timer_start(&timer);
    if (err < 0)
        TEST_ERROR;

    /* The system and user times may not be present on some systems.  They
     * will be -1.0 if they are not.
     */
    if (timer.initial.system < 0.0 || timer.initial.user < 0.0) {
        SKIPPED();
        printf("NOTE: No suitable way to get system/user times on this platform.\n");
        return 0;
    }

    /* Do some fake work */
    for (i = 0; i < 1024; i++) {
        buf = (char *)malloc(1024 * (size_t)i);
        free(buf);
    }

    err = H5_timer_stop(&timer);
    if (err < 0)
        TEST_ERROR;

    err = H5_timer_get_times(timer, &times);
    if (err < 0)
        TEST_ERROR;

    /* System and user times should be non-negative. */
    if (times.system < 0.0 || times.user < 0.0)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:    test_timer_elapsed
 *
 * Purpose:     Tests the ability to get elapsed times from the timers.
 *              We should always be able to get an elapsed time,
 *              regardless of the time libraries or platform.
 *
 * Return:      Success:        0
 *              Failure:        -1
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_timer_elapsed(void)
{
    int           i;
    char         *buf = NULL;
    H5_timer_t    timer;
    H5_timevals_t times;
    herr_t        err;

    TESTING("elapsed times");

    err = H5_timer_init(&timer);
    if (err < 0)
        TEST_ERROR;

    err = H5_timer_start(&timer);
    if (err < 0)
        TEST_ERROR;

    /* Do some fake work */
    for (i = 0; i < 1024; i++) {
        buf = (char *)malloc(1024 * (size_t)i);
        free(buf);
    }

    err = H5_timer_stop(&timer);
    if (err < 0)
        TEST_ERROR;

    err = H5_timer_get_times(timer, &times);
    if (err < 0)
        TEST_ERROR;

    /* Elapsed time should be non-negative. */
    if (times.elapsed < 0.0)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
}

static herr_t
test_timer_functionality(void)
{
    int        i;
    char      *buf = NULL;
    H5_timer_t timer;

    H5_timevals_t times;
    double        prev_etime;
    double        prev_total_etime;

    herr_t err;

    TESTING("timer functionality");

    /*****************
     * CHECK STARTUP *
     *****************/

    /* Timer should be running after start */
    err = H5_timer_init(&timer);
    if (err < 0 || timer.is_running)
        TEST_ERROR;

    /* Times should be initialized to zero */
    err = H5_timer_get_times(timer, &times);
    if (err < 0 || !H5_DBL_ABS_EQUAL(times.elapsed, 0.0))
        TEST_ERROR;

    err = H5_timer_get_total_times(timer, &times);
    if (err < 0 || !H5_DBL_ABS_EQUAL(times.elapsed, 0.0))
        TEST_ERROR;

    /********************
     * CHECK START/STOP *
     ********************/

    /* Running state should change after start */
    err = H5_timer_start(&timer);
    if (err < 0 || !timer.is_running)
        TEST_ERROR;

    /* Do some fake work */
    for (i = 0; i < 1024; i++) {
        buf = (char *)malloc(1024 * (size_t)i);
        free(buf);
    }

    /* Running state should change after stop */
    err = H5_timer_stop(&timer);
    if (err < 0 || timer.is_running)
        TEST_ERROR;

    /* Times should be positive and non-negative */
    err = H5_timer_get_times(timer, &times);
    if (err < 0 || times.elapsed < 0.0)
        TEST_ERROR;

    err = H5_timer_get_total_times(timer, &times);
    if (err < 0 || times.elapsed < 0.0)
        TEST_ERROR;

    /**********************
     * CHECK INTERRUPTING *
     **********************/

    /* Timer should change stat and refresh to 0s */
    err = H5_timer_init(&timer);
    if (err < 0 || timer.is_running)
        TEST_ERROR;

    err = H5_timer_get_times(timer, &times);
    if (err < 0 || !H5_DBL_ABS_EQUAL(times.elapsed, 0.0))
        TEST_ERROR;

    err = H5_timer_get_total_times(timer, &times);
    if (err < 0 || !H5_DBL_ABS_EQUAL(times.elapsed, 0.0))
        TEST_ERROR;

    /* Timer state should flip */
    err = H5_timer_start(&timer);
    if (err < 0 || !timer.is_running)
        TEST_ERROR;

    /* Do some fake work */
    for (i = 0; i < 1024; i++) {
        buf = (char *)malloc(1024 * (size_t)i);
        free(buf);
    }

    /* Times should be non-negative */
    err = H5_timer_get_times(timer, &times);
    if (err < 0 || times.elapsed < 0.0)
        TEST_ERROR;
    prev_etime = times.elapsed;

    err = H5_timer_get_total_times(timer, &times);
    if (err < 0 || times.elapsed < 0.0)
        TEST_ERROR;
    prev_total_etime = times.elapsed;

    /* Do some fake work */
    for (i = 0; i < 1024; i++) {
        buf = (char *)malloc(1024 * (size_t)i);
        free(buf);
    }

    /* State should flip on stop */
    err = H5_timer_stop(&timer);
    if (err < 0 || timer.is_running)
        TEST_ERROR;

    /* Times should be >= than the cached intermediate times */
    err = H5_timer_get_times(timer, &times);
    if (err < 0 || times.elapsed < prev_etime)
        TEST_ERROR;

    err = H5_timer_get_total_times(timer, &times);
    if (err < 0 || times.elapsed < prev_total_etime)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
}

/*-------------------------------------------------------------------------
 * Function:    main
 *
 * Purpose:     Tests the basic functionality of the platform-independent
 *              timers
 *
 * Return:      Success:        0
 *              Failure:        1
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
    int nerrors = 0;

    h5_reset();

    printf("Testing platform-independent timer functionality.\n");

    nerrors += test_time_formatting() < 0 ? 1 : 0;
    nerrors += test_timer_system_user() < 0 ? 1 : 0;
    nerrors += test_timer_elapsed() < 0 ? 1 : 0;
    nerrors += test_timer_functionality() < 0 ? 1 : 0;

    if (nerrors) {
        printf("***** %d platform-independent timer TEST%s FAILED! *****\n", nerrors, nerrors > 1 ? "S" : "");
        return 1;
    }
    else {
        printf("All platform-independent timer tests passed.\n");
        return 0;
    }
}