test_conditional.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <zf_log.c>
  2. #include <zf_test.h>
  3. static unsigned g_logged = 0;
  4. /* Keep it extern (non-static), so compiler has less chances to optimize access
  5. * to this variable.
  6. */
  7. unsigned g_forty_two = 42;
  8. static void mock_output_callback(const zf_log_message *msg, void *arg)
  9. {
  10. (void)msg; (void)arg;
  11. ++g_logged;
  12. }
  13. static unsigned was_logged()
  14. {
  15. const unsigned logged = g_logged;
  16. g_logged = 0;
  17. return logged;
  18. }
  19. static unsigned forty_two()
  20. {
  21. return g_forty_two;
  22. }
  23. #define EXPECTED_LINES(n) TEST_VERIFY_EQUAL(was_logged(), (n))
  24. static void test_conditional()
  25. {
  26. ZF_LOG_IF(1 < 2, ZF_LOGI("True"));
  27. EXPECTED_LINES(1);
  28. ZF_LOG_IF(2 < 1, ZF_LOGI("False"));
  29. EXPECTED_LINES(0);
  30. ZF_LOG_IF(g_forty_two == 42, ZF_LOGI("True"));
  31. EXPECTED_LINES(1);
  32. ZF_LOG_IF(g_forty_two != 42, ZF_LOGI("False"));
  33. EXPECTED_LINES(0);
  34. ZF_LOG_IF(g_forty_two == 42, ZF_LOGI("True"));
  35. EXPECTED_LINES(1);
  36. ZF_LOG_IF(g_forty_two != 42, ZF_LOGI("False"));
  37. EXPECTED_LINES(0);
  38. ZF_LOG_IF(forty_two() == 42, ZF_LOGI("True"));
  39. EXPECTED_LINES(1);
  40. ZF_LOG_IF(forty_two() != 42, ZF_LOGI("False"));
  41. EXPECTED_LINES(0);
  42. }
  43. int main(int argc, char *argv[])
  44. {
  45. zf_log_set_output_v(ZF_LOG_PUT_STD, 0, mock_output_callback);
  46. TEST_RUNNER_CREATE(argc, argv);
  47. TEST_EXECUTE(test_conditional());
  48. return TEST_RUNNER_EXIT_CODE();
  49. }