Asterisk - The Open Source Telephony Project  18.5.0
test_conversions.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2017, Digium, Inc.
5  *
6  * Kevin Harwell <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!
20  * \file
21  * \brief Conversions Unit Tests
22  *
23  * \author Kevin Harwell <[email protected]>
24  *
25  */
26 
27 /*** MODULEINFO
28  <depend>TEST_FRAMEWORK</depend>
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 #include "asterisk/test.h"
35 #include "asterisk/module.h"
36 #include "asterisk/conversions.h"
37 
38 #define CATEGORY "/main/conversions/"
39 
40 AST_TEST_DEFINE(str_to_int)
41 {
42  const char *invalid = "abc";
43  const char *invalid_partial = "7abc";
44  const char *negative = "-7";
45  const char *negative_spaces = " -7";
46  const char *negative_out_of_range = "-9999999999";
47  const char *out_of_range = "9999999999";
48  const char *spaces = " ";
49  const char *valid = "7";
50  const char *valid_spaces = " 7";
51  int val;
52  char str[64];
53 
54  switch (cmd) {
55  case TEST_INIT:
56  info->name = __func__;
57  info->category = CATEGORY;
58  info->summary = "convert a string to a signed integer";
59  info->description = info->summary;
60  return AST_TEST_NOT_RUN;
61  case TEST_EXECUTE:
62  break;
63  }
64 
65  ast_test_validate(test, ast_str_to_int(NULL, &val));
66  ast_test_validate(test, ast_str_to_int("\0", &val));
67  ast_test_validate(test, ast_str_to_int(invalid, &val));
68  ast_test_validate(test, ast_str_to_int(invalid_partial, &val));
69  ast_test_validate(test, !ast_str_to_int(negative, &val));
70  ast_test_validate(test, !ast_str_to_int(negative_spaces, &val));
71  ast_test_validate(test, ast_str_to_int(negative_out_of_range, &val));
72  ast_test_validate(test, ast_str_to_int(out_of_range, &val));
73  ast_test_validate(test, ast_str_to_int(spaces, &val));
74  ast_test_validate(test, !ast_str_to_int(valid, &val));
75  ast_test_validate(test, !ast_str_to_int(valid_spaces, &val));
76 
77  ast_test_validate(test, snprintf(str, sizeof(str), "%d", INT_MAX) > 0);
78  ast_test_validate(test, !ast_str_to_int(str, &val));
79  ast_test_validate(test, val == INT_MAX);
80 
81  ast_test_validate(test, snprintf(str, sizeof(str), "%d", INT_MIN) > 0);
82  ast_test_validate(test, !ast_str_to_int(str, &val));
83  ast_test_validate(test, val == INT_MIN);
84 
85  return AST_TEST_PASS;
86 }
87 
88 AST_TEST_DEFINE(str_to_uint)
89 {
90  const char *invalid = "abc";
91  const char *invalid_partial = "7abc";
92  const char *negative = "-7";
93  const char *negative_spaces = " -7";
94  const char *out_of_range = "9999999999";
95  const char *spaces = " ";
96  const char *valid = "7";
97  const char *valid_spaces = " 7";
98  unsigned int val;
99  char str[64];
100 
101  switch (cmd) {
102  case TEST_INIT:
103  info->name = __func__;
104  info->category = CATEGORY;
105  info->summary = "convert a string to an unsigned integer";
106  info->description = info->summary;
107  return AST_TEST_NOT_RUN;
108  case TEST_EXECUTE:
109  break;
110  }
111 
112  ast_test_validate(test, ast_str_to_uint(NULL, &val));
113  ast_test_validate(test, ast_str_to_uint("\0", &val));
114  ast_test_validate(test, ast_str_to_uint(invalid, &val));
115  ast_test_validate(test, ast_str_to_uint(invalid_partial, &val));
116  ast_test_validate(test, ast_str_to_uint(negative, &val));
117  ast_test_validate(test, ast_str_to_uint(negative_spaces, &val));
118  ast_test_validate(test, ast_str_to_uint(out_of_range, &val));
119  ast_test_validate(test, ast_str_to_uint(spaces, &val));
120  ast_test_validate(test, !ast_str_to_uint(valid, &val));
121  ast_test_validate(test, !ast_str_to_uint(valid_spaces, &val));
122 
123  ast_test_validate(test, snprintf(str, sizeof(str), "%u", UINT_MAX) > 0);
124  ast_test_validate(test, !ast_str_to_uint(str, &val));
125  ast_test_validate(test, val == UINT_MAX);
126 
127  return AST_TEST_PASS;
128 }
129 
130 AST_TEST_DEFINE(str_to_long)
131 {
132  const char *invalid = "abc";
133  const char *invalid_partial = "7abc";
134  const char *negative = "-7";
135  const char *negative_spaces = " -7";
136  const char *negative_out_of_range = "-99999999999999999999";
137  const char *out_of_range = "99999999999999999999";
138  const char *spaces = " ";
139  const char *valid = "7";
140  const char *valid_spaces = " 7";
141  long val;
142  char str[64];
143 
144  switch (cmd) {
145  case TEST_INIT:
146  info->name = __func__;
147  info->category = CATEGORY;
148  info->summary = "convert a string to a signed long";
149  info->description = info->summary;
150  return AST_TEST_NOT_RUN;
151  case TEST_EXECUTE:
152  break;
153  }
154 
155  ast_test_validate(test, ast_str_to_long(NULL, &val));
156  ast_test_validate(test, ast_str_to_long("\0", &val));
157  ast_test_validate(test, ast_str_to_long(invalid, &val));
158  ast_test_validate(test, ast_str_to_long(invalid_partial, &val));
159  ast_test_validate(test, !ast_str_to_long(negative, &val));
160  ast_test_validate(test, !ast_str_to_long(negative_spaces, &val));
161  ast_test_validate(test, ast_str_to_long(negative_out_of_range, &val));
162  ast_test_validate(test, ast_str_to_long(out_of_range, &val));
163  ast_test_validate(test, ast_str_to_long(spaces, &val));
164  ast_test_validate(test, !ast_str_to_long(valid, &val));
165  ast_test_validate(test, !ast_str_to_long(valid_spaces, &val));
166 
167  ast_test_validate(test, snprintf(str, sizeof(str), "%ld", LONG_MAX) > 0);
168  ast_test_validate(test, !ast_str_to_long(str, &val));
169  ast_test_validate(test, val == LONG_MAX);
170 
171  ast_test_validate(test, snprintf(str, sizeof(str), "%ld", LONG_MIN) > 0);
172  ast_test_validate(test, !ast_str_to_long(str, &val));
173  ast_test_validate(test, val == LONG_MIN);
174 
175  return AST_TEST_PASS;
176 }
177 
178 AST_TEST_DEFINE(str_to_ulong)
179 {
180  const char *invalid = "abc";
181  const char *invalid_partial = "7abc";
182  const char *negative = "-7";
183  const char *negative_spaces = " -7";
184  const char *out_of_range = "99999999999999999999";
185  const char *spaces = " ";
186  const char *valid = "7";
187  const char *valid_spaces = " 7";
188  unsigned long val;
189  char str[64];
190 
191  switch (cmd) {
192  case TEST_INIT:
193  info->name = __func__;
194  info->category = CATEGORY;
195  info->summary = "convert a string to an unsigned long";
196  info->description = info->summary;
197  return AST_TEST_NOT_RUN;
198  case TEST_EXECUTE:
199  break;
200  }
201 
202  ast_test_validate(test, ast_str_to_ulong(NULL, &val));
203  ast_test_validate(test, ast_str_to_ulong("\0", &val));
204  ast_test_validate(test, ast_str_to_ulong(invalid, &val));
205  ast_test_validate(test, ast_str_to_ulong(invalid_partial, &val));
206  ast_test_validate(test, ast_str_to_ulong(negative, &val));
207  ast_test_validate(test, ast_str_to_ulong(negative_spaces, &val));
208  ast_test_validate(test, ast_str_to_ulong(out_of_range, &val));
209  ast_test_validate(test, ast_str_to_ulong(spaces, &val));
210  ast_test_validate(test, !ast_str_to_ulong(valid, &val));
211  ast_test_validate(test, !ast_str_to_ulong(valid_spaces, &val));
212 
213  ast_test_validate(test, snprintf(str, sizeof(str), "%lu", ULONG_MAX) > 0);
214  ast_test_validate(test, !ast_str_to_ulong(str, &val));
215  ast_test_validate(test, val == ULONG_MAX);
216 
217  return AST_TEST_PASS;
218 }
219 
220 AST_TEST_DEFINE(str_to_imax)
221 {
222  const char *invalid = "abc";
223  const char *invalid_partial = "7abc";
224  const char *negative = "-7";
225  const char *negative_spaces = " -7";
226  const char *negative_out_of_range = "-99999999999999999999999999999999999999999999999999";
227  const char *out_of_range = "99999999999999999999999999999999999999999999999999";
228  const char *spaces = " ";
229  const char *valid = "7";
230  const char *valid_spaces = " 7";
231  intmax_t val;
232  char str[64];
233 
234  switch (cmd) {
235  case TEST_INIT:
236  info->name = __func__;
237  info->category = CATEGORY;
238  info->summary = "convert a string to a signed max size integer";
239  info->description = info->summary;
240  return AST_TEST_NOT_RUN;
241  case TEST_EXECUTE:
242  break;
243  }
244 
245  ast_test_validate(test, ast_str_to_imax(NULL, &val));
246  ast_test_validate(test, ast_str_to_imax("\0", &val));
247  ast_test_validate(test, ast_str_to_imax(invalid, &val));
248  ast_test_validate(test, ast_str_to_imax(invalid_partial, &val));
249  ast_test_validate(test, !ast_str_to_imax(negative, &val));
250  ast_test_validate(test, !ast_str_to_imax(negative_spaces, &val));
251  ast_test_validate(test, ast_str_to_imax(negative_out_of_range, &val));
252  ast_test_validate(test, ast_str_to_imax(out_of_range, &val));
253  ast_test_validate(test, ast_str_to_imax(spaces, &val));
254  ast_test_validate(test, !ast_str_to_imax(valid, &val));
255  ast_test_validate(test, !ast_str_to_imax(valid_spaces, &val));
256 
257  ast_test_validate(test, snprintf(str, sizeof(str), "%jd", INTMAX_MAX) > 0);
258  ast_test_validate(test, !ast_str_to_imax(str, &val));
259  ast_test_validate(test, val == INTMAX_MAX);
260 
261  ast_test_validate(test, snprintf(str, sizeof(str), "%jd", INTMAX_MIN) > 0);
262  ast_test_validate(test, !ast_str_to_imax(str, &val));
263  ast_test_validate(test, val == INTMAX_MIN);
264 
265  return AST_TEST_PASS;
266 }
267 
268 
269 AST_TEST_DEFINE(str_to_umax)
270 {
271  const char *invalid = "abc";
272  const char *invalid_partial = "7abc";
273  const char *negative = "-7";
274  const char *negative_spaces = " -7";
275  const char *out_of_range = "99999999999999999999999999999999999999999999999999";
276  const char *spaces = " ";
277  const char *valid = "7";
278  const char *valid_spaces = " 7";
279  uintmax_t val;
280  char str[64];
281 
282  switch (cmd) {
283  case TEST_INIT:
284  info->name = __func__;
285  info->category = CATEGORY;
286  info->summary = "convert a string to an unsigned max size integer";
287  info->description = info->summary;
288  return AST_TEST_NOT_RUN;
289  case TEST_EXECUTE:
290  break;
291  }
292 
293  ast_test_validate(test, ast_str_to_umax(NULL, &val));
294  ast_test_validate(test, ast_str_to_umax("\0", &val));
295  ast_test_validate(test, ast_str_to_umax(invalid, &val));
296  ast_test_validate(test, ast_str_to_umax(invalid_partial, &val));
297  ast_test_validate(test, ast_str_to_umax(negative, &val));
298  ast_test_validate(test, ast_str_to_umax(negative_spaces, &val));
299  ast_test_validate(test, ast_str_to_umax(out_of_range, &val));
300  ast_test_validate(test, ast_str_to_umax(spaces, &val));
301  ast_test_validate(test, !ast_str_to_umax(valid, &val));
302  ast_test_validate(test, !ast_str_to_umax(valid_spaces, &val));
303 
304  ast_test_validate(test, snprintf(str, sizeof(str), "%ju", UINTMAX_MAX) > 0);
305  ast_test_validate(test, !ast_str_to_umax(str, &val));
306  ast_test_validate(test, val == UINTMAX_MAX);
307 
308  return AST_TEST_PASS;
309 }
310 
311 static int load_module(void)
312 {
313  AST_TEST_REGISTER(str_to_int);
314  AST_TEST_REGISTER(str_to_uint);
315  AST_TEST_REGISTER(str_to_long);
316  AST_TEST_REGISTER(str_to_ulong);
317  AST_TEST_REGISTER(str_to_imax);
318  AST_TEST_REGISTER(str_to_umax);
320 }
321 
322 static int unload_module(void)
323 {
324  AST_TEST_UNREGISTER(str_to_int);
325  AST_TEST_UNREGISTER(str_to_uint);
326  AST_TEST_UNREGISTER(str_to_long);
327  AST_TEST_UNREGISTER(str_to_ulong);
328  AST_TEST_UNREGISTER(str_to_imax);
329  AST_TEST_UNREGISTER(str_to_umax);
330  return 0;
331 }
332 
333 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Conversions test module");
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
Definition: ast_expr2.c:325
int ast_str_to_umax(const char *str, uintmax_t *res)
Convert the given string to an unsigned max size integer.
Definition: conversions.c:119
static int unload_module(void)
Test Framework API.
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
#define CATEGORY
const char * str
Definition: app_jack.c:147
#define NULL
Definition: resample.c:96
int ast_str_to_imax(const char *str, intmax_t *res)
Convert the given string to a signed max size integer.
Definition: conversions.c:92
Conversion utility functions.
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
def info(msg)
int ast_str_to_ulong(const char *str, unsigned long *res)
Convert the given string to an unsigned long.
Definition: conversions.c:80
int ast_str_to_uint(const char *str, unsigned int *res)
Convert the given string to an unsigned integer.
Definition: conversions.c:56
static int load_module(void)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
int ast_str_to_long(const char *str, long *res)
Convert the given string to a signed long.
Definition: conversions.c:68
AST_TEST_DEFINE(str_to_int)