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