easy unit testing for C in a single header file
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.7KB

  1. /* SIMPLE_TEST_PRINT_FUN may be redefined like so. default is `printf` */
  2. #define stderr_redirect(...) fprintf(stderr, __VA_ARGS__)
  3. #define SIMPLE_TEST_PRINT_FUN stderr_redirect
  4. /* colour output is on by default, but can be disabled */
  5. /* #define SIMPLE_TEST_USE_COLOUR false */
  6. #include "../simple-test.h"
  7. /* global variables, functions, and includes must come before BEGIN_TEST */
  8. char *global_s;
  9. /* a teardown function must be of type `void function(void)` and can be called
  10. * by TESTs which include them using the TEARDOWN macro. the call occurs either
  11. * after an ASSERT fails or after the TEST is finished.
  12. *
  13. * the macro may be called multiple times, but only the most recently set
  14. * teardown function will be called.
  15. *
  16. * here i'm using it to free memory that's alloced inside tests below */
  17. void teardown_fun(void)
  18. {
  19. free(global_s);
  20. }
  21. /* must appear before an (optional) REGISTER_TEARDOWN and all TESTs */
  22. BEGIN_TEST
  23. /* run a test. provided description must be a string literal */
  24. TEST("basic assertion")
  25. {
  26. /* ASSERT fails if passed some sort of non-true value (0, false, NULL) */
  27. ASSERT(1);
  28. }
  29. TEST("basic not assertion")
  30. {
  31. bool b = false;
  32. ASSERT_NOT(b);
  33. }
  34. TEST("boolean comparison")
  35. {
  36. bool a = false, b = true;
  37. /* fail if parameters are not equal */
  38. ASSERT_EQ(a, b);
  39. }
  40. TEST("type mismatch")
  41. {
  42. char a = 'a';
  43. int i = 97;
  44. char *b = NULL;
  45. /* for convenience's sake, when presented with mismatched types, assertions
  46. * try to resolve them in a way that's most likely to match the
  47. * programmer's intentions. here 'i' is interpreted as a char */
  48. ASSERT_EQ(a, i);
  49. /* if there isn't a straightforward comparison to make, though (as is the
  50. * case here, with a 'char' and 'char *'), a type mismatch error occurs */
  51. ASSERT_EQ(a, b);
  52. }
  53. TEST("ECHO example")
  54. {
  55. int i;
  56. /* ECHO can be used to neatly report information during a run */
  57. if (true)
  58. ECHO("loop until i not less than 1");
  59. for (i = 0; i < 2; i++) {
  60. /* it takes printf format strings and variable args as well */
  61. ECHO("i == %d", i);
  62. ASSERT_LT(i, 1);
  63. }
  64. }
  65. TEST("string comparison")
  66. {
  67. char *s = "test";
  68. global_s = strdup("test");
  69. /* this tells this test to call the previously defined teardown function on
  70. * exiting (successfully or otherwise) */
  71. TEARDOWN(teardown_fun);
  72. /* strings are compared by content, so this assertion succeeds */
  73. ASSERT_EQ(s, global_s);
  74. }
  75. TEST("pointer comparison")
  76. {
  77. char *s = "test";
  78. global_s = strdup("test");
  79. TEARDOWN(teardown_fun);
  80. /* you can cast parameters in order to use a different type of comparison.
  81. * here i'm casting the 'char *' to 'void *' so assertion performs a
  82. * pointer comparison, which fails */
  83. ASSERT_EQ((void*)s, (void*)global_s);
  84. }
  85. /* must come after all TESTs */
  86. END_TEST