[contrib][haskell] Hkl.Xrd.Calibration
[hkl.git] / tests / hkl-axis-t.c
blob3e453a7ba2467f309d8ccf9a6fbc1dd4dbeb9e7d
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-2017 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>
22 #include "hkl.h"
23 #include <tap/basic.h>
24 #include <tap/float.h>
25 #include <tap/hkl-tap.h>
27 #include <hkl-axis-private.h>
29 static void new(void)
31 HklParameter *axis;
32 static HklVector v = {{1, 0, 0}};
33 double min, max;
35 axis = hkl_parameter_new_rotation("rotation", &v, &hkl_unit_angle_deg);
36 is_string("rotation", hkl_parameter_name_get(axis), __func__);
37 hkl_parameter_min_max_get(axis, &min, &max, HKL_UNIT_DEFAULT);
38 is_double(-M_PI, min, HKL_EPSILON, __func__);
39 is_double(M_PI, max, HKL_EPSILON, __func__);
40 is_double(0., hkl_parameter_value_get(axis, HKL_UNIT_DEFAULT), HKL_EPSILON, __func__);
41 ok(TRUE == hkl_parameter_fit_get(axis), __func__);
42 hkl_parameter_free(axis);
44 axis = hkl_parameter_new_translation("translation", &v, &hkl_unit_length_mm);
45 is_string("translation", hkl_parameter_name_get(axis), __func__);
46 hkl_parameter_min_max_get(axis, &min, &max, HKL_UNIT_DEFAULT);
47 is_double(-DBL_MAX, min, HKL_EPSILON, __func__);
48 is_double(DBL_MAX, max, HKL_EPSILON, __func__);
49 is_double(0., hkl_parameter_value_get(axis, HKL_UNIT_DEFAULT), HKL_EPSILON, __func__);
50 ok(TRUE == hkl_parameter_fit_get(axis), __func__);
51 hkl_parameter_free(axis);
54 static void get_quaternions(void)
56 static HklVector v_ref = {{1, 0, 0}};
57 static HklQuaternion q1_ref = {{1, 0, 0, 0}};
58 static HklQuaternion q2_ref = {{M_SQRT1_2, -M_SQRT1_2, 0, 0}};
59 HklParameter *axis;
61 axis = hkl_parameter_new_rotation("rotation", &v_ref, &hkl_unit_angle_deg);
62 is_quaternion(&q1_ref, hkl_parameter_quaternion_get(axis), __func__);
63 ok(TRUE == hkl_parameter_value_set(axis, -M_PI_2, HKL_UNIT_DEFAULT, NULL), __func__);
64 is_quaternion(&q2_ref, hkl_parameter_quaternion_get(axis), __func__);
65 hkl_parameter_free(axis);
67 axis = hkl_parameter_new_translation("translation", &v_ref, &hkl_unit_length_mm);
68 ok(NULL == hkl_parameter_quaternion_get(axis), __func__);
69 hkl_parameter_free(axis);
72 static void copy(void)
74 static HklVector v = {{1, 0, 0}};
75 static HklQuaternion q_ref = {{M_SQRT1_2, -M_SQRT1_2, 0, 0}};
76 HklParameter *axis;
77 HklParameter *copy;
78 double min, max;
81 axis = hkl_parameter_new_rotation("rotation", &v, &hkl_unit_angle_deg);
82 ok(TRUE == hkl_parameter_value_set(axis, -M_PI_2, HKL_UNIT_DEFAULT, NULL), __func__);
83 copy = hkl_parameter_new_copy(axis);
84 is_string("rotation", hkl_parameter_name_get(copy), __func__);
85 hkl_parameter_min_max_get(copy, &min, &max, HKL_UNIT_DEFAULT);
86 is_double(-M_PI, min, HKL_EPSILON, __func__);
87 is_double(M_PI, max, HKL_EPSILON, __func__);
88 is_double(-M_PI_2, hkl_parameter_value_get(copy, HKL_UNIT_DEFAULT), HKL_EPSILON, __func__);
89 ok(TRUE == hkl_parameter_fit_get(copy), __func__);
90 is_quaternion(&q_ref, hkl_parameter_quaternion_get(copy), __func__);
91 hkl_parameter_free(axis);
92 hkl_parameter_free(copy);
94 axis = hkl_parameter_new_translation("translation", &v, &hkl_unit_length_mm);
95 ok(TRUE == hkl_parameter_value_set(axis, 100, HKL_UNIT_DEFAULT, NULL), __func__);
96 copy = hkl_parameter_new_copy(axis);
97 is_string("translation", hkl_parameter_name_get(copy), __func__);
98 hkl_parameter_min_max_get(copy, &min, &max, HKL_UNIT_DEFAULT);
99 is_double(-DBL_MAX, min, HKL_EPSILON, __func__);
100 is_double(DBL_MAX, max, HKL_EPSILON, __func__);
101 is_double(100, hkl_parameter_value_get(copy, HKL_UNIT_DEFAULT), HKL_EPSILON, __func__);
102 ok(TRUE == hkl_parameter_fit_get(copy), __func__);
103 ok(NULL == hkl_parameter_quaternion_get(copy), __func__);
104 hkl_parameter_free(axis);
105 hkl_parameter_free(copy);
108 static void is_valid(void)
110 static HklVector v = {{1, 0, 0}};
111 HklParameter *axis;
113 axis = hkl_parameter_new_rotation("rotation", &v, &hkl_unit_angle_deg);
115 ok(TRUE == hkl_parameter_value_set(axis, 45, HKL_UNIT_USER, NULL), __func__);
116 ok(TRUE == hkl_parameter_is_valid(axis), __func__);
117 ok(TRUE == hkl_parameter_min_max_set(axis, -270, 0, HKL_UNIT_USER, NULL), __func__);
118 ok(FALSE == hkl_parameter_is_valid(axis), __func__);
119 ok(TRUE == hkl_parameter_value_set(axis, -45, HKL_UNIT_USER, NULL), __func__);
120 ok(TRUE == hkl_parameter_is_valid(axis), __func__);
121 ok(TRUE == hkl_parameter_min_max_set(axis, 350, 450, HKL_UNIT_USER, NULL), __func__);
122 ok(TRUE == hkl_parameter_value_set(axis, 45, HKL_UNIT_USER, NULL), __func__);
123 ok(TRUE == hkl_parameter_is_valid(axis), __func__);
124 ok(TRUE == hkl_parameter_value_set(axis, -45, HKL_UNIT_USER, NULL), __func__);
125 ok(FALSE == hkl_parameter_is_valid(axis), __func__);
126 ok(TRUE == hkl_parameter_min_max_set(axis, -10, 90, HKL_UNIT_USER, NULL), __func__);
127 ok(TRUE == hkl_parameter_value_set(axis, 405, HKL_UNIT_USER, NULL), __func__);
128 ok(TRUE == hkl_parameter_is_valid(axis), __func__);
129 ok(TRUE == hkl_parameter_value_set(axis, -405, HKL_UNIT_USER, NULL), __func__);
130 ok(FALSE == hkl_parameter_is_valid(axis), __func__);
131 hkl_parameter_free(axis);
133 axis = hkl_parameter_new_translation("translation", &v, &hkl_unit_length_mm);
134 ok(TRUE == hkl_parameter_value_set(axis, 45, HKL_UNIT_USER, NULL), __func__);
135 ok(TRUE == hkl_parameter_is_valid(axis), __func__);
136 ok(TRUE == hkl_parameter_min_max_set(axis, -270, 0, HKL_UNIT_USER, NULL), __func__);
137 ok(FALSE == hkl_parameter_is_valid(axis), __func__);
138 ok(TRUE == hkl_parameter_value_set(axis, -45, HKL_UNIT_USER, NULL), __func__);
139 ok(TRUE == hkl_parameter_is_valid(axis), __func__);
140 hkl_parameter_free(axis);
143 static void set_value_smallest_in_range(void)
145 HklParameter *axis;
146 static HklVector v = {{1, 0, 0}};
148 axis = hkl_parameter_new_rotation("rotation", &v, &hkl_unit_angle_deg);
150 ok(TRUE == hkl_parameter_min_max_set(axis, -190, 190, HKL_UNIT_USER, NULL), __func__);
152 ok(TRUE == hkl_parameter_value_set(axis, 185, HKL_UNIT_USER, NULL), __func__);
153 hkl_parameter_value_set_smallest_in_range(axis);
154 is_double(-175., hkl_parameter_value_get(axis, HKL_UNIT_USER), HKL_EPSILON, __func__);
156 ok(TRUE == hkl_parameter_value_set(axis, 545, HKL_UNIT_USER, NULL), __func__);
157 hkl_parameter_value_set_smallest_in_range(axis);
158 is_double(-175., hkl_parameter_value_get(axis, HKL_UNIT_USER), HKL_EPSILON, __func__);
160 ok(TRUE == hkl_parameter_value_set(axis, -185, HKL_UNIT_USER, NULL), __func__);
161 hkl_parameter_value_set_smallest_in_range(axis);
162 is_double(-185., hkl_parameter_value_get(axis, HKL_UNIT_USER), HKL_EPSILON, __func__);
164 ok(TRUE == hkl_parameter_value_set(axis, 175, HKL_UNIT_USER, NULL), __func__);
165 hkl_parameter_value_set_smallest_in_range(axis);
166 is_double(-185., hkl_parameter_value_get(axis, HKL_UNIT_USER), HKL_EPSILON, __func__);
168 ok(TRUE == hkl_parameter_value_set(axis, 190, HKL_UNIT_USER, NULL), __func__);
169 hkl_parameter_value_set_smallest_in_range(axis);
170 is_double(-170., hkl_parameter_value_get(axis, HKL_UNIT_USER), HKL_EPSILON, __func__);
172 ok(TRUE == hkl_parameter_value_set(axis, -190, HKL_UNIT_USER, NULL), __func__);
173 hkl_parameter_value_set_smallest_in_range(axis);
174 is_double(-190., hkl_parameter_value_get(axis, HKL_UNIT_USER), HKL_EPSILON, __func__);
176 hkl_parameter_free(axis);
179 static void get_value_closest(void)
181 HklParameter *axis1, *axis2;
182 static HklVector v = {{1, 0, 0}};
184 axis1 = hkl_parameter_new_rotation("rotation", &v, &hkl_unit_angle_deg);
185 axis2 = hkl_parameter_new_rotation("rotation", &v, &hkl_unit_angle_deg);
187 ok(TRUE == hkl_parameter_value_set(axis1, 0, HKL_UNIT_USER, NULL), __func__);
188 ok(TRUE == hkl_parameter_value_set(axis2, 0, HKL_UNIT_USER, NULL), __func__);
189 is_double(0., hkl_parameter_value_get_closest(axis1,
190 axis2),
191 HKL_EPSILON, __func__);
193 /* change the range of axis1 */
194 ok(TRUE == hkl_parameter_min_max_set(axis1, -270, 180, HKL_UNIT_USER, NULL), __func__);
195 ok(TRUE == hkl_parameter_value_set(axis1, 100, HKL_UNIT_USER, NULL), __func__);
197 ok(TRUE == hkl_parameter_value_set(axis2, -75, HKL_UNIT_USER, NULL), __func__);
198 is_double(100 * HKL_DEGTORAD, hkl_parameter_value_get_closest(axis1,
199 axis2),
200 HKL_EPSILON, __func__);
201 ok(TRUE == hkl_parameter_value_set(axis2, -85, HKL_UNIT_USER, NULL), __func__);
202 is_double(-260 * HKL_DEGTORAD, hkl_parameter_value_get_closest(axis1,
203 axis2),
204 HKL_EPSILON, __func__);
206 hkl_parameter_free(axis1);
209 static void transformation_cmp(void)
211 int res = TRUE;
212 HklParameter *axis1, *axis2, *translation1, *translation2;
213 static HklVector v1 = {{1, 0, 0}};
214 static HklVector v2 = {{0, 1, 0}};
216 axis1 = hkl_parameter_new_rotation("rotation", &v1, &hkl_unit_angle_deg);
217 axis2 = hkl_parameter_new_rotation("rotation", &v2, &hkl_unit_angle_deg);
218 translation1 = hkl_parameter_new_translation("translation", &v1, &hkl_unit_length_mm);
219 translation2 = hkl_parameter_new_translation("translation", &v2, &hkl_unit_length_mm);
221 res &= DIAG(0 == hkl_parameter_transformation_cmp(axis1, axis1));
222 res &= DIAG(0 != hkl_parameter_transformation_cmp(axis1, axis2));
223 res &= DIAG(0 != hkl_parameter_transformation_cmp(axis1, translation1));
224 res &= DIAG(0 != hkl_parameter_transformation_cmp(axis1, translation2));
225 res &= DIAG(0 != hkl_parameter_transformation_cmp(axis2, translation1));
226 res &= DIAG(0 != hkl_parameter_transformation_cmp(axis2, translation2));
227 res &= DIAG(0 == hkl_parameter_transformation_cmp(translation1, translation1));
228 res &= DIAG(0 != hkl_parameter_transformation_cmp(translation1, translation2));
229 res &= DIAG(0 != hkl_parameter_transformation_cmp(translation1, axis1));
230 res &= DIAG(0 != hkl_parameter_transformation_cmp(translation1, axis2));
231 res &= DIAG(0 != hkl_parameter_transformation_cmp(translation2, axis1));
232 res &= DIAG(0 != hkl_parameter_transformation_cmp(translation2, axis2));
234 ok(res == TRUE, __func__);
236 hkl_parameter_free(translation2);
237 hkl_parameter_free(translation1);
238 hkl_parameter_free(axis2);
239 hkl_parameter_free(axis1);
242 int main(void)
244 plan(73);
246 new();
247 get_quaternions();
248 copy();
249 is_valid();
250 set_value_smallest_in_range();
251 get_value_closest();
252 transformation_cmp();
254 return 0;