Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 1 | /* $Id$ */
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 2 | /*
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 4 | *
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or modify
|
| 6 | * it under the terms of the GNU General Public License as published by
|
| 7 | * the Free Software Foundation; either version 2 of the License, or
|
| 8 | * (at your option) any later version.
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 9 | *
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 10 | * This program is distributed in the hope that it will be useful,
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 13 | * GNU General Public License for more details.
|
| 14 | *
|
| 15 | * You should have received a copy of the GNU General Public License
|
| 16 | * along with this program; if not, write to the Free Software
|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 18 | */
|
| 19 | #include "test.h"
|
| 20 |
|
| 21 | /**
|
| 22 | * \page page_pjlib_sleep_test Test: Sleep, Time, and Timestamp
|
| 23 | *
|
| 24 | * This file provides implementation of \b sleep_test().
|
| 25 | *
|
| 26 | * \section sleep_test_sec Scope of the Test
|
| 27 | *
|
| 28 | * This tests:
|
| 29 | * - whether pj_thread_sleep() works.
|
| 30 | * - whether pj_gettimeofday() works.
|
| 31 | * - whether pj_get_timestamp() and friends works.
|
| 32 | *
|
| 33 | * API tested:
|
| 34 | * - pj_thread_sleep()
|
| 35 | * - pj_gettimeofday()
|
| 36 | * - PJ_TIME_VAL_SUB()
|
| 37 | * - PJ_TIME_VAL_LTE()
|
| 38 | * - pj_get_timestamp()
|
| 39 | * - pj_get_timestamp_freq() (implicitly)
|
| 40 | * - pj_elapsed_time()
|
| 41 | * - pj_elapsed_usec()
|
| 42 | *
|
| 43 | *
|
| 44 | * This file is <b>pjlib-test/sleep.c</b>
|
| 45 | *
|
| 46 | * \include pjlib-test/sleep.c
|
| 47 | */
|
| 48 |
|
| 49 | #if INCLUDE_SLEEP_TEST
|
| 50 |
|
| 51 | #include <pjlib.h>
|
| 52 |
|
| 53 | #define THIS_FILE "sleep_test"
|
| 54 |
|
| 55 | static int simple_sleep_test(void)
|
| 56 | {
|
| 57 | enum { COUNT = 5 };
|
| 58 | int i;
|
| 59 | pj_status_t rc;
|
| 60 |
|
| 61 | PJ_LOG(3,(THIS_FILE, "..will write messages every 1 second:"));
|
| 62 |
|
| 63 | for (i=0; i<COUNT; ++i) {
|
| 64 | rc = pj_thread_sleep(1000);
|
| 65 | if (rc != PJ_SUCCESS) {
|
| 66 | app_perror("...error: pj_thread_sleep()", rc);
|
| 67 | return -10;
|
| 68 | }
|
| 69 | PJ_LOG(3,(THIS_FILE, "...wake up.."));
|
| 70 | }
|
| 71 |
|
| 72 | return 0;
|
| 73 | }
|
| 74 |
|
| 75 | static int sleep_duration_test(void)
|
| 76 | {
|
| 77 | enum { MIS = 20, DURATION = 1000, DURATION2 = 500 };
|
| 78 | pj_status_t rc;
|
| 79 |
|
| 80 | PJ_LOG(3,(THIS_FILE, "..running sleep duration test"));
|
| 81 |
|
| 82 | /* Test pj_thread_sleep() and pj_gettimeofday() */
|
| 83 | {
|
| 84 | pj_time_val start, stop;
|
| 85 | pj_uint32_t msec;
|
| 86 |
|
| 87 | /* Mark start of test. */
|
| 88 | rc = pj_gettimeofday(&start);
|
| 89 | if (rc != PJ_SUCCESS) {
|
| 90 | app_perror("...error: pj_gettimeofday()", rc);
|
| 91 | return -10;
|
| 92 | }
|
| 93 |
|
| 94 | /* Sleep */
|
| 95 | rc = pj_thread_sleep(DURATION);
|
| 96 | if (rc != PJ_SUCCESS) {
|
| 97 | app_perror("...error: pj_thread_sleep()", rc);
|
| 98 | return -20;
|
| 99 | }
|
| 100 |
|
| 101 | /* Mark end of test. */
|
| 102 | rc = pj_gettimeofday(&stop);
|
| 103 |
|
| 104 | /* Calculate duration (store in stop). */
|
| 105 | PJ_TIME_VAL_SUB(stop, start);
|
| 106 |
|
| 107 | /* Convert to msec. */
|
| 108 | msec = PJ_TIME_VAL_MSEC(stop);
|
| 109 |
|
| 110 | /* Check if it's within range. */
|
| 111 | if (msec < DURATION * (100-MIS)/100 ||
|
| 112 | msec > DURATION * (100+MIS)/100)
|
| 113 | {
|
| 114 | PJ_LOG(3,(THIS_FILE,
|
| 115 | "...error: slept for %d ms instead of %d ms "
|
| 116 | "(outside %d%% err window)",
|
| 117 | msec, DURATION, MIS));
|
| 118 | return -30;
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 |
|
| 123 | /* Test pj_thread_sleep() and pj_get_timestamp() and friends */
|
| 124 | {
|
| 125 | pj_time_val t1, t2;
|
| 126 | pj_timestamp start, stop;
|
| 127 | pj_time_val elapsed;
|
| 128 | pj_uint32_t msec;
|
| 129 |
|
| 130 | /* Mark start of test. */
|
| 131 | rc = pj_get_timestamp(&start);
|
| 132 | if (rc != PJ_SUCCESS) {
|
| 133 | app_perror("...error: pj_get_timestamp()", rc);
|
| 134 | return -60;
|
| 135 | }
|
| 136 |
|
| 137 | /* ..also with gettimeofday() */
|
| 138 | pj_gettimeofday(&t1);
|
| 139 |
|
| 140 | /* Sleep */
|
| 141 | rc = pj_thread_sleep(DURATION2);
|
| 142 | if (rc != PJ_SUCCESS) {
|
| 143 | app_perror("...error: pj_thread_sleep()", rc);
|
| 144 | return -70;
|
| 145 | }
|
| 146 |
|
| 147 | /* Mark end of test. */
|
| 148 | pj_get_timestamp(&stop);
|
| 149 |
|
| 150 | /* ..also with gettimeofday() */
|
| 151 | pj_gettimeofday(&t2);
|
| 152 |
|
| 153 | /* Compare t1 and t2. */
|
| 154 | if (PJ_TIME_VAL_LTE(t2, t1)) {
|
| 155 | PJ_LOG(3,(THIS_FILE, "...error: t2 is less than t1!!"));
|
| 156 | return -75;
|
| 157 | }
|
| 158 |
|
| 159 | /* Get elapsed time in time_val */
|
| 160 | elapsed = pj_elapsed_time(&start, &stop);
|
| 161 |
|
| 162 | msec = PJ_TIME_VAL_MSEC(elapsed);
|
| 163 |
|
| 164 | /* Check if it's within range. */
|
| 165 | if (msec < DURATION2 * (100-MIS)/100 ||
|
| 166 | msec > DURATION2 * (100+MIS)/100)
|
| 167 | {
|
| 168 | PJ_LOG(3,(THIS_FILE,
|
| 169 | "...error: slept for %d ms instead of %d ms "
|
| 170 | "(outside %d%% err window)",
|
| 171 | msec, DURATION2, MIS));
|
| 172 | return -30;
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | /* All done. */
|
| 177 | return 0;
|
| 178 | }
|
| 179 |
|
| 180 | int sleep_test()
|
| 181 | {
|
| 182 | int rc;
|
| 183 |
|
| 184 | rc = simple_sleep_test();
|
| 185 | if (rc != PJ_SUCCESS)
|
| 186 | return rc;
|
| 187 |
|
| 188 | rc = sleep_duration_test();
|
| 189 | if (rc != PJ_SUCCESS)
|
| 190 | return rc;
|
| 191 |
|
| 192 | return 0;
|
| 193 | }
|
| 194 |
|
| 195 | #else
|
| 196 | /* To prevent warning about "translation unit is empty"
|
| 197 | * when this test is disabled.
|
| 198 | */
|
| 199 | int dummy_sleep_test;
|
| 200 | #endif /* INCLUDE_SLEEP_TEST */
|