BRL-CAD
bn_list.c
Go to the documentation of this file.
1 /* T E S T _ L I S T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2011-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "bn.h"
28 #include "bu.h"
29 #include "plot3.h"
30 
31 #define BUFFER_SIZE 2000
32 #define MAX_POINTS 30
33 
34 /*
35  * Helper functions
36  */
37 
38 /* Initialises the buffers and opens a memory file for the plot output */
39 FILE *
40 initialise_buffers(char *expected_buf)
41 {
42  FILE *temp;
43 
44  if (expected_buf)
45  memset(expected_buf, 0, BUFFER_SIZE);
46 
47  temp = bu_temp_file(NULL, 0);
48 
49  if (temp == NULL)
50  return 0;
51 
52  rewind(temp);
53 
54  return temp;
55 }
56 
57 /* Checks the result against the expected and closes the result fd */
58 int
59 compare_result(char *expected_buf, FILE *result_fd)
60 {
61  size_t ret;
62  char result_buf[BUFFER_SIZE];
63  memset(result_buf, 0, BUFFER_SIZE);
64 
65  rewind(result_fd);
66  ret = fread(result_buf, sizeof(char), BUFFER_SIZE, result_fd);
67  if (ret < BUFFER_SIZE)
68  perror("fread");
69 
70  fclose(result_fd);
71 
72  return bu_strncmp(expected_buf, result_buf, BUFFER_SIZE) == 0;
73 }
74 
75 /* Checks the result length and closes the result fd */
76 int
77 check_result_len(FILE *result_fd)
78 {
79  size_t ret;
80  char result_buf[BUFFER_SIZE+1];
81  memset(result_buf, 0, BUFFER_SIZE+1);
82 
83  rewind(result_fd);
84  ret = fread(result_buf, sizeof(char), BUFFER_SIZE, result_fd);
85  if (ret < BUFFER_SIZE)
86  perror("fread");
87  fclose(result_fd);
88 
89  return strlen(result_buf);
90 }
91 
92 /* Converts an array of doubles to integers */
93 void
94 convert_points(double *d_values, int *i_values, int count)
95 {
96  while (--count > 0) {
97  *i_values++ = (int) *d_values++;
98  }
99 }
100 
101 
102 /*
103  * Tests
104  */
105 
106 /* Produces the expected output of tp_i2list based on the input */
107 void
108 make_tp_i2list_expected(char *buf, int buflen, int *x, int *y, int npoints)
109 {
110  int chars_written;
111 
112  if (npoints <= 0)
113  return;
114 
115  chars_written = snprintf(buf, buflen, "m %d %d\n", *x++, *y++);
116  buf += chars_written;
117  buflen -= chars_written;
118 
119  while (--npoints > 0) {
120  chars_written = snprintf(buf, buflen, "n %d %d\n", *x++, *y++);
121  buf += chars_written;
122  buflen -= chars_written;
123  }
124 }
125 
126 /* This test ensures the tp_i2list function produces the output it's supposed to */
127 int
128 test_tp_i2list(int *x, int *y, int npoints)
129 {
130  char expected_buf[BUFFER_SIZE];
131  FILE *buf_out = initialise_buffers(expected_buf);
132 
133  if (!buf_out) {
134  printf("File Invalid\n");
135  return 0;
136  }
137 
138  make_tp_i2list_expected(expected_buf, BUFFER_SIZE, x, y, npoints);
139 
140  /* Set plot output to human readable */
141  pl_setOutputMode(PL_OUTPUT_MODE_TEXT);
142 
143  /* Plot the points */
144  tp_i2list(buf_out, x, y, npoints);
145 
146  return compare_result(expected_buf, buf_out);
147 }
148 
149 
150 /* Produces the expected output of tp_2list based on the input */
151 void
152 make_tp_2list_expected(char *buf, int buflen, double *x, double *y, int npoints)
153 {
154  int chars_written;
155 
156  if (npoints <= 0)
157  return;
158 
159  chars_written = snprintf(buf, buflen, "o %g %g\n", *x++, *y++);
160  buf += chars_written;
161  buflen -= chars_written;
162 
163  while (--npoints > 0) {
164  chars_written = snprintf(buf, buflen, "q %g %g\n", *x++, *y++);
165  buf += chars_written;
166  buflen -= chars_written;
167  }
168 }
169 
170 /* This test ensures the tp_2list function produces the output it's supposed to */
171 int
172 test_tp_2list(double *x, double *y, int npoints)
173 {
174  char expected_buf[BUFFER_SIZE];
175  FILE *buf_out = initialise_buffers(expected_buf);
176 
177  if (!buf_out) {
178  printf("File Invalid\n");
179  return 0;
180  }
181 
182  make_tp_2list_expected(expected_buf, BUFFER_SIZE, x, y, npoints);
183 
184  pl_setOutputMode(PL_OUTPUT_MODE_TEXT);
185  tp_2list(buf_out, x, y, npoints);
186 
187  return compare_result(expected_buf, buf_out);
188 }
189 
190 
191 /*
192  * This test tests the tp_2mlist marker function to ensure it
193  * doesn't have any SEGFAULTs when invalid values are put in, and
194  * produces output when required and not when none is required
195  */
196 int
197 test_tp_2mlist(double *x, double *y, int npoints)
198 {
199  FILE *buf_out = initialise_buffers(NULL);
200  int flag, mark, interval, size;
201 
202  if (!buf_out) {
203  printf("File Invalid\n");
204  return 0;
205  }
206 
207  pl_setOutputMode(PL_OUTPUT_MODE_TEXT);
208 
209  /* Test some invalid values (if it gets past this section, no SEGFAULTS have occurred) */
210  flag = -5;
211  mark = -5;
212  interval = -5;
213  size = -5;
214  for (; flag < 5; flag++)
215  for (; mark < 5; mark++)
216  for (; interval < 5; interval++)
217  for (; size < 5; size++)
218  tp_2mlist(buf_out, x, y, npoints, flag, mark, interval, size);
219  fclose(buf_out);
220 
221  /* Check it doesn't produce output when there are no points */
222  flag = 1;
223  mark = 1;
224  interval = 1;
225  size = 1;
226 
227  buf_out = initialise_buffers(NULL);
228  tp_2mlist(buf_out, x, y, 0, flag, mark, interval, size);
229 
230  if (check_result_len(buf_out) > 0) {
231  printf("Produced output when no points were given.\n");
232  return 0;
233  }
234 
235  /* If we don't actually have any points then nothing else is correct */
236  if (npoints <= 0)
237  return 1;
238 
239 
240  /* Check it doesn't produce output if all output is turned off */
241  flag = 0;
242  mark = 1;
243  interval = 1;
244  size = 1;
245 
246  buf_out = initialise_buffers(NULL);
247  tp_2mlist(buf_out, x, y, 0, flag, mark, interval, size);
248 
249  if (check_result_len(buf_out) > 0) {
250  printf("Produced output when output was turned off.\n");
251  return 0;
252  }
253 
254 
255  /* Check it doesn't produce output if mark is null */
256  flag = 1;
257  mark = 0;
258  interval = 1;
259  size = 1;
260 
261  buf_out = initialise_buffers(NULL);
262  tp_2mlist(buf_out, x, y, 0, flag, mark, interval, size);
263 
264  if (check_result_len(buf_out) > 0) {
265  printf("Produced output when mark was null.\n");
266  return 0;
267  }
268 
269 
270  /* Check it does produce output all else is good */
271  flag = 1;
272  mark = 1;
273  interval = 1;
274  size = 1;
275 
276  buf_out = initialise_buffers(NULL);
277  tp_2mlist(buf_out, x, y, npoints, flag, mark, interval, size);
278 
279  if (check_result_len(buf_out) <= 0) {
280  printf("Didn't produce output when expected.\n");
281  return 0;
282  }
283 
284  return 1;
285 }
286 
287 
288 /* Produces the expected output of tp_3list based on the input */
289 void
290 make_tp_3list_expected(char *buf, int buflen, double *x, double *y, double *z, int npoints)
291 {
292  int chars_written;
293 
294  if (npoints <= 0)
295  return;
296 
297  chars_written = snprintf(buf, buflen, "O %g %g %g\n", *x++, *y++, *z++);
298  buf += chars_written;
299  buflen -= chars_written;
300 
301  while (--npoints > 0) {
302  chars_written = snprintf(buf, buflen, "Q %g %g %g\n", *x++, *y++, *z++);
303  buf += chars_written;
304  buflen -= chars_written;
305  }
306 }
307 
308 /* This test ensures the tp_3list function produces the output it's supposed to */
309 int
310 test_tp_3list(double *x, double *y, double *z, int npoints)
311 {
312  char expected_buf[BUFFER_SIZE];
313  FILE *buf_out = initialise_buffers(expected_buf);
314 
315  if (!buf_out) {
316  printf("File Invalid\n");
317  return 0;
318  }
319 
320  make_tp_3list_expected(expected_buf, BUFFER_SIZE, x, y, z, npoints);
321 
322  pl_setOutputMode(PL_OUTPUT_MODE_TEXT);
323  tp_3list(buf_out, x, y, z, npoints);
324 
325  return compare_result(expected_buf, buf_out);
326 }
327 
328 
329 /*
330  * Test Running
331  */
332 
333 
334 /* Testing list.c 2D functions */
335 int
336 automatic_2d_test(double *double_x, double *double_y, int npoints)
337 {
338  int int_x[MAX_POINTS], int_y[MAX_POINTS];
339 
340  convert_points(double_x, int_x, npoints);
341  convert_points(double_y, int_y, npoints);
342 
343  if (!test_tp_i2list(int_x, int_y, npoints)) {
344  printf("tp_i2list test failed\n");
345  return 0;
346  }
347  if (!test_tp_2list(double_x, double_y, npoints)) {
348  printf("tp_2list test failed\n");
349  return 0;
350  }
351  if (!test_tp_2mlist(double_x, double_y, npoints)) {
352  printf("tp_2mlist test failed\n");
353  return 0;
354  }
355 
356  return 1;
357 }
358 
359 /* 3D functions */
360 int
361 automatic_3d_test(double *double_x, double *double_y, double *double_z, int npoints)
362 {
363  if (!test_tp_3list(double_x, double_y, double_z, npoints)) {
364  printf("tp_3list test failed\n");
365  return 0;
366  }
367 
368  return 1;
369 }
370 
371 
372 int
373 main(int argc, char *argv[])
374 {
375  double x_data[MAX_POINTS];
376  double y_data[MAX_POINTS];
377  double z_data[MAX_POINTS];
378 
379  int i = 0;
380  double x, y, z;
381 
382  if (argc < 1) {
383  printf("Must supply at least the dimension.\n");
384  return -1;
385  }
386 
387  /* If it's a 2D test */
388  if (*argv[1] == '2') {
389  while (i < (argc-2) && i < MAX_POINTS) {
390  sscanf(argv[i+2], "%lg,%lg", &x, &y);
391 
392  x_data[i] = x;
393  y_data[i] = y;
394 
395  i++;
396  }
397 
398  return !automatic_2d_test(x_data, y_data, i);
399  } else if (*argv[1] == '3') {
400  /* or a 3D test */
401 
402  while (i < (argc-2) && i < MAX_POINTS) {
403  sscanf(argv[i+2], "%lg,%lg,%lg", &x, &y, &z);
404 
405  x_data[i] = x;
406  y_data[i] = y;
407  z_data[i] = z;
408 
409  i++;
410  }
411 
412  return !automatic_3d_test(x_data, y_data, z_data, i);
413  } else {
414  printf("Wrong dimension specified.\n");
415  return -1;
416  }
417 }
418 
419 
420 /*
421  * Local Variables:
422  * mode: C
423  * tab-width: 8
424  * indent-tabs-mode: t
425  * c-file-style: "stroustrup"
426  * End:
427  * ex: shiftwidth=4 tabstop=8
428  */
int test_tp_3list(double *x, double *y, double *z, int npoints)
Definition: bn_list.c:310
int main(int argc, char *argv[])
Definition: bn_list.c:373
int automatic_2d_test(double *double_x, double *double_y, int npoints)
Definition: bn_list.c:336
Header file for the BRL-CAD common definitions.
void make_tp_2list_expected(char *buf, int buflen, double *x, double *y, int npoints)
Definition: bn_list.c:152
int test_tp_2list(double *x, double *y, int npoints)
Definition: bn_list.c:172
void convert_points(double *d_values, int *i_values, int count)
Definition: bn_list.c:94
void tp_3list(FILE *fp, register double *x, register double *y, register double *z, register int npoints)
Definition: list.c:80
#define BUFFER_SIZE
Definition: bn_list.c:31
int bu_strncmp(const char *string1, const char *string2, size_t n)
Definition: str.c:191
void * memset(void *s, int c, size_t n)
void tp_2mlist(FILE *fp, register double *x, register double *y, int npoints, int flag, int mark, int interval, double size)
Definition: list.c:107
FILE * initialise_buffers(char *expected_buf)
Definition: bn_list.c:40
FILE * bu_temp_file(char *filepath, size_t len)
Definition: temp.c:180
void make_tp_3list_expected(char *buf, int buflen, double *x, double *y, double *z, int npoints)
Definition: bn_list.c:290
void pl_setOutputMode(int mode)
Definition: plot3.c:172
int test_tp_2mlist(double *x, double *y, int npoints)
Definition: bn_list.c:197
int check_result_len(FILE *result_fd)
Definition: bn_list.c:77
#define MAX_POINTS
Definition: bn_list.c:32
int compare_result(char *expected_buf, FILE *result_fd)
Definition: bn_list.c:59
void tp_i2list(register FILE *fp, register int *x, register int *y, register int npoints)
Definition: list.c:33
int test_tp_i2list(int *x, int *y, int npoints)
Definition: bn_list.c:128
int automatic_3d_test(double *double_x, double *double_y, double *double_z, int npoints)
Definition: bn_list.c:361
void tp_2list(register FILE *fp, register double *x, register double *y, register int npoints)
Definition: list.c:49
void make_tp_i2list_expected(char *buf, int buflen, int *x, int *y, int npoints)
Definition: bn_list.c:108