aboutsummaryrefslogtreecommitdiffstats
path: root/sumtimeval.c
blob: 47ef7056e59de6d67f7a1cfaf00adc09e2e5e080 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "libsimple.h"
#ifndef TEST


int
libsimple_sumtimeval(struct timeval *sum, const struct timeval *augend, const struct timeval *addend)
{
	struct timespec a, b, s;
	int r;
	libsimple_timeval2timespec(&a, augend);
	libsimple_timeval2timespec(&b, addend);
	r = libsimple_sumtimespec(&s, &a, &b);
	if (r && errno != ERANGE)
		return r;
	if (libsimple_timespec2timeval(sum, &s) && r)
		errno = ERANGE;
	return r;
}


#else
#include "test.h"

int
main(void)
{
	struct timeval r, a, b;

	a.tv_sec = 0, a.tv_usec = 0;
	b.tv_sec = 0, b.tv_usec = 0;
	assert(!libsimple_sumtimeval(&r, &a, &b));
	assert(r.tv_sec  == 0);
	assert(r.tv_usec == 0);

	a.tv_sec = 10, a.tv_usec = 10000L;
	b.tv_sec =  0, b.tv_usec =     0L;
	assert(!libsimple_sumtimeval(&r, &a, &b));
	assert(r.tv_sec  == 10);
	assert(r.tv_usec == 10000L);

	a.tv_sec =  10, a.tv_usec = 10000L;
	b.tv_sec = -10, b.tv_usec =     0L;
	assert(!libsimple_sumtimeval(&r, &a, &b));
	assert(r.tv_sec  == 0);
	assert(r.tv_usec == 10000L);

	a.tv_sec =  10, a.tv_usec = 10000L;
	b.tv_sec = -20, b.tv_usec = 20000L;
	assert(!libsimple_sumtimeval(&r, &a, &b));
	assert(r.tv_sec  == -10);
	assert(r.tv_usec == 30000L);

	a.tv_sec = 1, a.tv_usec = 999999L;
	b.tv_sec = 1, b.tv_usec =      1L;
	assert(!libsimple_sumtimeval(&r, &a, &b));
	assert(r.tv_sec  == 3);
	assert(r.tv_usec == 0L);

	a.tv_sec = TIME_MAX, a.tv_usec = 0L;
	b.tv_sec = TIME_MAX, b.tv_usec = 0L;
	assert(libsimple_sumtimeval(&r, &a, &b) == -1);
	assert(r.tv_sec  == TIME_MAX);
	assert(r.tv_usec == 999999L);

	a.tv_sec = TIME_MAX, a.tv_usec = 999999L;
	b.tv_sec =        0, b.tv_usec =      1L;
	assert(libsimple_sumtimeval(&r, &a, &b) == -1);
	assert(r.tv_sec  == TIME_MAX);
	assert(r.tv_usec == 999999L);

	a.tv_sec = TIME_MAX, a.tv_usec =      1L;
	b.tv_sec =        0, b.tv_usec = 999999L;
	assert(libsimple_sumtimeval(&r, &a, &b) == -1);
	assert(r.tv_sec  == TIME_MAX);
	assert(r.tv_usec == 999999L);

	a.tv_sec = TIME_MIN, a.tv_usec = 0L;
	b.tv_sec = TIME_MIN, b.tv_usec = 0L;
	assert(libsimple_sumtimeval(&r, &a, &b) == -1);
	assert(r.tv_sec  == TIME_MIN);
	assert(r.tv_usec == 0L);

	a.tv_sec = TIME_MIN, a.tv_usec = 100L;
	b.tv_sec = TIME_MIN, b.tv_usec = 100L;
	assert(libsimple_sumtimeval(&r, &a, &b) == -1);
	assert(r.tv_sec  == TIME_MIN);
	assert(r.tv_usec == 0L);

	return 0;
}

#endif