Asterisk - The Open Source Telephony Project GIT-master-7e7a603
Functions | Variables
test_expr.c File Reference

Expression Tests. More...

#include "asterisk.h"
#include "asterisk/utils.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/ast_expr.h"
Include dependency graph for test_expr.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (expr_test)
 
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 = "Expression evaluation tests" , .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

Expression Tests.

Author
Tilghman Lesher <tlesher AT digium DOT com> 

Verify that the expression parser works as intended.

Definition in file test_expr.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 190 of file test_expr.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 190 of file test_expr.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 190 of file test_expr.c.

◆ AST_TEST_DEFINE()

AST_TEST_DEFINE ( expr_test  )

Definition at line 41 of file test_expr.c.

42{
43 int res = AST_TEST_PASS, i, len;
44 struct {
45 char *input;
46 const char *output;
47 } tests[] = {
48 { "2 + 2", "4" },
49 { " 2 + 2 ", "4" },
50 { "", "" },
51 { "2 - 4", "-2" },
52 { "4 - 2", "2" },
53 { "-4 - -2", "-2" },
54 { "4 + 2 * 8", "20" },
55 { "(4 + 2) * 8", "48" },
56 { "4 + (2 * 8)", "20" },
57 { "4 + (2 * 8) ? 3 :: 6", "3" },
58 { "4 + 8 / 2", "8" },
59 { "FLOOR(4 + 8 / 3)", "6" }, /* Floating point op on 1.6 and higher, need FLOOR() to keep result sane */
60 { "(4+8) / 3", "4" },
61 { "4 + 8 % 3", "6" },
62 { "4 + 9 % 3", "4" },
63 { "(4+9) %3", "1" },
64 { "(4+8) %3", "0" },
65 { "(4+9) %3", "1" },
66 { "(4+8) %3", "0" },
67 { "(4+9) % 3", "1" },
68 { "(4+8) % 3", "0" },
69 { "(4+9) % 3", "1" },
70 { "(4+8) % 3", "0" },
71 { "(4+9)% 3", "1" },
72 { "(4+8)% 3", "0" },
73 { "(4+9)% 3", "1" },
74 { "(4+8)% 3", "0" },
75 { "4 & 4", "4" },
76 { "0 & 4", "0" },
77 { "0 & 0", "0" },
78 { "2 | 0", "2" },
79 { "2 | 4", "2" },
80 { "0 | 0", "0" },
81 { "!0 | 0", "1" },
82 { "!4 | 0", "0" },
83 { "4 | !0", "4" },
84 { "!4 | !0", "1" },
85 { "0", "0" },
86 { "!0", "1" },
87 { "00", "00" },
88 { "!00", "1" },
89 { "1", "1" },
90 { "!1", "0" },
91 { "01", "01" },
92 { "!01", "0" },
93 { "3 < 4", "1" },
94 { "4 < 3", "0" },
95 { "3 > 4", "0" },
96 { "4 > 3", "1" },
97 { "3 = 3", "1" },
98 { "3 = 4", "0" },
99 { "3 != 3", "0" },
100 { "3 != 4", "1" },
101 { "3 >= 4", "0" },
102 { "3 >= 3", "1" },
103 { "4 >= 3", "1" },
104 { "3 <= 4", "1" },
105 { "4 <= 3", "0" },
106 { "4 <= 4", "1" },
107 { "3 > 4 & 4 < 3", "0" },
108 { "4 > 3 & 3 < 4", "1" },
109 { "x = x", "1" },
110 { "y = x", "0" },
111 { "x != y", "1" },
112 { "x != x", "0" },
113 { "\"Something interesting\" =~ interesting", "11" },
114 { "\"Something interesting\" =~ Something", "9" },
115 { "\"Something interesting\" : Something", "9" },
116 { "\"Something interesting\" : interesting", "0" },
117 { "\"Something interesting\" =~ \"interesting\"", "11" },
118 { "\"Something interesting\" =~ \"Something\"", "9" },
119 { "\"Something interesting\" : \"Something\"", "9" },
120 { "\"Something interesting\" : \"interesting\"", "0" },
121 { "\"Something interesting\" =~ (interesting)", "11" },
122 { "\"Something interesting\" =~ (Something)", "9" },
123 { "\"Something interesting\" : (Something)", "9" },
124 { "\"Something interesting\" : (interesting)", "0" },
125 { "\"Something interesting\" =~ \"\\(interesting\\)\"", "0" },
126 { "\"Something interesting\" =~ \"\\(Something\\)\"", "0" },
127 { "\"Something interesting\" : \"\\(Something\\)\"", "0" },
128 { "\"Something interesting\" : \"\\(interesting\\)\"", "0" },
129 { "\"011043567857575\" : \"011\\(..\\)\"", "0" },
130 { "\"9011043567857575\" : \"011\\(..\\)\"", "0" },
131 { "\"011043567857575\" =~ \"011\\(..\\)\"", "0" },
132 { "\"9011043567857575\" =~ \"011\\(..\\)\"", "0" },
133 { "\"Something interesting\" =~ (interesting)", "11" },
134 { "\"Something interesting\" =~ (Something)", "9" },
135 { "\"Something interesting\" : (Something)", "9" },
136 { "\"Something interesting\" : (interesting)", "0" },
137 { "\"Something interesting\" =~ \"(interesting)\"", "interesting" },
138 { "\"Something interesting\" =~ \"(Something)\"", "Something" },
139 { "\"Something interesting\" : \"(Something)\"", "Something" },
140 { "\"Something interesting\" : \"(interesting)\"", "" },
141 { "\"011043567857575\" : \"011(..)\"", "04" },
142 { "\"9011043567857575\" : \"011(..)\"", "" },
143 { "\"011043567857575\" =~ \"011(..)\"", "04" },
144 { "\"9011043567857575\" =~ \"011(..)\"", "04" },
145 { "3", "3" },
146 { "something", "something" },
147 { "043", "043" },
148 { "${GLOBAL(ULKOPREFIX)}9${x}", "${GLOBAL(ULKOPREFIX)}9${x}" },
149 { "512059${x}", "512059${x}" },
150 };
151 char buf[32];
152
153 switch (cmd) {
154 case TEST_INIT:
155 info->name = "expr_test";
156 info->category = "/main/ast_expr/";
157 info->summary = "unit test for the internal expression engine";
158 info->description =
159 "Verifies behavior for the internal expression engine";
160 return AST_TEST_NOT_RUN;
161 case TEST_EXECUTE:
162 break;
163 }
164
165 for (i = 0; i < ARRAY_LEN(tests); i++) {
166 memset(buf, 0, sizeof(buf));
167 len = ast_expr(tests[i].input, buf, sizeof(buf), NULL);
168 buf[len] = '\0';
169 if (strcmp(buf, tests[i].output)) {
170 ast_test_status_update(test, "Case %d: expression '%s' evaluated as '%s', but should have evaluated as '%s'\n", i + 1, tests[i].input, buf, tests[i].output);
171 res = AST_TEST_FAIL;
172 }
173 }
174
175 return res;
176}
static int input(yyscan_t yyscanner)
Definition: ast_expr2f.c:1570
int ast_expr(char *expr, char *buf, int length, struct ast_channel *chan)
Evaluate the given expression.
Definition: ast_expr2f.c:2391
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
def info(msg)
#define NULL
Definition: resample.c:96
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_FAIL
Definition: test.h:196
@ AST_TEST_NOT_RUN
Definition: test.h:194
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, ast_expr(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, buf, sip_to_pjsip::info(), input(), len(), NULL, TEST_EXECUTE, and TEST_INIT.

◆ load_module()

static int load_module ( void  )
static

Definition at line 184 of file test_expr.c.

185{
186 AST_TEST_REGISTER(expr_test);
188}
@ 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 178 of file test_expr.c.

179{
180 AST_TEST_UNREGISTER(expr_test);
181 return 0;
182}
#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 = "Expression evaluation tests" , .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 190 of file test_expr.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 190 of file test_expr.c.