Asterisk - The Open Source Telephony Project  18.5.0
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_int)
 
 AST_TEST_DEFINE (str_to_uint)
 
 AST_TEST_DEFINE (str_to_long)
 
 AST_TEST_DEFINE (str_to_ulong)
 
 AST_TEST_DEFINE (str_to_imax)
 
 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.

Referenced by AST_TEST_DEFINE().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 333 of file test_conversions.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 333 of file test_conversions.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 333 of file test_conversions.c.

◆ AST_TEST_DEFINE() [1/6]

AST_TEST_DEFINE ( str_to_int  )

Definition at line 40 of file test_conversions.c.

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

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 }
Definition: ast_expr2.c:325
#define CATEGORY
const char * str
Definition: app_jack.c:147
#define NULL
Definition: resample.c:96
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44
def info(msg)

◆ AST_TEST_DEFINE() [2/6]

AST_TEST_DEFINE ( str_to_uint  )

Definition at line 88 of file test_conversions.c.

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

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 }
Definition: ast_expr2.c:325
#define CATEGORY
const char * str
Definition: app_jack.c:147
#define NULL
Definition: resample.c:96
def info(msg)
int ast_str_to_uint(const char *str, unsigned int *res)
Convert the given string to an unsigned integer.
Definition: conversions.c:56

◆ AST_TEST_DEFINE() [3/6]

AST_TEST_DEFINE ( str_to_long  )

Definition at line 130 of file test_conversions.c.

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

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 }
Definition: ast_expr2.c:325
#define CATEGORY
const char * str
Definition: app_jack.c:147
#define NULL
Definition: resample.c:96
def info(msg)
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() [4/6]

AST_TEST_DEFINE ( str_to_ulong  )

Definition at line 178 of file test_conversions.c.

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

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 }
Definition: ast_expr2.c:325
#define CATEGORY
const char * str
Definition: app_jack.c:147
#define NULL
Definition: resample.c:96
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

◆ AST_TEST_DEFINE() [5/6]

AST_TEST_DEFINE ( str_to_imax  )

Definition at line 220 of file test_conversions.c.

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

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 }
Definition: ast_expr2.c:325
#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
def info(msg)

◆ AST_TEST_DEFINE() [6/6]

AST_TEST_DEFINE ( str_to_umax  )

Definition at line 269 of file test_conversions.c.

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

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 }
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
#define CATEGORY
const char * str
Definition: app_jack.c:147
#define NULL
Definition: resample.c:96
def info(msg)

◆ load_module()

static int load_module ( void  )
static

Definition at line 311 of file test_conversions.c.

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

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 }
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 322 of file test_conversions.c.

References AST_TEST_UNREGISTER.

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 }
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

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 333 of file test_conversions.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 333 of file test_conversions.c.