Merge "doc/lang/language-criteria.txt: Link to wiki."
[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         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 #include "asterisk/utils.h"
37 #include "asterisk/module.h"
38 #include "asterisk/test.h"
39 #include "asterisk/ast_expr.h"
40
41 AST_TEST_DEFINE(expr_test)
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 }
177
178 static int unload_module(void)
179 {
180         AST_TEST_UNREGISTER(expr_test);
181         return 0;
182 }
183
184 static int load_module(void)
185 {
186         AST_TEST_REGISTER(expr_test);
187         return AST_MODULE_LOAD_SUCCESS;
188 }
189
190 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Expression evaluation tests");