BRL-CAD
bu_escape.c
Go to the documentation of this file.
1 /* T E S T _ E S C A P E . 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 <stdio.h>
24 
25 #include "bu.h"
26 
27 static int
28 compare(const char *input, const char *output, const char *correct)
29 {
30  if (BU_STR_EQUAL(output, correct)) {
31  printf("%24s -> %28s [PASS]\n", input, output);
32  return 1;
33  } else {
34  printf("%24s -> %28s [FAIL] (should be '%s')\n", input, output, correct);
35  return 0;
36  }
37 }
38 
39 
40 int
41 main(int ac, char *av[])
42 {
43  int function_num = 0;
44  int test_num = 0;
45  char *bufp;
46  char buffer[32];
47 
48  if (ac < 3)
49  fprintf(stderr,"Usage: %s function_to_test test_number\n", av[0]);
50 
51  sscanf(av[1], "%d", &function_num);
52  sscanf(av[2], "%d", &test_num);
53 
54  if (function_num == 1) {
55 #define CMP_UNESC(in, out) compare((in), bu_str_unescape((in), buffer, 32), (out))
56  switch (test_num) {
57  case 1:
58  return !CMP_UNESC(NULL, "");
59  case 2:
60  return !CMP_UNESC("", "");
61  case 3:
62  return !CMP_UNESC(" ", " ");
63  case 4:
64  return !CMP_UNESC("hello", "hello");
65  case 5:
66  return !CMP_UNESC("\"", "\"");
67  case 6:
68  return !CMP_UNESC("\'", "\'");
69  case 7:
70  return !CMP_UNESC("\\", "");
71  case 8:
72  return !CMP_UNESC("\\\"", "\"");
73  case 9:
74  return !CMP_UNESC("\\\\", "\\");
75  case 10:
76  return !CMP_UNESC("\"hello\"", "\"hello\"");
77  case 11:
78  return !CMP_UNESC("\'hello\'", "'hello'");
79  case 12:
80  return !CMP_UNESC("\\hello", "hello");
81  case 13:
82  return !CMP_UNESC("\\hello\"", "hello\"");
83  case 14:
84  return !CMP_UNESC("hello\\\\", "hello\\");
85  case 15:
86  return !CMP_UNESC("\"hello\'\"", "\"hello'\"");
87  case 16:
88  return !CMP_UNESC("\"hello\'", "\"hello'");
89  case 17:
90  return !CMP_UNESC("\'hello\'", "'hello'");
91  case 18:
92  return !CMP_UNESC("\'hello\"", "'hello\"");
93  case 19:
94  return !CMP_UNESC("\"\"hello\"", "\"\"hello\"");
95  case 20:
96  return !CMP_UNESC("\'\'hello\'\'", "''hello''");
97  case 21:
98  return !CMP_UNESC("\'\"hello\"\'", "'\"hello\"'");
99  case 22:
100  return !CMP_UNESC("\"\"hello\"\"", "\"\"hello\"\"");
101  case 23:
102  return !CMP_UNESC("\\\"\\\"\\\"hello\\", "\"\"\"hello");
103  }
104  return 1;
105  }
106 
107  if (function_num == 2) {
108 #define CMP_ESC(in, c, out) compare((in), bu_str_escape((in), (c), buffer, 32), (out))
109  switch (test_num) {
110  case 1:
111  return !CMP_ESC(NULL, NULL, "");
112  case 2:
113  return !CMP_ESC(NULL, "", "");
114  case 3:
115  return !CMP_ESC("", NULL, "");
116  case 4:
117  return !CMP_ESC("", "", "");
118  case 5:
119  return !CMP_ESC(" ", "", " ");
120  case 6:
121  return !CMP_ESC("[ ]", " ", "[\\ ]");
122  case 7:
123  return !CMP_ESC("[ ]", " ", "[\\ \\ ]");
124  case 8:
125  return !CMP_ESC("h e l l o", " ", "h\\ e\\ l\\ l\\ o");
126  case 9:
127  return !CMP_ESC("h\\ ello", " ", "h\\\\ ello");
128  case 10:
129  return !CMP_ESC("[]", "\\", "[]");
130  case 11:
131  return !CMP_ESC("\\", "\\", "\\\\");
132  case 12:
133  return !CMP_ESC("\\\\", "\\", "\\\\\\\\");
134  case 13:
135  return !CMP_ESC("\\a\\b", "\\", "\\\\a\\\\b");
136  case 14:
137  return !CMP_ESC("abc", "a", "\\abc");
138  case 15:
139  return !CMP_ESC("abc", "b", "a\\bc");
140  case 16:
141  return !CMP_ESC("abc", "c", "ab\\c");
142  case 17:
143  return !CMP_ESC("abc", "ab", "\\a\\bc");
144  case 18:
145  return !CMP_ESC("abc", "bc", "a\\b\\c");
146  case 19:
147  return !CMP_ESC("abc", "abc", "\\a\\b\\c");
148  case 20:
149  return !CMP_ESC("aaa", "bc", "aaa");
150  case 21:
151  return !CMP_ESC("aaa", "a", "\\a\\a\\a");
152  case 22:
153  return !CMP_ESC("aaa", "aaa", "\\a\\a\\a");
154  case 23:
155  return !CMP_ESC("abc", "^a", "a\\b\\c");
156  case 24:
157  return !CMP_ESC("abc", "^b", "\\ab\\c");
158  case 25:
159  return !CMP_ESC("abc", "^c", "\\a\\bc");
160  case 26:
161  return !CMP_ESC("abc", "^ab", "ab\\c");
162  case 27:
163  return !CMP_ESC("abc", "^bc", "\\abc");
164  case 28:
165  return !CMP_ESC("abc", "^abc", "abc");
166  case 29:
167  return !CMP_ESC("aaa", "^bc", "\\a\\a\\a");
168  case 30:
169  return !CMP_ESC("aaa", "^a", "aaa");
170  case 31:
171  return !CMP_ESC("aaa", "^aaa", "aaa");
172  }
173  return 1;
174  }
175 
176  if (function_num == 3) {
177  int pass = 0;
178  switch (test_num) {
179  case 1:
180  bufp = bu_str_unescape(bu_str_escape("abc", "b", buffer, 32), NULL, 0);
181  pass = compare("abc", bufp, "abc");
182  bu_free(bufp, NULL);
183 
184  case 2:
185  bufp = bu_str_unescape(bu_str_escape("abc\\cba", "b", buffer, 32), NULL, 0);
186  pass = compare("abc\\cba", bufp, "abccba");
187  bu_free(bufp, NULL);
188 
189  case 3:
190  bufp = bu_str_unescape(bu_str_escape("abc\\\\cba", "b", buffer, 32), NULL, 0);
191  pass = compare("abc\\\\cba", bufp, "abc\\cba");
192  bu_free(bufp, NULL);
193 
194  case 4:
195  bufp = bu_str_unescape(bu_str_escape("abc\\\\\\c\\ba\\", "b", buffer, 32), NULL, 0);
196  pass = compare("abc\\\\\\c\\ba\\", bufp, "abc\\c\\ba");
197  bu_free(bufp, NULL);
198  }
199  return !pass;
200  }
201  return 1;
202 }
203 
204 
205 /*
206  * Local Variables:
207  * mode: C
208  * tab-width: 8
209  * indent-tabs-mode: t
210  * c-file-style: "stroustrup"
211  * End:
212  * ex: shiftwidth=4 tabstop=8
213  */
Header file for the BRL-CAD common definitions.
#define CMP_ESC(in, c, out)
char * bu_str_unescape(const char *input, char *output, size_t size)
Definition: escape.c:149
char * bu_str_escape(const char *input, const char *expression, char *output, size_t size)
Definition: escape.c:53
int main(int ac, char *av[])
Definition: bu_escape.c:41
#define CMP_UNESC(in, out)
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126