BRL-CAD
bu_str.c
Go to the documentation of this file.
1 /* B U _ S T R . C
2  * BRL-CAD
3  *
4  * Copyright (c) 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 "bu.h"
28 
29 #include "vmath.h"
30 
31 #define SIGNUM(x) ((x > 0) - (x < 0))
32 
33 static int
34 test_bu_strlcatm(int argc, char *argv[])
35 {
36  char *dst;
37  char *expected_result = argv[6];
38  int ret, expected_ret;
39  int size;
40  int len;
41 
42  /* CMake won't pass empty strings as test parameters properly;
43  * assume expected_result is supposed to be empty.
44  */
45  if (argc == 6) {
46  expected_result = "";
47  argc++;
48  }
49 
50  if (argc != 7) {
51  bu_exit(1, "ERROR: input format is string1 string2 result_size expected_ret expected_result [%s]\n", argv[0]);
52  }
53 
54  sscanf(argv[4], "%d", &size);
55  sscanf(argv[5], "%d", &expected_ret);
56  if (size > 0) {
57  dst = (char *)bu_malloc(size, "test_bu_strlcatm");
58  } else {
59  dst = (char *)bu_malloc(1, "test_bu_strlcatm");
60  dst[0] = '\0';
61  }
62  bu_strlcpym(dst, argv[2], size, "test_bu_strlcatm");
63 
64  ret = bu_strlcatm(dst, argv[3], size, "test_bu_strlcatm");
65  len = strlen(dst);
66 
67  printf("Result: \"%s\" (return value %d)", dst, ret);
68 
69  return !(BU_STR_EQUAL(expected_result, dst)
70  && (expected_ret == ret)
71  && len <= size
72  && dst[len] == '\0');
73 }
74 
75 static int
76 test_bu_strlcpym(int argc, char *argv[])
77 {
78  char *dst;
79  char *expected_result = argv[5];
80  int ret, expected_ret;
81  int size;
82  int len;
83 
84  /* CMake won't pass empty strings as test parameters properly;
85  * assume expected_result is supposed to be empty.
86  */
87  if (argc == 5) {
88  expected_result = "";
89  argc++;
90  }
91 
92  if (argc != 6) {
93  bu_exit(1, "ERROR: input format is string result_size expected_ret expected_result [%s]\n", argv[0]);
94  }
95 
96  sscanf(argv[3], "%d", &size);
97  sscanf(argv[4], "%d", &expected_ret);
98  if (size > 0) {
99  dst = (char *)bu_malloc(size, "test_bu_strlcpym");
100  } else {
101  dst = (char *)bu_malloc(1, "test_bu_strlcpym");
102  dst[0] = '\0';
103  }
104 
105  ret = bu_strlcpym(dst, argv[2], size, "test_bu_strlcpym");
106  len = strlen(dst);
107 
108  printf("Result: \"%s\" (return value %d)", dst, ret);
109 
110  return !(BU_STR_EQUAL(expected_result, dst)
111  && (expected_ret == ret)
112  && len <= size
113  && dst[len] == '\0');
114 }
115 
116 static int
117 test_bu_strdupm(int argc, char *argv[])
118 {
119  char *dst;
120 
121  if (argc != 3) {
122  bu_exit(1, "ERROR: input format is string [%s]\n", argv[0]);
123  }
124 
125  dst = bu_strdupm(argv[2], "test_bu_strlcpym");
126 
127  printf("Result: \"%s\"", dst);
128 
129  return !(BU_STR_EQUAL(argv[2], dst));
130 }
131 
132 static int
133 test_bu_strcmp_like_functions(int argc, char *argv[], int (*fun)(const char *, const char *))
134 {
135  int ret, expected_ret;
136 
137  if (argc != 5) {
138  bu_exit(1, "ERROR: input format is string1 string2 expected_ret [%s]\n", argv[0]);
139  }
140  sscanf(argv[4], "%d", &expected_ret);
141 
142  ret = (fun)(argv[2], argv[3]);
143 
144  printf("Result: %d", ret);
145 
146  return !(SIGNUM(expected_ret) == SIGNUM(ret));
147 }
148 
149 static int
150 test_bu_strncmp_like_functions(int argc, char *argv[], int (*fun)(const char *, const char *, size_t))
151 {
152  int n;
153  int ret, expected_ret;
154 
155  if (argc != 6) {
156  bu_exit(1, "ERROR: input format is string1 string2 n expected_ret [%s]\n", argv[0]);
157  }
158  sscanf(argv[4], "%d", &n);
159  sscanf(argv[5], "%d", &expected_ret);
160 
161  ret = (*fun)(argv[2], argv[3], n);
162 
163  printf("Result: %d", ret);
164 
165  return !(SIGNUM(expected_ret) == SIGNUM(ret));
166 }
167 
168 int
169 main(int argc, char *argv[])
170 {
171  int function_num = 0;
172 
173  if (argc < 2) {
174  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
175  }
176 
177  sscanf(argv[1], "%d", &function_num);
178 
179  switch (function_num) {
180  case 1:
181  return test_bu_strlcatm(argc, argv);
182  case 2:
183  return test_bu_strlcpym(argc, argv);
184  case 3:
185  return test_bu_strdupm(argc, argv);
186  case 4:
187  return test_bu_strcmp_like_functions(argc, argv, &bu_strcmp);
188  case 5:
189  return test_bu_strncmp_like_functions(argc, argv, &bu_strncmp);
190  case 6:
191  return test_bu_strcmp_like_functions(argc, argv, &bu_strcasecmp);
192  case 7:
193  return test_bu_strncmp_like_functions(argc, argv, &bu_strncasecmp);
194  }
195 
196  bu_log("ERROR: function_num %d is not valid [%s]\n", function_num, argv[0]);
197  return 1;
198 }
199 
200 
201 /*
202  * Local Variables:
203  * mode: C
204  * tab-width: 8
205  * indent-tabs-mode: t
206  * c-file-style: "stroustrup"
207  * End:
208  * ex: shiftwidth=4 tabstop=8
209  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int main(int argc, char *argv[])
Definition: bu_str.c:169
Header file for the BRL-CAD common definitions.
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
int bu_strncmp(const char *string1, const char *string2, size_t n)
Definition: str.c:191
int bu_strncasecmp(const char *string1, const char *string2, size_t n)
Definition: str.c:239
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
char * bu_strdupm(const char *cp, const char *label)
int bu_strcmp(const char *string1, const char *string2)
Definition: str.c:171
size_t bu_strlcpym(char *dst, const char *src, size_t size, const char *label)
Definition: str.c:96
size_t bu_strlcatm(char *dst, const char *src, size_t size, const char *label)
Definition: str.c:40
#define SIGNUM(x)
Definition: bu_str.c:31
int bu_strcasecmp(const char *string1, const char *string2)
Definition: str.c:211
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126