/*
* 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 .
*/
#include "config.h"
#include
#include
#include
#include
#include
#include
#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;
}