test.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "test.h"
  5. #include "function_list" // list of function prototypes auto-generated in 'build'
  6. const test_fn Tests[] = {
  7. #include "test_list" // list of test function names auto-generated in 'build'
  8. };
  9. bool Passed = true;
  10. int Num_failures = 0;
  11. int run_tests(void) {
  12. for (size_t i = 0; i < sizeof(Tests)/sizeof(Tests[0]); ++i)
  13. run_test(i);
  14. fprintf(stderr, "\n");
  15. if (Num_failures > 0) {
  16. fprintf(stderr, "%d failure%s\n", Num_failures, (Num_failures > 1 ? "s" : ""));
  17. return 1;
  18. }
  19. return 0;
  20. }
  21. void run_test(int i) {
  22. Passed = true;
  23. reset();
  24. (*Tests[i])();
  25. if (Passed) fprintf(stderr, ".");
  26. else ++Num_failures;
  27. }
  28. const char* Test_names[] = {
  29. #include "test_name_list" // auto-generated; see 'build*' scripts
  30. };
  31. int run_single_test(const char* test_name) {
  32. for (size_t i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) {
  33. if (strcmp(Test_names[i], test_name) != 0) continue;
  34. run_test(i);
  35. if (Passed) fprintf(stderr, "\n");
  36. return !Passed;
  37. }
  38. return 1; // no test found
  39. }