test.h 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. // Implement this function to initialize each test, and to clean up at the end.
  2. void reset(void);
  3. // Write your tests in functions with the precise first line:
  4. // `void test_xxxxx(void) {`
  5. typedef void (*test_fn)(void);
  6. // The 'build' script will auto-generate a list of such functions.
  7. extern const test_fn Tests[]; // convention: global variables are capitalized
  8. // Inside each test, signal failure by setting the global variable 'Passed' to
  9. // false.
  10. #include <stdbool.h>
  11. extern bool Passed;
  12. // A helper to help signal failure and print out failing tests.
  13. #include <stdio.h>
  14. #define CHECK(X) \
  15. if (Passed && !(X)) { \
  16. fprintf(stderr, "\nF - %s(%s:%d): %s\n", __FUNCTION__, __FILE__, __LINE__, #X); \
  17. Passed = false; \
  18. return; /* stop at the very first failure inside a test */ \
  19. }
  20. // To run all your tests, call `run_tests()`, say within main after parsing
  21. // some commandline flag. It will print a dot for each passing test, and more
  22. // verbose messages about failing tests.
  23. int run_tests(void);
  24. // To run a single test, call `run_single_test()` with a string containing the
  25. // name of the test to run, say from a commandline argument.
  26. int run_single_test(const char*);