Asterisk - The Open Source Telephony Project GIT-master-f36a736
Macros | Functions | Variables
test_conversions.c File Reference

Conversions Unit Tests. More...

#include "asterisk.h"
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/conversions.h"
Include dependency graph for test_conversions.c:

Go to the source code of this file.

Macros

#define CATEGORY   "/main/conversions/"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (str_to_imax)
 
 AST_TEST_DEFINE (str_to_int)
 
 AST_TEST_DEFINE (str_to_long)
 
 AST_TEST_DEFINE (str_to_uint)
 
 AST_TEST_DEFINE (str_to_ulong)
 
 AST_TEST_DEFINE (str_to_umax)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Conversions test module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Conversions Unit Tests.

Author
Kevin Harwell kharw.nosp@m.ell@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file test_conversions.c.

Macro Definition Documentation

◆ CATEGORY

#define CATEGORY   "/main/conversions/"

Definition at line 38 of file test_conversions.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 345 of file test_conversions.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 345 of file test_conversions.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 345 of file test_conversions.c.

◆ AST_TEST_DEFINE() [1/6]

AST_TEST_DEFINE ( str_to_imax  )

Definition at line 228 of file test_conversions.c.

229{
230 const char *invalid = "abc";
231 const char *invalid_partial = "7abc";
232 const char *negative = "-7";
233 const char *negative_spaces = " -7";
234 const char *negative_out_of_range = "-99999999999999999999999999999999999999999999999999";
235 const char *out_of_range = "99999999999999999999999999999999999999999999999999";
236 const char *spaces = " ";
237 const char *valid = "7";
238 const char *valid_spaces = " 7";
239 const char *valid_decimal = "08";
240 intmax_t val;
241 char str[64];
242
243 switch (cmd) {
244 case TEST_INIT:
245 info->name = __func__;
246 info->category = CATEGORY;
247 info->summary = "convert a string to a signed max size integer";
248 info->description = info->summary;
249 return AST_TEST_NOT_RUN;
250 case TEST_EXECUTE:
251 break;
252 }
253
254 ast_test_validate(test, ast_str_to_imax(NULL, &val));
255 ast_test_validate(test, ast_str_to_imax("\0", &val));
256 ast_test_validate(test, ast_str_to_imax(invalid, &val));
257 ast_test_validate(test, ast_str_to_imax(invalid_partial, &val));
258 ast_test_validate(test, !ast_str_to_imax(negative, &val));
259 ast_test_validate(test, !ast_str_to_imax(negative_spaces, &val));
260 ast_test_validate(test, ast_str_to_imax(negative_out_of_range, &val));
261 ast_test_validate(test, ast_str_to_imax(out_of_range, &val));
262 ast_test_validate(test, ast_str_to_imax(spaces, &val));
263 ast_test_validate(test, !ast_str_to_imax(valid, &val));
264 ast_test_validate(test, !ast_str_to_imax(valid_spaces, &val));
265 ast_test_validate(test, !ast_str_to_imax(valid_decimal, &val));
266
267 ast_test_validate(test, snprintf(str, sizeof(str), "%jd", INTMAX_MAX) > 0);
268 ast_test_validate(test, !ast_str_to_imax(str, &val));
269 ast_test_validate(test, val == INTMAX_MAX);
270
271 ast_test_validate(test, snprintf(str, sizeof(str), "%jd", INTMAX_MIN) > 0);
272 ast_test_validate(test, !ast_str_to_imax(str, &val));
273 ast_test_validate(test, val == INTMAX_MIN);
274
275 return AST_TEST_PASS;
276}
const char * str
Definition: app_jack.c:147
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
def info(msg)
#define NULL
Definition: resample.c:96
Definition: ast_expr2.c:325
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_NOT_RUN
Definition: test.h:194
#define CATEGORY

References ast_str_to_imax(), AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, sip_to_pjsip::info(), NULL, str, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [2/6]

AST_TEST_DEFINE ( str_to_int  )

Definition at line 40 of file test_conversions.c.

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 const char *valid_decimal = "08";
52 int val;
53 char str[64];
54
55 switch (cmd) {
56 case TEST_INIT:
57 info->name = __func__;
58 info->category = CATEGORY;
59 info->summary = "convert a string to a signed integer";
60 info->description = info->summary;
61 return AST_TEST_NOT_RUN;
62 case TEST_EXECUTE:
63 break;
64 }
65
66 ast_test_validate(test, ast_str_to_int(NULL, &val));
67 ast_test_validate(test, ast_str_to_int("\0", &val));
68 ast_test_validate(test, ast_str_to_int(invalid, &val));
69 ast_test_validate(test, ast_str_to_int(invalid_partial, &val));
70 ast_test_validate(test, !ast_str_to_int(negative, &val));
71 ast_test_validate(test, !ast_str_to_int(negative_spaces, &val));
72 ast_test_validate(test, ast_str_to_int(negative_out_of_range, &val));
73 ast_test_validate(test, ast_str_to_int(out_of_range, &val));
74 ast_test_validate(test, ast_str_to_int(spaces, &val));
75 ast_test_validate(test, !ast_str_to_int(valid, &val));
76 ast_test_validate(test, !ast_str_to_int(valid_spaces, &val));
77 ast_test_validate(test, !ast_str_to_int(valid_decimal, &val));
78
79 ast_test_validate(test, snprintf(str, sizeof(str), "%d", INT_MAX) > 0);
80 ast_test_validate(test, !ast_str_to_int(str, &val));
81 ast_test_validate(test, val == INT_MAX);
82
83 ast_test_validate(test, snprintf(str, sizeof(str), "%d", INT_MIN) > 0);
84 ast_test_validate(test, !ast_str_to_int(str, &val));
85 ast_test_validate(test, val == INT_MIN);
86
87 return AST_TEST_PASS;
88}
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44

References ast_str_to_int(), AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, sip_to_pjsip::info(), NULL, str, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [3/6]

AST_TEST_DEFINE ( str_to_long  )

Definition at line 134 of file test_conversions.c.

135{
136 const char *invalid = "abc";
137 const char *invalid_partial = "7abc";
138 const char *negative = "-7";
139 const char *negative_spaces = " -7";
140 const char *negative_out_of_range = "-99999999999999999999";
141 const char *out_of_range = "99999999999999999999";
142 const char *spaces = " ";
143 const char *valid = "7";
144 const char *valid_spaces = " 7";
145 const char *valid_decimal = "08";
146 long val;
147 char str[64];
148
149 switch (cmd) {
150 case TEST_INIT:
151 info->name = __func__;
152 info->category = CATEGORY;
153 info->summary = "convert a string to a signed long";
154 info->description = info->summary;
155 return AST_TEST_NOT_RUN;
156 case TEST_EXECUTE:
157 break;
158 }
159
160 ast_test_validate(test, ast_str_to_long(NULL, &val));
161 ast_test_validate(test, ast_str_to_long("\0", &val));
162 ast_test_validate(test, ast_str_to_long(invalid, &val));
163 ast_test_validate(test, ast_str_to_long(invalid_partial, &val));
164 ast_test_validate(test, !ast_str_to_long(negative, &val));
165 ast_test_validate(test, !ast_str_to_long(negative_spaces, &val));
166 ast_test_validate(test, ast_str_to_long(negative_out_of_range, &val));
167 ast_test_validate(test, ast_str_to_long(out_of_range, &val));
168 ast_test_validate(test, ast_str_to_long(spaces, &val));
169 ast_test_validate(test, !ast_str_to_long(valid, &val));
170 ast_test_validate(test, !ast_str_to_long(valid_spaces, &val));
171 ast_test_validate(test, !ast_str_to_long(valid_decimal, &val));
172
173 ast_test_validate(test, snprintf(str, sizeof(str), "%ld", LONG_MAX) > 0);
174 ast_test_validate(test, !ast_str_to_long(str, &val));
175 ast_test_validate(test, val == LONG_MAX);
176
177 ast_test_validate(test, snprintf(str, sizeof(str), "%ld", LONG_MIN) > 0);
178 ast_test_validate(test, !ast_str_to_long(str, &val));
179 ast_test_validate(test, val == LONG_MIN);
180
181 return AST_TEST_PASS;
182}
int ast_str_to_long(const char *str, long *res)
Convert the given string to a signed long.
Definition: conversions.c:68

References ast_str_to_long(), AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, sip_to_pjsip::info(), NULL, str, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [4/6]

AST_TEST_DEFINE ( str_to_uint  )

Definition at line 90 of file test_conversions.c.

91{
92 const char *invalid = "abc";
93 const char *invalid_partial = "7abc";
94 const char *negative = "-7";
95 const char *negative_spaces = " -7";
96 const char *out_of_range = "9999999999";
97 const char *spaces = " ";
98 const char *valid = "7";
99 const char *valid_spaces = " 7";
100 const char *valid_decimal = "08";
101 unsigned int val;
102 char str[64];
103
104 switch (cmd) {
105 case TEST_INIT:
106 info->name = __func__;
107 info->category = CATEGORY;
108 info->summary = "convert a string to an unsigned integer";
109 info->description = info->summary;
110 return AST_TEST_NOT_RUN;
111 case TEST_EXECUTE:
112 break;
113 }
114
115 ast_test_validate(test, ast_str_to_uint(NULL, &val));
116 ast_test_validate(test, ast_str_to_uint("\0", &val));
117 ast_test_validate(test, ast_str_to_uint(invalid, &val));
118 ast_test_validate(test, ast_str_to_uint(invalid_partial, &val));
119 ast_test_validate(test, ast_str_to_uint(negative, &val));
120 ast_test_validate(test, ast_str_to_uint(negative_spaces, &val));
121 ast_test_validate(test, ast_str_to_uint(out_of_range, &val));
122 ast_test_validate(test, ast_str_to_uint(spaces, &val));
123 ast_test_validate(test, !ast_str_to_uint(valid, &val));
124 ast_test_validate(test, !ast_str_to_uint(valid_spaces, &val));
125 ast_test_validate(test, !ast_str_to_uint(valid_decimal, &val));
126
127 ast_test_validate(test, snprintf(str, sizeof(str), "%u", UINT_MAX) > 0);
128 ast_test_validate(test, !ast_str_to_uint(str, &val));
129 ast_test_validate(test, val == UINT_MAX);
130
131 return AST_TEST_PASS;
132}
int ast_str_to_uint(const char *str, unsigned int *res)
Convert the given string to an unsigned integer.
Definition: conversions.c:56

References ast_str_to_uint(), AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, sip_to_pjsip::info(), NULL, str, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [5/6]

AST_TEST_DEFINE ( str_to_ulong  )

Definition at line 184 of file test_conversions.c.

185{
186 const char *invalid = "abc";
187 const char *invalid_partial = "7abc";
188 const char *negative = "-7";
189 const char *negative_spaces = " -7";
190 const char *out_of_range = "99999999999999999999";
191 const char *spaces = " ";
192 const char *valid = "7";
193 const char *valid_spaces = " 7";
194 const char *valid_decimal = "08";
195 unsigned long val;
196 char str[64];
197
198 switch (cmd) {
199 case TEST_INIT:
200 info->name = __func__;
201 info->category = CATEGORY;
202 info->summary = "convert a string to an unsigned long";
203 info->description = info->summary;
204 return AST_TEST_NOT_RUN;
205 case TEST_EXECUTE:
206 break;
207 }
208
209 ast_test_validate(test, ast_str_to_ulong(NULL, &val));
210 ast_test_validate(test, ast_str_to_ulong("\0", &val));
211 ast_test_validate(test, ast_str_to_ulong(invalid, &val));
212 ast_test_validate(test, ast_str_to_ulong(invalid_partial, &val));
213 ast_test_validate(test, ast_str_to_ulong(negative, &val));
214 ast_test_validate(test, ast_str_to_ulong(negative_spaces, &val));
215 ast_test_validate(test, ast_str_to_ulong(out_of_range, &val));
216 ast_test_validate(test, ast_str_to_ulong(spaces, &val));
217 ast_test_validate(test, !ast_str_to_ulong(valid, &val));
218 ast_test_validate(test, !ast_str_to_ulong(valid_spaces, &val));
219 ast_test_validate(test, !ast_str_to_ulong(valid_decimal, &val));
220
221 ast_test_validate(test, snprintf(str, sizeof(str), "%lu", ULONG_MAX) > 0);
222 ast_test_validate(test, !ast_str_to_ulong(str, &val));
223 ast_test_validate(test, val == ULONG_MAX);
224
225 return AST_TEST_PASS;
226}
int ast_str_to_ulong(const char *str, unsigned long *res)
Convert the given string to an unsigned long.
Definition: conversions.c:80

References ast_str_to_ulong(), AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, sip_to_pjsip::info(), NULL, str, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [6/6]

AST_TEST_DEFINE ( str_to_umax  )

Definition at line 279 of file test_conversions.c.

280{
281 const char *invalid = "abc";
282 const char *invalid_partial = "7abc";
283 const char *negative = "-7";
284 const char *negative_spaces = " -7";
285 const char *out_of_range = "99999999999999999999999999999999999999999999999999";
286 const char *spaces = " ";
287 const char *valid = "7";
288 const char *valid_spaces = " 7";
289 const char *valid_decimal = "08";
290 uintmax_t val;
291 char str[64];
292
293 switch (cmd) {
294 case TEST_INIT:
295 info->name = __func__;
296 info->category = CATEGORY;
297 info->summary = "convert a string to an unsigned max size integer";
298 info->description = info->summary;
299 return AST_TEST_NOT_RUN;
300 case TEST_EXECUTE:
301 break;
302 }
303
304 ast_test_validate(test, ast_str_to_umax(NULL, &val));
305 ast_test_validate(test, ast_str_to_umax("\0", &val));
306 ast_test_validate(test, ast_str_to_umax(invalid, &val));
307 ast_test_validate(test, ast_str_to_umax(invalid_partial, &val));
308 ast_test_validate(test, ast_str_to_umax(negative, &val));
309 ast_test_validate(test, ast_str_to_umax(negative_spaces, &val));
310 ast_test_validate(test, ast_str_to_umax(out_of_range, &val));
311 ast_test_validate(test, ast_str_to_umax(spaces, &val));
312 ast_test_validate(test, !ast_str_to_umax(valid, &val));
313 ast_test_validate(test, !ast_str_to_umax(valid_spaces, &val));
314 ast_test_validate(test, !ast_str_to_umax(valid_decimal, &val));
315
316 ast_test_validate(test, snprintf(str, sizeof(str), "%ju", UINTMAX_MAX) > 0);
317 ast_test_validate(test, !ast_str_to_umax(str, &val));
318 ast_test_validate(test, val == UINTMAX_MAX);
319
320 return AST_TEST_PASS;
321}
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

References ast_str_to_umax(), AST_TEST_NOT_RUN, AST_TEST_PASS, CATEGORY, sip_to_pjsip::info(), NULL, str, TEST_EXECUTE, and TEST_INIT.

◆ load_module()

static int load_module ( void  )
static

Definition at line 323 of file test_conversions.c.

324{
325 AST_TEST_REGISTER(str_to_int);
326 AST_TEST_REGISTER(str_to_uint);
327 AST_TEST_REGISTER(str_to_long);
328 AST_TEST_REGISTER(str_to_ulong);
329 AST_TEST_REGISTER(str_to_imax);
330 AST_TEST_REGISTER(str_to_umax);
332}
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 334 of file test_conversions.c.

335{
336 AST_TEST_UNREGISTER(str_to_int);
337 AST_TEST_UNREGISTER(str_to_uint);
338 AST_TEST_UNREGISTER(str_to_long);
339 AST_TEST_UNREGISTER(str_to_ulong);
340 AST_TEST_UNREGISTER(str_to_imax);
341 AST_TEST_UNREGISTER(str_to_umax);
342 return 0;
343}
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

References AST_TEST_UNREGISTER.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Conversions test module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 345 of file test_conversions.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 345 of file test_conversions.c.