1 #if defined HAVE_CONFIG_H
2 # include "config.h"
3 #endif	/* HAVE_CONFIG_H */
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <inttypes.h>
7 #include "time-core.h"
8 
9 #define CHECK_RES(rc, pred, args...)		\
10 	if (pred) {				\
11 		fprintf(stderr, args);		\
12 		res = rc;			\
13 	}
14 
15 #define CHECK(pred, args...)			\
16 	CHECK_RES(1, pred, args)
17 
18 #define CHECK_EQ(slot, val, args...)		\
19 	CHECK(slot != val, args, slot, val)
20 
21 static int
add_chk(struct dt_t_s tes,struct dt_t_s ref)22 add_chk(struct dt_t_s tes, struct dt_t_s ref)
23 {
24 	int res = 0;
25 
26 	CHECK(tes.typ != ref.typ,
27 	      "  TYPE DIFFERS %u ... should be %u\n",
28 	      (unsigned int)tes.typ,
29 	      (unsigned int)ref.typ);
30 
31 	if (!ref.dur) {
32 		CHECK(tes.dur, "  DURATION BIT SET\n");
33 	} else {
34 		CHECK(!tes.dur, "  DURATION BIT NOT SET\n");
35 	}
36 	if (!ref.neg) {
37 		CHECK(tes.neg, "  NEGATED BIT SET\n");
38 	} else {
39 		CHECK(!tes.neg, "  NEGATED BIT NOT SET\n");
40 	}
41 
42 	if (tes.typ == DT_HMS) {
43 		CHECK_EQ((unsigned int)tes.hms.h, (unsigned int)ref.hms.h,
44 			 "  HOUR %u ... should be %u\n");
45 		CHECK_EQ((unsigned int)tes.hms.m, (unsigned int)ref.hms.m,
46 			 "  MINUTE %u ... should be %u\n");
47 		CHECK_EQ((unsigned int)tes.hms.s, (unsigned int)ref.hms.s,
48 			 "  SECOND %u ... should be %u\n");
49 		/* make sure the padding leaves no garbage */
50 		CHECK_RES(res, tes.hms.u & ~0x1f3f3f3fffffff,
51 			  "  PADDING NOT NAUGHT %x\n",
52 			  (unsigned int)(tes.hms.u & ~0x1f3f3f3fffffff));
53 	}
54 
55 	CHECK(tes.carry != ref.carry,
56 	      "  CARRY DIFFERS %d ... should be %d\n",
57 	      (signed int)tes.carry,
58 	      (signed int)ref.carry);
59 	return res;
60 }
61 
62 int
main(void)63 main(void)
64 {
65 	int rc = 0;
66 	struct dt_t_s t;
67 	int dur;
68 	struct dt_t_s res;
69 	struct dt_t_s chk;
70 
71 	/* 12:34:56 + 17s */
72 	t = dt_t_initialiser();
73 	t.typ = DT_HMS;
74 	t.hms.h = 12;
75 	t.hms.m = 34;
76 	t.hms.s = 56;
77 
78 	dur = 17;
79 
80 	/* should be 12:35:13 */
81 	chk = dt_t_initialiser();
82 	chk.typ = DT_HMS;
83 	chk.hms.h = 12;
84 	chk.hms.m = 35;
85 	chk.hms.s = 13;
86 
87 	/* add, then check */
88 	if (res = dt_tadd_s(t, dur, 0), add_chk(res, chk)) {
89 		rc = 1;
90 	}
91 
92 
93 	/* 12:34:56 + 11*3600s + 25*60s + 4s */
94 	t = dt_t_initialiser();
95 	t.typ = DT_HMS;
96 	t.hms.h = 12;
97 	t.hms.m = 34;
98 	t.hms.s = 56;
99 
100 	dur = 11 * 3600 + 25 * 60 + 4;
101 
102 	/* should be 00:00:00 */
103 	chk = dt_t_initialiser();
104 	chk.typ = DT_HMS;
105 	chk.hms.h = 00;
106 	chk.hms.m = 00;
107 	chk.hms.s = 00;
108 	chk.carry = 1;
109 
110 	/* add, then check */
111 	if (res = dt_tadd_s(t, dur, 0), add_chk(res, chk)) {
112 		rc = 1;
113 	}
114 
115 
116 	/* 12:34:56 + 11*3600s + 25*60s + 4s on a leap day */
117 	t = dt_t_initialiser();
118 	t.typ = DT_HMS;
119 	t.hms.h = 12;
120 	t.hms.m = 34;
121 	t.hms.s = 56;
122 
123 	dur = 11 * 3600 + 25 * 60 + 4;
124 
125 	/* should be 00:00:00 */
126 	chk = dt_t_initialiser();
127 	chk.typ = DT_HMS;
128 	chk.hms.h = 23;
129 	chk.hms.m = 59;
130 	chk.hms.s = 60;
131 	chk.carry = 0;
132 
133 	/* add, then check */
134 	if (res = dt_tadd_s(t, dur, 1), add_chk(res, chk)) {
135 		rc = 1;
136 	}
137 
138 
139 	/* 12:34:56 + 11*3600s + 25*60s + 3s on a -leap day */
140 	t = dt_t_initialiser();
141 	t.typ = DT_HMS;
142 	t.hms.h = 12;
143 	t.hms.m = 34;
144 	t.hms.s = 56;
145 
146 	dur = 11 * 3600 + 25 * 60 + 3;
147 
148 	/* should be 00:00:00 */
149 	chk = dt_t_initialiser();
150 	chk.typ = DT_HMS;
151 	chk.hms.h = 00;
152 	chk.hms.m = 00;
153 	chk.hms.s = 00;
154 	chk.carry = 1;
155 
156 	/* add, then check */
157 	if (res = dt_tadd_s(t, dur, -1), add_chk(res, chk)) {
158 		rc = 1;
159 	}
160 
161 
162 	/* 12:34:56 + 11*3600s + 25*60s + 2s on a -leap day */
163 	t = dt_t_initialiser();
164 	t.typ = DT_HMS;
165 	t.hms.h = 12;
166 	t.hms.m = 34;
167 	t.hms.s = 56;
168 
169 	dur = 11 * 3600 + 25 * 60 + 2;
170 
171 	/* should be 00:00:00 */
172 	chk = dt_t_initialiser();
173 	chk.typ = DT_HMS;
174 	chk.hms.h = 23;
175 	chk.hms.m = 59;
176 	chk.hms.s = 58;
177 	chk.carry = 0;
178 
179 	/* add, then check */
180 	if (res = dt_tadd_s(t, dur, -1), add_chk(res, chk)) {
181 		rc = 1;
182 	}
183 
184 
185 	/* 12:34:56 + 11*3600s + 25*60s + 3s on a +leap day */
186 	t = dt_t_initialiser();
187 	t.typ = DT_HMS;
188 	t.hms.h = 12;
189 	t.hms.m = 34;
190 	t.hms.s = 56;
191 
192 	dur = 11 * 3600 + 25 * 60 + 3;
193 
194 	/* should be 00:00:00 */
195 	chk = dt_t_initialiser();
196 	chk.typ = DT_HMS;
197 	chk.hms.h = 23;
198 	chk.hms.m = 59;
199 	chk.hms.s = 59;
200 	chk.carry = 0;
201 
202 	/* add, then check */
203 	if (res = dt_tadd_s(t, dur, 1), add_chk(res, chk)) {
204 		rc = 1;
205 	}
206 	return rc;
207 }
208 
209 /* time-core-add.c ends here */
210