123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- /*
- * Copyright (C) 2020 Weber Yann
- *
- * This file is part of pyrpn.
- *
- * pyrpn is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * any later version.
- *
- * pyrpn is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with pyrpn. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "config.h"
-
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/mman.h>
-
- #include "rpn_lib.h"
- #include "rpn_jit.h"
- #include "rpn_parse.h"
-
- int test0()
- {
- unsigned long res;
-
- rpn_expr_t expr;
- rpn_run_f expr_run;
-
- expr.stack_sz = 8;
- expr.args_count = 0;
-
- if(_rpn_expr_init_map(&expr) < 0)
- {
- perror("Error starting map");
- return 1;
- }
-
- unsigned long val;
- /*
- val = 2;
- CODE_VALUE_CPY(&expr, val);
- val = 40;
- CODE_VALUE_CPY(&expr, val);
- CODE_VALUE_CPY(&expr, val);
- CODE_VALUE_CPY(&expr, val);
- CODE_VALUE_CPY(&expr, val);
- CODE_VALUE_CPY(&expr, val);
- CODE_VALUE_CPY(&expr, val);
- CODE_VALUE_CPY(&expr, val);
- CODE_VALUE_CPY(&expr, val);
- CODE_VALUE_CPY(&expr, val);
- CODE_PART_CPY(&expr, rpn_add);
- CODE_PART_CPY(&expr, rpn_add);
- CODE_PART_CPY(&expr, rpn_add);
- CODE_PART_CPY(&expr, rpn_add);
- CODE_PART_CPY(&expr, rpn_add);
- CODE_PART_CPY(&expr, rpn_add);
- CODE_PART_CPY(&expr, rpn_add);
- CODE_PART_CPY(&expr, rpn_add);
- CODE_PART_CPY(&expr, rpn_add);
- */
- val = 1;
- CODE_ARG_CPY(&expr, val);
- val = 0;
- CODE_ARG_CPY(&expr, val);
- CODE_PART_CPY(&expr, rpn_add);
-
- if(_rpn_expr_end_map(&expr) < 0)
- {
- perror("Error ending map");
- return 2;
- }
-
- expr_run = expr.code_map;
-
- long unsigned int args[4] = {1337,42,0,4242};
- //printf("Ready to run !\n");
- res = expr_run(8, args, NULL);
- //printf("Tada : %ld %p\n", res, res);
- res++;
-
- return 0;
- }
-
- int test_add()
- {
- unsigned long res;
-
- rpn_expr_t expr;
- if(rpn_expr_init(&expr, 8, 0) < 0 ||
- rpn_expr_compile(&expr, "16 26 +"))
- {
- dprintf(2, "Error initializing expr");
- return 1;
- }
- res = rpn_expr_eval(&expr, NULL);
- //printf("Result = %ld\n", res);
- if(res != 42)
- {
- dprintf(2, "Error : expected 42 but %ld received\n",
- res);
- return 2;
- }
- rpn_expr_close(&expr);
- return 0;
- }
-
- int test_args()
- {
- unsigned long res, args[2];
-
- rpn_expr_t expr;
-
- if(rpn_expr_init(&expr, 8, 2))
- {
- dprintf(2, "Error initializing expr");
- return 1;
- }
- if(rpn_expr_compile(&expr, "A0 A1 +") < 0)
- {
- dprintf(2, "Compilation error : %s\n",
- expr.err_reason);
- return 1;
- }
-
- args[0] = 16;
- args[1] = 26;
- res = rpn_expr_eval(&expr, args);
- //printf("Result = %ld\n", res);
- if(res != 42)
- {
- dprintf(2, "Error : expected 42 but %ld received\n",
- res);
- return 2;
- }
- rpn_expr_close(&expr);
- return 0;
- }
-
- int test_stack_sz()
- {
- rpn_expr_t expr;
- int stack_sz, i;
- unsigned long res, args[2];
-
- for(stack_sz=4; stack_sz<256; stack_sz++)
- {
- if(rpn_expr_init(&expr, stack_sz, 2))
- {
- dprintf(2, "Error initializing expr");
- return 1;
- }
- if(rpn_expr_compile(&expr,
- "+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ") < 0)
- {
- dprintf(2, "Compilation error : %s\n",
- expr.err_reason);
- return 1;
- }
-
- args[0] = 16;
- args[1] = 26;
- for(i=0; i<256; i++)
- {
- res = rpn_expr_eval(&expr, args);
- //dprintf(2, "DEBUG : eval %d:%d res = %ld\n", stack_sz, i, res);
- //printf("Result = %ld\n", res);
- if(res != 0)
- {
- dprintf(2, "Error : expected 0 but %ld received\n",
- res);
- return 2;
- }
- }
- rpn_expr_close(&expr);
- }
- return 0;
- }
-
-
- int test_tokenization()
- {
- rpn_expr_t expr;
- int stack_sz, argc, i;
- char *expr_orig, *expr_untok;
-
-
- stack_sz = 32;
- argc=4;
- for(i=0;i<20;i++)
- {
- if(rpn_expr_init(&expr, stack_sz, argc))
- {
- dprintf(2, "Error initializing expr");
- return 1;
- }
- expr_orig = rpn_random(20+i, argc);
- if(rpn_expr_compile(&expr, expr_orig) < 0)
- {
- dprintf(2, "Compilation error : %s\n",
- expr.err_reason);
- return 1;
- }
- expr_untok = rpn_tokenized_expr(&(expr.toks), 0);
- if(strcmp(expr_untok, expr_orig))
- {
- dprintf(2,
- "Untokenized str differ from original : \"%s\" != \"%s\"\n",
- expr_orig, expr_untok);
- return 1;
- }
- rpn_expr_close(&expr);
- free(expr_untok);
- }
-
- return 0;
- }
-
-
-
-
- #define RUNTEST(NAME) \
- printf("Running %s\n", #NAME); \
- printf("%s()\t", #NAME); \
- fsync(1); \
- if(NAME()) \
- { \
- printf("[failed]\n"); \
- res++; \
- } \
- else \
- { \
- printf("[OK]\n"); \
- }
-
- int main()
- {
- int res;
-
- res = 0;
- //RUNTEST(test0);
- RUNTEST(test_add);
- RUNTEST(test_args);
- RUNTEST(test_stack_sz);
- RUNTEST(test_tokenization);
- return res;
- }
|