|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2022 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "object.h" |
|
30 |
|
31 #include "../util/object.h" |
|
32 |
|
33 #include <cx/linked_list.h> |
|
34 #include <cx/compare.h> |
|
35 |
|
36 #include "object.h" |
|
37 |
|
38 |
|
39 UCX_TEST(test_expr_parse_expr_value) { |
|
40 pool_handle_t *pool = pool_create(); |
|
41 |
|
42 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring)); |
|
43 cxstring token = cx_str("123"); |
|
44 cxListAdd(tokens, &token); |
|
45 |
|
46 UCX_TEST_BEGIN; |
|
47 |
|
48 size_t pos = 0; |
|
49 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); |
|
50 |
|
51 UCX_TEST_ASSERT(pos == 1, "wrong token pos"); |
|
52 UCX_TEST_ASSERT(expr, "expression is null"); |
|
53 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_INT, "wrong type"); |
|
54 |
|
55 UCX_TEST_END; |
|
56 |
|
57 pool_destroy(pool); |
|
58 } |
|
59 |
|
60 UCX_TEST(test_expr_parse_expr_value_str) { |
|
61 pool_handle_t *pool = pool_create(); |
|
62 |
|
63 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring)); |
|
64 cxstring token = cx_str("\"hello world\""); |
|
65 cxListAdd(tokens, &token); |
|
66 |
|
67 UCX_TEST_BEGIN; |
|
68 |
|
69 size_t pos = 0; |
|
70 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); |
|
71 |
|
72 UCX_TEST_ASSERT(pos == 1, "wrong token pos"); |
|
73 UCX_TEST_ASSERT(expr, "expression is null"); |
|
74 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_STRING, "wrong type"); |
|
75 UCX_TEST_ASSERT(!cx_strcmp(expr->value.str, cx_str("hello world")), "wrong value"); |
|
76 |
|
77 UCX_TEST_END; |
|
78 |
|
79 pool_destroy(pool); |
|
80 } |
|
81 |
|
82 UCX_TEST(test_expr_parse_expr_value_bool) { |
|
83 pool_handle_t *pool = pool_create(); |
|
84 |
|
85 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring)); |
|
86 cxstring token = cx_str("true"); |
|
87 cxListAdd(tokens, &token); |
|
88 |
|
89 UCX_TEST_BEGIN; |
|
90 |
|
91 size_t pos = 0; |
|
92 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); |
|
93 |
|
94 UCX_TEST_ASSERT(pos == 1, "wrong token pos"); |
|
95 UCX_TEST_ASSERT(expr, "expression is null"); |
|
96 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_BOOL, "wrong type"); |
|
97 UCX_TEST_ASSERT(expr->value.b == 1, "wrong value"); |
|
98 |
|
99 UCX_TEST_END; |
|
100 |
|
101 pool_destroy(pool); |
|
102 } |
|
103 |
|
104 UCX_TEST(test_expr_parse_expr_value_var) { |
|
105 pool_handle_t *pool = pool_create(); |
|
106 |
|
107 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring)); |
|
108 cxstring token = cx_str("$test"); |
|
109 cxListAdd(tokens, &token); |
|
110 |
|
111 UCX_TEST_BEGIN; |
|
112 |
|
113 size_t pos = 0; |
|
114 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); |
|
115 |
|
116 UCX_TEST_ASSERT(pos == 1, "wrong token pos"); |
|
117 UCX_TEST_ASSERT(expr, "expression is null"); |
|
118 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_VARIABLE, "wrong type"); |
|
119 UCX_TEST_ASSERT(!cx_strcmp(expr->value.var, cx_str("test")), "wrong var name"); |
|
120 |
|
121 UCX_TEST_END; |
|
122 |
|
123 pool_destroy(pool); |
|
124 } |
|
125 |
|
126 UCX_TEST(test_expr_parse_expr_not_value) { |
|
127 pool_handle_t *pool = pool_create(); |
|
128 |
|
129 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring)); |
|
130 cxstring token = cx_str("not"); |
|
131 cxListAdd(tokens, &token); |
|
132 token = cx_str("true"); |
|
133 cxListAdd(tokens, &token); |
|
134 |
|
135 UCX_TEST_BEGIN; |
|
136 |
|
137 size_t pos = 0; |
|
138 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); |
|
139 |
|
140 UCX_TEST_ASSERT(pos == 2, "wrong token pos"); |
|
141 UCX_TEST_ASSERT(expr, "expression is null"); |
|
142 |
|
143 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_UNARY, "wrong root expression type"); |
|
144 UCX_TEST_ASSERT(expr->left, "missing left expression"); |
|
145 UCX_TEST_ASSERT(!expr->right, "right expression should be null"); |
|
146 UCX_TEST_ASSERT(expr->left->type == NSAPI_EXPRESSION_BOOL, "left expression has wrong type"); |
|
147 UCX_TEST_ASSERT(expr->left->value.b == 1, "left expression has wrong value"); |
|
148 |
|
149 UCX_TEST_END; |
|
150 |
|
151 pool_destroy(pool); |
|
152 } |
|
153 |
|
154 UCX_TEST(test_expr_parse_expr_sign_value) { |
|
155 pool_handle_t *pool = pool_create(); |
|
156 |
|
157 CxList *tokens1 = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring)); |
|
158 cxstring token = cx_str("+"); |
|
159 cxListAdd(tokens1, &token); |
|
160 token = cx_str("123"); |
|
161 cxListAdd(tokens1, &token); |
|
162 |
|
163 CxList *tokens2 = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring)); |
|
164 token = cx_str("-"); |
|
165 cxListAdd(tokens2, &token); |
|
166 token = cx_str("123"); |
|
167 cxListAdd(tokens2, &token); |
|
168 |
|
169 UCX_TEST_BEGIN; |
|
170 |
|
171 size_t pos = 0; |
|
172 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens1, &pos); |
|
173 |
|
174 UCX_TEST_ASSERT(pos == 2, "test1: wrong token pos"); |
|
175 UCX_TEST_ASSERT(expr, "test1: expression is null"); |
|
176 |
|
177 pos = 0; |
|
178 expr = expr_parse_logical_expr(pool, tokens2, &pos); |
|
179 |
|
180 UCX_TEST_ASSERT(pos == 2, "test2: wrong token pos"); |
|
181 UCX_TEST_ASSERT(expr, "test2: expression is null"); |
|
182 |
|
183 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_UNARY, "wrong expression type"); |
|
184 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_SUB, "wrong expression operator"); |
|
185 UCX_TEST_ASSERT(expr->left, "missing left expresion"); |
|
186 UCX_TEST_ASSERT(expr->left->type == NSAPI_EXPRESSION_INT, "left expression has wrong type"); |
|
187 UCX_TEST_ASSERT(expr->left->value.i == 123, "left expression has wrong value"); |
|
188 |
|
189 UCX_TEST_END; |
|
190 |
|
191 pool_destroy(pool); |
|
192 } |
|
193 |
|
194 |
|
195 |
|
196 UCX_TEST(test_expr_parse_expr_compare2values) { |
|
197 pool_handle_t *pool = pool_create(); |
|
198 |
|
199 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring)); |
|
200 cxstring token = cx_str("2"); |
|
201 cxListAdd(tokens, &token); |
|
202 token = cx_str("=="); |
|
203 cxListAdd(tokens, &token); |
|
204 token = cx_str("2"); |
|
205 cxListAdd(tokens, &token); |
|
206 |
|
207 UCX_TEST_BEGIN; |
|
208 |
|
209 size_t pos = 0; |
|
210 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); |
|
211 |
|
212 UCX_TEST_ASSERT(pos == 3, "wrong token pos"); |
|
213 UCX_TEST_ASSERT(expr, "expression is null"); |
|
214 |
|
215 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_BINARY, "wrong expression type"); |
|
216 UCX_TEST_ASSERT(expr->left, "left expression is null"); |
|
217 UCX_TEST_ASSERT(expr->right, "right expression is null"); |
|
218 |
|
219 UCX_TEST_ASSERT(expr->left->operator == NSAPI_EXPRESSION_NOOP, "left should be a literal with no operator"); |
|
220 UCX_TEST_ASSERT(expr->right->operator == NSAPI_EXPRESSION_NOOP, "right should be a literal with no operator"); |
|
221 |
|
222 UCX_TEST_END; |
|
223 |
|
224 pool_destroy(pool); |
|
225 } |
|
226 |
|
227 UCX_TEST(test_expr_parse_expr_compare2value_expr) { |
|
228 pool_handle_t *pool = pool_create(); |
|
229 |
|
230 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring)); |
|
231 cxstring token = cx_str("2"); |
|
232 cxListAdd(tokens, &token); |
|
233 token = cx_str("=="); |
|
234 cxListAdd(tokens, &token); |
|
235 token = cx_str("1"); |
|
236 cxListAdd(tokens, &token); |
|
237 token = cx_str("+"); |
|
238 cxListAdd(tokens, &token); |
|
239 token = cx_str("1"); |
|
240 cxListAdd(tokens, &token); |
|
241 |
|
242 UCX_TEST_BEGIN; |
|
243 |
|
244 size_t pos = 0; |
|
245 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); |
|
246 |
|
247 UCX_TEST_ASSERT(pos == 5, "wrong token pos"); |
|
248 UCX_TEST_ASSERT(expr, "expression is null"); |
|
249 |
|
250 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_BINARY, "wrong expression type"); |
|
251 UCX_TEST_ASSERT(expr->left, "left expression is null"); |
|
252 UCX_TEST_ASSERT(expr->right, "right expression is null"); |
|
253 |
|
254 UCX_TEST_ASSERT(expr->left->operator == NSAPI_EXPRESSION_NOOP, "left should be a literal with no operator"); |
|
255 UCX_TEST_ASSERT(expr->right->operator == NSAPI_EXPRESSION_ADD, "right should be a binary expression"); |
|
256 |
|
257 UCX_TEST_END; |
|
258 |
|
259 pool_destroy(pool); |
|
260 } |
|
261 |
|
262 UCX_TEST(test_expr_parse_expr_compare2expr_value) { |
|
263 pool_handle_t *pool = pool_create(); |
|
264 |
|
265 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring)); |
|
266 cxstring token = cx_str("1"); |
|
267 cxListAdd(tokens, &token); |
|
268 token = cx_str("+"); |
|
269 cxListAdd(tokens, &token); |
|
270 token = cx_str("1"); |
|
271 cxListAdd(tokens, &token); |
|
272 token = cx_str("=="); |
|
273 cxListAdd(tokens, &token); |
|
274 token = cx_str("2"); |
|
275 cxListAdd(tokens, &token); |
|
276 |
|
277 UCX_TEST_BEGIN; |
|
278 |
|
279 size_t pos = 0; |
|
280 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); |
|
281 |
|
282 UCX_TEST_ASSERT(pos == 5, "wrong token pos"); |
|
283 UCX_TEST_ASSERT(expr, "expression is null"); |
|
284 |
|
285 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_BINARY, "wrong expression type"); |
|
286 UCX_TEST_ASSERT(expr->left, "left expression is null"); |
|
287 UCX_TEST_ASSERT(expr->right, "right expression is null"); |
|
288 UCX_TEST_ASSERT(expr->right->value.i == 2, "right wrong value"); |
|
289 |
|
290 UCX_TEST_ASSERT(expr->left->operator == NSAPI_EXPRESSION_ADD, "left should be a binary operation"); |
|
291 UCX_TEST_ASSERT(expr->right->operator == NSAPI_EXPRESSION_NOOP, "right should be NOOP"); |
|
292 UCX_TEST_ASSERT(expr->left->left, "ADD-op missing left"); |
|
293 UCX_TEST_ASSERT(expr->left->right, "ADD-op missing right"); |
|
294 UCX_TEST_ASSERT(expr->left->left->value.i == 1, "ADD-op: wrong left value"); |
|
295 UCX_TEST_ASSERT(expr->left->right->value.i == 1, "ADD-op: wrong right value"); |
|
296 |
|
297 UCX_TEST_END; |
|
298 |
|
299 pool_destroy(pool); |
|
300 } |
|
301 |
|
302 UCX_TEST(test_expr_parse_expr_bracket) { |
|
303 pool_handle_t *pool = pool_create(); |
|
304 |
|
305 // expression: 2 * (1 + 2) == 6 |
|
306 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring)); |
|
307 cxstring token = cx_str("2"); |
|
308 cxListAdd(tokens, &token); |
|
309 token = cx_str("*"); |
|
310 cxListAdd(tokens, &token); |
|
311 token = cx_str("("); |
|
312 cxListAdd(tokens, &token); |
|
313 token = cx_str("1"); |
|
314 cxListAdd(tokens, &token); |
|
315 token = cx_str("+"); |
|
316 cxListAdd(tokens, &token); |
|
317 token = cx_str("2"); |
|
318 cxListAdd(tokens, &token); |
|
319 token = cx_str(")"); |
|
320 cxListAdd(tokens, &token); |
|
321 token = cx_str("=="); |
|
322 cxListAdd(tokens, &token); |
|
323 token = cx_str("6"); |
|
324 cxListAdd(tokens, &token); |
|
325 |
|
326 UCX_TEST_BEGIN; |
|
327 |
|
328 size_t pos = 0; |
|
329 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); |
|
330 |
|
331 UCX_TEST_ASSERT(pos == 9, "wrong token pos"); |
|
332 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_EQ, "root: wrong operator"); |
|
333 UCX_TEST_ASSERT(expr->left, "missing left expression"); |
|
334 UCX_TEST_ASSERT(expr->right, "missing right expression"); |
|
335 UCX_TEST_ASSERT(expr->right->type == NSAPI_EXPRESSION_INT, "right expression has wrong type"); |
|
336 UCX_TEST_ASSERT(expr->left->operator == NSAPI_EXPRESSION_MUL, "left expression has wrong operator"); |
|
337 UCX_TEST_ASSERT(expr->left->left, "mul: missing left"); |
|
338 UCX_TEST_ASSERT(expr->left->right, "mul: missing right"); |
|
339 UCX_TEST_ASSERT(expr->left->right->operator == NSAPI_EXPRESSION_ADD, "missing add operator"); |
|
340 UCX_TEST_ASSERT(expr->left->right->left, "add: missing left"); |
|
341 UCX_TEST_ASSERT(expr->left->right->left, "add: missing right"); |
|
342 |
|
343 UCX_TEST_END; |
|
344 |
|
345 pool_destroy(pool); |
|
346 } |