LAL  7.5.0.1-89842e6
H5FileIOTest.c
Go to the documentation of this file.
1 #include <lal/LALConfig.h>
2 
3 #ifndef LAL_HDF5_ENABLED
4 int main(void) { return 77; /* don't do any testing */ }
5 #else
6 
7 #include <limits.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <lal/LALStdlib.h>
11 #include <lal/AVFactories.h>
12 #include <lal/StringVector.h>
13 #include <lal/Date.h>
14 #include <lal/Units.h>
15 #include <lal/TimeSeries.h>
16 #include <lal/FrequencySeries.h>
17 #include <lal/H5FileIO.h>
18 
19 #define FNAME "test.h5"
20 #define GROUP "path/to"
21 #define DSET "testdset"
22 
23 #define NDIM 3
24 #define DIM0 2
25 #define DIM1 4
26 #define DIM2 3
27 #define NPTS (DIM0 * DIM1 * DIM2)
28 
29 #define EPOCH { 123456789, 987654321 }
30 static LIGOTimeGPS epoch = EPOCH;
31 static LIGOTimeGPS now;
32 
33 static UINT2 count;
34 
35 static int generate_int_data(void)
36 {
37  return rand() % SHRT_MAX;
38 }
39 
40 static float generate_float_data(void)
41 {
42  return rand() / (RAND_MAX + 1.0);
43 }
44 
45 static float complex generate_complex_data(void)
46 {
47  return rand() / (RAND_MAX + 1.0) + I * rand() / (RAND_MAX + 1.0);
48 }
49 
50 static char * generate_string_data(void)
51 {
52  const char *s;
53  switch (rand() % 18) {
54  case 0: s = "lorem"; break;
55  case 1: s = "ipsum"; break;
56  case 2: s = "dolor"; break;
57  case 3: s = "sit"; break;
58  case 4: s = "amet"; break;
59  case 5: s = "consectetur"; break;
60  case 6: s = "adipiscing"; break;
61  case 7: s = "elit"; break;
62  case 8: s = "sed"; break;
63  case 9: s = "eiusmod"; break;
64  case 10: s = "tempor"; break;
65  case 11: s = "incididunt"; break;
66  case 12: s = "ut"; break;
67  case 13: s = "labore"; break;
68  case 14: s = "et"; break;
69  case 15: s = "dolore"; break;
70  case 16: s = "magna"; break;
71  case 17: s = "aliqua"; break;
72  default:
73  fprintf(stderr, "Cannot get here!");
74  exit(1);
75  }
76  return XLALStringDuplicate(s);
77 }
78 
79 #define DEFINE_WRITE_FUNCTION(type) \
80  static int write_ ## type (type *x) { \
81  LALH5File *file; \
82  LALH5File *group; \
83  XLALGPSTimeNow(&now); \
84  ++count; \
85  file = XLALH5FileOpen(FNAME, "w"); \
86  XLALH5FileAddLIGOTimeGPSAttribute(file, "creation_time_gps", &now); \
87  XLALH5FileAddScalarAttribute(file, "test_count", &count, LAL_U2_TYPE_CODE); \
88  group = XLALH5GroupOpen(file, GROUP); \
89  XLALH5FileAddStringAttribute(group, "test_data_type", #type); \
90  XLALH5FileWrite ## type (group, DSET, x); \
91  XLALH5FileClose(group); \
92  XLALH5FileClose(file); \
93  return 0; \
94  }
95 
96 #define DEFINE_READ_FUNCTION(type) \
97  static type * read_ ## type (void) { \
98  type *x; \
99  LALH5File *file; \
100  LALH5File *group; \
101  LIGOTimeGPS creation_time; \
102  UINT2 cnt; \
103  char t[sizeof(#type)]; \
104  file = XLALH5FileOpen(FNAME, "r"); \
105  XLALH5FileQueryLIGOTimeGPSAttributeValue(&creation_time, file, "creation_time_gps"); \
106  if (XLALGPSCmp(&creation_time, &now)) { \
107  fprintf(stderr, " FAIL\n"); \
108  exit(1); /* fail */ \
109  } \
110  XLALH5FileQueryScalarAttributeValue(&cnt, file, "test_count"); \
111  if (cnt != count) { \
112  fprintf(stderr, " FAIL\n"); \
113  exit(1); /* fail */ \
114  } \
115  group = XLALH5GroupOpen(file, GROUP); \
116  XLALH5FileQueryStringAttributeValue(t, sizeof(t), group, "test_data_type"); \
117  XLALH5FileClose(group); \
118  if (strcmp(t, #type)) { \
119  fprintf(stderr, " FAIL\n"); \
120  exit(1); /* fail */ \
121  } \
122  x = XLALH5FileRead ## type (file, GROUP "/" DSET); \
123  XLALH5FileClose(file); \
124  return x; \
125  }
126 
127 #define DEFINE_TEST_FUNCTION(type) \
128  static void test_ ## type (void) { \
129  type *orig; \
130  type *copy; \
131  fprintf(stderr, "Testing Read/Write of %s...", #type); \
132  orig = create_ ## type (); \
133  write_ ## type (orig); \
134  copy = read_ ## type (); \
135  if (compare_ ## type (orig, copy)) { \
136  fprintf(stderr, " FAIL\n"); \
137  exit(1); /* fail */ \
138  } \
139  XLALDestroy ## type (copy); \
140  XLALDestroy ## type (orig); \
141  fprintf(stderr, " PASS\n"); \
142  }
143 
144 /* VECTOR ROUTINES */
145 
146 #define DEFINE_CREATE_VECTOR_FUNCTION(type) \
147  static type * create_ ## type (void) { \
148  type *v; \
149  size_t i; \
150  v = XLALCreate ## type (NPTS); \
151  for (i = 0; i < NPTS; ++i) \
152  v->data[i] = GENERATE_DATA(); \
153  return v; \
154  }
155 
156 #define DEFINE_COMPARE_VECTOR_FUNCTION(type) \
157  static int compare_ ## type (type *v1, type *v2) { \
158  if (v1->length != v2->length) \
159  return 1; \
160  return memcmp(v1->data, v2->data, v1->length * sizeof(*v1->data)); \
161  }
162 
163 static LALStringVector * create_StringVector(void)
164 {
165  LALStringVector *v;
166  size_t i;
167  v = XLALCreateEmptyStringVector(NPTS);
168  for (i = 0; i < NPTS; ++i)
169  v->data[i] = generate_string_data();
170  return v;
171 }
172 
173 static int compare_StringVector(LALStringVector *v1, LALStringVector *v2)
174 {
175  if (v1->length != v2->length)
176  return 1;
177  for (size_t i = 0; i < v1->length; ++i)
178  if (strcmp(v1->data[i], v2->data[i]) != 0)
179  return 1;
180  return 0;
181 }
182 
183 /* ARRAY ROUTINES */
184 
185 #define DEFINE_CREATE_ARRAY_FUNCTION(type) \
186  static type * create_ ## type (void) { \
187  type *a; \
188  size_t i; \
189  a = XLALCreate ## type ## L(NDIM, DIM0, DIM1, DIM2); \
190  for (i = 0; i < NPTS; ++i) \
191  a->data[i] = GENERATE_DATA(); \
192  return a; \
193  }
194 
195 #define DEFINE_COMPARE_ARRAY_FUNCTION(type) \
196  static int compare_ ## type (type *a1, type *a2) { \
197  size_t sz1 = 1, sz2 = 1, i; \
198  if (a1->dimLength->length != a2->dimLength->length) \
199  return 1; \
200  for (i = 0; i < a1->dimLength->length; ++i) { \
201  if (a1->dimLength->data[i] != a2->dimLength->data[i]) \
202  return 1; \
203  sz1 *= a1->dimLength->data[i]; \
204  sz2 *= a2->dimLength->data[i]; \
205  } \
206  if (sz1 != sz2) \
207  return 1; \
208  return memcmp(a1->data, a2->data, sz1 * sizeof(*a1->data)); \
209  }
210 
211 /* TIME/FREQUENCY SERIES ROUTINES */
212 
213 #define DEFINE_CREATE_TIME_FREQUENCY_SERIES_FUNCTION(type) \
214  static type * create_ ## type (void) { \
215  type *s; \
216  size_t i; \
217  s = XLALCreate ## type ("test_" #type, &epoch, 0.0, 0.1, &lalStrainUnit, NPTS); \
218  for (i = 0; i < NPTS; ++i) \
219  s->data->data[i] = GENERATE_DATA(); \
220  return s; \
221  }
222 
223 #define DEFINE_COMPARE_TIME_SERIES_FUNCTION(type) \
224  static int compare_ ## type (type *s1, type *s2) { \
225  int c; \
226  if (s1->data->length != s2->data->length) \
227  return 1; \
228  if ((c = strcmp(s1->name, s2->name))) \
229  return c; \
230  if ((c = (s1->deltaT > s2->deltaT) - (s1->deltaT < s2->deltaT))) \
231  return c; \
232  if ((c = (s1->f0 > s2->f0) - (s1->f0 < s2->f0))) \
233  return c; \
234  if (XLALUnitCompare(&s1->sampleUnits, &s2->sampleUnits)) \
235  return 1; \
236  return memcmp(s1->data->data, s2->data->data, s1->data->length * sizeof(*s1->data->data)); \
237  }
238 
239 #define DEFINE_COMPARE_FREQUENCY_SERIES_FUNCTION(type) \
240  static int compare_ ## type (type *s1, type *s2) { \
241  int c; \
242  if (s1->data->length != s2->data->length) \
243  return 1; \
244  if ((c = strcmp(s1->name, s2->name))) \
245  return c; \
246  if ((c = (s1->deltaF > s2->deltaF) - (s1->deltaF < s2->deltaF))) \
247  return c; \
248  if ((c = (s1->f0 > s2->f0) - (s1->f0 < s2->f0))) \
249  return c; \
250  if (XLALUnitCompare(&s1->sampleUnits, &s2->sampleUnits)) \
251  return 1; \
252  return memcmp(s1->data->data, s2->data->data, s1->data->length * sizeof(*s1->data->data)); \
253  }
254 
255 #define DEFINE_VECTOR_FUNCTIONS(type) \
256  DEFINE_WRITE_FUNCTION(type) \
257  DEFINE_READ_FUNCTION(type) \
258  DEFINE_COMPARE_VECTOR_FUNCTION(type) \
259  DEFINE_CREATE_VECTOR_FUNCTION(type) \
260  DEFINE_TEST_FUNCTION(type)
261 
262 #define DEFINE_ARRAY_FUNCTIONS(type) \
263  DEFINE_WRITE_FUNCTION(type) \
264  DEFINE_READ_FUNCTION(type) \
265  DEFINE_COMPARE_ARRAY_FUNCTION(type) \
266  DEFINE_CREATE_ARRAY_FUNCTION(type) \
267  DEFINE_TEST_FUNCTION(type)
268 
269 #define DEFINE_TIME_SERIES_FUNCTIONS(type) \
270  DEFINE_WRITE_FUNCTION(type) \
271  DEFINE_READ_FUNCTION(type) \
272  DEFINE_COMPARE_TIME_SERIES_FUNCTION(type) \
273  DEFINE_CREATE_TIME_FREQUENCY_SERIES_FUNCTION(type) \
274  DEFINE_TEST_FUNCTION(type)
275 
276 #define DEFINE_FREQUENCY_SERIES_FUNCTIONS(type) \
277  DEFINE_WRITE_FUNCTION(type) \
278  DEFINE_READ_FUNCTION(type) \
279  DEFINE_COMPARE_FREQUENCY_SERIES_FUNCTION(type) \
280  DEFINE_CREATE_TIME_FREQUENCY_SERIES_FUNCTION(type) \
281  DEFINE_TEST_FUNCTION(type)
282 
283 #define GENERATE_DATA generate_int_data
284 DEFINE_VECTOR_FUNCTIONS(CHARVector)
285 DEFINE_VECTOR_FUNCTIONS(INT2Vector)
286 DEFINE_VECTOR_FUNCTIONS(INT4Vector)
287 DEFINE_VECTOR_FUNCTIONS(INT8Vector)
288 DEFINE_VECTOR_FUNCTIONS(UINT2Vector)
289 DEFINE_VECTOR_FUNCTIONS(UINT4Vector)
290 DEFINE_VECTOR_FUNCTIONS(UINT8Vector)
291 #undef GENERATE_DATA
292 #define GENERATE_DATA generate_float_data
293 DEFINE_VECTOR_FUNCTIONS(REAL4Vector)
294 DEFINE_VECTOR_FUNCTIONS(REAL8Vector)
295 #undef GENERATE_DATA
296 #define GENERATE_DATA generate_complex_data
297 DEFINE_VECTOR_FUNCTIONS(COMPLEX8Vector)
298 DEFINE_VECTOR_FUNCTIONS(COMPLEX16Vector)
299 #undef GENERATE_DATA
300 
301 #define StringVector LALStringVector
302 DEFINE_WRITE_FUNCTION(StringVector)
303 DEFINE_READ_FUNCTION(StringVector)
304 DEFINE_TEST_FUNCTION(StringVector)
305 
306 #define GENERATE_DATA generate_int_data
307 DEFINE_ARRAY_FUNCTIONS(INT2Array)
308 DEFINE_ARRAY_FUNCTIONS(INT4Array)
309 DEFINE_ARRAY_FUNCTIONS(INT8Array)
310 DEFINE_ARRAY_FUNCTIONS(UINT2Array)
311 DEFINE_ARRAY_FUNCTIONS(UINT4Array)
312 DEFINE_ARRAY_FUNCTIONS(UINT8Array)
313 #undef GENERATE_DATA
314 #define GENERATE_DATA generate_float_data
315 DEFINE_ARRAY_FUNCTIONS(REAL4Array)
316 DEFINE_ARRAY_FUNCTIONS(REAL8Array)
317 #undef GENERATE_DATA
318 #define GENERATE_DATA generate_complex_data
319 DEFINE_ARRAY_FUNCTIONS(COMPLEX8Array)
320 DEFINE_ARRAY_FUNCTIONS(COMPLEX16Array)
321 #undef GENERATE_DATA
322 
323 #define GENERATE_DATA generate_int_data
324 DEFINE_TIME_SERIES_FUNCTIONS(INT2TimeSeries)
325 DEFINE_TIME_SERIES_FUNCTIONS(INT4TimeSeries)
326 DEFINE_TIME_SERIES_FUNCTIONS(INT8TimeSeries)
327 DEFINE_TIME_SERIES_FUNCTIONS(UINT2TimeSeries)
328 DEFINE_TIME_SERIES_FUNCTIONS(UINT4TimeSeries)
329 DEFINE_TIME_SERIES_FUNCTIONS(UINT8TimeSeries)
330 #undef GENERATE_DATA
331 #define GENERATE_DATA generate_float_data
332 DEFINE_TIME_SERIES_FUNCTIONS(REAL4TimeSeries)
333 DEFINE_TIME_SERIES_FUNCTIONS(REAL8TimeSeries)
334 #undef GENERATE_DATA
335 #define GENERATE_DATA generate_complex_data
336 DEFINE_TIME_SERIES_FUNCTIONS(COMPLEX8TimeSeries)
337 DEFINE_TIME_SERIES_FUNCTIONS(COMPLEX16TimeSeries)
338 #undef GENERATE_DATA
339 
340 #define GENERATE_DATA generate_float_data
341 DEFINE_FREQUENCY_SERIES_FUNCTIONS(REAL4FrequencySeries)
342 DEFINE_FREQUENCY_SERIES_FUNCTIONS(REAL8FrequencySeries)
343 #undef GENERATE_DATA
344 #define GENERATE_DATA generate_complex_data
345 DEFINE_FREQUENCY_SERIES_FUNCTIONS(COMPLEX8FrequencySeries)
346 DEFINE_FREQUENCY_SERIES_FUNCTIONS(COMPLEX16FrequencySeries)
347 #undef GENERATE_DATA
348 
349 int main(void)
350 {
352 
353  test_CHARVector();
354  test_INT2Vector();
355  test_INT4Vector();
356  test_INT8Vector();
357  test_UINT2Vector();
358  test_UINT4Vector();
359  test_UINT8Vector();
360  test_REAL4Vector();
361  test_REAL8Vector();
362  test_COMPLEX8Vector();
363  test_COMPLEX16Vector();
364  test_StringVector();
365 
366  test_INT2Array();
367  test_INT4Array();
368  test_INT8Array();
369  test_UINT2Array();
370  test_UINT4Array();
371  test_UINT8Array();
372  test_REAL4Array();
373  test_REAL8Array();
374  test_COMPLEX8Array();
375  test_COMPLEX16Array();
376 
377  test_INT2TimeSeries();
378  test_INT4TimeSeries();
379  test_INT8TimeSeries();
380  test_UINT2TimeSeries();
381  test_UINT4TimeSeries();
382  test_UINT8TimeSeries();
383  test_REAL4TimeSeries();
384  test_REAL8TimeSeries();
385  test_COMPLEX8TimeSeries();
386  test_COMPLEX16TimeSeries();
387 
388  test_REAL4FrequencySeries();
389  test_REAL8FrequencySeries();
390  test_COMPLEX8FrequencySeries();
391  test_COMPLEX16FrequencySeries();
392 
394  return 0;
395 }
396 
397 #endif /* ! HAVE_HDF5 */
int main(void)
Definition: H5FileIOTest.c:4
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
#define fprintf
uint16_t UINT2
Two-byte unsigned integer.
char * XLALStringDuplicate(const char *s)
Like strdup but uses LAL allocation routines (free with LALFree).
Definition: LALString.c:89
LALStringVector * XLALCreateEmptyStringVector(UINT4 length)
Create an empty string vector of the given length.
Definition: StringVector.c:164
void XLALAbortErrorHandler(const char *func, const char *file, int line, int errnum)
The XLAL error handler that raises SIGABRT.
Definition: XLALError.c:599
XLALErrorHandlerType * XLALSetErrorHandler(XLALErrorHandlerType *newHandler)
Sets the error handler to a new handler and returns the old handler.
Definition: XLALError.c:372
Vector of type CHAR, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:73
Multidimentional array of COMPLEX16, see DATATYPE-Array types for more details.
Definition: LALDatatypes.h:238
See DATATYPE-FrequencySeries types for documentation.
Definition: LALDatatypes.h:909
Time series of COMPLEX16 data, see DATATYPE-TimeSeries types for more details.
Definition: LALDatatypes.h:600
Vector of type COMPLEX16, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:172
Multidimentional array of COMPLEX8, see DATATYPE-Array types for more details.
Definition: LALDatatypes.h:232
See DATATYPE-FrequencySeries types for documentation.
Definition: LALDatatypes.h:899
Time series of COMPLEX8 data, see DATATYPE-TimeSeries types for more details.
Definition: LALDatatypes.h:590
Vector of type COMPLEX8, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:163
Multidimentional array of INT2, see DATATYPE-Array types for more details.
Definition: LALDatatypes.h:184
Time series of INT2 data, see DATATYPE-TimeSeries types for more details.
Definition: LALDatatypes.h:511
Vector of type INT2, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:91
Multidimentional array of INT4, see DATATYPE-Array types for more details.
Definition: LALDatatypes.h:196
Time series of INT4 data, see DATATYPE-TimeSeries types for more details.
Definition: LALDatatypes.h:530
Vector of type INT4, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:109
Multidimentional array of INT8, see DATATYPE-Array types for more details.
Definition: LALDatatypes.h:208
Time series of INT8 data, see DATATYPE-TimeSeries types for more details.
Definition: LALDatatypes.h:550
Vector of type INT8, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:127
Vector of type CHAR*, ie 'strings', see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:82
UINT4 length
Number of elements in array.
Definition: LALDatatypes.h:86
CHAR ** data
Pointer to the data array.
Definition: LALDatatypes.h:87
Epoch relative to GPS epoch, see LIGOTimeGPS type for more details.
Definition: LALDatatypes.h:458
Multidimentional array of REAL4, see DATATYPE-Array types for more details.
Definition: LALDatatypes.h:220
See DATATYPE-FrequencySeries types for documentation.
Definition: LALDatatypes.h:879
Time series of REAL4 data, see DATATYPE-TimeSeries types for more details.
Definition: LALDatatypes.h:570
Vector of type REAL4, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:145
Multidimentional array of REAL8, see DATATYPE-Array types for more details.
Definition: LALDatatypes.h:226
See DATATYPE-FrequencySeries types for documentation.
Definition: LALDatatypes.h:889
Time series of REAL8 data, see DATATYPE-TimeSeries types for more details.
Definition: LALDatatypes.h:580
Vector of type REAL8, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:154
Multidimentional array of UINT2, see DATATYPE-Array types for more details.
Definition: LALDatatypes.h:190
Time series of UINT2 data, see DATATYPE-TimeSeries types for more details.
Definition: LALDatatypes.h:521
Vector of type UINT2, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:100
Multidimentional array of UINT4, see DATATYPE-Array types for more details.
Definition: LALDatatypes.h:202
Time series of UINT4 data, see DATATYPE-TimeSeries types for more details.
Definition: LALDatatypes.h:540
Vector of type UINT4, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:118
Multidimentional array of UINT8, see DATATYPE-Array types for more details.
Definition: LALDatatypes.h:214
Time series of UINT8 data, see DATATYPE-TimeSeries types for more details.
Definition: LALDatatypes.h:560
Vector of type UINT8, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:136
enum @1 epoch