[contrib/haskell] typo
[hkl.git] / tests / hkl-lattice-t.c
blob2c08d9a16f07281c036ce788618ed93682643b18
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-2019, 2021 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 #define CHECK_PARAM(_lattice, _param, _value) \
28 is_double((_value), \
29 hkl_parameter_value_get(hkl_lattice_## _param ##_get(_lattice), \
30 HKL_UNIT_DEFAULT), \
31 HKL_EPSILON, __func__);
33 #define CHECK_LATTICE(_lattice, _a, _b, _c, _alpha, _beta, _gamma) do { \
34 CHECK_PARAM(_lattice, a, _a); \
35 CHECK_PARAM(_lattice, b, _b); \
36 CHECK_PARAM(_lattice, c, _c); \
37 CHECK_PARAM(_lattice, alpha, _alpha); \
38 CHECK_PARAM(_lattice, beta, _beta); \
39 CHECK_PARAM(_lattice, gamma, _gamma); \
40 }while(0);
42 #define SET_PARAM(_lattice, _param, _value) do { \
43 GError *error; \
44 HklParameter *p = hkl_parameter_new_copy(hkl_lattice_ ## _param ## _get(_lattice)); \
45 ok(hkl_parameter_value_set(p, _value, HKL_UNIT_DEFAULT, NULL), __func__); \
46 ok(TRUE == hkl_lattice_ ## _param ## _set(_lattice, p, NULL), __func__); \
47 error = NULL; \
48 ok(TRUE == hkl_lattice_ ## _param ## _set(_lattice, p, &error), __func__); \
49 ok(error == NULL, __func__); \
50 hkl_parameter_free(p); \
51 }while(0);
53 #define SET_LATTICE(_lattice, _a, _b, _c, _alpha, _beta, _gamma) do{ \
54 SET_PARAM(_lattice, a, _a); \
55 SET_PARAM(_lattice, b, _b); \
56 SET_PARAM(_lattice, c, _c); \
57 SET_PARAM(_lattice, alpha, _alpha); \
58 SET_PARAM(_lattice, beta, _beta); \
59 SET_PARAM(_lattice, gamma, _gamma); \
60 }while(0);
62 static void new(void)
64 HklLattice *lattice;
65 GError *error;
67 /* can not set this lattice */
68 lattice = hkl_lattice_new(1.54, 1.54, 1.54,
69 90*HKL_DEGTORAD, 10*HKL_DEGTORAD, 120*HKL_DEGTORAD,
70 NULL);
71 ok(NULL == lattice, __func__);
73 /* check GError generation */
74 error = NULL;
75 lattice = hkl_lattice_new(1.54, 1.54, 1.54,
76 90*HKL_DEGTORAD, 10*HKL_DEGTORAD, 120*HKL_DEGTORAD,
77 &error);
78 ok(NULL == lattice, __func__);
79 ok(error != NULL, __func__);
80 g_clear_error(&error);
82 /* but can create this one */
83 lattice = hkl_lattice_new(1.54, 1.54, 1.54,
84 90*HKL_DEGTORAD, 90*HKL_DEGTORAD, 90*HKL_DEGTORAD,
85 NULL);
86 ok(0 == !lattice, __func__);
87 CHECK_LATTICE(lattice,
88 1.54, 1.54, 1.54,
89 90*HKL_DEGTORAD, 90*HKL_DEGTORAD, 90*HKL_DEGTORAD);
90 hkl_lattice_free(lattice);
92 /* but can create this one and no GError are produce */
93 lattice = hkl_lattice_new(1.54, 1.54, 1.54,
94 90*HKL_DEGTORAD, 90*HKL_DEGTORAD, 90*HKL_DEGTORAD,
95 &error);
96 ok(0 == !lattice, __func__);
97 ok(error == NULL, __func__);
98 CHECK_LATTICE(lattice,
99 1.54, 1.54, 1.54,
100 90*HKL_DEGTORAD, 90*HKL_DEGTORAD, 90*HKL_DEGTORAD);
101 hkl_lattice_free(lattice);
104 static void new_copy(void)
106 HklLattice *lattice;
107 HklLattice *copy;
109 lattice = hkl_lattice_new(1.54, 1.54, 1.54,
110 90*HKL_DEGTORAD, 90*HKL_DEGTORAD, 90*HKL_DEGTORAD,
111 NULL);
113 /* copy constructor */
114 copy = hkl_lattice_new_copy(lattice);
116 CHECK_LATTICE(copy,
117 1.54, 1.54, 1.54,
118 90*HKL_DEGTORAD, 90*HKL_DEGTORAD, 90*HKL_DEGTORAD);
120 hkl_lattice_free(lattice);
121 hkl_lattice_free(copy);
124 static void set(void)
126 HklLattice *lattice;
127 GError *error;
129 lattice = hkl_lattice_new_default();
131 /* can not set this lattice */
132 ok(FALSE == hkl_lattice_set(lattice,
133 1.54, 1.54, 1.54,
134 90*HKL_DEGTORAD, 10*HKL_DEGTORAD, 120*HKL_DEGTORAD,
135 HKL_UNIT_DEFAULT, NULL),
136 __func__);
138 /* can not set this lattice with GError */
139 error = NULL;
140 ok(FALSE == hkl_lattice_set(lattice,
141 1.54, 1.54, 1.54,
142 90*HKL_DEGTORAD, 10*HKL_DEGTORAD, 120*HKL_DEGTORAD,
143 HKL_UNIT_DEFAULT, &error),
144 __func__);
145 ok(error != NULL, __func__);
146 g_clear_error(&error);
148 /* can set this lattice */
149 ok(TRUE == hkl_lattice_set(lattice,
150 1.54, 1.54, 1.54,
151 90*HKL_DEGTORAD, 91*HKL_DEGTORAD, 92*HKL_DEGTORAD,
152 HKL_UNIT_DEFAULT, NULL),
153 __func__);
154 CHECK_LATTICE(lattice,
155 1.54, 1.54, 1.54,
156 90*HKL_DEGTORAD, 91*HKL_DEGTORAD, 92*HKL_DEGTORAD);
158 /* can set this lattice with no GError set */
159 ok(TRUE == hkl_lattice_set(lattice,
160 1.54, 1.54, 1.54,
161 90*HKL_DEGTORAD, 91*HKL_DEGTORAD, 90*HKL_DEGTORAD,
162 HKL_UNIT_DEFAULT, &error),
163 __func__);
164 ok(error == NULL, __func__);
165 CHECK_LATTICE(lattice,
166 1.54, 1.54, 1.54,
167 90*HKL_DEGTORAD, 91*HKL_DEGTORAD, 90*HKL_DEGTORAD);
171 /* can set this lattice in HKL_UNIT_USER with no GError set */
172 ok(TRUE == hkl_lattice_set(lattice, 1.54, 1.54, 1.54, 90, 91, 92,
173 HKL_UNIT_USER, NULL),
174 __func__);
175 CHECK_LATTICE(lattice,
176 1.54, 1.54, 1.54,
177 90*HKL_DEGTORAD, 91*HKL_DEGTORAD, 92*HKL_DEGTORAD);
179 /* can set this lattice in HKL_UNIT_USER with no GError set */
180 ok(TRUE == hkl_lattice_set(lattice,
181 1.54, 1.54, 1.54, 90, 91, 90,
182 HKL_UNIT_USER, &error),
183 __func__);
184 ok(error == NULL, __func__);
185 CHECK_LATTICE(lattice,
186 1.54, 1.54, 1.54,
187 90*HKL_DEGTORAD, 91*HKL_DEGTORAD, 90*HKL_DEGTORAD);
189 /* check individual accessor */
190 SET_LATTICE(lattice, 1.54, 1.54, 1.54, 90*HKL_DEGTORAD, 91 * HKL_DEGTORAD, 91 * HKL_DEGTORAD);
191 CHECK_LATTICE(lattice, 1.54, 1.54, 1.54, 90*HKL_DEGTORAD, 91 * HKL_DEGTORAD, 91 * HKL_DEGTORAD);
193 hkl_lattice_free(lattice);
196 static void reciprocal(void)
198 HklLattice *lattice;
199 HklLattice *reciprocal;
201 lattice = hkl_lattice_new_default();
202 reciprocal = hkl_lattice_new_default();
204 /* cubic */
205 ok(TRUE == hkl_lattice_set(lattice, 1.54, 1.54, 1.54,
206 90*HKL_DEGTORAD, 90*HKL_DEGTORAD, 90*HKL_DEGTORAD,
207 HKL_UNIT_DEFAULT, NULL),
208 __func__);
210 ok(TRUE == hkl_lattice_reciprocal(lattice, reciprocal), __func__);
212 CHECK_LATTICE(reciprocal,
213 HKL_TAU / 1.54, HKL_TAU / 1.54, HKL_TAU / 1.54,
214 90*HKL_DEGTORAD, 90*HKL_DEGTORAD, 90*HKL_DEGTORAD);
216 /* orthorombic */
217 ok(TRUE == hkl_lattice_set(lattice,
218 1., 3., 4.,
219 90 * HKL_DEGTORAD, 90 * HKL_DEGTORAD, 90 * HKL_DEGTORAD,
220 HKL_UNIT_DEFAULT, NULL),
221 __func__);
222 ok(TRUE == hkl_lattice_reciprocal(lattice, reciprocal), __func__);
224 CHECK_LATTICE(reciprocal,
225 HKL_TAU / 1., HKL_TAU / 3., HKL_TAU / 4.,
226 90*HKL_DEGTORAD, 90*HKL_DEGTORAD, 90*HKL_DEGTORAD);
228 /* hexagonal1 */
229 ok(TRUE == hkl_lattice_set(lattice,
230 1., 2., 1.,
231 90 * HKL_DEGTORAD, 120 * HKL_DEGTORAD, 90 * HKL_DEGTORAD,
232 HKL_UNIT_DEFAULT, NULL),
233 __func__);
234 ok(TRUE == hkl_lattice_reciprocal(lattice, reciprocal), __func__);
236 CHECK_LATTICE(reciprocal,
237 HKL_TAU * 2. / sqrt(3.), HKL_TAU / 2., HKL_TAU * 2. / sqrt(3.),
238 90. * HKL_DEGTORAD, 60. * HKL_DEGTORAD, 90. * HKL_DEGTORAD);
240 /* hexagonal2 */
241 ok(TRUE == hkl_lattice_set(lattice, 2., 1., 1.,
242 120 * HKL_DEGTORAD, 90 * HKL_DEGTORAD, 90 * HKL_DEGTORAD,
243 HKL_UNIT_DEFAULT, NULL),
244 __func__);
245 ok(TRUE == hkl_lattice_reciprocal(lattice, reciprocal), __func__);
247 CHECK_LATTICE(reciprocal,
248 HKL_TAU / 2., HKL_TAU * 2. / sqrt(3.), HKL_TAU * 2. / sqrt(3.),
249 60. * HKL_DEGTORAD, 90. * HKL_DEGTORAD, 90. * HKL_DEGTORAD);
251 /* triclinic1 */
252 ok(TRUE == hkl_lattice_set(lattice, 9.32, 8.24, 13.78,
253 91.23 * HKL_DEGTORAD, 93.64 * HKL_DEGTORAD, 122.21 * HKL_DEGTORAD,
254 HKL_UNIT_DEFAULT, NULL),
255 __func__);
256 ok(TRUE == hkl_lattice_reciprocal(lattice, reciprocal), __func__);
258 CHECK_LATTICE(reciprocal,
259 HKL_TAU * 0.1273130168,HKL_TAU * 0.1437422974,HKL_TAU * 0.0728721120,
260 1.5052513337, 1.482101482, 1.0055896011);
262 /* triclinic2 */
263 ok(TRUE == hkl_lattice_set(lattice, 18.423, 18.417,
264 18.457, 89.99 * HKL_DEGTORAD, 89.963 * HKL_DEGTORAD, 119.99 * HKL_DEGTORAD,
265 HKL_UNIT_DEFAULT, NULL),
266 __func__);
267 ok(TRUE == hkl_lattice_reciprocal(lattice, reciprocal), __func__);
269 CHECK_LATTICE(reciprocal,
270 HKL_TAU * 0.0626708259,HKL_TAU * 0.0626912310,HKL_TAU * 0.0541800061,
271 1.5713705262, 1.5716426508, 1.0473718249);
273 hkl_lattice_free(lattice);
274 hkl_lattice_free(reciprocal);
277 static void volume(void)
279 HklLattice *lattice;
280 HklLattice *reciprocal;
282 lattice = hkl_lattice_new_default();
283 reciprocal = hkl_lattice_new_default();
285 /* cubic */
286 (DIAG(hkl_lattice_set(lattice, 1.54, 1.54, 1.54,
287 90*HKL_DEGTORAD, 90*HKL_DEGTORAD, 90*HKL_DEGTORAD,
288 HKL_UNIT_DEFAULT, NULL)));
289 CHECK_PARAM(lattice, volume, 1.54*1.54*1.54);
291 hkl_lattice_reciprocal(lattice, reciprocal);
292 CHECK_PARAM(reciprocal, volume, (HKL_TAU * HKL_TAU * HKL_TAU)/(1.54*1.54*1.54));
294 hkl_lattice_free(lattice);
295 hkl_lattice_free(reciprocal);
298 static void get_B(void)
300 HklMatrix *B_ref = hkl_matrix_new_full(HKL_TAU / 1.54, 0, 0,
301 0, HKL_TAU / 1.54, 0,
302 0, 0, HKL_TAU / 1.54);
303 HklLattice *lattice;
304 HklMatrix *B = hkl_matrix_new();
306 /* cubic */
307 lattice = hkl_lattice_new(1.54, 1.54, 1.54,
308 90 * HKL_DEGTORAD, 90 * HKL_DEGTORAD, 90 * HKL_DEGTORAD,
309 NULL);
311 hkl_lattice_get_B(lattice, B);
312 is_matrix(B_ref, B, __func__);
314 hkl_lattice_free(lattice);
315 hkl_matrix_free(B);
316 hkl_matrix_free(B_ref);
319 static void get_1_B(void)
321 HklMatrix *I_ref = hkl_matrix_new_full(1, 0, 0,
322 0, 1, 0,
323 0, 0, 1);
324 HklLattice *lattice;
325 HklMatrix *I = hkl_matrix_new();
326 HklMatrix *B_1 = hkl_matrix_new();
328 /* cubic */
329 lattice = hkl_lattice_new(1.54, 1.54, 1.54,
330 90 * HKL_DEGTORAD, 90 * HKL_DEGTORAD, 90 * HKL_DEGTORAD,
331 NULL);
333 hkl_lattice_get_B(lattice, I);
334 hkl_lattice_get_1_B(lattice, B_1);
336 /* B times B^-1 = Identity */
337 hkl_matrix_times_matrix(I, B_1);
338 is_matrix(I_ref, I, __func__);
340 hkl_lattice_free(lattice);
341 hkl_matrix_free(B_1);
342 hkl_matrix_free(I);
343 hkl_matrix_free(I_ref);
346 int main(void)
348 plan(139);
350 new();
351 new_copy();
352 set();
353 reciprocal();
354 volume();
355 get_B();
356 get_1_B();
358 return 0;