File: time-core-add.c

package info (click to toggle)
dateutils 0.3.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,236 kB
  • ctags: 1,790
  • sloc: ansic: 20,002; makefile: 1,434; yacc: 200; sh: 167; lex: 105
file content (221 lines) | stat: -rw-r--r-- 4,304 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#if defined HAVE_CONFIG_H
# include "config.h"
#endif	/* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include "time-core.h"

#define CHECK_RES(rc, pred, args...)		\
	if (pred) {				\
		fprintf(stderr, args);		\
		res = rc;			\
	}

#define CHECK(pred, args...)			\
	CHECK_RES(1, pred, args)

#define CHECK_EQ(slot, val, args...)		\
	CHECK(slot != val, args, slot, val)

static int
add_chk(struct dt_t_s tes, struct dt_t_s ref)
{
	int res = 0;

	CHECK(tes.typ != ref.typ,
	      "  TYPE DIFFERS %u ... should be %u\n",
	      (unsigned int)tes.typ,
	      (unsigned int)ref.typ);

	if (!ref.dur) {
		CHECK(tes.dur, "  DURATION BIT SET\n");
	} else {
		CHECK(!tes.dur, "  DURATION BIT NOT SET\n");
	}
	if (!ref.neg) {
		CHECK(tes.neg, "  NEGATED BIT SET\n");
	} else {
		CHECK(!tes.neg, "  NEGATED BIT NOT SET\n");
	}

	if (tes.typ == DT_HMS) {
		CHECK_EQ((unsigned int)tes.hms.h, (unsigned int)ref.hms.h,
			 "  HOUR %u ... should be %u\n");
		CHECK_EQ((unsigned int)tes.hms.m, (unsigned int)ref.hms.m,
			 "  MINUTE %u ... should be %u\n");
		CHECK_EQ((unsigned int)tes.hms.s, (unsigned int)ref.hms.s,
			 "  SECOND %u ... should be %u\n");
		/* make sure the padding leaves no garbage */
		CHECK_RES(res, tes.hms.u & ~0x1f3f3f3fffffff,
			  "  PADDING NOT NAUGHT %x\n",
			  (unsigned int)(tes.hms.u & ~0x1f3f3f3fffffff));
	}

	CHECK(tes.carry != ref.carry,
	      "  CARRY DIFFERS %d ... should be %d\n",
	      (signed int)tes.carry,
	      (signed int)ref.carry);
	return res;
}

int
main(void)
{
	int rc = 0;
	struct dt_t_s t;
	struct dt_t_s dur;
	struct dt_t_s res;
	struct dt_t_s chk;

	/* 12:34:56 + 17s */
	t = dt_t_initialiser();
	t.typ = DT_HMS;
	t.hms.h = 12;
	t.hms.m = 34;
	t.hms.s = 56;

	dur = dt_t_initialiser();
	dur.sdur = 17;
	dur.dur = 1;

	/* should be 12:35:13 */
	chk = dt_t_initialiser();
	chk.typ = DT_HMS;
	chk.hms.h = 12;
	chk.hms.m = 35;
	chk.hms.s = 13;

	/* add, then check */
	if (res = dt_tadd(t, dur, 0), add_chk(res, chk)) {
		rc = 1;
	}


	/* 12:34:56 + 11*3600s + 25*60s + 4s */
	t = dt_t_initialiser();
	t.typ = DT_HMS;
	t.hms.h = 12;
	t.hms.m = 34;
	t.hms.s = 56;

	dur = dt_t_initialiser();
	dur.sdur = 11 * 3600 + 25 * 60 + 4;
	dur.dur = 1;

	/* should be 00:00:00 */
	chk = dt_t_initialiser();
	chk.typ = DT_HMS;
	chk.hms.h = 00;
	chk.hms.m = 00;
	chk.hms.s = 00;
	chk.carry = 1;

	/* add, then check */
	if (res = dt_tadd(t, dur, 0), add_chk(res, chk)) {
		rc = 1;
	}


	/* 12:34:56 + 11*3600s + 25*60s + 4s on a leap day */
	t = dt_t_initialiser();
	t.typ = DT_HMS;
	t.hms.h = 12;
	t.hms.m = 34;
	t.hms.s = 56;

	dur = dt_t_initialiser();
	dur.sdur = 11 * 3600 + 25 * 60 + 4;
	dur.dur = 1;

	/* should be 00:00:00 */
	chk = dt_t_initialiser();
	chk.typ = DT_HMS;
	chk.hms.h = 23;
	chk.hms.m = 59;
	chk.hms.s = 60;
	chk.carry = 0;

	/* add, then check */
	if (res = dt_tadd(t, dur, 1), add_chk(res, chk)) {
		rc = 1;
	}


	/* 12:34:56 + 11*3600s + 25*60s + 3s on a -leap day */
	t = dt_t_initialiser();
	t.typ = DT_HMS;
	t.hms.h = 12;
	t.hms.m = 34;
	t.hms.s = 56;

	dur = dt_t_initialiser();
	dur.sdur = 11 * 3600 + 25 * 60 + 3;
	dur.dur = 1;

	/* should be 00:00:00 */
	chk = dt_t_initialiser();
	chk.typ = DT_HMS;
	chk.hms.h = 00;
	chk.hms.m = 00;
	chk.hms.s = 00;
	chk.carry = 1;

	/* add, then check */
	if (res = dt_tadd(t, dur, -1), add_chk(res, chk)) {
		rc = 1;
	}


	/* 12:34:56 + 11*3600s + 25*60s + 2s on a -leap day */
	t = dt_t_initialiser();
	t.typ = DT_HMS;
	t.hms.h = 12;
	t.hms.m = 34;
	t.hms.s = 56;

	dur = dt_t_initialiser();
	dur.sdur = 11 * 3600 + 25 * 60 + 2;
	dur.dur = 1;

	/* should be 00:00:00 */
	chk = dt_t_initialiser();
	chk.typ = DT_HMS;
	chk.hms.h = 23;
	chk.hms.m = 59;
	chk.hms.s = 58;
	chk.carry = 0;

	/* add, then check */
	if (res = dt_tadd(t, dur, -1), add_chk(res, chk)) {
		rc = 1;
	}


	/* 12:34:56 + 11*3600s + 25*60s + 3s on a +leap day */
	t = dt_t_initialiser();
	t.typ = DT_HMS;
	t.hms.h = 12;
	t.hms.m = 34;
	t.hms.s = 56;

	dur = dt_t_initialiser();
	dur.sdur = 11 * 3600 + 25 * 60 + 3;
	dur.dur = 1;

	/* should be 00:00:00 */
	chk = dt_t_initialiser();
	chk.typ = DT_HMS;
	chk.hms.h = 23;
	chk.hms.m = 59;
	chk.hms.s = 59;
	chk.carry = 0;

	/* add, then check */
	if (res = dt_tadd(t, dur, 1), add_chk(res, chk)) {
		rc = 1;
	}
	return rc;
}

/* time-core-add.c ends here */