BRL-CAD
bu_encode.c
Go to the documentation of this file.
1 /* T E S T _ E N C O D 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 #include <string.h>
25 
26 #include "bu.h"
27 
28 
29 /* Test for bu_vls_encode/bu_vls_decode for reversibility:
30  *
31  * 1. encode the input string
32  * 2. decode the encoded string
33  * 3. decoded string should be the same as input
34  *
35  */
36 int
37 test_encode(const char *str)
38 {
39  int status = 0;
40  int len_s = str ? strlen(str) : 0;
41  int len_d = 0; /* for length of decoded */
42  int f_wid = 28; /* desired total field width */
43  struct bu_vls encoded = BU_VLS_INIT_ZERO;
44  struct bu_vls decoded = BU_VLS_INIT_ZERO;
45 
46  bu_vls_encode(&encoded, str);
47  bu_vls_decode(&decoded, bu_vls_addr(&encoded)); /* should be same as input string */
48 
49  len_d = bu_vls_strlen(&decoded);
50  if (f_wid < len_s)
51  f_wid = len_s + 1;
52  if (f_wid < len_d)
53  f_wid = len_d + 1;
54 
55  if (BU_STR_EQUAL(str, bu_vls_addr(&decoded))
56  /* && !BU_STR_EQUAL(str, bu_vls_addr(&encoded)) */
57  ) {
58  /* a hack for str showing '(null)' in printf if zero length */
59  if (len_s == 0)
60  len_s = 6;
61  printf("{%*s}%*s -> {%*s}%*s [PASS]\n",
62  len_s, str, f_wid - len_s, " ",
63  len_d, bu_vls_addr(&decoded), f_wid - len_d, " "
64  );
65  } else {
66  /* a hack for str showing '(null)' in printf if zero length */
67  if (len_s == 0)
68  len_s = 6;
69  printf("{%*s}%*s -> {%*s}%*s [FAIL] (should be: {%s})\n",
70  len_s, str, f_wid - len_s, " ",
71  len_d, bu_vls_addr(&decoded), f_wid - len_d, " ",
72  str
73  );
74  status = 1;
75  }
76 
77  bu_vls_free(&encoded);
78  bu_vls_free(&decoded);
79 
80  return status;
81 }
82 
83 
84 int
85 main(int ac, char *av[])
86 {
87  int test_num = 0;
88  if (ac != 2)
89  fprintf(stderr,"Usage: %s test_number\n", av[0]);
90 
91  sscanf(av[1], "%d", &test_num);
92 
93  switch (test_num) {
94  case 1:
95  return test_encode(NULL);
96  case 2:
97  return test_encode("");
98  case 3:
99  return test_encode(" ");
100  case 4:
101  return test_encode("hello");
102  case 5:
103  return test_encode("\"");
104  case 6:
105  return test_encode("\'");
106  case 7:
107  return test_encode("\\");
108  case 8:
109  return test_encode("\\\"");
110  case 9:
111  return test_encode("\\\\");
112  case 10:
113  return test_encode("\"hello\"");
114  case 11:
115  return test_encode("\'hello\'");
116  case 12:
117  return test_encode("\\hello");
118  case 13:
119  return test_encode("\\hello\"");
120  case 14:
121  return test_encode("hello\\\\");
122  case 15:
123  return test_encode("\"hello\'\"");
124  case 16:
125  return test_encode("\"hello\'");
126  case 17:
127  return test_encode("\'hello\'");
128  case 18:
129  return test_encode("\'hello\"");
130  case 19:
131  return test_encode("\"\"hello\"");
132  case 20:
133  return test_encode("\'\'hello\'\'");
134  case 21:
135  return test_encode("\'\"hello\"\'");
136  case 22:
137  return test_encode("\"\"hello\"\"");
138  case 23:
139  return test_encode("\"\"\"hello\"\"\"");
140  case 24:
141  return test_encode("\"\"\"[exec join $hello {world}]\"\"\"");
142  }
143 
144  return 1;
145 }
146 
147 
148 /*
149  * Local Variables:
150  * mode: C
151  * tab-width: 8
152  * indent-tabs-mode: t
153  * c-file-style: "stroustrup"
154  * End:
155  * ex: shiftwidth=4 tabstop=8
156  */
const char * bu_vls_encode(struct bu_vls *vp, const char *str)
Definition: encode.c:34
Header file for the BRL-CAD common definitions.
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
const char * bu_vls_decode(struct bu_vls *vp, const char *str)
Definition: encode.c:79
size_t bu_vls_strlen(const struct bu_vls *vp)
Definition: vls.c:189
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
int test_encode(const char *str)
Definition: bu_encode.c:37
int main(int ac, char *av[])
Definition: bu_encode.c:85
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
Definition: vls.h:56
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126