1
0

expect.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 LIBHDFS_NATIVE_TESTS_EXPECT_H
  19. #define LIBHDFS_NATIVE_TESTS_EXPECT_H
  20. #include <inttypes.h>
  21. #include <stdio.h>
  22. struct hdfsFile_internal;
  23. #define EXPECT_ZERO(x) \
  24. do { \
  25. int __my_ret__ = x; \
  26. if (__my_ret__) { \
  27. int __my_errno__ = errno; \
  28. fprintf(stderr, "TEST_ERROR: failed on %s:%d with return " \
  29. "code %d (errno: %d): got nonzero from %s\n", \
  30. __FILE__, __LINE__, __my_ret__, __my_errno__, #x); \
  31. return __my_ret__; \
  32. } \
  33. } while (0);
  34. #define EXPECT_NULL(x) \
  35. do { \
  36. const void* __my_ret__ = x; \
  37. int __my_errno__ = errno; \
  38. if (__my_ret__ != NULL) { \
  39. fprintf(stderr, "TEST_ERROR: failed on %s:%d (errno: %d): " \
  40. "got non-NULL value %p from %s\n", \
  41. __FILE__, __LINE__, __my_errno__, __my_ret__, #x); \
  42. return -1; \
  43. } \
  44. } while (0);
  45. #define EXPECT_NONNULL(x) \
  46. do { \
  47. const void* __my_ret__ = x; \
  48. int __my_errno__ = errno; \
  49. if (__my_ret__ == NULL) { \
  50. fprintf(stderr, "TEST_ERROR: failed on %s:%d (errno: %d): " \
  51. "got NULL from %s\n", __FILE__, __LINE__, __my_errno__, #x); \
  52. return -1; \
  53. } \
  54. } while (0);
  55. #define EXPECT_NEGATIVE_ONE_WITH_ERRNO(x, e) \
  56. do { \
  57. int __my_ret__ = x; \
  58. int __my_errno__ = errno; \
  59. if (__my_ret__ != -1) { \
  60. fprintf(stderr, "TEST_ERROR: failed on %s:%d with return " \
  61. "code %d (errno: %d): expected -1 from %s\n", \
  62. __FILE__, __LINE__, \
  63. __my_ret__, __my_errno__, #x); \
  64. return -1; \
  65. } \
  66. if (__my_errno__ != e) { \
  67. fprintf(stderr, "TEST_ERROR: failed on %s:%d with return " \
  68. "code %d (errno: %d): expected errno = %d from %s\n", \
  69. __FILE__, __LINE__, __my_ret__, __my_errno__, e, #x); \
  70. return -1; \
  71. } \
  72. } while (0);
  73. #define EXPECT_NONZERO(x) \
  74. do { \
  75. int __my_ret__ = x; \
  76. int __my_errno__ = errno; \
  77. if (!__my_ret__) { \
  78. fprintf(stderr, "TEST_ERROR: failed on %s:%d with return " \
  79. "code %d (errno: %d): got zero from %s\n", __FILE__, __LINE__, \
  80. __my_ret__, __my_errno__, #x); \
  81. return -1; \
  82. } \
  83. } while (0);
  84. #define EXPECT_NONNEGATIVE(x) \
  85. do { \
  86. int __my_ret__ = x; \
  87. int __my_errno__ = errno; \
  88. if (__my_ret__ < 0) { \
  89. fprintf(stderr, "TEST_ERROR: failed on %s:%d with return " \
  90. "code %d (errno: %d): got negative return from %s\n", \
  91. __FILE__, __LINE__, __my_ret__, __my_errno__, #x); \
  92. return __my_ret__; \
  93. } \
  94. } while (0);
  95. #define EXPECT_INT_EQ(x, y) \
  96. do { \
  97. int __my_ret__ = y; \
  98. int __my_errno__ = errno; \
  99. if (__my_ret__ != (x)) { \
  100. fprintf(stderr, "TEST_ERROR: failed on %s:%d with return " \
  101. "code %d (errno: %d): expected %d\n", \
  102. __FILE__, __LINE__, __my_ret__, __my_errno__, (x)); \
  103. return -1; \
  104. } \
  105. } while (0);
  106. #define EXPECT_INT64_EQ(x, y) \
  107. do { \
  108. int64_t __my_ret__ = y; \
  109. int __my_errno__ = errno; \
  110. if (__my_ret__ != (x)) { \
  111. fprintf(stderr, "TEST_ERROR: failed on %s:%d with return " \
  112. "value %"PRId64" (errno: %d): expected %"PRId64"\n", \
  113. __FILE__, __LINE__, __my_ret__, __my_errno__, (x)); \
  114. return -1; \
  115. } \
  116. } while (0);
  117. #define RETRY_ON_EINTR_GET_ERRNO(ret, expr) do { \
  118. ret = expr; \
  119. if (!ret) \
  120. break; \
  121. ret = -errno; \
  122. } while (ret == -EINTR);
  123. /**
  124. * Test that an HDFS file has the given statistics.
  125. *
  126. * Any parameter can be set to UINT64_MAX to avoid checking it.
  127. *
  128. * @return 0 on success; error code otherwise
  129. */
  130. int expectFileStats(struct hdfsFile_internal *file,
  131. uint64_t expectedTotalBytesRead,
  132. uint64_t expectedTotalLocalBytesRead,
  133. uint64_t expectedTotalShortCircuitBytesRead,
  134. uint64_t expectedTotalZeroCopyBytesRead);
  135. #endif