c_test.g 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # This file is part of asmc, a bootstrapping OS with minimal seed
  2. # Copyright (C) 2018-2019 Giovanni Mascellani <gio@debian.org>
  3. # https://gitlab.com/giomasce/asmc
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. const TESTS_RAN 0
  15. const TESTS_SUCCESSFUL 4
  16. const SIZEOF_TESTS 8
  17. $test_expected_stdout
  18. $test_stdout_ok
  19. fun test_write 1 {
  20. $c
  21. @c 0 param = ;
  22. if test_expected_stdout **c c == {
  23. @test_expected_stdout test_expected_stdout 1 + = ;
  24. } else {
  25. @test_stdout_ok 0 = ;
  26. }
  27. }
  28. fun c_run_testcase 3 {
  29. $tests
  30. $filename
  31. $function
  32. $result
  33. $out
  34. @tests 4 param = ;
  35. @filename 3 param = ;
  36. @function 2 param = ;
  37. @result 1 param = ;
  38. @out 0 param = ;
  39. "Testing " log ;
  40. function log ;
  41. " in file " log ;
  42. filename log ;
  43. "..." log ;
  44. # Preprocessing
  45. $ctx
  46. @ctx ppctx_init = ;
  47. ctx PPCTX_VERBOSE take_addr 0 = ;
  48. ctx filename ppctx_set_base_filename ;
  49. $tokens
  50. @tokens 4 vector_init = ;
  51. tokens ctx filename preproc_file ;
  52. @tokens tokens remove_whites = ;
  53. @tokens tokens collapse_strings = ;
  54. #"Finished preprocessing\n" log ;
  55. #tokens print_token_list ;
  56. # Compilation
  57. $cctx
  58. @cctx tokens cctx_init = ;
  59. cctx CCTX_VERBOSE take_addr 0 = ;
  60. cctx CCTX_DEBUG take_addr 0 = ;
  61. cctx cctx_compile ;
  62. # Hack the handles in order to intercept calls to write
  63. cctx CCTX_HANDLES take 0 vector_at_addr @test_write = ;
  64. @test_expected_stdout out = ;
  65. @test_stdout_ok 1 = ;
  66. # Debug output
  67. #"TYPES TABLE\n" log ;
  68. #cctx cctx_dump_types ;
  69. #"TYPE NAMES TABLE\n" log ;
  70. #cctx cctx_dump_typenames ;
  71. #"GLOBALS TABLE\n" log ;
  72. #cctx cctx_dump_globals ;
  73. # Try to execute the code
  74. #"Executing compiled code...\n" log ;
  75. if cctx "__init_stdlib" cctx_has_global {
  76. $init_global
  77. @init_global cctx "__init_stdlib" cctx_get_global = ;
  78. $init_addr
  79. @init_addr init_global GLOBAL_LOC take = ;
  80. init_addr \0 ;
  81. }
  82. $function_global
  83. @function_global cctx function cctx_get_global = ;
  84. $function_addr
  85. @function_addr function_global GLOBAL_LOC take = ;
  86. $res
  87. @res function_addr \0 = ;
  88. # Cleanup
  89. tokens free_vect_of_ptrs ;
  90. cctx cctx_destroy ;
  91. ctx ppctx_destroy ;
  92. tests TESTS_RAN take_addr tests TESTS_RAN take 1 + = ;
  93. if res result == test_expected_stdout **c 0 == && test_stdout_ok && {
  94. " passed!\n" log ;
  95. tests TESTS_SUCCESSFUL take_addr tests TESTS_SUCCESSFUL take 1 + = ;
  96. } else {
  97. " FAILED!\n" log ;
  98. " -> Returned " log ;
  99. res itoa log ;
  100. " instead of " log ;
  101. result itoa log ;
  102. "\n" log ;
  103. }
  104. }
  105. fun c_run_testcases 0 {
  106. $tests
  107. @tests SIZEOF_TESTS malloc = ;
  108. tests TESTS_RAN take_addr 0 = ;
  109. tests TESTS_SUCCESSFUL take_addr 0 = ;
  110. tests "/disk1/tests/test_lang.c" "test_false" 0 "" c_run_testcase ;
  111. tests "/disk1/tests/test_lang.c" "test_true" 1 "" c_run_testcase ;
  112. tests "/disk1/tests/test_lang.c" "test_octal" 1 "" c_run_testcase ;
  113. tests "/disk1/tests/test_lang.c" "test_while" 5040 "" c_run_testcase ;
  114. tests "/disk1/tests/test_lang.c" "test_do_while" 5040 "" c_run_testcase ;
  115. tests "/disk1/tests/test_lang.c" "test_for" 5040 "" c_run_testcase ;
  116. tests "/disk1/tests/test_lang.c" "test_array" 200 "" c_run_testcase ;
  117. tests "/disk1/tests/test_lang.c" "test_struct" 40 "" c_run_testcase ;
  118. tests "/disk1/tests/test_lang.c" "test_enum" 11 "" c_run_testcase ;
  119. tests "/disk1/tests/test_lang.c" "test_strings" 1 "" c_run_testcase ;
  120. tests "/disk1/tests/test_lang.c" "test_define" 5 "" c_run_testcase ;
  121. tests "/disk1/tests/test_lang.c" "test_extension" 0xffffffff "" c_run_testcase ;
  122. tests "/disk1/tests/test_lang.c" "test_unary" 1 "" c_run_testcase ;
  123. tests "/disk1/tests/test_lang.c" "test_shifts" 1 "" c_run_testcase ;
  124. tests "/disk1/tests/test_lang.c" "test_logic" 1 "" c_run_testcase ;
  125. tests "/disk1/tests/test_lang.c" "test_switch" 1 "" c_run_testcase ;
  126. tests "/disk1/tests/test_lang.c" "test_goto" 1 "" c_run_testcase ;
  127. tests "/disk1/tests/test_lang.c" "test_sizeof" 1 "" c_run_testcase ;
  128. tests "/disk1/tests/test_lang.c" "test_comma" 1 "" c_run_testcase ;
  129. tests "/disk1/tests/test_lang.c" "test_initializers" 1 "" c_run_testcase ;
  130. tests "/disk1/tests/test_llong.c" "test_llong" 1 "" c_run_testcase ;
  131. tests "/disk1/tests/test_llong.c" "test_llong_sum" 1 "" c_run_testcase ;
  132. tests "/disk1/tests/test_llong.c" "test_llong_ops" 1 "" c_run_testcase ;
  133. tests "/disk1/tests/test_llong.c" "test_llong_mul_div" 1 "" c_run_testcase ;
  134. tests "/disk1/tests/test_anon_struct.c" "test_anon_struct" 1 "" c_run_testcase ;
  135. tests "/disk1/tests/test_ternary.c" "test_ternary" 1 "" c_run_testcase ;
  136. tests "/disk1/tests/test_ternary.c" "test_ternary_ptr" 1 "" c_run_testcase ;
  137. tests "/disk1/tests/test_ternary.c" "test_ternary_void" 1 "" c_run_testcase ;
  138. tests "/disk1/tests/test_ternary.c" "test_bool" 1 "" c_run_testcase ;
  139. tests "/disk1/tests/test_cast.c" "test_cast" 1 "" c_run_testcase ;
  140. tests "/disk1/tests/test_op_assign.c" "test_ptr_assign" 1 "" c_run_testcase ;
  141. tests "/disk1/tests/test_op_assign.c" "test_int_assign" 1 "" c_run_testcase ;
  142. tests "/disk1/tests/test_op_assign.c" "test_ptr_incdec" 1 "" c_run_testcase ;
  143. tests "/disk1/tests/test_op_assign.c" "test_int_incdec" 1 "" c_run_testcase ;
  144. tests "/disk1/tests/test_ret_obj.c" "test_ret_obj" 1 "" c_run_testcase ;
  145. tests "/disk1/tests/test_stdio.c" "test_fputs" 1 "This is a test string\n" c_run_testcase ;
  146. tests "/disk1/tests/test_stdio.c" "test_puts" 1 "This is a test string\n\n" c_run_testcase ;
  147. tests "/disk1/tests/test_stdio.c" "test_putchar" 1 "X" c_run_testcase ;
  148. tests "/disk1/tests/test_stdio.c" "test_fputc" 1 "X" c_run_testcase ;
  149. tests "/disk1/tests/test_stdio.c" "test_putc" 1 "X" c_run_testcase ;
  150. tests "/disk1/tests/test_stdio.c" "test_sprintf" 1 "" c_run_testcase ;
  151. tests "/disk1/tests/test_stdio.c" "test_printf" 1 "hello\nhello world\n" c_run_testcase ;
  152. tests "/disk1/tests/test_stdio.c" "test_sscanf" 1 "" c_run_testcase ;
  153. tests "/disk1/tests/test_stdio.c" "test_large_numbers" 1 "" c_run_testcase ;
  154. tests "/disk1/tests/test_stdlib.c" "test_malloc_free" 1 "" c_run_testcase ;
  155. tests "/disk1/tests/test_stdlib.c" "test_calloc_free" 1 "" c_run_testcase ;
  156. tests "/disk1/tests/test_stdlib.c" "test_malloc_realloc_free" 1 "" c_run_testcase ;
  157. tests "/disk1/tests/test_stdlib.c" "test_free_null" 1 "" c_run_testcase ;
  158. tests "/disk1/tests/test_stdlib.c" "test_realloc_null_free" 1 "" c_run_testcase ;
  159. tests "/disk1/tests/test_stdlib.c" "test_qsort" 1 "" c_run_testcase ;
  160. tests "/disk1/tests/test_stdlib.c" "test_strtoull_zero" 1 "" c_run_testcase ;
  161. tests "/disk1/tests/test_string.c" "test_strcmp1" 1 "" c_run_testcase ;
  162. tests "/disk1/tests/test_string.c" "test_strcmp2" 1 "" c_run_testcase ;
  163. tests "/disk1/tests/test_string.c" "test_strcmp3" 1 "" c_run_testcase ;
  164. tests "/disk1/tests/test_string.c" "test_strcmp4" 1 "" c_run_testcase ;
  165. tests "/disk1/tests/test_string.c" "test_strcmp5" 1 "" c_run_testcase ;
  166. tests "/disk1/tests/test_stdarg.c" "test_stdarg" 1 "" c_run_testcase ;
  167. tests "/disk1/tests/test_setjmp.c" "test_setjmp" 0 "" c_run_testcase ;
  168. tests "/disk1/tests/test_setjmp.c" "test_setjmp2" 0 "called\ncalled\ncalled\n" c_run_testcase ;
  169. tests TESTS_SUCCESSFUL take itoa log ;
  170. " / " log ;
  171. tests TESTS_RAN take itoa log ;
  172. " tests succesfully passed\n" log ;
  173. tests free ;
  174. }