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-2014 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 #include <tap/basic.h>
24 #include <tap/float.h>
26 #include "hkl/ccan/container_of/container_of.h"
27 #include "hkl-axis-private.h"
32 static HklVector v
= {{1, 0, 0}};
34 axis
= hkl_parameter_new_axis("omega", &v
);
36 is_string("omega", axis
->name
, __func__
);
37 is_double(-M_PI
, axis
->range
.min
, HKL_EPSILON
, __func__
);
38 is_double(M_PI
, axis
->range
.max
, HKL_EPSILON
, __func__
);
39 is_double(0., hkl_parameter_value_get(axis
, HKL_UNIT_DEFAULT
), HKL_EPSILON
, __func__
);
40 ok(TRUE
== axis
->fit
, __func__
);
41 ok(TRUE
== axis
->changed
, __func__
);
43 hkl_parameter_free(axis
);
46 static void get_quaternions(void)
49 static HklVector v
= {{1, 0, 0}};
51 axis
= container_of(hkl_parameter_new_axis("omega", &v
),
54 is_double(1., axis
->q
.data
[0], HKL_EPSILON
, __func__
);
55 is_double(0., axis
->q
.data
[1], HKL_EPSILON
, __func__
);
56 is_double(0., axis
->q
.data
[2], HKL_EPSILON
, __func__
);
57 is_double(0., axis
->q
.data
[3], HKL_EPSILON
, __func__
);
59 hkl_parameter_value_set(&axis
->parameter
, -M_PI_2
, HKL_UNIT_DEFAULT
, NULL
);
60 is_double(1./sqrt(2.), axis
->q
.data
[0], HKL_EPSILON
, __func__
);
61 is_double(-1./sqrt(2.), axis
->q
.data
[1], HKL_EPSILON
, __func__
);
62 is_double(0., axis
->q
.data
[2], HKL_EPSILON
, __func__
);
63 is_double(0., axis
->q
.data
[3], HKL_EPSILON
, __func__
);
65 hkl_parameter_free(&axis
->parameter
);
68 static void copy(void)
72 static HklVector v
= {{1, 0, 0}};
74 axis
= container_of(hkl_parameter_new_axis("omega", &v
),
76 hkl_parameter_value_set(&axis
->parameter
, -M_PI_2
, HKL_UNIT_DEFAULT
, NULL
);
78 copy
= container_of(hkl_parameter_new_copy(&axis
->parameter
),
80 is_string("omega", copy
->parameter
.name
, __func__
);
81 is_double(-M_PI
, copy
->parameter
.range
.min
, HKL_EPSILON
, __func__
);
82 is_double(M_PI
, copy
->parameter
.range
.max
, HKL_EPSILON
, __func__
);
83 is_double(-M_PI_2
, hkl_parameter_value_get(©
->parameter
, HKL_UNIT_DEFAULT
), HKL_EPSILON
, __func__
);
84 ok(TRUE
== copy
->parameter
.fit
, __func__
);
85 ok(TRUE
== copy
->parameter
.changed
, __func__
);
86 is_double(1./sqrt(2.), copy
->q
.data
[0], HKL_EPSILON
, __func__
);
87 is_double(-1./sqrt(2.), copy
->q
.data
[1], HKL_EPSILON
, __func__
);
88 is_double(0., copy
->q
.data
[2], HKL_EPSILON
, __func__
);
89 is_double(0., copy
->q
.data
[3], HKL_EPSILON
, __func__
);
91 hkl_parameter_free(&axis
->parameter
);
92 hkl_parameter_free(©
->parameter
);
95 static void is_valid(void)
98 static HklVector v
= {{1, 0, 0}};
100 axis1
= container_of(hkl_parameter_new_axis("omega", &v
),
103 hkl_parameter_value_set(&axis1
->parameter
, 45, HKL_UNIT_USER
, NULL
);
104 ok(TRUE
== hkl_parameter_is_valid(&axis1
->parameter
), __func__
);
106 /* change the range of axis1 */
107 hkl_parameter_min_max_set(&axis1
->parameter
, -270, 0, HKL_UNIT_USER
, NULL
);
108 ok(FALSE
== hkl_parameter_is_valid(&axis1
->parameter
), __func__
);
110 hkl_parameter_value_set(&axis1
->parameter
, -45, HKL_UNIT_USER
, NULL
);
111 ok(TRUE
== hkl_parameter_is_valid(&axis1
->parameter
), __func__
);
113 hkl_parameter_min_max_set(&axis1
->parameter
, 350, 450, HKL_UNIT_USER
, NULL
);
114 hkl_parameter_value_set(&axis1
->parameter
, 45, HKL_UNIT_USER
, NULL
);
115 ok(TRUE
== hkl_parameter_is_valid(&axis1
->parameter
), __func__
);
116 hkl_parameter_value_set(&axis1
->parameter
, -45, HKL_UNIT_USER
, NULL
);
117 ok(FALSE
== hkl_parameter_is_valid(&axis1
->parameter
), __func__
);
119 hkl_parameter_min_max_set(&axis1
->parameter
, -10, 90, HKL_UNIT_USER
, NULL
);
120 hkl_parameter_value_set(&axis1
->parameter
, 405, HKL_UNIT_USER
, NULL
);
121 ok(TRUE
== hkl_parameter_is_valid(&axis1
->parameter
), __func__
);
122 hkl_parameter_value_set(&axis1
->parameter
, -405, HKL_UNIT_USER
, NULL
);
123 ok(FALSE
== hkl_parameter_is_valid(&axis1
->parameter
), __func__
);
125 hkl_parameter_free(&axis1
->parameter
);
128 static void set_value_smallest_in_range(void)
131 static HklVector v
= {{1, 0, 0}};
133 axis
= container_of(hkl_parameter_new_axis("omega", &v
),
136 hkl_parameter_min_max_set(&axis
->parameter
, -190, 190, HKL_UNIT_USER
, NULL
);
138 hkl_parameter_value_set(&axis
->parameter
, 185, HKL_UNIT_USER
, NULL
);
139 hkl_parameter_value_set_smallest_in_range(&axis
->parameter
);
140 is_double(-175., hkl_parameter_value_get(&axis
->parameter
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
142 hkl_parameter_value_set(&axis
->parameter
, 545, HKL_UNIT_USER
, NULL
);
143 hkl_parameter_value_set_smallest_in_range(&axis
->parameter
);
144 is_double(-175., hkl_parameter_value_get(&axis
->parameter
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
146 hkl_parameter_value_set(&axis
->parameter
, -185, HKL_UNIT_USER
, NULL
);
147 hkl_parameter_value_set_smallest_in_range(&axis
->parameter
);
148 is_double(-185., hkl_parameter_value_get(&axis
->parameter
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
150 hkl_parameter_value_set(&axis
->parameter
, 175, HKL_UNIT_USER
, NULL
);
151 hkl_parameter_value_set_smallest_in_range(&axis
->parameter
);
152 is_double(-185., hkl_parameter_value_get(&axis
->parameter
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
154 hkl_parameter_value_set(&axis
->parameter
, 190, HKL_UNIT_USER
, NULL
);
155 hkl_parameter_value_set_smallest_in_range(&axis
->parameter
);
156 is_double(-170., hkl_parameter_value_get(&axis
->parameter
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
158 hkl_parameter_value_set(&axis
->parameter
, -190, HKL_UNIT_USER
, NULL
);
159 hkl_parameter_value_set_smallest_in_range(&axis
->parameter
);
160 is_double(-190., hkl_parameter_value_get(&axis
->parameter
, HKL_UNIT_USER
), HKL_EPSILON
, __func__
);
162 hkl_parameter_free(&axis
->parameter
);
165 static void get_value_closest(void)
167 HklAxis
*axis1
, *axis2
;
168 static HklVector v
= {{1, 0, 0}};
170 axis1
= container_of(hkl_parameter_new_axis("omega", &v
),
172 axis2
= container_of(hkl_parameter_new_axis("omega", &v
),
175 hkl_parameter_value_set(&axis1
->parameter
, 0, HKL_UNIT_USER
, NULL
);
176 hkl_parameter_value_set(&axis2
->parameter
, 0, HKL_UNIT_USER
, NULL
);
177 is_double(0., hkl_parameter_value_get_closest(&axis1
->parameter
,
179 HKL_EPSILON
, __func__
);
181 /* change the range of axis1 */
182 hkl_parameter_min_max_set(&axis1
->parameter
, -270, 180, HKL_UNIT_USER
, NULL
);
183 hkl_parameter_value_set(&axis1
->parameter
, 100, HKL_UNIT_USER
, NULL
);
185 hkl_parameter_value_set(&axis2
->parameter
, -75, HKL_UNIT_USER
, NULL
);
186 is_double(100 * HKL_DEGTORAD
, hkl_parameter_value_get_closest(&axis1
->parameter
,
188 HKL_EPSILON
, __func__
);
190 hkl_parameter_value_set(&axis2
->parameter
, -85, HKL_UNIT_USER
, NULL
);
191 is_double(-260 * HKL_DEGTORAD
, hkl_parameter_value_get_closest(&axis1
->parameter
,
193 HKL_EPSILON
, __func__
);
195 hkl_parameter_free(&axis1
->parameter
);
196 hkl_parameter_free(&axis2
->parameter
);
199 int main(int argc
, char** argv
)
207 set_value_smallest_in_range();