Merged revisions 295078 via svnmerge from
[asterisk/asterisk.git] / tests / test_expr.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * Tilghman Lesher <tlesher AT digium DOT com>
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 Expression Tests
22  *
23  * \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
24  *
25  * Verify that the expression parser works as intended.
26  * \ingroup tests
27  */
28
29 /*** MODULEINFO
30         <depend>TEST_FRAMEWORK</depend>
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include "asterisk/utils.h"
38 #include "asterisk/module.h"
39 #include "asterisk/test.h"
40 #include "asterisk/ast_expr.h"
41
42 AST_TEST_DEFINE(expr_test)
43 {
44         int res = AST_TEST_PASS, i, len;
45         struct {
46                 char *input;
47                 const char *output;
48         } tests[] = {
49                 { "2 + 2", "4" },
50                 { "      2     +       2            ", "4" },
51                 { "", "" },
52                 { "2 - 4", "-2" },
53                 { "4 - 2", "2" },
54                 { "-4 - -2", "-2" },
55                 { "4 + 2 * 8", "20" },
56                 { "(4 + 2) * 8", "48" },
57                 { "4 + (2 * 8)", "20" },
58                 { "4 + (2 * 8) ? 3 :: 6", "3" },
59                 { "4 + 8 / 2", "8" },
60                 { "FLOOR(4 + 8 / 3)", "6" }, /* Floating point op on 1.6 and higher, need FLOOR() to keep result sane */
61                 { "(4+8) / 3", "4" },
62                 { "4 + 8 % 3", "6" },
63                 { "4 + 9 % 3", "4" },
64                 { "(4+9) %3", "1" },
65                 { "(4+8) %3", "0" },
66                 { "(4+9) %3", "1" },
67                 { "(4+8) %3", "0" },
68                 { "(4+9) % 3", "1" },
69                 { "(4+8) % 3", "0" },
70                 { "(4+9) % 3", "1" },
71                 { "(4+8) % 3", "0" },
72                 { "(4+9)% 3", "1" },
73                 { "(4+8)% 3", "0" },
74                 { "(4+9)% 3", "1" },
75                 { "(4+8)% 3", "0" },
76                 { "4 & 4", "4" },
77                 { "0 & 4", "0" },
78                 { "0 & 0", "0" },
79                 { "2 | 0", "2" },
80                 { "2 | 4", "2" },
81                 { "0 | 0", "0" },
82                 { "!0 | 0", "1" },
83                 { "!4 | 0", "0" },
84                 { "4 | !0", "4" },
85                 { "!4 | !0", "1" },
86                 { "0", "0" },
87                 { "!0", "1" },
88                 { "00", "00" },
89                 { "!00", "1" },
90                 { "1", "1" },
91                 { "!1", "0" },
92                 { "01", "01" },
93                 { "!01", "0" },
94                 { "3 < 4", "1" },
95                 { "4 < 3", "0" },
96                 { "3 > 4", "0" },
97                 { "4 > 3", "1" },
98                 { "3 = 3", "1" },
99                 { "3 = 4", "0" },
100                 { "3 != 3", "0" },
101                 { "3 != 4", "1" },
102                 { "3 >= 4", "0" },
103                 { "3 >= 3", "1" },
104                 { "4 >= 3", "1" },
105                 { "3 <= 4", "1" },
106                 { "4 <= 3", "0" },
107                 { "4 <= 4", "1" },
108                 { "3 > 4 & 4 < 3", "0" },
109                 { "4 > 3 & 3 < 4", "1" },
110                 { "x = x", "1" },
111                 { "y = x", "0" },
112                 { "x != y", "1" },
113                 { "x != x", "0" },
114                 { "\"Something interesting\" =~ interesting", "11" },
115                 { "\"Something interesting\" =~ Something", "9" },
116                 { "\"Something interesting\" : Something", "9" },
117                 { "\"Something interesting\" : interesting", "0" },
118                 { "\"Something interesting\" =~ \"interesting\"", "11" },
119                 { "\"Something interesting\" =~ \"Something\"", "9" },
120                 { "\"Something interesting\" : \"Something\"", "9" },
121                 { "\"Something interesting\" : \"interesting\"", "0" },
122                 { "\"Something interesting\" =~ (interesting)", "11" },
123                 { "\"Something interesting\" =~ (Something)", "9" },
124                 { "\"Something interesting\" : (Something)", "9" },
125                 { "\"Something interesting\" : (interesting)", "0" },
126                 { "\"Something interesting\" =~ \"\\(interesting\\)\"", "0" },
127                 { "\"Something interesting\" =~ \"\\(Something\\)\"", "0" },
128                 { "\"Something interesting\" : \"\\(Something\\)\"", "0" },
129                 { "\"Something interesting\" : \"\\(interesting\\)\"", "0" },
130                 { "\"011043567857575\" : \"011\\(..\\)\"", "0" },
131                 { "\"9011043567857575\" : \"011\\(..\\)\"", "0" },
132                 { "\"011043567857575\" =~ \"011\\(..\\)\"", "0" },
133                 { "\"9011043567857575\" =~ \"011\\(..\\)\"", "0" },
134                 { "\"Something interesting\" =~ (interesting)", "11" },
135                 { "\"Something interesting\" =~ (Something)", "9" },
136                 { "\"Something interesting\" : (Something)", "9" },
137                 { "\"Something interesting\" : (interesting)", "0" },
138                 { "\"Something interesting\" =~ \"(interesting)\"", "interesting" },
139                 { "\"Something interesting\" =~ \"(Something)\"", "Something" },
140                 { "\"Something interesting\" : \"(Something)\"", "Something" },
141                 { "\"Something interesting\" : \"(interesting)\"", "" },
142                 { "\"011043567857575\" : \"011(..)\"", "04" },
143                 { "\"9011043567857575\" : \"011(..)\"", "" },
144                 { "\"011043567857575\" =~ \"011(..)\"", "04" },
145                 { "\"9011043567857575\" =~ \"011(..)\"", "04" },
146                 { "3", "3" },
147                 { "something", "something" },
148                 { "043", "043" },
149                 { "${GLOBAL(ULKOPREFIX)}9${x}", "${GLOBAL(ULKOPREFIX)}9${x}" },
150                 { "512059${x}", "512059${x}" },
151         };
152         char buf[32];
153
154         switch (cmd) {
155         case TEST_INIT:
156                 info->name = "expr_test";
157                 info->category = "/main/ast_expr/";
158                 info->summary = "unit test for the internal expression engine";
159                 info->description =
160                         "Verifies behavior for the internal expression engine\n";
161                 return AST_TEST_NOT_RUN;
162         case TEST_EXECUTE:
163                 break;
164         }
165
166         for (i = 0; i < ARRAY_LEN(tests); i++) {
167                 memset(buf, 0, sizeof(buf));
168                 len = ast_expr(tests[i].input, buf, sizeof(buf), NULL);
169                 buf[len] = '\0';
170                 if (strcmp(buf, tests[i].output)) {
171                         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);
172                         res = AST_TEST_FAIL;
173                 }
174         }
175
176         return res;
177 }
178
179 static int unload_module(void)
180 {
181         AST_TEST_UNREGISTER(expr_test);
182         return 0;
183 }
184
185 static int load_module(void)
186 {
187         AST_TEST_REGISTER(expr_test);
188         return AST_MODULE_LOAD_SUCCESS;
189 }
190
191 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Expression evaluation tests");