test.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "pocketpy_c.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. //tests the c bindings for pocketpy
  5. void check_impl(pkpy_vm* vm, bool result, int lineno) {
  6. if (!result) {
  7. printf("ERROR: failed where it should have succeed at line %i\n", lineno);
  8. char* message;
  9. if (!pkpy_clear_error(vm, &message)) {
  10. printf("clear error reported everything was fine\n");
  11. exit(1);
  12. }
  13. printf("%s\n", message);
  14. free(message);
  15. exit(1);
  16. }
  17. }
  18. void fail_impl(pkpy_vm* vm, bool result, int lineno) {
  19. if (result) {
  20. printf("ERROR: succeeded where it should have failed line %i\n", lineno);
  21. exit(1);
  22. } else {
  23. char* message;
  24. if (pkpy_clear_error(vm, &message)) {
  25. printf("actually errored!\n");
  26. free(message);
  27. exit(1);
  28. }
  29. }
  30. }
  31. void error_impl(pkpy_vm* vm, bool result, int lineno) {
  32. if (result) {
  33. printf("ERROR: succeeded where it should have failed line %i\n", lineno);
  34. exit(1);
  35. } else {
  36. char* message;
  37. if (!pkpy_clear_error(vm, &message))
  38. printf("clear error reported everything was fine\n");
  39. else {
  40. printf("successfully errored with this message: \n");
  41. printf("%s\n", message);
  42. free(message);
  43. }
  44. }
  45. }
  46. #define check(r) check_impl(vm, (r), __LINE__)
  47. #define fail(r) fail_impl(vm, (r), __LINE__)
  48. #define error(r) error_impl(vm, (r), __LINE__)
  49. int test_binding(pkpy_vm* vm) {
  50. pkpy_push_int(vm, 12);
  51. return 1;
  52. }
  53. int test_multiple_return(pkpy_vm* vm) {
  54. pkpy_push_int(vm, 12);
  55. pkpy_push_int(vm, 13);
  56. return 2;
  57. }
  58. int test_return_none(pkpy_vm* vm) {
  59. return 0;
  60. }
  61. int test_error_propagate(pkpy_vm* vm) {
  62. pkpy_get_global(vm, "does not exist");
  63. return 1;
  64. }
  65. int test_nested_error(pkpy_vm* vm) {
  66. pkpy_get_global(vm, "error_from_python");
  67. pkpy_call(vm, 0);
  68. return 0;
  69. }
  70. pkpy_vm* vm;
  71. void cleanup(void) {
  72. pkpy_vm_destroy(vm);
  73. }
  74. int main(int argc, char** argv) {
  75. vm = pkpy_vm_create(true, true);
  76. atexit(cleanup);
  77. //test run
  78. check(pkpy_vm_run(vm, "print('hello world!')"));
  79. error(pkpy_get_global(vm, "nonexistatn"));
  80. printf("\ntesting int methods\n");
  81. int r_int;
  82. check(pkpy_push_int(vm, 11));
  83. check(pkpy_set_global(vm, "eleven"));
  84. check(pkpy_vm_run(vm, "print(eleven)"));
  85. check(pkpy_get_global(vm, "eleven"));
  86. check(pkpy_is_int(vm, -1));
  87. check(pkpy_to_int(vm, -1, &r_int));
  88. printf("%i\n", r_int);
  89. fail(pkpy_is_float(vm, -1));
  90. fail(pkpy_is_bool(vm, -1));
  91. fail(pkpy_is_string(vm, -1));
  92. fail(pkpy_is_none(vm, -1));
  93. fail(pkpy_is_voidp(vm, -1));
  94. printf("\ntesting float methods\n");
  95. double r_float;
  96. check(pkpy_push_float(vm, 11.11));
  97. check(pkpy_set_global(vm, "elevenf"));
  98. check(pkpy_vm_run(vm, "print(elevenf)"));
  99. check(pkpy_get_global(vm, "elevenf"));
  100. check(pkpy_is_float(vm, -1));
  101. check(pkpy_to_float(vm, -1, &r_float));
  102. printf("%f\n", r_float);
  103. fail(pkpy_is_int(vm, -1));
  104. fail(pkpy_is_bool(vm, -1));
  105. fail(pkpy_is_string(vm, -1));
  106. fail(pkpy_is_none(vm, -1));
  107. fail(pkpy_is_voidp(vm, -1));
  108. printf("\ntesting bool methods\n");
  109. bool r_bool;
  110. check(pkpy_push_bool(vm, false));
  111. check(pkpy_set_global(vm, "false_test"));
  112. check(pkpy_vm_run(vm, "print(false_test)"));
  113. check(pkpy_get_global(vm, "false_test"));
  114. check(pkpy_is_bool(vm, -1));
  115. check(pkpy_to_bool(vm, -1, &r_bool));
  116. printf("%i\n", r_bool);
  117. fail(pkpy_is_int(vm, -1));
  118. fail(pkpy_is_float(vm, -1));
  119. fail(pkpy_is_string(vm, -1));
  120. fail(pkpy_is_none(vm, -1));
  121. fail(pkpy_is_voidp(vm, -1));
  122. printf("\ntesting string methods\n");
  123. char* r_string;
  124. check(pkpy_push_string(vm, "hello!"));
  125. check(pkpy_set_global(vm, "hello1"));
  126. check(pkpy_vm_run(vm, "print(hello1)"));
  127. check(pkpy_push_stringn(vm, "hello!", 5));
  128. check(pkpy_is_string(vm, -1));
  129. check(pkpy_to_string(vm, -1, &r_string));
  130. printf("%s\n", r_string);
  131. free(r_string);
  132. const char* r_stringn;
  133. int r_size;
  134. check(pkpy_to_stringn(vm, -1, &r_stringn, &r_size));
  135. printf("%.*s\n", r_size, r_stringn);
  136. fail(pkpy_is_int(vm, -1));
  137. fail(pkpy_is_float(vm, -1));
  138. fail(pkpy_is_bool(vm, -1));
  139. fail(pkpy_is_none(vm, -1));
  140. fail(pkpy_is_voidp(vm, -1));
  141. printf("\ntesting None methods\n");
  142. check(pkpy_push_none(vm));
  143. check(pkpy_set_global(vm, "none"));
  144. check(pkpy_vm_run(vm, "print(none)"));
  145. check(pkpy_get_global(vm, "none"));
  146. check(pkpy_is_none(vm, -1));
  147. fail(pkpy_is_int(vm, -1));
  148. fail(pkpy_is_float(vm, -1));
  149. fail(pkpy_is_bool(vm, -1));
  150. fail(pkpy_is_string(vm, -1));
  151. fail(pkpy_is_voidp(vm, -1));
  152. printf("\ntesting voidp methods\n");
  153. void* vp = (void*) 123;
  154. check(pkpy_push_voidp(vm, vp));
  155. check(pkpy_set_global(vm, "vp"));
  156. check(pkpy_vm_run(vm, "print(vp)"));
  157. check(pkpy_get_global(vm, "vp"));
  158. check(pkpy_is_voidp(vm, -1));
  159. vp = NULL;
  160. check(pkpy_to_voidp(vm, -1, &vp));
  161. printf("%i\n", (int) (intptr_t) vp);
  162. fail(pkpy_is_int(vm, -1));
  163. fail(pkpy_is_float(vm, -1));
  164. fail(pkpy_is_bool(vm, -1));
  165. fail(pkpy_is_string(vm, -1));
  166. fail(pkpy_is_none(vm, -1));
  167. printf("\ntesting sizing and indexing\n");
  168. int stack_size = pkpy_stack_size(vm);
  169. printf("stack size %i\n", stack_size);
  170. check(pkpy_check_stack(vm, 10));
  171. check(pkpy_check_stack(vm, 26));
  172. fail(pkpy_check_stack(vm, 27));
  173. check(pkpy_is_int(vm, 0));
  174. check(pkpy_is_float(vm, 1));
  175. check(pkpy_is_bool(vm, 2));
  176. check(pkpy_is_string(vm, 3));
  177. check(pkpy_is_none(vm, 4));
  178. check(pkpy_is_voidp(vm, 5));
  179. check(pkpy_is_int(vm, -6));
  180. check(pkpy_is_float(vm, -5));
  181. check(pkpy_is_bool(vm, -4));
  182. check(pkpy_is_string(vm, -3));
  183. check(pkpy_is_none(vm, -2));
  184. check(pkpy_is_voidp(vm, -1));
  185. check(pkpy_push(vm, -3));
  186. check(pkpy_is_string(vm, -1));
  187. printf("\ntesting error catching\n");
  188. error(pkpy_vm_run(vm, "let's make sure syntax errors get caught"));
  189. check(pkpy_stack_size(vm) == 0); //stack should be cleared after error is resolved
  190. printf("\ntesting calls\n");
  191. check(pkpy_vm_run(vm, "def x(x, y) : return x - y"));
  192. check(pkpy_vm_run(vm, "def vararg_x(*x) : return sum(x)"));
  193. check(pkpy_vm_run(vm, "def keyword_x(x=1, y=1) : return x+y"));
  194. check(pkpy_vm_run(vm, "def retmany_x() : return 1, 2, 3"));
  195. check(pkpy_get_global(vm, "x"));
  196. check(pkpy_push_int(vm, 2));
  197. check(pkpy_push_int(vm, 3));
  198. check(pkpy_call(vm, 2));
  199. check(pkpy_to_int(vm, -1, &r_int));
  200. printf("x : %i\n", r_int);
  201. check(pkpy_get_global(vm, "vararg_x"));
  202. check(pkpy_push_int(vm, 1));
  203. check(pkpy_push_int(vm, 2));
  204. check(pkpy_push_int(vm, 3));
  205. check(pkpy_push_int(vm, 4));
  206. check(pkpy_push_int(vm, 5));
  207. check(pkpy_push_int(vm, 6));
  208. check(pkpy_call(vm, 6));
  209. check(pkpy_to_int(vm, -1, &r_int));
  210. printf("vararg_x : %i\n", r_int);
  211. check(pkpy_get_global(vm, "keyword_x"));
  212. check(pkpy_push_int(vm, 3));
  213. check(pkpy_call(vm, 1));
  214. check(pkpy_to_int(vm, -1, &r_int));
  215. printf("keyword_x : %i\n", r_int);
  216. check(pkpy_get_global(vm, "keyword_x"));
  217. check(pkpy_call(vm, 0));
  218. check(pkpy_to_int(vm, -1, &r_int));
  219. printf("keyword_x : %i\n", r_int);
  220. check(pkpy_stack_size(vm) == 4);
  221. check(pkpy_get_global(vm, "retmany_x"));
  222. check(pkpy_call(vm, 0));
  223. check(pkpy_stack_size(vm) == 7);
  224. check(pkpy_to_int(vm, -3, &r_int));
  225. printf("retmany_x : %i\n", r_int);
  226. check(pkpy_to_int(vm, -2, &r_int));
  227. printf("retmany_x : %i\n", r_int);
  228. check(pkpy_to_int(vm, -1, &r_int));
  229. printf("retmany_x : %i\n", r_int);
  230. check(pkpy_get_global(vm, "x"));
  231. error(pkpy_call(vm, 0));
  232. check(pkpy_vm_run(vm, "l = []"));
  233. check(pkpy_get_global(vm, "l"));
  234. check(pkpy_push_string(vm, "hello"));
  235. check(pkpy_call_method(vm, "append", 1));
  236. check(pkpy_vm_run(vm, "print(l)"));
  237. printf("\ntesting pushing functions\n");
  238. check(pkpy_push_function(vm, test_binding));
  239. check(pkpy_set_global(vm, "test_binding"));
  240. check(pkpy_vm_run(vm, "print(test_binding())"));
  241. check(pkpy_push_function(vm, test_multiple_return));
  242. check(pkpy_set_global(vm, "test_multiple_return"));
  243. //uncomment if _exec changes
  244. //check(pkpy_vm_run(vm, "test_multiple_return()"));
  245. //check(pkpy_stack_size(vm) == 2);
  246. check(pkpy_push_function(vm, test_error_propagate));
  247. check(pkpy_set_global(vm, "test_error_propagate"));
  248. error(pkpy_vm_run(vm, "test_error_propagate()"));
  249. check(pkpy_get_global(vm, "test_multiple_return"));
  250. check(pkpy_call(vm, 0));
  251. check(pkpy_stack_size(vm) == 2);
  252. check(pkpy_pop(vm, 2));
  253. check(pkpy_stack_size(vm) == 0);
  254. check(pkpy_check_global(vm, "test_error_propagate"));
  255. fail(pkpy_check_global(vm, "nonexistant"));
  256. error(pkpy_vm_run(vm, "raise NameError('testing error throwing from python')"));
  257. pkpy_vm_run(vm, "test_error_propagate()");
  258. check(pkpy_check_error(vm));
  259. fprintf(stderr, "testing code going to standard error, can ignore next error\n");
  260. pkpy_clear_error(vm, NULL);
  261. //with the current way execptions are handled, this will fail and pass the
  262. //error clean through, ignoring the python handling
  263. //
  264. //maybe worth fixing someday, but for now it is functionating as implemented
  265. error(pkpy_vm_run(vm, "try : test_error_propagate(); except NameError : pass"));
  266. error(pkpy_error(vm, "test direct error mechanism"));
  267. //more complicated error handling
  268. //
  269. //at the moment this is disabled, as this use case is not supported
  270. //it will cause the program to abort
  271. //
  272. //a python exception thrown from python can not pass through a c binding
  273. //
  274. //this means for now the api can only be used to make shallow bindings, or
  275. //else care must be taken to not let an exception go through a c binding by
  276. //catching it in python first
  277. //
  278. //at such a time this interferes with a real world use case of the bindings
  279. //we can revisit it
  280. //
  281. //check(pkpy_vm_run(vm, "def error_from_python() : raise NotImplementedError()"));
  282. //check(pkpy_push_function(vm, test_nested_error));
  283. //check(pkpy_set_global(vm, "test_nested_error"));
  284. //fail(pkpy_vm_run(vm, "test_nested_error()"));
  285. return 0;
  286. }