Fast IFS using RPN notation
python
c
x86-64
nasm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2020 Weber Yann
  3. *
  4. * This file is part of pyrpn.
  5. *
  6. * pyrpn is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * pyrpn is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with pyrpn. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <sys/mman.h>
  26. #include "rpn_lib.h"
  27. #include "rpn_jit.h"
  28. #include "rpn_parse.h"
  29. int test0()
  30. {
  31. unsigned long res;
  32. rpn_expr_t expr;
  33. rpn_run_f expr_run;
  34. expr.stack_sz = 8;
  35. expr.args_count = 0;
  36. if(_rpn_expr_init_map(&expr) < 0)
  37. {
  38. perror("Error starting map");
  39. return 1;
  40. }
  41. unsigned long val;
  42. /*
  43. val = 2;
  44. CODE_VALUE_CPY(&expr, val);
  45. val = 40;
  46. CODE_VALUE_CPY(&expr, val);
  47. CODE_VALUE_CPY(&expr, val);
  48. CODE_VALUE_CPY(&expr, val);
  49. CODE_VALUE_CPY(&expr, val);
  50. CODE_VALUE_CPY(&expr, val);
  51. CODE_VALUE_CPY(&expr, val);
  52. CODE_VALUE_CPY(&expr, val);
  53. CODE_VALUE_CPY(&expr, val);
  54. CODE_VALUE_CPY(&expr, val);
  55. CODE_PART_CPY(&expr, rpn_add);
  56. CODE_PART_CPY(&expr, rpn_add);
  57. CODE_PART_CPY(&expr, rpn_add);
  58. CODE_PART_CPY(&expr, rpn_add);
  59. CODE_PART_CPY(&expr, rpn_add);
  60. CODE_PART_CPY(&expr, rpn_add);
  61. CODE_PART_CPY(&expr, rpn_add);
  62. CODE_PART_CPY(&expr, rpn_add);
  63. CODE_PART_CPY(&expr, rpn_add);
  64. */
  65. val = 1;
  66. CODE_ARG_CPY(&expr, val);
  67. val = 0;
  68. CODE_ARG_CPY(&expr, val);
  69. CODE_PART_CPY(&expr, rpn_add);
  70. if(_rpn_expr_end_map(&expr) < 0)
  71. {
  72. perror("Error ending map");
  73. return 2;
  74. }
  75. expr_run = expr.code_map;
  76. long unsigned int args[4] = {1337,42,0,4242};
  77. //printf("Ready to run !\n");
  78. res = expr_run(8, args, NULL);
  79. //printf("Tada : %ld %p\n", res, res);
  80. res++;
  81. return 0;
  82. }
  83. int test_add()
  84. {
  85. unsigned long res;
  86. rpn_expr_t expr;
  87. if(rpn_expr_init(&expr, 8, 0) < 0 ||
  88. rpn_expr_compile(&expr, "16 26 +"))
  89. {
  90. dprintf(2, "Error initializing expr");
  91. return 1;
  92. }
  93. res = rpn_expr_eval(&expr, NULL);
  94. //printf("Result = %ld\n", res);
  95. if(res != 42)
  96. {
  97. dprintf(2, "Error : expected 42 but %ld received\n",
  98. res);
  99. return 2;
  100. }
  101. rpn_expr_close(&expr);
  102. return 0;
  103. }
  104. int test_args()
  105. {
  106. unsigned long res, args[2];
  107. rpn_expr_t expr;
  108. if(rpn_expr_init(&expr, 8, 2))
  109. {
  110. dprintf(2, "Error initializing expr");
  111. return 1;
  112. }
  113. if(rpn_expr_compile(&expr, "A0 A1 +") < 0)
  114. {
  115. dprintf(2, "Compilation error : %s\n",
  116. expr.err_reason);
  117. return 1;
  118. }
  119. args[0] = 16;
  120. args[1] = 26;
  121. res = rpn_expr_eval(&expr, args);
  122. //printf("Result = %ld\n", res);
  123. if(res != 42)
  124. {
  125. dprintf(2, "Error : expected 42 but %ld received\n",
  126. res);
  127. return 2;
  128. }
  129. rpn_expr_close(&expr);
  130. return 0;
  131. }
  132. int test_stack_sz()
  133. {
  134. rpn_expr_t expr;
  135. int stack_sz, i;
  136. unsigned long res, args[2];
  137. for(stack_sz=4; stack_sz<256; stack_sz++)
  138. {
  139. if(rpn_expr_init(&expr, stack_sz, 2))
  140. {
  141. dprintf(2, "Error initializing expr");
  142. return 1;
  143. }
  144. if(rpn_expr_compile(&expr,
  145. "+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ") < 0)
  146. {
  147. dprintf(2, "Compilation error : %s\n",
  148. expr.err_reason);
  149. return 1;
  150. }
  151. args[0] = 16;
  152. args[1] = 26;
  153. for(i=0; i<256; i++)
  154. {
  155. res = rpn_expr_eval(&expr, args);
  156. //dprintf(2, "DEBUG : eval %d:%d res = %ld\n", stack_sz, i, res);
  157. //printf("Result = %ld\n", res);
  158. if(res != 0)
  159. {
  160. dprintf(2, "Error : expected 0 but %ld received\n",
  161. res);
  162. return 2;
  163. }
  164. }
  165. rpn_expr_close(&expr);
  166. }
  167. return 0;
  168. }
  169. int test_tokenization()
  170. {
  171. rpn_expr_t expr;
  172. int stack_sz, argc, i;
  173. char *expr_orig, *expr_untok;
  174. stack_sz = 32;
  175. argc=4;
  176. for(i=0;i<20;i++)
  177. {
  178. if(rpn_expr_init(&expr, stack_sz, argc))
  179. {
  180. dprintf(2, "Error initializing expr");
  181. return 1;
  182. }
  183. expr_orig = rpn_random(20+i, argc);
  184. if(rpn_expr_compile(&expr, expr_orig) < 0)
  185. {
  186. dprintf(2, "Compilation error : %s\n",
  187. expr.err_reason);
  188. return 1;
  189. }
  190. expr_untok = rpn_tokenized_expr(&(expr.toks), 0);
  191. if(strcmp(expr_untok, expr_orig))
  192. {
  193. dprintf(2,
  194. "Untokenized str differ from original : \"%s\" != \"%s\"\n",
  195. expr_orig, expr_untok);
  196. return 1;
  197. }
  198. rpn_expr_close(&expr);
  199. free(expr_untok);
  200. }
  201. return 0;
  202. }
  203. #define RUNTEST(NAME) \
  204. printf("Running %s\n", #NAME); \
  205. printf("%s()\t", #NAME); \
  206. fsync(1); \
  207. if(NAME()) \
  208. { \
  209. printf("[failed]\n"); \
  210. res++; \
  211. } \
  212. else \
  213. { \
  214. printf("[OK]\n"); \
  215. }
  216. int main()
  217. {
  218. int res;
  219. res = 0;
  220. //RUNTEST(test0);
  221. RUNTEST(test_add);
  222. RUNTEST(test_args);
  223. RUNTEST(test_stack_sz);
  224. RUNTEST(test_tokenization);
  225. return res;
  226. }