CR2RE Pipeline Reference Manual 1.6.8
hdrl_multiiter-test.c
1/* $Id: hdrl_multiiter-test.c,v 1.0 2017 Exp $
2 *
3 * This file is part of the HDRL
4 * Copyright (C) 2013 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25/*-----------------------------------------------------------------------------
26 Includes
27 -----------------------------------------------------------------------------*/
28
29#include "hdrl_multiiter.h"
30#include "hdrl_frameiter.h"
31
32#include <cpl.h>
33
34
35/*----------------------------------------------------------------------------*/
40/*----------------------------------------------------------------------------*/
41
42cpl_frameset * create_frames(void){
43
44 size_t nframes = 5;
45 size_t next = 4;
46 cpl_frameset * frames = cpl_frameset_new();
47 for (size_t i = 0; i < nframes; i++) {
48 char * fn = cpl_sprintf("hdrl_multiiter-test_%zu_%zd.fits", i, (intptr_t)getpid());
49 cpl_propertylist * plist = cpl_propertylist_new();
50 cpl_propertylist_update_string(plist, "TAG", fn);
51 cpl_propertylist_save(plist, fn, CPL_IO_CREATE);
52 cpl_propertylist_delete(plist);
53 for (size_t j = 1; j < next + 1; j++) {
54 cpl_image * img = cpl_image_new(50, 70, CPL_TYPE_INT);
55 cpl_image_add_scalar(img, i * next + j);
56 cpl_image_save(img, fn, CPL_TYPE_INT, NULL, CPL_IO_EXTEND);
57 cpl_image_delete(img);
58 }
59 cpl_frame * frm = cpl_frame_new();
60 cpl_frame_set_filename(frm, fn);
61 cpl_frame_set_tag(frm, "RAW");
62 cpl_frameset_insert(frames, frm);
63 cpl_free(fn);
64 }
65
66 return frames;
67}
68
69void test_invalid(cpl_frameset *frames){
70
71 hdrl_iter *subiters[] = {
73 frames, 0, 2,
74 (intptr_t[]){HDRL_FRAMEITER_AXIS_FRAME, HDRL_FRAMEITER_AXIS_EXT},
75 (intptr_t[]){0, 1},
76 (intptr_t[]){1, 2},
77 NULL),
79 frames, 0, 2,
80 (intptr_t[]){HDRL_FRAMEITER_AXIS_FRAME, HDRL_FRAMEITER_AXIS_EXT},
81 (intptr_t[]){0, 2},
82 (intptr_t[]){1, 2},
83 NULL),
84 };
85 cpl_test_error(CPL_ERROR_NONE);
86
87 /* empty and fail tests */
88 hdrl_iter *it;
89
90 it = hdrl_multiiter_new(0, subiters, HDRL_ITER_ALLOW_EMPTY);
91 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
92 cpl_test_null(it);
93
94 it = hdrl_multiiter_new(2, NULL, HDRL_ITER_ALLOW_EMPTY);
95 cpl_test_error(CPL_ERROR_NULL_INPUT);
96 cpl_test_null(it);
97/*
98 it = hdrl_multiiter_new(2, subiters, HDRL_ITER_IMAGELIST);
99 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
100 cpl_test_null(it);
101*/
102 it = hdrl_multiiter_new(2, subiters, HDRL_ITER_ALLOW_EMPTY);
103 cpl_test_error(CPL_ERROR_NONE);
104 cpl_test_nonnull(it);
105
106 cpl_size size = hdrl_iter_length(it);
107 cpl_test_error(CPL_ERROR_NONE);
108 cpl_test_eq(size, 10);
109
110
111 hdrl_iter_delete(it);
112}
113
114void test_basic(cpl_frameset *frames)
115{
116 hdrl_iter *subiters[] = {
118 frames, 0, 2,
119 (intptr_t[]){HDRL_FRAMEITER_AXIS_FRAME, HDRL_FRAMEITER_AXIS_EXT},
120 (intptr_t[]){0, 1},
121 (intptr_t[]){1, 2},
122 NULL),
124 frames, 0, 2,
125 (intptr_t[]){HDRL_FRAMEITER_AXIS_FRAME, HDRL_FRAMEITER_AXIS_EXT},
126 (intptr_t[]){0, 2},
127 (intptr_t[]){1, 2},
128 NULL),
129 NULL,
130 };
131 cpl_test_error(CPL_ERROR_NONE);
132
133 /* ugly need to know length of other iterators
134 * TODO add broadcasting do avoid need for dim override */
135 subiters[2] =
136 hdrl_frameiter_new(frames, 0, 2,
137 (intptr_t[]){HDRL_FRAMEITER_AXIS_FRAME, HDRL_FRAMEITER_AXIS_EXT},
138 (intptr_t[]){0, 1},
139 (intptr_t[]){0, 0},
140 (intptr_t[]){1, hdrl_iter_length(subiters[0])}
141 );
142 cpl_test_error(CPL_ERROR_NONE);
143
144 hdrl_iter *it = hdrl_multiiter_new(3, subiters, 0);
145 cpl_test_nonnull(it);
146
147 int cnt = 0;
148 for (hdrl_frameiter_data ** h = hdrl_iter_next(it); h != NULL;
149 h = hdrl_iter_next(it)) {
150 for (size_t i = 0; i < 3; i++) {
151 int d;
152 cpl_test_eq(cpl_image_get_size_x(h[i]->image), 50);
153 if (i < 2) {
154 cpl_test_eq(cpl_image_get(h[i]->image, 1, 1, &d), cnt + 1);
155 cnt++;
156 }
157 else {
158 cpl_test_eq(cpl_image_get(h[i]->image, 1, 1, &d), 1);
159 }
160 cpl_image_delete(h[i]->image);
161 cpl_propertylist_delete(h[i]->plist);
162 }
163 }
164
165 hdrl_iter_delete(it);
166}
167
168
169void test_empty(cpl_frameset *frames)
170{
171 hdrl_iter *subiters[] = {
173 frames, HDRL_ITER_OWNS_DATA, 2,
174 (intptr_t[]){HDRL_FRAMEITER_AXIS_FRAME, HDRL_FRAMEITER_AXIS_EXT},
175 (intptr_t[]){0, 1},
176 (intptr_t[]){1, 1},
177 NULL),
179 frames, HDRL_ITER_OWNS_DATA, 1,
180 (intptr_t[]){HDRL_FRAMEITER_AXIS_EXT},
181 (intptr_t[]){1},
182 NULL,
183 NULL)
184 };
185 cpl_test_error(CPL_ERROR_NONE);
186
187 hdrl_iter *it = hdrl_multiiter_new(2, subiters, HDRL_ITER_ALLOW_EMPTY);
188 cpl_test_nonnull(it);
189
190 int cnt = 0;
191 for (hdrl_frameiter_data ** h = hdrl_iter_next(it); h != NULL;
192 h = hdrl_iter_next(it)) {
193 int d;
194 cpl_test_eq(cpl_image_get_size_x(h[0]->image), 50);
195 cpl_test_eq(cpl_image_get(h[0]->image, 1, 1, &d), cnt + 1);
196 if (cnt < hdrl_iter_length(subiters[1])) {
197 cpl_test_eq(cpl_image_get(h[1]->image, 1, 1, &d), cnt + 1);
198 /* take ownership test */
199 cpl_image_delete(h[1]->image);
200 h[1]->image = NULL;
201 }
202 else {
203 cpl_test_null(h[1]);
204 }
205 cnt++;
206 }
207
208 cpl_test_eq(hdrl_iter_length(subiters[0]), cnt);
209
210 hdrl_iter_delete(it);
211}
212
213/*----------------------------------------------------------------------------*/
217/*----------------------------------------------------------------------------*/
218int main(void)
219{
220 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
221
222 cpl_frameset *frames = create_frames();
223
224 test_invalid(frames);
225 cpl_test_error(CPL_ERROR_NONE);
226
227 test_basic( frames);
228 cpl_test_error(CPL_ERROR_NONE);
229
230 test_empty( frames);
231 cpl_test_error(CPL_ERROR_NONE);
232
233 cpl_frameset_delete(frames);
234 cpl_test_zero(system("rm -f hdrl_multiiter-test_*fits"));
235
236 cpl_test_error(CPL_ERROR_NONE);
237
238 return cpl_test_end(0);
239}
hdrl_iter * hdrl_frameiter_new(const cpl_frameset *frames, hdrl_iter_flags flags, intptr_t naxes, intptr_t *axes, intptr_t *offsets, intptr_t *strides, intptr_t *dims)
create iterator over cpl_frameset
hdrl_iter * hdrl_multiiter_new(intptr_t niters, hdrl_iter *iters[], hdrl_iter_flags flags)
iterate over multiple iterators