LAL  7.5.0.1-bede9b2
SequenceTest.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 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #include <lal/LALDatatypes.h>
24 #include <lal/Sequence.h>
25 #include <lal/LALMalloc.h>
26 
27 static REAL4Sequence *random_sequence(size_t length)
28 {
30 
31  while(length--)
32  s->data[length] = rand() / (float) RAND_MAX;
33  return(s);
34 }
35 
36 
37 static INT4Sequence *sequential_sequence(size_t length)
38 {
40 
41  while(length--)
42  s->data[length] = length;
43  return(s);
44 }
45 
46 
47 static int cmp_real4(REAL4 *a, REAL4 *b, size_t n)
48 {
49  REAL4 d;
50 
51  while(n--) {
52  d = *a - *b;
53  if(d < 0.0)
54  return(-1);
55  if(d > 0.0)
56  return(+1);
57  }
58  return(0);
59 }
60 
61 
62 static int cmp(REAL4Sequence *a, REAL4Sequence *b)
63 {
64  int result;
65 
66  result = cmp_real4(a->data, b->data, (b->length < a->length) ? b->length : a->length);
67  if(result)
68  return(result);
69  return(a->length - b->length);
70 }
71 
72 
73 int main(void)
74 {
75  REAL4Sequence *x, *y;
76  INT4Sequence *a;
77  int i;
78 
79  /*
80  * Destroy
81  */
82 
83  /* NULL pointer */
85 
86  /* Incompletely/Incorrectly initialized structure */
87  x = XLALCalloc(1, sizeof(*x));
88  x->length = 1000;
89  x->data = NULL;
91 
92 
93  /*
94  * Create
95  */
96 
97  /* try segfaulting on array access */
99  x->data[0] = 1.0;
100  if((x->length != 1) || (x->data[0] != 1.0)) {
101  fprintf(stderr, "Create test 1 failed\n");
102  exit(1);
103  }
105 
106  /*
107  * Cut
108  */
109 
110  /* byte-by-byte compare extraction of a piece */
111  x = random_sequence(1024);
112  y = XLALCutREAL4Sequence(x, 256, 512);
113  if(cmp_real4(x->data + 256, y->data, 512)) {
114  fprintf(stderr, "Cut test 1 failed\n");
115  exit(1);
116  }
118 
119  /* byte-by-byte compare extraction of entire sequence */
120  y = XLALCutREAL4Sequence(x, 0, 1024);
121  if(cmp_real4(x->data, y->data, 1024)) {
122  fprintf(stderr, "Cut test 2 failed\n");
123  exit(1);
124  }
127 
128  /*
129  * Copy
130  */
131 
132  /* byte-by-byte compare copy */
133  x = random_sequence(1024);
135  if(cmp(x, y)) {
136  fprintf(stderr, "Copy test 1 failed\n");
137  exit(1);
138  }
141 
142  /*
143  * Shift
144  */
145 
146  /* test a positive shift */
147  x = random_sequence(1024);
150  for(i = 0; i < 512; i++)
151  if(y->data[i] != 0.0) {
152  fprintf(stderr, "Shift test 1a failed\n");
153  exit(1);
154  }
155  if(cmp_real4(x->data, y->data + 512, 512)) {
156  fprintf(stderr, "Shift test 1b failed\n");
157  exit(1);
158  }
159 
160  /* test a subsequent negative shift */
161  XLALShiftREAL4Sequence(y, -768);
162  for(i = 256; i < 1024; i++)
163  if(y->data[i] != 0.0) {
164  fprintf(stderr, "Shift test 2a failed\n");
165  exit(1);
166  }
167  if(cmp_real4(x->data + 256, y->data, 256)) {
168  fprintf(stderr, "Shift test 2b failed\n");
169  exit(1);
170  }
173 
174  /*
175  * Resize
176  */
177 
178  /* test resize to subset */
179  a = sequential_sequence(1024);
180  XLALResizeINT4Sequence(a, 256, 512);
181  for(i = 0; i < (int) a->length; i++)
182  if(a->data[i] != i + 256) {
183  fprintf(stderr, "Resize test 1a failed\n");
184  exit(1);
185  }
186  if(a->length != 512) {
187  fprintf(stderr, "Resize test 1b failed\n");
188  exit(1);
189  }
191 
192  /* test resize to superset */
193  a = sequential_sequence(16);
194  for(i = 0; i < (int) a->length; i++)
195  fprintf(stdout, "%d: %d\n", i, a->data[i]);
196  fprintf(stdout, "\n");
197  XLALResizeINT4Sequence(a, -8, 32);
198  for(i = 0; i < (int) a->length; i++)
199  fprintf(stdout, "%d: %d\n", i, a->data[i]);
200  if(a->length != 32) {
201  fprintf(stderr, "Resize test 2a failed\n");
202  exit(1);
203  }
204  for(i = 0; i < 8; i++)
205  if(a->data[i] != 0) {
206  fprintf(stderr, "Resize test 2b failed\n");
207  exit(1);
208  }
209  for(; i < 24; i++)
210  if(a->data[i] != i - 8) {
211  fprintf(stderr, "Resize test 2c failed\n");
212  exit(1);
213  }
214  for(; i < 32; i++)
215  if(a->data[i] != 0) {
216  fprintf(stderr, "Resize test 2d failed\n");
217  exit(1);
218  }
220 
221  /*
222  * Sum
223  */
224 
225  a = sequential_sequence(1024);
226  if(XLALINT4SequenceSum(a, 0, a->length) != (1023 + 0) * 1024 / 2) {
227  fprintf(stderr, "Sum test 1 failed\n");
228  exit(1);
229  }
231 
232  /*
233  * Sum squares
234  */
235 
236  a = sequential_sequence(1024);
237  if(XLALINT4SequenceSumSquares(a, 0, a->length) != 1023 * (1023 + 1) * (2 * 1023 + 1) / 6) {
238  fprintf(stderr, "Sum squares test 1 failed\n");
239  exit(1);
240  }
242 
243  /*
244  * Success
245  */
246 
247  exit(0);
248 }
static REAL4Sequence * random_sequence(size_t length)
Definition: SequenceTest.c:27
static int cmp_real4(REAL4 *a, REAL4 *b, size_t n)
Definition: SequenceTest.c:47
static int cmp(REAL4Sequence *a, REAL4Sequence *b)
Definition: SequenceTest.c:62
int main(void)
Definition: SequenceTest.c:73
static INT4Sequence * sequential_sequence(size_t length)
Definition: SequenceTest.c:37
#define fprintf
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
REAL4Sequence * XLALCutREAL4Sequence(REAL4Sequence *sequence, size_t first, size_t length)
void XLALDestroyINT4Sequence(INT4Sequence *sequence)
INT4 XLALINT4SequenceSum(const INT4Sequence *sequence, size_t first, size_t count)
INT4Sequence * XLALResizeINT4Sequence(INT4Sequence *sequence, int first, size_t length)
REAL4Sequence * XLALCopyREAL4Sequence(REAL4Sequence *sequence)
void XLALDestroyREAL4Sequence(REAL4Sequence *sequence)
INT4Sequence * XLALCreateINT4Sequence(size_t length)
REAL4Sequence * XLALCreateREAL4Sequence(size_t length)
UINT4 XLALINT4SequenceSumSquares(const INT4Sequence *sequence, size_t first, size_t count)
void XLALShiftREAL4Sequence(REAL4Sequence *sequence, int count)
Vector of type INT4, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:109
INT4 * data
Pointer to the data array.
Definition: LALDatatypes.h:114
Vector of type REAL4, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:145
REAL4 * data
Pointer to the data array.
Definition: LALDatatypes.h:150
UINT4 length
Number of elements in array.
Definition: LALDatatypes.h:149