LAL  7.5.0.1-89842e6
FrequencySeriesTest.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007 Bernd Machenschalk, Kipp Cannon
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 
20 /*
21  * Note that low-level sequence manipulation is tested in the Sequence
22  * verification code. The object here is mostly to test meta-data
23  * manipulation.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 
29 #include <lal/Date.h>
30 #include <lal/LALDatatypes.h>
31 #include <lal/Sequence.h>
32 #include <lal/FrequencySeries.h>
33 #include <lal/Units.h>
34 
36 
37 
39 {
41 
42  while(n--)
43  s->data->data[n] = rand() / (float) RAND_MAX;
44  return(s);
45 }
46 
47 
49 {
51 
52  while(n--)
53  s->data->data[n] = n;
54  return(s);
55 }
56 
57 
58 static int cmp_real4(REAL4 *a, REAL4 *b, size_t n)
59 {
60  REAL4 d;
61 
62  while(n--) {
63  d = *a - *b;
64  if(d < 0.0)
65  return(-1);
66  if(d > 0.0)
67  return(+1);
68  }
69  return(0);
70 }
71 
72 
73 int main(void)
74 {
77  int i;
78 
79  /*
80  * Destroy
81  */
82 
83  /* NULL pointer */
85 
86  /* Incompletely/Incorrectly initialized structure */
87  x = XLALCalloc(1, sizeof(*x));
89  x = XLALCalloc(1, sizeof(*x));
90  x->data = XLALCalloc(1, sizeof(*x->data));
92 
93 
94  /*
95  * Create
96  */
97 
98  /* try segfaulting on array access */
99  x = XLALCreateREAL4FrequencySeries("blah", &gps_zero, 0.0, 1.0 / 1, &lalDimensionlessUnit, 1);
100  x->data->data[0] = 1.0;
101  if((x->f0 != 0.0) || (x->deltaF != 1.0) || (x->data->length != 1) || (x->data->data[0] != 1.0)) {
102  fprintf(stderr, "Create test 1 failed\n");
103  exit(1);
104  }
106 
107  /*
108  * Cut
109  */
110 
111  /* check metadata */
112  x = random_frequencyseries(1024);
113  y = XLALCutREAL4FrequencySeries(x, 256, 512);
114  if((y->deltaF != x->deltaF) || (y->f0 != x->f0 + 256 * x->deltaF) || cmp_real4(x->data->data + 256, y->data->data, 512) || (XLALGPSDiff(&x->epoch, &y->epoch) != 0)) {
115  fprintf(stderr, "Cut test 1 failed\n");
116  exit(1);
117  }
120 
121  /*
122  * Shrink
123  */
124 
125  /* check metadata */
127  XLALShrinkINT4FrequencySeries(a, 256, 512);
128  for(i = 0; i < (int) a->data->length; i++)
129  if(a->data->data[i] != i + 256) {
130  fprintf(stderr, "Resize test 1a failed\n");
131  exit(1);
132  }
133  if((a->data->length != 512) || (a->f0 != 256 * a->deltaF)) {
134  fprintf(stderr, "Resize test 1b failed\n");
135  exit(1);
136  }
138 
139  /*
140  * Add
141  */
142 
143  /* add a series to itself */
146  for(i = 0; i < (int) a->data->length; i++)
147  if(a->data->data[i] != 2 * i) {
148  fprintf(stderr, "Add test 1 failed\n");
149  exit(1);
150  }
152 
153  /* add identical data */
154  x = random_frequencyseries(1024);
155  y = XLALCutREAL4FrequencySeries(x, 0, x->data->length);
157  for(i = 0; i < (int) x->data->length; i++)
158  if(x->data->data[i] != 2 * y->data->data[i]) {
159  fprintf(stderr, "Add test 2 failed\n");
160  exit(1);
161  }
164 
165  /*
166  * Success
167  */
168 
169  exit(0);
170 }
static int cmp_real4(REAL4 *a, REAL4 *b, size_t n)
static REAL4FrequencySeries * random_frequencyseries(size_t n)
static INT4FrequencySeries * sequential_frequencyseries(size_t n)
int main(void)
static LIGOTimeGPS gps_zero
#define fprintf
INT4FrequencySeries * XLALCreateINT4FrequencySeries(const CHAR *name, const LIGOTimeGPS *epoch, REAL8 f0, REAL8 deltaF, const LALUnit *sampleUnits, size_t length)
REAL4FrequencySeries * XLALAddREAL4FrequencySeries(REAL4FrequencySeries *arg1, const REAL4FrequencySeries *arg2)
INT4FrequencySeries * XLALShrinkINT4FrequencySeries(INT4FrequencySeries *series, size_t first, size_t length)
REAL4FrequencySeries * XLALCreateREAL4FrequencySeries(const CHAR *name, const LIGOTimeGPS *epoch, REAL8 f0, REAL8 deltaF, const LALUnit *sampleUnits, size_t length)
void XLALDestroyREAL4FrequencySeries(REAL4FrequencySeries *series)
REAL4FrequencySeries * XLALCutREAL4FrequencySeries(const REAL4FrequencySeries *series, size_t first, size_t length)
INT4FrequencySeries * XLALAddINT4FrequencySeries(INT4FrequencySeries *arg1, const INT4FrequencySeries *arg2)
void XLALDestroyINT4FrequencySeries(INT4FrequencySeries *series)
#define LIGOTIMEGPSZERO
Zero-initializer for LIGOTimeGPS structs.
Definition: LALDatatypes.h:464
float REAL4
Single precision real floating-point number (4 bytes).
#define XLALCalloc(m, n)
Definition: LALMalloc.h:45
static const INT4 a
Definition: Random.c:79
const LALUnit lalDimensionlessUnit
dimensionless units
Definition: UnitDefs.c:156
REAL8 XLALGPSDiff(const LIGOTimeGPS *t1, const LIGOTimeGPS *t0)
Difference between two GPS times as double.
Definition: XLALTime.c:157
See DATATYPE-FrequencySeries types for documentation.
Definition: LALDatatypes.h:839
INT4Sequence * data
Definition: LALDatatypes.h:845
INT4 * data
Pointer to the data array.
Definition: LALDatatypes.h:114
Epoch relative to GPS epoch, see LIGOTimeGPS type for more details.
Definition: LALDatatypes.h:458
See DATATYPE-FrequencySeries types for documentation.
Definition: LALDatatypes.h:879
REAL4Sequence * data
Definition: LALDatatypes.h:885
REAL4 * data
Pointer to the data array.
Definition: LALDatatypes.h:150