Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

663 строки
22 KiB

  1. #define epsilon 2.2204460492503131E-16
  2. #define testresult int
  3. #define pass 1
  4. #define fail 0
  5. #define print_assert_equal_fail(variable, value, type, format) \
  6. printf("\n%s:%d: Assertion failed\n\tfor '" #variable "'" \
  7. "\n\texpected: " format \
  8. "\n\tgot: " format "\n", \
  9. __FILE__, __LINE__, (type)value, (type)variable)
  10. #define print_assert_not_equal_fail(variable, value, type, format) \
  11. printf("\n%s:%d: Assertion failed\n\tfor '" #variable "'" \
  12. "\n\texpected not: " format \
  13. "\n\tgot anyways: " format "\n", \
  14. __FILE__, __LINE__, (type)value, (type)variable)
  15. #define assert_equal_int(variable, value) \
  16. if (variable != value) { \
  17. print_assert_equal_fail(variable, value, size_t, "%zd"); \
  18. return fail; \
  19. }
  20. #define assert_not_equal_int(variable, value) \
  21. if (variable == value) { \
  22. print_assert_not_equal_fail(variable, value, size_t, "%zd"); \
  23. return fail; \
  24. }
  25. #define assert_no_error() \
  26. if (Globals::error) { \
  27. print_assert_equal_fail(Globals::error, 0, size_t, "%zd"); \
  28. printf("\nExpected no error to occur," \
  29. " but an error occured anyways:\n"); \
  30. log_error(); \
  31. return fail; \
  32. } \
  33. #define assert_error() \
  34. if (!Globals::error) { \
  35. print_assert_not_equal_fail(Globals::error, 0, size_t, "%zd"); \
  36. printf("\nExpected an error to occur," \
  37. " but no error occured:\n"); \
  38. return fail; \
  39. } \
  40. #define assert_equal_double(variable, value) \
  41. if (fabs((double)variable - (double)value) > epsilon) { \
  42. print_assert_equal_fail(variable, value, double, "%f"); \
  43. return fail; \
  44. }
  45. #define assert_not_equal_double(variable, value) \
  46. if (fabs((double)variable - (double)value) <= epsilon) { \
  47. print_assert_not_equal_fail(variable, value, double, "%f"); \
  48. return fail; \
  49. }
  50. #define assert_equal_string(variable, value) \
  51. if (!string_equal(variable, value)) { \
  52. print_assert_equal_fail(&variable->data, value, char*, "%s"); \
  53. return fail; \
  54. }
  55. #define assert_equal_type(node, _type) \
  56. if (Memory::get_type(node) != _type) { \
  57. print_assert_equal_fail( \
  58. Lisp_Object_Type_to_string(Memory::get_type(node)), \
  59. Lisp_Object_Type_to_string(_type), char*, "%s"); \
  60. return fail; \
  61. } \
  62. #define assert_null(variable) \
  63. assert_equal_int(variable, nullptr)
  64. #define assert_not_null(variable) \
  65. assert_not_equal_int(variable, nullptr)
  66. #define invoke_test(name) \
  67. fputs("" #name ":", stdout); \
  68. if (name() == pass) { \
  69. for(size_t i = strlen(#name); i < 70; ++i) \
  70. fputs((i%3==1)? "." : " ", stdout); \
  71. fputs(console_green "passed\n" console_normal, stdout); \
  72. } \
  73. else { \
  74. result = false; \
  75. for(int i = -1; i < 70; ++i) \
  76. fputs((i%3==1)? "." : " ", stdout); \
  77. fputs(console_red "failed\n" console_normal, stdout); \
  78. if(Globals::error) { \
  79. free(Globals::error); \
  80. Globals::error = nullptr; \
  81. } \
  82. } \
  83. #define invoke_test_script(name) \
  84. fputs("" name ":", stdout); \
  85. if (test_file("tests/" name ".slime") == pass) { \
  86. for(size_t i = strlen(name); i < 70; ++i) \
  87. fputs((i%3==1)? "." : " ", stdout); \
  88. fputs(console_green "passed\n" console_normal, stdout); \
  89. } \
  90. else { \
  91. result = false; \
  92. for(int i = -1; i < 70; ++i) \
  93. fputs((i%3==1)? "." : " ", stdout); \
  94. fputs(console_red "failed\n" console_normal, stdout); \
  95. if(Globals::error) { \
  96. free(Globals::error); \
  97. Globals::error = nullptr; \
  98. } \
  99. }
  100. proc test_array_lists_adding_and_removing() -> testresult {
  101. // test adding and removing
  102. Int_Array_List list = create_Int_array_list();
  103. append_to_array_list(&list, 1);
  104. append_to_array_list(&list, 2);
  105. append_to_array_list(&list, 3);
  106. append_to_array_list(&list, 4);
  107. assert_equal_int(list.next_index, 4);
  108. remove_index_from_array_list(&list, 0);
  109. assert_equal_int(list.next_index, 3);
  110. assert_equal_int(list.data[0], 4);
  111. assert_equal_int(list.data[1], 2);
  112. assert_equal_int(list.data[2], 3);
  113. remove_index_from_array_list(&list, 2);
  114. assert_equal_int(list.next_index, 2);
  115. assert_equal_int(list.data[0], 4);
  116. assert_equal_int(list.data[1], 2);
  117. return pass;
  118. }
  119. proc test_array_lists_sorting() -> testresult {
  120. // test adding and removing
  121. Int_Array_List list = create_Int_array_list();
  122. append_to_array_list(&list, 1);
  123. append_to_array_list(&list, 2);
  124. append_to_array_list(&list, 3);
  125. append_to_array_list(&list, 4);
  126. sort_array_list(&list);
  127. assert_equal_int(list.next_index, 4);
  128. assert_equal_int(list.data[0], 1);
  129. assert_equal_int(list.data[1], 2);
  130. assert_equal_int(list.data[2], 3);
  131. assert_equal_int(list.data[3], 4);
  132. append_to_array_list(&list, 0);
  133. append_to_array_list(&list, 5);
  134. assert_equal_int(list.next_index, 6);
  135. sort_array_list(&list);
  136. assert_equal_int(list.data[0], 0);
  137. assert_equal_int(list.data[1], 1);
  138. assert_equal_int(list.data[2], 2);
  139. assert_equal_int(list.data[3], 3);
  140. assert_equal_int(list.data[4], 4);
  141. assert_equal_int(list.data[5], 5);
  142. return pass;
  143. }
  144. proc test_array_lists_searching() -> testresult {
  145. Int_Array_List list = create_Int_array_list();
  146. append_to_array_list(&list, 1);
  147. append_to_array_list(&list, 2);
  148. append_to_array_list(&list, 3);
  149. append_to_array_list(&list, 4);
  150. int index = sorted_array_list_find(&list, 3);
  151. assert_equal_int(index, 2);
  152. index = sorted_array_list_find(&list, 1);
  153. assert_equal_int(index, 0);
  154. index = sorted_array_list_find(&list, 5);
  155. assert_equal_int(index, -1);
  156. return pass;
  157. }
  158. proc test_eval_operands() -> testresult {
  159. char operands_string[] = "((eval 1) (+ 1 2) \"okay\" (eval :haha))";
  160. Lisp_Object* operands = Parser::parse_single_expression(operands_string);
  161. int operands_length;
  162. try operands = eval_arguments(operands, &operands_length);
  163. assert_no_error();
  164. assert_equal_int(list_length(operands), 4);
  165. assert_equal_type(operands, Lisp_Object_Type::Pair);
  166. assert_equal_type(operands->value.pair.first, Lisp_Object_Type::Number);
  167. assert_equal_double(operands->value.pair.first->value.number, 1);
  168. operands = operands->value.pair.rest;
  169. assert_equal_type(operands, Lisp_Object_Type::Pair);
  170. assert_equal_type(operands->value.pair.first, Lisp_Object_Type::Number);
  171. assert_equal_double(operands->value.pair.first->value.number, 3);
  172. operands = operands->value.pair.rest;
  173. assert_equal_type(operands, Lisp_Object_Type::Pair);
  174. assert_equal_type(operands->value.pair.first, Lisp_Object_Type::String);
  175. assert_equal_string(operands->value.pair.first->value.string, "okay");
  176. operands = operands->value.pair.rest;
  177. assert_equal_type(operands, Lisp_Object_Type::Pair);
  178. assert_equal_type(operands->value.pair.first, Lisp_Object_Type::Keyword);
  179. assert_equal_string(operands->value.pair.first->value.symbol.identifier, "haha");
  180. return pass;
  181. }
  182. proc test_parse_atom() -> testresult {
  183. int index_in_text = 0;
  184. char string[] =
  185. "123 -1.23e-2 " // numbers
  186. "\"asd\" " // strings
  187. ":key1 :key:2 " // keywords
  188. "sym +"; // symbols
  189. // test numbers
  190. Lisp_Object* result = Parser::parse_atom(string, &index_in_text);
  191. assert_equal_type(result, Lisp_Object_Type::Number);
  192. assert_equal_double(result->value.number, 123);
  193. ++index_in_text;
  194. result = Parser::parse_atom(string, &index_in_text);
  195. assert_equal_type(result, Lisp_Object_Type::Number);
  196. assert_equal_double(result->value.number, -1.23e-2);
  197. // test strings
  198. ++index_in_text;
  199. result = Parser::parse_atom(string, &index_in_text);
  200. assert_equal_type(result, Lisp_Object_Type::String);
  201. assert_equal_string(result->value.string, "asd");
  202. // test keywords
  203. ++index_in_text;
  204. result = Parser::parse_atom(string, &index_in_text);
  205. assert_equal_type(result, Lisp_Object_Type::Keyword);
  206. assert_equal_string(result->value.symbol.identifier, "key1");
  207. ++index_in_text;
  208. result = Parser::parse_atom(string, &index_in_text);
  209. assert_equal_type(result, Lisp_Object_Type::Keyword);
  210. assert_equal_string(result->value.symbol.identifier, "key:2");
  211. // test symbols
  212. ++index_in_text;
  213. result = Parser::parse_atom(string, &index_in_text);
  214. assert_equal_type(result, Lisp_Object_Type::Symbol);
  215. assert_equal_string(result->value.symbol.identifier, "sym");
  216. ++index_in_text;
  217. result = Parser::parse_atom(string, &index_in_text);
  218. assert_equal_type(result, Lisp_Object_Type::Symbol);
  219. assert_equal_string(result->value.symbol.identifier, "+");
  220. return pass;
  221. }
  222. proc test_parse_expression() -> testresult {
  223. int index_in_text = 0;
  224. char string[] = "(fun + 12)";
  225. Lisp_Object* result = Parser::parse_expression(string, &index_in_text);
  226. assert_no_error();
  227. assert_equal_type(result, Lisp_Object_Type::Pair);
  228. assert_equal_type(result->value.pair.first, Lisp_Object_Type::Symbol);
  229. assert_equal_string(result->value.pair.first->value.symbol.identifier, "fun");
  230. result = result->value.pair.rest;
  231. assert_equal_type(result, Lisp_Object_Type::Pair);
  232. assert_equal_type(result->value.pair.first, Lisp_Object_Type::Symbol);
  233. assert_equal_string(result->value.pair.first->value.symbol.identifier, "+");
  234. result = result->value.pair.rest;
  235. assert_equal_type(result, Lisp_Object_Type::Pair);
  236. assert_equal_type(result->value.pair.first, Lisp_Object_Type::Number);
  237. assert_equal_double(result->value.pair.first->value.number, 12);
  238. result = result->value.pair.rest;
  239. assert_equal_type(result, Lisp_Object_Type::Nil);
  240. char string2[] = "(define fun (lambda (x) (+ 5 (* x x ))))";
  241. index_in_text = 0;
  242. result = Parser::parse_expression(string2, &index_in_text);
  243. assert_no_error();
  244. assert_equal_type(result, Lisp_Object_Type::Pair);
  245. assert_equal_type(result->value.pair.first, Lisp_Object_Type::Symbol);
  246. assert_equal_string(result->value.pair.first->value.symbol.identifier, "define");
  247. result = result->value.pair.rest;
  248. assert_equal_type(result, Lisp_Object_Type::Pair);
  249. assert_equal_type(result->value.pair.first, Lisp_Object_Type::Symbol);
  250. assert_equal_string(result->value.pair.first->value.symbol.identifier, "fun");
  251. result = result->value.pair.rest;
  252. assert_equal_type(result, Lisp_Object_Type::Pair);
  253. assert_equal_type(result->value.pair.first, Lisp_Object_Type::Pair);
  254. assert_equal_type(result->value.pair.first->value.pair.first, Lisp_Object_Type::Symbol);
  255. assert_equal_string(result->value.pair.first->value.pair.first->value.symbol.identifier, "lambda");
  256. result = result->value.pair.rest;
  257. return pass;
  258. }
  259. proc test_built_in_add() -> testresult {
  260. char exp_string[] = "(+ 10 4)";
  261. Lisp_Object* expression = Parser::parse_single_expression(exp_string);
  262. Lisp_Object* result;
  263. try result = eval_expr(expression);
  264. assert_no_error();
  265. assert_not_null(result);
  266. assert_equal_type(result, Lisp_Object_Type::Number);
  267. assert_equal_double(result->value.number, 14);
  268. return pass;
  269. }
  270. proc test_built_in_substract() -> testresult {
  271. char exp_string[] = "(- 10 4)";
  272. Lisp_Object* expression = Parser::parse_single_expression(exp_string);
  273. Lisp_Object* result;
  274. try result = eval_expr(expression);
  275. assert_no_error();
  276. assert_not_null(result);
  277. assert_equal_type(result, Lisp_Object_Type::Number);
  278. assert_equal_double(result->value.number, 6);
  279. return pass;
  280. }
  281. proc test_built_in_multiply() -> testresult {
  282. char exp_string[] = "(* 10 4)";
  283. Lisp_Object* expression = Parser::parse_single_expression(exp_string);
  284. Lisp_Object* result;
  285. try result = eval_expr(expression);
  286. assert_no_error();
  287. assert_not_null(result);
  288. assert_equal_type(result, Lisp_Object_Type::Number);
  289. assert_equal_double(result->value.number, 40);
  290. return pass;
  291. }
  292. proc test_built_in_divide() -> testresult {
  293. char exp_string[] = "(/ 20 4)";
  294. Lisp_Object* expression = Parser::parse_single_expression(exp_string);
  295. Lisp_Object* result;
  296. try result = eval_expr(expression);
  297. assert_no_error();
  298. assert_not_null(result);
  299. assert_equal_type(result, Lisp_Object_Type::Number);
  300. assert_equal_double(result->value.number, 5);
  301. return pass;
  302. }
  303. proc test_built_in_if() -> testresult {
  304. char exp_string1[] = "(if 1 4 5)";
  305. Lisp_Object* expression = Parser::parse_single_expression(exp_string1);
  306. Lisp_Object* result;
  307. try result = eval_expr(expression);
  308. assert_no_error();
  309. assert_not_null(result);
  310. assert_equal_type(result, Lisp_Object_Type::Number);
  311. assert_equal_double(result->value.number, 4);
  312. char exp_string2[] = "(if () 4 5)";
  313. expression = Parser::parse_single_expression(exp_string2);
  314. try result = eval_expr(expression);
  315. assert_no_error();
  316. assert_not_null(result);
  317. assert_equal_type(result, Lisp_Object_Type::Number);
  318. assert_equal_double(result->value.number, 5);
  319. return pass;
  320. }
  321. proc test_built_in_and() -> testresult {
  322. char exp_string1[] = "(and 1 \"asd\" 4)";
  323. Lisp_Object* expression = Parser::parse_single_expression(exp_string1);
  324. Lisp_Object* result;
  325. try result = eval_expr(expression);
  326. assert_no_error();
  327. assert_not_null(result);
  328. assert_equal_type(result, Lisp_Object_Type::T);
  329. // a false case
  330. char exp_string2[] = "(and () \"asd\" 4)";
  331. expression = Parser::parse_single_expression(exp_string2);
  332. try result = eval_expr(expression);
  333. assert_no_error();
  334. assert_not_null(result);
  335. assert_equal_type(result, Lisp_Object_Type::Nil);
  336. return pass;
  337. }
  338. proc test_built_in_or() -> testresult {
  339. char exp_string1[] = "(or \"asd\" nil)";
  340. Lisp_Object* expression = Parser::parse_single_expression(exp_string1);
  341. Lisp_Object* result;
  342. try result = eval_expr(expression);
  343. assert_no_error();
  344. assert_not_null(result);
  345. assert_equal_type(result, Lisp_Object_Type::T);
  346. // a false case
  347. char exp_string2[] = "(or () ())";
  348. expression = Parser::parse_single_expression(exp_string2);
  349. try result = eval_expr(expression);
  350. assert_no_error();
  351. assert_not_null(result);
  352. assert_equal_type(result, Lisp_Object_Type::Nil);
  353. return pass;
  354. }
  355. proc test_built_in_not() -> testresult {
  356. char exp_string1[] = "(not ())";
  357. Lisp_Object* expression = Parser::parse_single_expression(exp_string1);
  358. Lisp_Object* result;
  359. try result = eval_expr(expression);
  360. // a true case
  361. assert_no_error();
  362. assert_not_null(result);
  363. assert_equal_type(result, Lisp_Object_Type::T);
  364. // a false case
  365. char exp_string2[] = "(not \"asd xD\")";
  366. expression = Parser::parse_single_expression(exp_string2);
  367. try result = eval_expr(expression);
  368. assert_no_error();
  369. assert_not_null(result);
  370. assert_equal_type(result, Lisp_Object_Type::Nil);
  371. return pass;
  372. }
  373. proc test_built_in_type() -> testresult {
  374. // Environment* env;
  375. // try env = get_root_environment();
  376. // normal type testing
  377. char exp_string1[] = "(begin (define a 10)(type a))";
  378. Lisp_Object* expression = Parser::parse_single_expression(exp_string1);
  379. Lisp_Object* result = eval_expr(expression);
  380. assert_no_error();
  381. assert_not_null(result);
  382. assert_equal_type(result, Lisp_Object_Type::Keyword);
  383. assert_equal_string(result->value.symbol.identifier, "number");
  384. // setting user type
  385. char exp_string2[] = "(begin (set-type a :my-type)(type a))";
  386. expression = Parser::parse_single_expression(exp_string2);
  387. result = eval_expr(expression);
  388. assert_no_error();
  389. assert_not_null(result);
  390. assert_equal_type(result, Lisp_Object_Type::Keyword);
  391. assert_equal_string(result->value.symbol.identifier, "my-type");
  392. // trying to set invalid user type
  393. char exp_string3[] = "(begin (set-type a \"wrong tpye\")(type a))";
  394. expression = Parser::parse_single_expression(exp_string3);
  395. assert_no_error();
  396. ignore_logging {
  397. dont_break_on_errors {
  398. result = eval_expr(expression);
  399. }
  400. }
  401. assert_error();
  402. delete_error();
  403. // deleting user type
  404. char exp_string4[] = "(begin (delete-type a)(type a))";
  405. expression = Parser::parse_single_expression(exp_string4);
  406. result = eval_expr(expression);
  407. assert_no_error();
  408. assert_not_null(result);
  409. assert_equal_type(result, Lisp_Object_Type::Keyword);
  410. assert_equal_string(result->value.symbol.identifier, "number");
  411. return pass;
  412. }
  413. proc test_singular_t_and_nil() -> testresult {
  414. Environment* env;
  415. try env = get_root_environment();
  416. // nil testing
  417. char exp_string1[] = "()";
  418. char exp_string2[] = "nil";
  419. Lisp_Object* expression = Parser::parse_single_expression(exp_string1);
  420. Lisp_Object* result = eval_expr(expression);
  421. assert_no_error();
  422. assert_not_null(result);
  423. assert_equal_type(result, Lisp_Object_Type::Nil);
  424. assert_equal_int(expression, result);
  425. Lisp_Object* expression2 = Parser::parse_single_expression(exp_string2);
  426. Lisp_Object* result2 = eval_expr(expression2);
  427. assert_no_error();
  428. assert_not_null(result);
  429. assert_equal_type(result, Lisp_Object_Type::Nil);
  430. assert_equal_int(result, result2);
  431. assert_equal_int(expression, Memory::nil);
  432. // t testing
  433. char exp_string3[] = "t";
  434. Lisp_Object* expression3 = Parser::parse_single_expression(exp_string3);
  435. Lisp_Object* result3 = eval_expr(expression3);
  436. assert_no_error();
  437. assert_not_null(result3);
  438. return pass;
  439. }
  440. proc test_file(const char* file) -> testresult {
  441. Memory::reset();
  442. assert_no_error();
  443. Environment* root_env = get_root_environment();
  444. Environment* user_env = Memory::create_child_environment(root_env);
  445. assert_no_error();
  446. push_environment(user_env);
  447. defer {
  448. pop_environment();
  449. };
  450. built_in_load(Memory::create_string(file));
  451. assert_no_error();
  452. return pass;
  453. }
  454. proc run_all_tests() -> bool {
  455. bool result = true;
  456. printf("-- Util --\n");
  457. invoke_test(test_array_lists_adding_and_removing);
  458. invoke_test(test_array_lists_sorting);
  459. invoke_test(test_array_lists_searching);
  460. Memory::init(4096 * 2000, 1024 * 32, 4096 * 16 * 10);
  461. printf("\n -- Parsing --\n");
  462. invoke_test(test_parse_atom);
  463. invoke_test(test_parse_expression);
  464. printf("\n-- Basic evaluating --\n");
  465. invoke_test(test_eval_operands);
  466. printf("\n-- Built ins --\n");
  467. invoke_test(test_built_in_add);
  468. invoke_test(test_built_in_substract);
  469. invoke_test(test_built_in_multiply);
  470. invoke_test(test_built_in_divide);
  471. invoke_test(test_built_in_if);
  472. invoke_test(test_built_in_and);
  473. invoke_test(test_built_in_or);
  474. invoke_test(test_built_in_not);
  475. invoke_test(test_built_in_type);
  476. printf("\n-- Memory management --\n");
  477. invoke_test(test_singular_t_and_nil);
  478. printf("\n-- Test Files --\n");
  479. invoke_test_script("alists");
  480. invoke_test_script("case_and_cond");
  481. invoke_test_script("evaluation_of_default_args");
  482. invoke_test_script("lexical_scope");
  483. invoke_test_script("class_macro");
  484. invoke_test_script("import_and_load");
  485. invoke_test_script("sicp");
  486. invoke_test_script("macro_expand");
  487. invoke_test_script("automata");
  488. return result;
  489. }
  490. #undef epsilon
  491. #undef testresult
  492. #undef pass
  493. #undef fail
  494. #undef print_assert_equal_fail
  495. #undef print_assert_not_equal_fail
  496. #undef assert_no_error
  497. #undef assert_equal_int
  498. #undef assert_not_equal_int
  499. #undef assert_equal_double
  500. #undef assert_not_equal_double
  501. #undef assert_equal_string
  502. #undef assert_equal_type
  503. #undef assert_null
  504. #undef assert_not_null
  505. #undef invoke_test
  506. #undef invoke_test_script