post release commit
[hkl.git] / tests / hkl-interval-t.c
blobfe405f45b117253d2fb53927d043d6b9b1f9201f
1 /* This file is part of the hkl library.
3 * The hkl library is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * The hkl library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2003-2016 Synchrotron SOLEIL
17 * L'Orme des Merisiers Saint-Aubin
18 * BP 48 91192 GIF-sur-YVETTE CEDEX
20 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
23 #define _GNU_SOURCE
24 #include "hkl.h"
25 #include <tap/basic.h>
26 #include <tap/float.h>
28 #include "hkl-interval-private.h"
30 static void cmp(void)
32 HklInterval interval_ref = {-1, 1};
33 HklInterval interval;
35 interval = interval_ref;
36 ok(FALSE == hkl_interval_cmp(&interval_ref, &interval), __func__);
39 static void plus_interval(void)
41 HklInterval i_ref = {-2, 8};
42 HklInterval i1 = {-1, 4};
43 HklInterval i2 = {-1, 4};
45 hkl_interval_plus_interval(&i1, &i2);
46 ok(FALSE == hkl_interval_cmp(&i_ref, &i1), __func__);
49 static void plus_double(void)
51 HklInterval i_ref = {-1, 9};
52 HklInterval i1 = {-2, 8};
54 hkl_interval_plus_double(&i1, 1);
55 ok(FALSE == hkl_interval_cmp(&i_ref, &i1), __func__);
58 static void times_interval(void)
60 HklInterval i_ref = {-9, 36};
61 HklInterval i1 = {-1, 9};
62 HklInterval i2 = {-1, 4};
64 hkl_interval_times_interval(&i1, &i2);
65 ok(FALSE == hkl_interval_cmp(&i_ref, &i1), __func__);
68 static void times_double(void)
70 HklInterval i_ref = {-108, 27};
71 HklInterval i1 = {-9, 36};
73 hkl_interval_times_double(&i1, -3);
74 ok(FALSE == hkl_interval_cmp(&i_ref, &i1), __func__);
77 static void divides_double(void)
79 HklInterval i_ref = {-9, 36};
80 HklInterval i1 = {-108, 27};
82 hkl_interval_divides_double(&i1, -3);
83 ok(FALSE == hkl_interval_cmp(&i_ref, &i1), __func__);
86 static void contain_zero(void)
88 HklInterval i1 = {-9, 36};
89 HklInterval i2 = {-108, -27};
91 ok(TRUE == hkl_interval_contain_zero(&i1), __func__);
92 ok(FALSE == hkl_interval_contain_zero(&i2), __func__);
95 static void cosinus(void)
97 /* we will test all 16 cases. */
98 /* we devide the trigonometric circle in 4 from quaters */
99 /* 0 [0, pi/2[ */
100 /* 1 [pi/2, pi[ */
101 /* 2 [pi, 3*pi/2[ */
102 /* 3 [3*pi/2, 2*pi[ */
103 HklInterval i_ref;
104 HklInterval i;
105 double min;
106 double max;
108 #define COS(a, b, min_ref, max_ref) do { \
109 i.min = min = a * HKL_DEGTORAD; \
110 i.max = max = b * HKL_DEGTORAD; \
111 i_ref.min = min_ref; \
112 i_ref.max = max_ref; \
113 hkl_interval_cos(&i); \
114 ok(FALSE == hkl_interval_cmp(&i_ref, &i), __func__); \
115 } while(0)
117 /* 1st max(0) */
118 /* min(0) */
119 COS(10, 14, cos(max), cos(min));
120 /* min(3) */
121 COS(-15, 14,cos(min), 1);
122 /* min(2) */
123 COS(-95, 14, cos(min), 1);
124 /* min(1) */
125 COS(-215, 14, -1, 1);
127 /* 2nd max(1) */
128 /* min(0) */
129 COS(10, 100, cos(max), cos(min));
130 /* min(3) */
131 COS(-20, 100, cos(max), 1);
132 /* min(2) */
133 COS(-110, 100, cos(min), 1);
134 COS(-95, 100, cos(max), 1);
135 /* min(1) */
136 COS(-190, 100,-1, 1);
138 /* 3rd max(2) */
139 /* min(0) */
140 COS(10, 190, -1, cos(min));
141 /* min(3) */
142 COS(95, 190, -1, cos(min));
143 COS(175, 190, -1, cos(max));
144 /* min(2) */
145 COS(185, 190, cos(min), cos(max));
146 COS(-95, 190, -1, 1);
147 /* min(1) */
148 COS(-45, 190, -1, 1);
150 /* 4th max(3) */
151 /* min(0) */
152 COS(-350, -30, -1, cos(min));
153 COS(-310, -30, -1, cos(max));
154 /* min(3) */
155 COS(-40, -30, cos(min), cos(max));
156 COS(-370, -30, -1, 1);
157 /* min(2) */
158 COS(-100, -30, cos(min), cos(max));
159 /* min(1) */
160 COS(-190, -30, -1, cos(max));
163 static void acosinus(void)
165 HklInterval i_ref = {acos(.5), acos(-.5)};
166 HklInterval i = {-.5, .5};
168 hkl_interval_acos(&i);
169 ok(FALSE == hkl_interval_cmp(&i_ref, &i), __func__);
172 static void sinus(void)
174 /* we will test all 16 cases. */
175 /* we devide the trigonometric circle in 4 from quaters */
176 /* 0 [0, pi/2[ */
177 /* 1 [pi/2, pi[ */
178 /* 2 [pi, 3*pi/2[ */
179 /* 3 [3*pi/2, 2*pi[ */
180 HklInterval i_ref;
181 HklInterval i;
182 double min, max;
184 #define SIN(a,b, min_ref, max_ref) do { \
185 i.min = min = a * HKL_DEGTORAD; \
186 i.max = max = b * HKL_DEGTORAD; \
187 i_ref.min = min_ref; \
188 i_ref.max = max_ref; \
189 hkl_interval_sin(&i); \
190 ok(FALSE == hkl_interval_cmp(&i_ref, &i), __func__); \
191 } while(0)
193 /* 1st max(0) */
194 /* min(0) */
195 SIN(10, 14,sin(min), sin(max));
196 SIN(-275, 14, -1, 1);
197 /* min(3) */
198 SIN(-15, 14, sin(min), sin(max));
199 /* min(2) */
200 SIN(-95, 14, -1, sin(max));
201 /* min(1) */
202 SIN(-185, 14, -1, sin(max));
203 SIN(-215, 14, -1, sin(min));
205 /* 2nd max(1) */
206 /* min(0) */
207 SIN(10, 100, sin(min), 1);
208 SIN(85, 100, sin(max), 1);
209 /* min(3) */
210 SIN(-20, 100, sin(min), 1);
211 /* min(2) */
212 SIN(-110, 100, -1, 1);
213 /* min(1) */
214 SIN(-190, 100, -1, 1);
215 SIN(95, 100, sin(max), sin(min));
217 /* 3rd max(2) */
218 /* min(0) */
219 SIN(10, 190, sin(max), 1);
220 /* min(3) */
221 SIN(95, 190, sin(max), sin(min));
222 /* min(2) */
223 SIN(-95, 190, -1, 1);
224 SIN(185, 190, sin(max), sin(min));
225 /* min(1) */
226 SIN(-5, 190, sin(max), 1);
227 SIN(-45, 190, sin(min), 1);
229 /* 4th max(3) */
230 /* min(0); */
231 SIN(-350, -30, -1, 1);
232 /* min(3); */
233 SIN(-40, -30, sin(min), sin(max));
234 SIN(-370, -30, -1, 1);
235 /* min(2); */
236 SIN(-100, -30, -1, sin(max));
237 SIN(-170, -30, -1, sin(min));
238 /* min(1); */
239 SIN(-190, -30, -1, sin(min));
242 static void asinus(void)
244 HklInterval i_ref = {asin(-.5), asin(.5)};
245 HklInterval i = {-.5, .5};
247 hkl_interval_asin(&i);
248 ok(FALSE == hkl_interval_cmp(&i_ref, &i), __func__);
251 static void tangeante(void)
253 HklInterval i;
254 HklInterval i_ref;
255 double min, max;
257 #define TAN(a,b, min_ref, max_ref) do { \
258 i.min = min = a * HKL_DEGTORAD; \
259 i.max = max = b * HKL_DEGTORAD; \
260 i_ref.min = min_ref; \
261 i_ref.max = max_ref; \
262 hkl_interval_tan(&i); \
263 ok(FALSE == hkl_interval_cmp(&i_ref, &i), __func__); \
264 } while(0)
266 TAN(-100, -89, -INFINITY, INFINITY);
269 // The limit case is not yet ok
270 TAN(-100, 11, 12, -90);
271 r_ref.set(-hklconstantmathinfinity, tan(current), tan(consign), hklconstantmathinfinity);
272 ok(r_ref, r);
276 static void atangeante(void)
278 HklInterval i_ref = {atan(-10.), atan(10)};
279 HklInterval i = {-10, 10};
281 hkl_interval_atan(&i);
282 ok(FALSE == hkl_interval_cmp(&i_ref, &i), __func__);
285 static void length(void)
287 HklInterval interval;
289 interval.min = 10;
290 interval.max = 11;
291 is_double(1., hkl_interval_length(&interval), HKL_EPSILON, __func__);
293 interval.min = -11;
294 interval.max = -10;
295 is_double(1., hkl_interval_length(&interval), HKL_EPSILON, __func__);
298 int main(void)
300 plan(59);
302 cmp();
303 plus_interval();
304 plus_double();
305 times_interval();
306 times_double();
307 divides_double();
308 contain_zero();
309 cosinus();
310 acosinus();
311 sinus();
312 asinus();
313 tangeante();
314 atangeante();
315 length();
317 return 0;