UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
test_tv.c
Go to the documentation of this file.
1/*
2 * FILE: test_tv.c
3 * AUTHORS: Colin Perkins <csp@csperkins.org>
4 *
5 * Copyright (c) 2003-2004 University of Glasgow
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, is permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the Computer Science
19 * Department at University College London
20 * 4. Neither the name of the University nor of the Department may be used
21 * to endorse or promote products derived from this software without
22 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include "config.h"
37#include "config_unix.h"
38#include "config_win32.h"
39#include "tv.h"
40#include "test_tv.h"
41
42int test_tv(void)
43{
44 struct timeval t1, t2;
45 double d;
46 double e = 0.0000001; /* epsilon for double comparisons. this is evil. */
47 uint32_t m1, m2, m3;
48
49 printf
50 ("Testing time conversion functions ........................................ ");
51
52 /* Test 1: check that tv_add() works... */
53 /* 1A) Normal operation */
54 t1.tv_sec = 1;
55 t1.tv_usec = 0;
56 tv_add(&t1, 0.8);
57 if ((t1.tv_sec != 1) && (t1.tv_usec != 800000)) {
58 printf("FAIL\n");
59 printf(" tv_add() part A\n");
60 return -1;
61 }
62 /* 1B) Wraparound of tv_usec during the addition */
63 tv_add(&t1, 0.5);
64 if ((t1.tv_sec != 2) && (t1.tv_usec != 300000)) {
65 printf("FAIL\n");
66 printf(" tv_add() part B\n");
67 return -1;
68 }
69 /* 1C) Wraparound when the offset is greater than one second */
70 tv_add(&t1, 4.2);
71 if ((t1.tv_sec != 6) && (t1.tv_usec != 500000)) {
72 printf("FAIL\n");
73 printf(" tv_add() part C\n");
74 return -1;
75 }
76
77 /* Test 2: check that tv_gt() works... */
78 /* 2A) Normal operation */
79 t1.tv_sec = 8143;
80 t1.tv_usec = 500000;
81 t2.tv_sec = 4294;
82 t2.tv_usec = 345678;
83 if (tv_gt(t2, t1) || !tv_gt(t1, t2)) {
84 printf("FAIL\n");
85 printf(" tv_gt() part A\n");
86 return -1;
87 }
88
89 /* 2B) See if it works when tv_sec values are the same */
90 t1.tv_sec = 8147;
91 t1.tv_usec = 500000;
92 t2.tv_sec = 8147;
93 t2.tv_usec = 345678;
94 if (tv_gt(t2, t1) || !tv_gt(t1, t2)) {
95 printf("FAIL\n");
96 printf(" tv_gt() part B\n");
97 return -1;
98 }
99
100 /* Test 3: check that tv_diff() works... */
101 /* 3A) normal operation. comparing floats is tricky :( */
102 t1.tv_sec = 1065356371; /* Sunday afternoon, 5th October 2003 */
103 t1.tv_usec = 234947;
104 t2.tv_sec = 1065356528; /* ...a few minutes later, see how */
105 t2.tv_usec = 864316; /* fast I type :) */
106 d = tv_diff(t2, t1);
107 if ((d <= 157.629369 - e) || (d >= 157.629369 + e)) {
108 printf("FAIL\n");
109 printf(" tv_diff: %f != 157.629369\n", d);
110 return -1;
111 }
112 /* 3B) when prev_time is newer than curr_time? Not what we */
113 /* expect to work, but may as well make sure it gives a */
114 /* sensible answer, rather than crashing out. */
115 d = tv_diff(t1, t2);
116 if ((d <= -157.629369 - e) || (d >= -157.629369 + e)) {
117 printf("FAIL\n");
118 printf(" tv_diff: %f != -157.629369\n", d);
119 return -1;
120 }
121
122 /* Test 4: check that tv_diff_usec() works... */
123 /* 4A) normal operation */
124 t1.tv_sec = 1065356371;
125 t1.tv_usec = 234947;
126 t2.tv_sec = 1065356528;
127 t2.tv_usec = 864316;
128 if (tv_diff_usec(t2, t1) != 157629369) {
129 printf("FAIL\n");
130 printf(" tv_diff_usec: A\n");
131 return -1;
132 }
133 /* 4B) see what happens if the tv_sec values are the same */
134 t1.tv_sec = 1065356371;
135 t1.tv_usec = 234947;
136 t2.tv_sec = 1065356371;
137 t2.tv_usec = 864316;
138 if (tv_diff_usec(t2, t1) != 629369) {
139 printf("FAIL\n");
140 printf(" tv_diff_usec: B\n");
141 return -1;
142 }
143 /* 4C) close values on a second boundary */
144 t1.tv_sec = 1065356371;
145 t1.tv_usec = 999999;
146 t2.tv_sec = 1065356372;
147 t2.tv_usec = 000000;
148 if (tv_diff_usec(t2, t1) != 1) {
149 printf("FAIL\n");
150 printf(" tv_diff_usec: B\n");
151 return -1;
152 }
153
154 /* Test 5: check that get_local_mediatime() works... */
155 m1 = get_local_mediatime();
156 usleep(250000); /* Sleep for a minimum of 250ms */
157 m2 = get_local_mediatime();
158 if (m2 - m1 < 22500) {
159 printf("FAIL\n");
160 printf(" get_local_mediatime() runs fast\n");
161 return -1;
162 }
163 m3 = get_local_mediatime();
164 if (m3 < m2) {
165 printf("FAIL\n");
166 printf(" get_local_mediatime() time runs backwards\n");
167 return -1;
168 }
169
170 printf("Ok\n");
171 return 0;
172}
int test_tv(void)
Definition test_tv.c:42
uint32_t tv_diff_usec(struct timeval curr_time, struct timeval prev_time)
Definition tv.c:109
void tv_add(struct timeval *ts, double offset_secs)
Definition tv.c:130
int tv_gt(struct timeval a, struct timeval b)
Definition tv.c:153
double tv_diff(struct timeval curr_time, struct timeval prev_time)
Definition tv.c:97
uint32_t get_local_mediatime(void)
Definition tv.c:65
int usleep(unsigned int usec)
Definition usleep.c:47