BRL-CAD
bu_booleanize.c
Go to the documentation of this file.
1 /* T E S T _ B O O L E A N I Z E . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2012-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 #include "bu.h"
23 
24 #include <string.h>
25 
26 
27 int
28 automatic_test(const char *input)
29 {
30 
31  char buf_input[1000], buf_aux[1000];
32  int buf_len, check, res, ans;
33 
34  if (input) {
35  bu_strlcpy(buf_input, input, strlen(input)+1);
36 
37  buf_len = strlen(buf_input);
38 
39  /* Remove ending white space */
40  while ((buf_len > 0) && (buf_input[buf_len-1] == ' ')) {
41  buf_input[buf_len-1] = '\0';
42  buf_len = strlen(buf_input);
43  }
44 
45  /* Remove leading white space */
46  while ((buf_len > 0) && (buf_input[0] == ' ')) {
47  bu_strlcpy(buf_aux, buf_input + 1, buf_len);
48  bu_strlcpy(buf_input, buf_aux, buf_len);
49  buf_len = strlen(buf_input);
50  }
51  res = bu_str_true(buf_input);
52 
53  /* empty/'n'/'N' as first character for buf_input string */
54  if ((buf_len == 0) || (buf_input[0] == 'n') || (buf_input[0] == 'N') ||
55  (bu_strcmp(buf_input, "(null)") == 0)) {
56  ans = 0;
57  check = (res == ans);
58  } else {
59  /* true value comes from here on */
60  /* 'y'/'Y' as first character/"1" or variants of 1 for buf_input string */
61  if ((buf_input[0] == 'y') || (buf_input[0] == 'Y') || (atol(buf_input) == 1)) {
62  ans = 1;
63  check = (res == ans);
64  } else {
65  /* "0" or variants of 0 */
66  if ((buf_input[0] == '0') && (atol(buf_input) == 0)) {
67  ans = 0;
68  check = (res == ans);
69  } else {
70  ans = (int)buf_input[0];
71  check = (res == ans);
72  }
73  }
74  }
75  }
76 
77  if (!input) {
78  ans = 0;
79  res = bu_str_true(NULL);
80  check = (res == ans);
81  }
82 
83  if (check) {
84  printf("%24s -> %d [PASSED]\n", buf_input, res);
85  } else {
86  printf("%24s -> %d (should be: %d) [FAIL]\n", buf_input, res, ans);
87  }
88 
89  return check;
90 }
91 
92 
93 int
94 main(int argc, char *argv[])
95 {
96  int pass = 0;
97 
98  if (argc > 2)
99  fprintf(stderr,"Usage: %s test_string\n", argv[0]);
100 
101  if (argc == 1) {
102  pass = automatic_test(NULL);
103  return !pass;
104  }
105 
106  if (argc > 1)
107  pass = automatic_test(argv[1]);
108 
109  return !pass;
110 }
111 
112 
113 /*
114  * Local Variables:
115  * mode: C
116  * tab-width: 8
117  * indent-tabs-mode: t
118  * c-file-style: "stroustrup"
119  * End:
120  * ex: shiftwidth=4 tabstop=8
121  */
int main(int argc, char *argv[])
Definition: bu_booleanize.c:94
int automatic_test(const char *input)
Definition: bu_booleanize.c:28
Header file for the BRL-CAD common definitions.
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
int bu_strcmp(const char *string1, const char *string2)
Definition: str.c:171
int bu_str_true(const char *str)
Definition: booleanize.c:32