test.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef HADOOP_CORE_COMMON_TEST_H
  19. #define HADOOP_CORE_COMMON_TEST_H
  20. #include <inttypes.h> /* for PRIdPTR */
  21. #include <stdarg.h> /* for va_list */
  22. #include <unistd.h> /* for size_t */
  23. #define TEST_ERROR_EQ 0
  24. #define TEST_ERROR_GE 1
  25. #define TEST_ERROR_GT 2
  26. #define TEST_ERROR_LE 3
  27. #define TEST_ERROR_LT 4
  28. #define TEST_ERROR_NE 5
  29. /**
  30. * Fail with an error message.
  31. *
  32. * @param fmt printf-style format string.
  33. * @param ... printf-style arguments.
  34. */
  35. void fail(const char *fmt, ...);
  36. /**
  37. * Check for a test condition.
  38. *
  39. * @param expected The string which is expected.
  40. * @param text Some text that will be printed out if the test
  41. * condition fails.
  42. * @param ty Comparison type. See TEST_ERROR_* constants.
  43. * @param fmt printf-style format string.
  44. * @param ap printf-style arguments.
  45. *
  46. * @return 0 on success; 1 on failure.
  47. */
  48. int vexpect(const char *expected, const char *text, int ty,
  49. const char *fmt, va_list ap);
  50. /**
  51. * Check for a test condition.
  52. *
  53. * @param expected The string which is expected.
  54. * @param text Some text that will be printed out if the test
  55. * condition fails.
  56. * @param ty Comparison type. See TEST_ERROR_* constants.
  57. * @param fmt printf-style format string.
  58. * @param ... printf-style arguments.
  59. *
  60. * @return 0 on success; 1 on failure.
  61. */
  62. int expect(const char *expected, const char *text, int ty,
  63. const char *fmt, ...);
  64. /**
  65. * Allocate a zero-initialized region of memory, or die.
  66. *
  67. * @param len The length
  68. *
  69. * @return A pointer to a zero-initialized malloc'ed region.
  70. */
  71. void *xcalloc(size_t len);
  72. #define TEST_ERROR_GET_LINE_HELPER2(x) #x
  73. #define TEST_ERROR_GET_LINE_HELPER(x) TEST_ERROR_GET_LINE_HELPER2(x)
  74. #define TEST_ERROR_LOCATION_TEXT __FILE__ " at line " \
  75. TEST_ERROR_GET_LINE_HELPER(__LINE__)
  76. #define EXPECT(...) do { if (expect(__VA_ARGS__)) return 1; } while (0);
  77. #define EXPECT_EQ(expected, fmt, ...) \
  78. EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \
  79. fmt, __VA_ARGS__);
  80. #define EXPECT_STR_EQ(expected, str) \
  81. EXPECT_EQ(expected, "%s", str)
  82. #define EXPECT_GE(expected, fmt, ...) \
  83. EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_GE, \
  84. fmt, __VA_ARGS__);
  85. #define EXPECT_GT(expected, fmt, ...) \
  86. EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_GT, \
  87. fmt, __VA_ARGS__);
  88. #define EXPECT_LE(expected, fmt, ...) \
  89. EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_LE, \
  90. fmt, __VA_ARGS__);
  91. #define EXPECT_LT(expected, fmt, ...) \
  92. EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_LT, \
  93. fmt, __VA_ARGS__);
  94. #define EXPECT_NE(expected, fmt, ...) \
  95. EXPECT(expected, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_NE, \
  96. fmt, __VA_ARGS__);
  97. #define COMMON_TEST__TO_STR(x) #x
  98. #define COMMON_TEST__TO_STR2(x) COMMON_TEST__TO_STR(x)
  99. #define EXPECT_INT_EQ(expected, x) do { \
  100. char expected_buf[16] = { 0 }; \
  101. snprintf(expected_buf, sizeof(expected_buf), "%d", expected); \
  102. EXPECT(expected_buf, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, "%d", x); \
  103. } while(0);
  104. #define EXPECT_INT64_EQ(expected, x) do { \
  105. char expected_buf[32] = { 0 }; \
  106. snprintf(expected_buf, sizeof(expected_buf), "%" PRId64, expected); \
  107. EXPECT(expected_buf, TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \
  108. "%" PRId64, x); \
  109. } while(0);
  110. #define EXPECT_NO_HADOOP_ERR(expr) do { \
  111. struct hadoop_err *err = expr; \
  112. EXPECT("", TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \
  113. "%s", (err ? hadoop_err_msg(err) : "")); \
  114. } while(0);
  115. #define EXPECT_INT_ZERO(x) \
  116. EXPECT("0", TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \
  117. "%d", x);
  118. #define EXPECT_TRUE(x) \
  119. EXPECT("1", TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \
  120. "%d", (!!x));
  121. #define EXPECT_FALSE(x) \
  122. EXPECT("0", TEST_ERROR_LOCATION_TEXT, TEST_ERROR_EQ, \
  123. "%d", (!!x));
  124. #define EXPECT_NONNULL(x) \
  125. EXPECT("0", TEST_ERROR_LOCATION_TEXT, TEST_ERROR_NE, \
  126. "%"PRIdPTR, x);
  127. #endif
  128. // vim: ts=4:sw=4:tw=79:et