blob: deeab055ac97f0f054695b6577bd8cdbf568b8a8 [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
Benny Prijonodd859a62005-11-01 16:42:51 +00002 */
3#include "test.h"
4#include <pj/os.h>
5#include <pj/log.h>
6
7
8/**
9 * \page page_pjlib_timestamp_test Test: Timestamp
10 *
11 * This file provides implementation of timestamp_test()
12 *
13 * \section timestamp_test_sec Scope of the Test
14 *
15 * This tests whether timestamp API works.
16 *
17 * API tested:
18 * - pj_get_timestamp_freq()
19 * - pj_get_timestamp()
20 * - pj_elapsed_usec()
21 * - PJ_LOG()
22 *
23 *
24 * This file is <b>pjlib-test/timestamp.c</b>
25 *
26 * \include pjlib-test/timestamp.c
27 */
28
29#if INCLUDE_TIMESTAMP_TEST
30
31#define THIS_FILE "timestamp"
32
33int timestamp_test(void)
34{
35 enum { CONSECUTIVE_LOOP = 1000 };
36 volatile unsigned i;
37 pj_timestamp freq, t1, t2;
38 unsigned elapsed;
39 pj_status_t rc;
40
41 PJ_LOG(3,(THIS_FILE, "...Testing timestamp (high res time)"));
42
43 /* Get and display timestamp frequency. */
44 if ((rc=pj_get_timestamp_freq(&freq)) != PJ_SUCCESS) {
45 app_perror("...ERROR: get timestamp freq", rc);
46 return -1000;
47 }
48
49 PJ_LOG(3,(THIS_FILE, "....frequency: hiword=%lu loword=%lu",
50 freq.u32.hi, freq.u32.lo));
51
52 PJ_LOG(3,(THIS_FILE, "...checking if time can run backwards (pls wait).."));
53
54 /*
55 * Check if consecutive readings should yield timestamp value
56 * that is bigger than previous value.
57 * First we get the first timestamp.
58 */
59 rc = pj_get_timestamp(&t1);
60 if (rc != PJ_SUCCESS) {
61 app_perror("...ERROR: get timestamp", rc);
62 return -1001;
63 }
64 for (i=0; i<CONSECUTIVE_LOOP; ++i) {
65 /*
66 volatile unsigned j;
67 for (j=0; j<1000; ++j)
68 ;
69 */
70 pj_thread_sleep(1);
71 rc = pj_get_timestamp(&t2);
72 if (rc != PJ_SUCCESS) {
73 app_perror("...ERROR: get timestamp", rc);
74 return -1002;
75 }
76 /* compare t2 with t1, expecting t2 >= t1. */
77 if (t2.u32.hi < t1.u32.hi ||
78 (t2.u32.hi == t1.u32.hi && t2.u32.lo < t1.u32.lo))
79 {
80 PJ_LOG(3,(THIS_FILE, "...ERROR: timestamp runs backwards!"));
81 return -1003;
82 }
83 }
84
85 /*
86 * Simple test to time some loop.
87 */
88 PJ_LOG(3,(THIS_FILE, "....testing simple 1000000 loop"));
89
90
91 /* Mark start time. */
92 if ((rc=pj_get_timestamp(&t1)) != PJ_SUCCESS) {
93 app_perror("....error: cat't get timestamp", rc);
94 return -1010;
95 }
96
97 /* Loop.. */
98 for (i=0; i<1000000; ++i)
99 ;
100
101 /* Mark end time. */
102 pj_get_timestamp(&t2);
103
104 /* Get elapsed time in usec. */
105 elapsed = pj_elapsed_usec(&t1, &t2);
106 PJ_LOG(3,(THIS_FILE, "....elapsed: %u usec", (unsigned)elapsed));
107
108 /* See if elapsed time is reasonable. */
109 if (elapsed < 1 || elapsed > 100000) {
110 PJ_LOG(3,(THIS_FILE, "....error: elapsed time outside window (%u, "
111 "t1.u32.hi=%u, t1.u32.lo=%u, "
112 "t2.u32.hi=%u, t2.u32.lo=%u)",
113 elapsed,
114 t1.u32.hi, t1.u32.lo, t2.u32.hi, t2.u32.lo));
115 return -1030;
116 }
117 return 0;
118}
119
120
121#else
122/* To prevent warning about "translation unit is empty"
123 * when this test is disabled.
124 */
125int dummy_timestamp_test;
126#endif /* INCLUDE_TIMESTAMP_TEST */
127