UNIXworkcode

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), NULL, 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_neg_value) { 61 pool_handle_t *pool = pool_create(); 62 63 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 64 cxstring token = cx_str("-123"); 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_INT, "wrong type"); 75 UCX_TEST_ASSERT(expr->value.i == -123, "wrong value"); 76 77 UCX_TEST_END; 78 79 pool_destroy(pool); 80 } 81 82 UCX_TEST(test_expr_parse_expr_value_str) { 83 pool_handle_t *pool = pool_create(); 84 85 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 86 cxstring token = cx_str("\"hello world\""); 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_STRING, "wrong type"); 97 UCX_TEST_ASSERT(!cx_strcmp(expr->value.str, cx_str("hello world")), "wrong value"); 98 99 UCX_TEST_END; 100 101 pool_destroy(pool); 102 } 103 104 UCX_TEST(test_expr_parse_expr_value_bool) { 105 pool_handle_t *pool = pool_create(); 106 107 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 108 cxstring token = cx_str("true"); 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_BOOL, "wrong type"); 119 UCX_TEST_ASSERT(expr->value.b == 1, "wrong value"); 120 121 UCX_TEST_END; 122 123 pool_destroy(pool); 124 } 125 126 UCX_TEST(test_expr_parse_expr_value_var) { 127 pool_handle_t *pool = pool_create(); 128 129 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 130 cxstring token = cx_str("$test"); 131 cxListAdd(tokens, &token); 132 133 UCX_TEST_BEGIN; 134 135 size_t pos = 0; 136 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 137 138 UCX_TEST_ASSERT(pos == 1, "wrong token pos"); 139 UCX_TEST_ASSERT(expr, "expression is null"); 140 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_VARIABLE, "wrong type"); 141 UCX_TEST_ASSERT(!cx_strcmp(expr->value.var, cx_str("test")), "wrong var name"); 142 143 UCX_TEST_END; 144 145 pool_destroy(pool); 146 } 147 148 UCX_TEST(test_expr_parse_expr_not_value) { 149 pool_handle_t *pool = pool_create(); 150 151 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 152 cxstring token = cx_str("not"); 153 cxListAdd(tokens, &token); 154 token = cx_str("true"); 155 cxListAdd(tokens, &token); 156 157 UCX_TEST_BEGIN; 158 159 size_t pos = 0; 160 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 161 162 UCX_TEST_ASSERT(pos == 2, "wrong token pos"); 163 UCX_TEST_ASSERT(expr, "expression is null"); 164 165 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_UNARY, "wrong root expression type"); 166 UCX_TEST_ASSERT(expr->left, "missing left expression"); 167 UCX_TEST_ASSERT(!expr->right, "right expression should be null"); 168 UCX_TEST_ASSERT(expr->left->type == NSAPI_EXPRESSION_BOOL, "left expression has wrong type"); 169 UCX_TEST_ASSERT(expr->left->value.b == 1, "left expression has wrong value"); 170 171 UCX_TEST_END; 172 173 pool_destroy(pool); 174 } 175 176 UCX_TEST(test_expr_parse_expr_sign_value) { 177 pool_handle_t *pool = pool_create(); 178 179 CxList *tokens1 = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 180 cxstring token = cx_str("+"); 181 cxListAdd(tokens1, &token); 182 token = cx_str("123"); 183 cxListAdd(tokens1, &token); 184 185 CxList *tokens2 = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 186 token = cx_str("-"); 187 cxListAdd(tokens2, &token); 188 token = cx_str("123"); 189 cxListAdd(tokens2, &token); 190 191 UCX_TEST_BEGIN; 192 193 size_t pos = 0; 194 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens1, &pos); 195 196 UCX_TEST_ASSERT(pos == 2, "test1: wrong token pos"); 197 UCX_TEST_ASSERT(expr, "test1: expression is null"); 198 199 pos = 0; 200 expr = expr_parse_logical_expr(pool, tokens2, &pos); 201 202 UCX_TEST_ASSERT(pos == 2, "test2: wrong token pos"); 203 UCX_TEST_ASSERT(expr, "test2: expression is null"); 204 205 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_UNARY, "wrong expression type"); 206 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_SUB, "wrong expression operator"); 207 UCX_TEST_ASSERT(expr->left, "missing left expresion"); 208 UCX_TEST_ASSERT(expr->left->type == NSAPI_EXPRESSION_INT, "left expression has wrong type"); 209 UCX_TEST_ASSERT(expr->left->value.i == 123, "left expression has wrong value"); 210 211 UCX_TEST_END; 212 213 pool_destroy(pool); 214 } 215 216 217 218 UCX_TEST(test_expr_parse_expr_compare2values) { 219 pool_handle_t *pool = pool_create(); 220 221 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 222 cxstring token = cx_str("2"); 223 cxListAdd(tokens, &token); 224 token = cx_str("=="); 225 cxListAdd(tokens, &token); 226 token = cx_str("2"); 227 cxListAdd(tokens, &token); 228 229 UCX_TEST_BEGIN; 230 231 size_t pos = 0; 232 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 233 234 UCX_TEST_ASSERT(pos == 3, "wrong token pos"); 235 UCX_TEST_ASSERT(expr, "expression is null"); 236 237 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_BINARY, "wrong expression type"); 238 UCX_TEST_ASSERT(expr->left, "left expression is null"); 239 UCX_TEST_ASSERT(expr->right, "right expression is null"); 240 241 UCX_TEST_ASSERT(expr->left->operator == NSAPI_EXPRESSION_NOOP, "left should be a literal with no operator"); 242 UCX_TEST_ASSERT(expr->right->operator == NSAPI_EXPRESSION_NOOP, "right should be a literal with no operator"); 243 244 UCX_TEST_END; 245 246 pool_destroy(pool); 247 } 248 249 UCX_TEST(test_expr_parse_expr_compare2value_expr) { 250 pool_handle_t *pool = pool_create(); 251 252 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 253 cxstring token = cx_str("2"); 254 cxListAdd(tokens, &token); 255 token = cx_str("=="); 256 cxListAdd(tokens, &token); 257 token = cx_str("1"); 258 cxListAdd(tokens, &token); 259 token = cx_str("+"); 260 cxListAdd(tokens, &token); 261 token = cx_str("1"); 262 cxListAdd(tokens, &token); 263 264 UCX_TEST_BEGIN; 265 266 size_t pos = 0; 267 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 268 269 UCX_TEST_ASSERT(pos == 5, "wrong token pos"); 270 UCX_TEST_ASSERT(expr, "expression is null"); 271 272 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_BINARY, "wrong expression type"); 273 UCX_TEST_ASSERT(expr->left, "left expression is null"); 274 UCX_TEST_ASSERT(expr->right, "right expression is null"); 275 276 UCX_TEST_ASSERT(expr->left->operator == NSAPI_EXPRESSION_NOOP, "left should be a literal with no operator"); 277 UCX_TEST_ASSERT(expr->right->operator == NSAPI_EXPRESSION_ADD, "right should be a binary expression"); 278 279 UCX_TEST_END; 280 281 pool_destroy(pool); 282 } 283 284 UCX_TEST(test_expr_parse_expr_compare2expr_value) { 285 pool_handle_t *pool = pool_create(); 286 287 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 288 cxstring token = cx_str("1"); 289 cxListAdd(tokens, &token); 290 token = cx_str("+"); 291 cxListAdd(tokens, &token); 292 token = cx_str("1"); 293 cxListAdd(tokens, &token); 294 token = cx_str("=="); 295 cxListAdd(tokens, &token); 296 token = cx_str("2"); 297 cxListAdd(tokens, &token); 298 299 UCX_TEST_BEGIN; 300 301 size_t pos = 0; 302 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 303 304 UCX_TEST_ASSERT(pos == 5, "wrong token pos"); 305 UCX_TEST_ASSERT(expr, "expression is null"); 306 307 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_BINARY, "wrong expression type"); 308 UCX_TEST_ASSERT(expr->left, "left expression is null"); 309 UCX_TEST_ASSERT(expr->right, "right expression is null"); 310 UCX_TEST_ASSERT(expr->right->value.i == 2, "right wrong value"); 311 312 UCX_TEST_ASSERT(expr->left->operator == NSAPI_EXPRESSION_ADD, "left should be a binary operation"); 313 UCX_TEST_ASSERT(expr->right->operator == NSAPI_EXPRESSION_NOOP, "right should be NOOP"); 314 UCX_TEST_ASSERT(expr->left->left, "ADD-op missing left"); 315 UCX_TEST_ASSERT(expr->left->right, "ADD-op missing right"); 316 UCX_TEST_ASSERT(expr->left->left->value.i == 1, "ADD-op: wrong left value"); 317 UCX_TEST_ASSERT(expr->left->right->value.i == 1, "ADD-op: wrong right value"); 318 319 UCX_TEST_END; 320 321 pool_destroy(pool); 322 } 323 324 UCX_TEST(test_expr_parse_expr_bracket) { 325 pool_handle_t *pool = pool_create(); 326 327 // expression: 2 * (1 + 2) == 6 328 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 329 cxstring token = cx_str("2"); 330 cxListAdd(tokens, &token); 331 token = cx_str("*"); 332 cxListAdd(tokens, &token); 333 token = cx_str("("); 334 cxListAdd(tokens, &token); 335 token = cx_str("1"); 336 cxListAdd(tokens, &token); 337 token = cx_str("+"); 338 cxListAdd(tokens, &token); 339 token = cx_str("2"); 340 cxListAdd(tokens, &token); 341 token = cx_str(")"); 342 cxListAdd(tokens, &token); 343 token = cx_str("=="); 344 cxListAdd(tokens, &token); 345 token = cx_str("6"); 346 cxListAdd(tokens, &token); 347 348 UCX_TEST_BEGIN; 349 350 size_t pos = 0; 351 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 352 353 UCX_TEST_ASSERT(pos == 9, "wrong token pos"); 354 UCX_TEST_ASSERT(expr, "expression is null"); 355 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_EQ, "root: wrong operator"); 356 UCX_TEST_ASSERT(expr->left, "missing left expression"); 357 UCX_TEST_ASSERT(expr->right, "missing right expression"); 358 UCX_TEST_ASSERT(expr->right->type == NSAPI_EXPRESSION_INT, "right expression has wrong type"); 359 UCX_TEST_ASSERT(expr->left->operator == NSAPI_EXPRESSION_MUL, "left expression has wrong operator"); 360 UCX_TEST_ASSERT(expr->left->left, "mul: missing left"); 361 UCX_TEST_ASSERT(expr->left->right, "mul: missing right"); 362 UCX_TEST_ASSERT(expr->left->right->operator == NSAPI_EXPRESSION_ADD, "missing add operator"); 363 UCX_TEST_ASSERT(expr->left->right->left, "add: missing left"); 364 UCX_TEST_ASSERT(expr->left->right->left, "add: missing right"); 365 366 UCX_TEST_END; 367 368 pool_destroy(pool); 369 } 370 371 UCX_TEST(test_expr_op_defined_simple) { 372 pool_handle_t *pool = pool_create(); 373 374 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 375 cxstring token = cx_str("defined"); 376 cxListAdd(tokens, &token); 377 378 token = cx_str("$testvar1"); 379 cxListAdd(tokens, &token); 380 381 UCX_TEST_BEGIN; 382 383 size_t pos = 0; 384 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 385 386 UCX_TEST_ASSERT(pos == 2, "wrong token pos"); 387 UCX_TEST_ASSERT(expr, "expression is null"); 388 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_UNARY, "wrong type"); 389 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_VALUE_DEFINED, "wrong operator"); 390 UCX_TEST_ASSERT(expr->left, "missing left operand"); 391 UCX_TEST_ASSERT(expr->left->type == NSAPI_EXPRESSION_VARIABLE, "operand is not a variable"); 392 393 UCX_TEST_END; 394 395 pool_destroy(pool); 396 } 397 398 UCX_TEST(test_expr_op_defined) { 399 pool_handle_t *pool = pool_create(); 400 401 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 402 cxstring token = cx_str("true"); 403 cxListAdd(tokens, &token); 404 405 token = cx_str("=="); 406 cxListAdd(tokens, &token); 407 408 token = cx_str("not"); 409 cxListAdd(tokens, &token); 410 411 token = cx_str("defined"); 412 cxListAdd(tokens, &token); 413 414 token = cx_str("$var"); 415 cxListAdd(tokens, &token); 416 417 UCX_TEST_BEGIN; 418 419 size_t pos = 0; 420 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 421 422 UCX_TEST_ASSERT(pos == 5, "wrong token pos"); 423 UCX_TEST_ASSERT(expr, "expression is null"); 424 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_EQ, "wrong operator"); 425 UCX_TEST_ASSERT(expr->left, "missing left"); 426 UCX_TEST_ASSERT(expr->right, "missing right"); 427 UCX_TEST_ASSERT(expr->left->type == NSAPI_EXPRESSION_BOOL, "left expression is not a bool"); 428 UCX_TEST_ASSERT(expr->right->operator == NSAPI_EXPRESSION_NOT, "right: wrong operator"); 429 UCX_TEST_ASSERT(expr->right->left, "not op: missing left"); 430 UCX_TEST_ASSERT(expr->right->left->operator == NSAPI_EXPRESSION_VALUE_DEFINED, "missing defined operator"); 431 432 UCX_TEST_END; 433 434 pool_destroy(pool); 435 } 436 437 UCX_TEST(test_expr_op_file_exists_simple) { 438 pool_handle_t *pool = pool_create(); 439 440 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 441 cxstring token = cx_str("-e"); 442 cxListAdd(tokens, &token); 443 444 token = cx_str("\"/path/file\""); 445 cxListAdd(tokens, &token); 446 447 UCX_TEST_BEGIN; 448 449 size_t pos = 0; 450 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 451 452 UCX_TEST_ASSERT(pos == 2, "wrong token pos"); 453 UCX_TEST_ASSERT(expr, "expression is null"); 454 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_UNARY, "wrong type"); 455 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_FILE_DIR_EXISTS, "wrong operator"); 456 UCX_TEST_ASSERT(expr->left, "missing left operand"); 457 UCX_TEST_ASSERT(expr->left->type == NSAPI_EXPRESSION_STRING, "operand is not a string"); 458 UCX_TEST_ASSERT(!cx_strcmp(expr->left->value.str, cx_str("/path/file")), "wrong string operand"); 459 460 UCX_TEST_END; 461 462 pool_destroy(pool); 463 } 464 465 UCX_TEST(test_expr_parse_expr_func_arg0) { 466 pool_handle_t *pool = pool_create(); 467 468 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 469 cxstring token = cx_str("test"); 470 cxListAdd(tokens, &token); 471 token = cx_str("("); 472 cxListAdd(tokens, &token); 473 token = cx_str(")"); 474 cxListAdd(tokens, &token); 475 476 UCX_TEST_BEGIN; 477 478 size_t pos = 0; 479 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 480 481 UCX_TEST_ASSERT(pos == 3, "wrong token pos"); 482 UCX_TEST_ASSERT(expr, "expression is null"); 483 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_IDENTIFIER, "wrong expression type"); 484 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_CALL, "wrong expression operator"); 485 UCX_TEST_ASSERT(!expr->left, "left is not null"); 486 UCX_TEST_ASSERT(!expr->right, "right is not null"); 487 488 UCX_TEST_END; 489 490 pool_destroy(pool); 491 } 492 493 494 int test_get_args(NSAPIExpression *arglist, size_t maxArgs, size_t *numArgs, NSAPIExpression *arg) { 495 if(!arg) { 496 return 0; 497 } 498 if(arg->operator == NSAPI_EXPRESSION_ARG) { 499 if(arg->left) { 500 if(test_get_args(arglist, maxArgs, numArgs, arg->left)) { 501 return 1; 502 } 503 } 504 if(arg->right) { 505 if(test_get_args(arglist, maxArgs, numArgs, arg->right)) { 506 return 1; 507 } 508 } 509 } else { 510 if(*numArgs >= maxArgs) { 511 return 1; 512 } 513 arglist[*numArgs] = *arg; 514 (*numArgs)++; 515 } 516 517 return 0; 518 } 519 520 521 UCX_TEST(test_expr_parse_expr_func_arg1) { 522 pool_handle_t *pool = pool_create(); 523 524 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 525 cxstring token = cx_str("test"); 526 cxListAdd(tokens, &token); 527 token = cx_str("("); 528 cxListAdd(tokens, &token); 529 token = cx_str("1"); 530 cxListAdd(tokens, &token); 531 token = cx_str(")"); 532 cxListAdd(tokens, &token); 533 534 UCX_TEST_BEGIN; 535 536 size_t pos = 0; 537 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 538 539 UCX_TEST_ASSERT(pos == tokens->size, "wrong token pos"); 540 UCX_TEST_ASSERT(expr, "expression is null"); 541 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_IDENTIFIER, "wrong expression type"); 542 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_CALL, "wrong expression operator"); 543 544 size_t numArgs = 0; 545 NSAPIExpression args[8]; 546 int err = test_get_args(args, 8, &numArgs, expr->left); 547 UCX_TEST_ASSERT(err == 0, "too much args"); 548 UCX_TEST_ASSERT(numArgs == 1, "wrong arg count"); 549 UCX_TEST_ASSERT(args[0].value.i == 1, "wrong arg value"); 550 551 UCX_TEST_END; 552 553 pool_destroy(pool); 554 } 555 556 UCX_TEST(test_expr_parse_expr_func_arg3) { 557 pool_handle_t *pool = pool_create(); 558 559 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 560 cxstring token = cx_str("test"); 561 cxListAdd(tokens, &token); 562 token = cx_str("("); 563 cxListAdd(tokens, &token); 564 token = cx_str("1"); 565 cxListAdd(tokens, &token); 566 token = cx_str(","); 567 cxListAdd(tokens, &token); 568 token = cx_str("2"); 569 cxListAdd(tokens, &token); 570 token = cx_str(","); 571 cxListAdd(tokens, &token); 572 token = cx_str("3"); 573 cxListAdd(tokens, &token); 574 token = cx_str(")"); 575 cxListAdd(tokens, &token); 576 577 UCX_TEST_BEGIN; 578 579 size_t pos = 0; 580 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 581 582 UCX_TEST_ASSERT(pos == tokens->size, "wrong token pos"); 583 UCX_TEST_ASSERT(expr, "expression is null"); 584 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_IDENTIFIER, "wrong expression type"); 585 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_CALL, "wrong expression operator"); 586 587 size_t numArgs = 0; 588 NSAPIExpression args[8]; 589 int err = test_get_args(args, 8, &numArgs, expr->left); 590 UCX_TEST_ASSERT(err == 0, "too much args"); 591 UCX_TEST_ASSERT(numArgs == 3, "wrong arg count"); 592 UCX_TEST_ASSERT(args[0].value.i == 1, "arg0: wrong value"); 593 UCX_TEST_ASSERT(args[1].value.i == 2, "arg1: wrong value"); 594 UCX_TEST_ASSERT(args[2].value.i == 3, "arg2: wrong value"); 595 596 UCX_TEST_END; 597 598 pool_destroy(pool); 599 } 600 601 UCX_TEST(test_expr_parse_expr_func_expr1) { 602 pool_handle_t *pool = pool_create(); 603 604 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 605 cxstring token = cx_str("test"); 606 cxListAdd(tokens, &token); 607 token = cx_str("("); 608 cxListAdd(tokens, &token); 609 token = cx_str("1"); 610 cxListAdd(tokens, &token); 611 token = cx_str(","); 612 cxListAdd(tokens, &token); 613 token = cx_str("2"); 614 cxListAdd(tokens, &token); 615 token = cx_str("+"); 616 cxListAdd(tokens, &token); 617 token = cx_str("3"); 618 cxListAdd(tokens, &token); 619 token = cx_str(")"); 620 cxListAdd(tokens, &token); 621 622 UCX_TEST_BEGIN; 623 624 size_t pos = 0; 625 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 626 627 UCX_TEST_ASSERT(pos == tokens->size, "wrong token pos"); 628 UCX_TEST_ASSERT(expr, "expression is null"); 629 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_IDENTIFIER, "wrong expression type"); 630 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_CALL, "wrong expression operator"); 631 632 size_t numArgs = 0; 633 NSAPIExpression args[8]; 634 int err = test_get_args(args, 8, &numArgs, expr->left); 635 UCX_TEST_ASSERT(err == 0, "too much args"); 636 UCX_TEST_ASSERT(numArgs == 2, "wrong arg count"); 637 UCX_TEST_ASSERT(args[0].value.i == 1, "arg0: wrong value"); 638 UCX_TEST_ASSERT(args[1].operator == NSAPI_EXPRESSION_ADD, "arg1: wrong operator"); 639 UCX_TEST_ASSERT(args[1].left && args[1].right, "arg1: missing operator values"); 640 UCX_TEST_ASSERT(args[1].left->value.i == 2 && args[1].right->value.i == 3, "arg1: wrong operator values"); 641 642 UCX_TEST_END; 643 644 pool_destroy(pool); 645 } 646 647 UCX_TEST(test_expr_parse_expr_func_expr2) { 648 pool_handle_t *pool = pool_create(); 649 650 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), NULL, sizeof(cxstring)); 651 cxstring token = cx_str("test"); 652 cxListAdd(tokens, &token); 653 token = cx_str("("); 654 cxListAdd(tokens, &token); 655 token = cx_str("2"); 656 cxListAdd(tokens, &token); 657 token = cx_str("+"); 658 cxListAdd(tokens, &token); 659 token = cx_str("3"); 660 cxListAdd(tokens, &token); 661 token = cx_str(","); 662 cxListAdd(tokens, &token); 663 token = cx_str("sub"); 664 cxListAdd(tokens, &token); 665 token = cx_str("("); 666 cxListAdd(tokens, &token); 667 token = cx_str("4"); 668 cxListAdd(tokens, &token); 669 token = cx_str(")"); 670 cxListAdd(tokens, &token); 671 token = cx_str(","); 672 cxListAdd(tokens, &token); 673 token = cx_str("6"); 674 cxListAdd(tokens, &token); 675 token = cx_str(")"); 676 cxListAdd(tokens, &token); 677 678 UCX_TEST_BEGIN; 679 680 size_t pos = 0; 681 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 682 683 UCX_TEST_ASSERT(pos == tokens->size, "wrong token pos"); 684 UCX_TEST_ASSERT(expr, "expression is null"); 685 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_IDENTIFIER, "wrong expression type"); 686 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_CALL, "wrong expression operator"); 687 688 size_t numArgs = 0; 689 NSAPIExpression args[8]; 690 int err = test_get_args(args, 8, &numArgs, expr->left); 691 UCX_TEST_ASSERT(err == 0, "too much args"); 692 UCX_TEST_ASSERT(numArgs == 3, "wrong arg count"); 693 UCX_TEST_ASSERT(args[0].operator == NSAPI_EXPRESSION_ADD, "arg0: wrong operator"); 694 UCX_TEST_ASSERT(args[0].left && args[0].right, "arg0: missing operator values"); 695 UCX_TEST_ASSERT(args[1].operator == NSAPI_EXPRESSION_CALL, "arg1: wrong operator"); 696 UCX_TEST_ASSERT(args[1].left, "arg1: missing args"); 697 UCX_TEST_ASSERT(args[2].type == NSAPI_EXPRESSION_INT, "arg2: wrong type"); 698 UCX_TEST_ASSERT(args[2].value.i == 6, "arg2: wrong value"); 699 700 UCX_TEST_END; 701 702 pool_destroy(pool); 703 } 704