test_common.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. */
  6. #ifndef VBOOT_REFERENCE_TEST_COMMON_H_
  7. #define VBOOT_REFERENCE_TEST_COMMON_H_
  8. #include <stdio.h>
  9. /* Used to get a line number as a constant string. Need to stringify it twice */
  10. #define STRINGIFY(x) #x
  11. #define TOSTRING(x) STRINGIFY(x)
  12. extern int gTestSuccess;
  13. /* Return 1 if result is equal to expected_result, else return 0.
  14. * Also update the global gTestSuccess flag if test fails. */
  15. int test_eq(int result, int expected,
  16. const char *preamble, const char *desc, const char *comment);
  17. #define TEST_EQ(result, expected, comment) \
  18. test_eq(result, expected, \
  19. __FILE__ ":" TOSTRING(__LINE__), \
  20. #result " == " #expected, \
  21. comment)
  22. #define TEST_EQ_S(result, expected) TEST_EQ(result, expected, NULL);
  23. /* Return 0 if result is equal to not_expected_result, else return 1.
  24. * Also update the global gTestSuccess flag if test fails. */
  25. int test_neq(int result, int not_expected,
  26. const char *preamble, const char *desc, const char *comment);
  27. #define TEST_NEQ(result, not_expected, comment) \
  28. test_neq(result, not_expected, \
  29. __FILE__ ":" TOSTRING(__LINE__), \
  30. #result " != " #not_expected, \
  31. comment)
  32. /* Return 1 if result pointer is equal to expected_result pointer,
  33. * else return 0. Does not check pointer contents, only the pointer
  34. * itself. Also update the global gTestSuccess flag if test fails. */
  35. int test_ptr_eq(const void* result, const void* expected,
  36. const char *preamble, const char *desc, const char *comment);
  37. #define TEST_PTR_EQ(result, expected, comment) \
  38. test_ptr_eq(result, expected, \
  39. __FILE__ ":" TOSTRING(__LINE__), \
  40. #result " == " #expected, \
  41. comment)
  42. /* Return 1 if result pointer is not equal to expected_result pointer,
  43. * else return 0. Does not check pointer contents, only the pointer
  44. * itself. Also update the global gTestSuccess flag if test fails. */
  45. int test_ptr_neq(const void* result, const void* not_expected,
  46. const char *preamble, const char *desc, const char *comment);
  47. #define TEST_PTR_NEQ(result, not_expected, comment) \
  48. test_ptr_neq(result, not_expected, \
  49. __FILE__ ":" TOSTRING(__LINE__), \
  50. #result " != " #not_expected, \
  51. comment)
  52. /* Return 1 if result string is equal to expected_result string,
  53. * else return 0. Also update the global gTestSuccess flag if test fails. */
  54. int test_str_eq(const char* result, const char* expected,
  55. const char *preamble, const char *desc, const char *comment);
  56. #define TEST_STR_EQ(result, expected, comment) \
  57. test_str_eq(result, expected, \
  58. __FILE__ ":" TOSTRING(__LINE__), \
  59. #result " == " #expected, \
  60. comment)
  61. /* Return 1 if result string is not equal to not_expected string,
  62. * else return 0. Also update the global gTestSuccess flag if test fails. */
  63. int test_str_neq(const char* result, const char* not_expected,
  64. const char *preamble, const char *desc, const char *comment);
  65. #define TEST_STR_NEQ(result, not_expected, comment) \
  66. test_str_neq(result, not_expected, \
  67. __FILE__ ":" TOSTRING(__LINE__), \
  68. #result " != " #not_expected, \
  69. comment)
  70. /* Return 1 if the result is true, else return 0.
  71. * Also update the global gTestSuccess flag if test fails. */
  72. int test_true(int result,
  73. const char *preamble, const char *desc, const char *comment);
  74. #define TEST_TRUE(result, comment) \
  75. test_true(result, \
  76. __FILE__ ":" TOSTRING(__LINE__), \
  77. #result " == true", \
  78. comment)
  79. /* Return 1 if the result is false, else return 0.
  80. * Also update the global gTestSuccess flag if test fails. */
  81. int test_false(int result,
  82. const char *preamble, const char *desc, const char *comment);
  83. #define TEST_FALSE(result, comment) \
  84. test_false(result, \
  85. __FILE__ ":" TOSTRING(__LINE__), \
  86. #result " == false", \
  87. comment)
  88. /* Return 1 if result is 0 (VB_ERROR_SUCCESS / VB2_SUCCESS), else return 0.
  89. * Also update the global gTestSuccess flag if test fails. */
  90. int test_succ(int result,
  91. const char *preamble, const char *desc, const char *comment);
  92. #define TEST_SUCC(result, comment) \
  93. test_succ(result, \
  94. __FILE__ ":" TOSTRING(__LINE__), \
  95. #result " == 0", \
  96. comment)
  97. /* ANSI Color coding sequences.
  98. *
  99. * Don't use \e as MSC does not recognize it as a valid escape sequence.
  100. */
  101. #define COL_GREEN "\x1b[1;32m"
  102. #define COL_YELLOW "\x1b[1;33m"
  103. #define COL_RED "\x1b[0;31m"
  104. #define COL_STOP "\x1b[m"
  105. /* Abort if asprintf fails. */
  106. #define xasprintf(...) \
  107. do { \
  108. if (asprintf(__VA_ARGS__) < 0) \
  109. abort(); \
  110. } while (0)
  111. #endif /* VBOOT_REFERENCE_TEST_COMMON_H_ */