1
0

expect.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <stdio.h>
  21. #define EXPECT_ZERO(x) \
  22. do { \
  23. int __my_ret__ = x; \
  24. if (__my_ret__) { \
  25. int __my_errno__ = errno; \
  26. fprintf(stderr, "TEST_ERROR: failed on line %d with return " \
  27. "code %d (errno: %d): got nonzero from %s\n", \
  28. __LINE__, __my_ret__, __my_errno__, #x); \
  29. return __my_ret__; \
  30. } \
  31. } while (0);
  32. #define EXPECT_NULL(x) \
  33. do { \
  34. void* __my_ret__ = x; \
  35. int __my_errno__ = errno; \
  36. if (__my_ret__ != NULL) { \
  37. fprintf(stderr, "TEST_ERROR: failed on line %d (errno: %d): " \
  38. "got non-NULL value %p from %s\n", \
  39. __LINE__, __my_errno__, __my_ret__, #x); \
  40. return -1; \
  41. } \
  42. } while (0);
  43. #define EXPECT_NONNULL(x) \
  44. do { \
  45. void* __my_ret__ = x; \
  46. int __my_errno__ = errno; \
  47. if (__my_ret__ == NULL) { \
  48. fprintf(stderr, "TEST_ERROR: failed on line %d (errno: %d): " \
  49. "got NULL from %s\n", __LINE__, __my_errno__, #x); \
  50. return -1; \
  51. } \
  52. } while (0);
  53. #define EXPECT_NEGATIVE_ONE_WITH_ERRNO(x, e) \
  54. do { \
  55. int __my_ret__ = x; \
  56. int __my_errno__ = errno; \
  57. if (__my_ret__ != -1) { \
  58. fprintf(stderr, "TEST_ERROR: failed on line %d with return " \
  59. "code %d (errno: %d): expected -1 from %s\n", __LINE__, \
  60. __my_ret__, __my_errno__, #x); \
  61. return -1; \
  62. } \
  63. if (__my_errno__ != e) { \
  64. fprintf(stderr, "TEST_ERROR: failed on line %d with return " \
  65. "code %d (errno: %d): expected errno = %d from %s\n", \
  66. __LINE__, __my_ret__, __my_errno__, e, #x); \
  67. return -1; \
  68. } \
  69. } while (0);
  70. #define EXPECT_NONZERO(x) \
  71. do { \
  72. int __my_ret__ = x; \
  73. int __my_errno__ = errno; \
  74. if (!__my_ret__) { \
  75. fprintf(stderr, "TEST_ERROR: failed on line %d with return " \
  76. "code %d (errno: %d): got zero from %s\n", __LINE__, \
  77. __my_ret__, __my_errno__, #x); \
  78. return -1; \
  79. } \
  80. } while (0);
  81. #define EXPECT_NONNEGATIVE(x) \
  82. do { \
  83. int __my_ret__ = x; \
  84. int __my_errno__ = errno; \
  85. if (__my_ret__ < 0) { \
  86. fprintf(stderr, "TEST_ERROR: failed on line %d with return " \
  87. "code %d (errno: %d): got negative return from %s\n", \
  88. __LINE__, __my_ret__, __my_errno__, #x); \
  89. return __my_ret__; \
  90. } \
  91. } while (0);
  92. #define EXPECT_INT_EQ(x, y) \
  93. do { \
  94. int __my_ret__ = y; \
  95. int __my_errno__ = errno; \
  96. if (__my_ret__ != (x)) { \
  97. fprintf(stderr, "TEST_ERROR: failed on line %d with return " \
  98. "code %d (errno: %d): expected %d\n", \
  99. __LINE__, __my_ret__, __my_errno__, (x)); \
  100. return -1; \
  101. } \
  102. } while (0);
  103. #define RETRY_ON_EINTR_GET_ERRNO(ret, expr) do { \
  104. ret = expr; \
  105. if (!ret) \
  106. break; \
  107. ret = -errno; \
  108. } while (ret == -EINTR);
  109. #endif