src/server/test/object.c

changeset 435
713ec3da79ec
parent 424
3df9258cd3cc
child 452
ce359a2b51fe
equal deleted inserted replaced
434:ff576305ae6e 435:713ec3da79ec
349 349
350 size_t pos = 0; 350 size_t pos = 0;
351 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos); 351 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos);
352 352
353 UCX_TEST_ASSERT(pos == 9, "wrong token pos"); 353 UCX_TEST_ASSERT(pos == 9, "wrong token pos");
354 UCX_TEST_ASSERT(expr, "expression is null");
354 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_EQ, "root: wrong operator"); 355 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_EQ, "root: wrong operator");
355 UCX_TEST_ASSERT(expr->left, "missing left expression"); 356 UCX_TEST_ASSERT(expr->left, "missing left expression");
356 UCX_TEST_ASSERT(expr->right, "missing right expression"); 357 UCX_TEST_ASSERT(expr->right, "missing right expression");
357 UCX_TEST_ASSERT(expr->right->type == NSAPI_EXPRESSION_INT, "right expression has wrong type"); 358 UCX_TEST_ASSERT(expr->right->type == NSAPI_EXPRESSION_INT, "right expression has wrong type");
358 UCX_TEST_ASSERT(expr->left->operator == NSAPI_EXPRESSION_MUL, "left expression has wrong operator"); 359 UCX_TEST_ASSERT(expr->left->operator == NSAPI_EXPRESSION_MUL, "left expression has wrong operator");
364 365
365 UCX_TEST_END; 366 UCX_TEST_END;
366 367
367 pool_destroy(pool); 368 pool_destroy(pool);
368 } 369 }
370
371 UCX_TEST(test_expr_parse_expr_func_arg0) {
372 pool_handle_t *pool = pool_create();
373
374 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring));
375 cxstring token = cx_str("test");
376 cxListAdd(tokens, &token);
377 token = cx_str("(");
378 cxListAdd(tokens, &token);
379 token = cx_str(")");
380 cxListAdd(tokens, &token);
381
382 UCX_TEST_BEGIN;
383
384 size_t pos = 0;
385 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos);
386
387 UCX_TEST_ASSERT(pos == 3, "wrong token pos");
388 UCX_TEST_ASSERT(expr, "expression is null");
389 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_IDENTIFIER, "wrong expression type");
390 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_CALL, "wrong expression operator");
391 UCX_TEST_ASSERT(!expr->left, "left is not null");
392 UCX_TEST_ASSERT(!expr->right, "right is not null");
393
394 UCX_TEST_END;
395
396 pool_destroy(pool);
397 }
398
399
400 int test_get_args(NSAPIExpression *arglist, size_t maxArgs, size_t *numArgs, NSAPIExpression *arg) {
401 if(!arg) {
402 return 0;
403 }
404 if(arg->operator == NSAPI_EXPRESSION_ARG) {
405 if(arg->left) {
406 if(test_get_args(arglist, maxArgs, numArgs, arg->left)) {
407 return 1;
408 }
409 }
410 if(arg->right) {
411 if(test_get_args(arglist, maxArgs, numArgs, arg->right)) {
412 return 1;
413 }
414 }
415 } else {
416 if(*numArgs >= maxArgs) {
417 return 1;
418 }
419 arglist[*numArgs] = *arg;
420 (*numArgs)++;
421 }
422
423 return 0;
424 }
425
426
427 UCX_TEST(test_expr_parse_expr_func_arg1) {
428 pool_handle_t *pool = pool_create();
429
430 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring));
431 cxstring token = cx_str("test");
432 cxListAdd(tokens, &token);
433 token = cx_str("(");
434 cxListAdd(tokens, &token);
435 token = cx_str("1");
436 cxListAdd(tokens, &token);
437 token = cx_str(")");
438 cxListAdd(tokens, &token);
439
440 UCX_TEST_BEGIN;
441
442 size_t pos = 0;
443 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos);
444
445 UCX_TEST_ASSERT(pos == tokens->size, "wrong token pos");
446 UCX_TEST_ASSERT(expr, "expression is null");
447 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_IDENTIFIER, "wrong expression type");
448 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_CALL, "wrong expression operator");
449
450 size_t numArgs = 0;
451 NSAPIExpression args[8];
452 int err = test_get_args(args, 8, &numArgs, expr->left);
453 UCX_TEST_ASSERT(err == 0, "too much args");
454 UCX_TEST_ASSERT(numArgs == 1, "wrong arg count");
455 UCX_TEST_ASSERT(args[0].value.i == 1, "wrong arg value");
456
457 UCX_TEST_END;
458
459 pool_destroy(pool);
460 }
461
462 UCX_TEST(test_expr_parse_expr_func_arg3) {
463 pool_handle_t *pool = pool_create();
464
465 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring));
466 cxstring token = cx_str("test");
467 cxListAdd(tokens, &token);
468 token = cx_str("(");
469 cxListAdd(tokens, &token);
470 token = cx_str("1");
471 cxListAdd(tokens, &token);
472 token = cx_str(",");
473 cxListAdd(tokens, &token);
474 token = cx_str("2");
475 cxListAdd(tokens, &token);
476 token = cx_str(",");
477 cxListAdd(tokens, &token);
478 token = cx_str("3");
479 cxListAdd(tokens, &token);
480 token = cx_str(")");
481 cxListAdd(tokens, &token);
482
483 UCX_TEST_BEGIN;
484
485 size_t pos = 0;
486 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos);
487
488 UCX_TEST_ASSERT(pos == tokens->size, "wrong token pos");
489 UCX_TEST_ASSERT(expr, "expression is null");
490 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_IDENTIFIER, "wrong expression type");
491 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_CALL, "wrong expression operator");
492
493 size_t numArgs = 0;
494 NSAPIExpression args[8];
495 int err = test_get_args(args, 8, &numArgs, expr->left);
496 UCX_TEST_ASSERT(err == 0, "too much args");
497 UCX_TEST_ASSERT(numArgs == 3, "wrong arg count");
498 UCX_TEST_ASSERT(args[0].value.i == 1, "arg0: wrong value");
499 UCX_TEST_ASSERT(args[1].value.i == 2, "arg1: wrong value");
500 UCX_TEST_ASSERT(args[2].value.i == 3, "arg2: wrong value");
501
502 UCX_TEST_END;
503
504 pool_destroy(pool);
505 }
506
507 UCX_TEST(test_expr_parse_expr_func_expr1) {
508 pool_handle_t *pool = pool_create();
509
510 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring));
511 cxstring token = cx_str("test");
512 cxListAdd(tokens, &token);
513 token = cx_str("(");
514 cxListAdd(tokens, &token);
515 token = cx_str("1");
516 cxListAdd(tokens, &token);
517 token = cx_str(",");
518 cxListAdd(tokens, &token);
519 token = cx_str("2");
520 cxListAdd(tokens, &token);
521 token = cx_str("+");
522 cxListAdd(tokens, &token);
523 token = cx_str("3");
524 cxListAdd(tokens, &token);
525 token = cx_str(")");
526 cxListAdd(tokens, &token);
527
528 UCX_TEST_BEGIN;
529
530 size_t pos = 0;
531 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos);
532
533 UCX_TEST_ASSERT(pos == tokens->size, "wrong token pos");
534 UCX_TEST_ASSERT(expr, "expression is null");
535 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_IDENTIFIER, "wrong expression type");
536 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_CALL, "wrong expression operator");
537
538 size_t numArgs = 0;
539 NSAPIExpression args[8];
540 int err = test_get_args(args, 8, &numArgs, expr->left);
541 UCX_TEST_ASSERT(err == 0, "too much args");
542 UCX_TEST_ASSERT(numArgs == 2, "wrong arg count");
543 UCX_TEST_ASSERT(args[0].value.i == 1, "arg0: wrong value");
544 UCX_TEST_ASSERT(args[1].operator == NSAPI_EXPRESSION_ADD, "arg1: wrong operator");
545 UCX_TEST_ASSERT(args[1].left && args[1].right, "arg1: missing operator values");
546 UCX_TEST_ASSERT(args[1].left->value.i == 2 && args[1].right->value.i == 3, "arg1: wrong operator values");
547
548 UCX_TEST_END;
549
550 pool_destroy(pool);
551 }
552
553 UCX_TEST(test_expr_parse_expr_func_expr2) {
554 pool_handle_t *pool = pool_create();
555
556 CxList *tokens = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(cxstring));
557 cxstring token = cx_str("test");
558 cxListAdd(tokens, &token);
559 token = cx_str("(");
560 cxListAdd(tokens, &token);
561 token = cx_str("2");
562 cxListAdd(tokens, &token);
563 token = cx_str("+");
564 cxListAdd(tokens, &token);
565 token = cx_str("3");
566 cxListAdd(tokens, &token);
567 token = cx_str(",");
568 cxListAdd(tokens, &token);
569 token = cx_str("sub");
570 cxListAdd(tokens, &token);
571 token = cx_str("(");
572 cxListAdd(tokens, &token);
573 token = cx_str("4");
574 cxListAdd(tokens, &token);
575 token = cx_str(")");
576 cxListAdd(tokens, &token);
577 token = cx_str(",");
578 cxListAdd(tokens, &token);
579 token = cx_str("6");
580 cxListAdd(tokens, &token);
581 token = cx_str(")");
582 cxListAdd(tokens, &token);
583
584 UCX_TEST_BEGIN;
585
586 size_t pos = 0;
587 NSAPIExpression *expr = expr_parse_logical_expr(pool, tokens, &pos);
588
589 UCX_TEST_ASSERT(pos == tokens->size, "wrong token pos");
590 UCX_TEST_ASSERT(expr, "expression is null");
591 UCX_TEST_ASSERT(expr->type == NSAPI_EXPRESSION_IDENTIFIER, "wrong expression type");
592 UCX_TEST_ASSERT(expr->operator == NSAPI_EXPRESSION_CALL, "wrong expression operator");
593
594 size_t numArgs = 0;
595 NSAPIExpression args[8];
596 int err = test_get_args(args, 8, &numArgs, expr->left);
597 UCX_TEST_ASSERT(err == 0, "too much args");
598 UCX_TEST_ASSERT(numArgs == 3, "wrong arg count");
599 UCX_TEST_ASSERT(args[0].operator == NSAPI_EXPRESSION_ADD, "arg0: wrong operator");
600 UCX_TEST_ASSERT(args[0].left && args[0].right, "arg0: missing operator values");
601 UCX_TEST_ASSERT(args[1].operator == NSAPI_EXPRESSION_CALL, "arg1: wrong operator");
602 UCX_TEST_ASSERT(args[1].left, "arg1: missing args");
603 UCX_TEST_ASSERT(args[2].type == NSAPI_EXPRESSION_INT, "arg2: wrong type");
604 UCX_TEST_ASSERT(args[2].value.i == 6, "arg2: wrong value");
605
606 UCX_TEST_END;
607
608 pool_destroy(pool);
609 }

mercurial