bsdtests.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * 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. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. #ifndef __BSD_TEST_H__
  21. #define __BSD_TEST_H__
  22. #if defined(HAVE_CONFIG_H)
  23. #if __has_include(<config/config_ac.h>)
  24. #include <config/config_ac.h>
  25. #else
  26. #include <config/config.h>
  27. #endif
  28. #endif
  29. #if !HAVE_PRINTFLIKE
  30. #ifndef __printflike
  31. #if __has_attribute(format)
  32. #define __printflike(a,b) __attribute__((format(printf, a, b)))
  33. #else
  34. #define __printflike(a,b)
  35. #endif // __has_attribute(format)
  36. #endif // !defined(__printflike)
  37. #endif // !HAVE_PRINTFLIKE
  38. #include <errno.h>
  39. #ifdef __APPLE__
  40. #include <mach/error.h>
  41. #endif
  42. #if defined(__APPLE__) || defined(HAVE_COREFOUNDATION)
  43. #include <CoreFoundation/CoreFoundation.h>
  44. #endif
  45. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  46. #include <unistd.h>
  47. #endif
  48. #include <string.h>
  49. #include <stdint.h>
  50. static inline const char*
  51. __BASENAME__(const char *_str_)
  52. {
  53. const char *_s_ = strrchr(_str_, '/');
  54. return (_s_ ? _s_ : _str_ - 1) + 1;
  55. }
  56. #define __SOURCE_FILE__ __BASENAME__(__FILE__)
  57. #if defined(__cplusplus)
  58. extern "C" {
  59. #endif
  60. /**
  61. * test_start() provides the TEST token. Use this once per test "tool"
  62. */
  63. void test_start(const char* desc);
  64. #if defined(__APPLE__) && defined(__MACH__)
  65. /**
  66. * Explicitly runs the 'leaks' test without stopping the process.
  67. */
  68. void test_leaks_pid(const char *name, pid_t pid);
  69. void test_leaks(const char *name);
  70. #endif
  71. /**
  72. * test_stop() checks for leaks during the tests using leaks-wrapper. Use this at the end of each "tool"
  73. */
  74. void test_stop(void) __attribute__((__noreturn__));
  75. void test_stop_after_delay(void *delay) __attribute__((__noreturn__));
  76. /**
  77. * Each test "tool" can used one or more of these functions to perform actual
  78. * testing and provide a PASS/FAIL token. All APIs take a descriptive string
  79. * that is printed after the token.
  80. */
  81. void _test_ptr_null(const char* file, long line, const char* desc, const void* ptr);
  82. #define test_ptr_null(a,b) _test_ptr_null(__SOURCE_FILE__, __LINE__, a, b)
  83. void test_ptr_null_format(void *ptr, const char *format, ...);
  84. void _test_ptr_notnull(const char* file, long line, const char* desc, const void* ptr);
  85. #define test_ptr_notnull(a,b) _test_ptr_notnull(__SOURCE_FILE__, __LINE__, a, b)
  86. void test_ptr_notnull_format(const void *ptr, const char *format, ...) __printflike(2, 3);
  87. void _test_ptr_not(const char* file, long line, const char* desc, const void* actual, const void* expected);
  88. #define test_ptr_not(a, b, c) _test_ptr_not(__SOURCE_FILE__, __LINE__, a, b, c)
  89. void test_ptr_not_format(const void* actual, const void* expected, const char *format, ...);
  90. void _test_ptr(const char* file, long line, const char* desc, const void* actual, const void* expected);
  91. #define test_ptr(a,b,c) _test_ptr(__SOURCE_FILE__, __LINE__, a, b, c)
  92. void test_ptr_format(const void* actual, const void* expected, const char *format, ...) __printflike(3,4);
  93. void _test_uint32(const char* file, long line, const char* desc, uint32_t actual, uint32_t expected);
  94. #define test_uint32(a,b,c) _test_uint32(__SOURCE_FILE__, __LINE__, a, b, c)
  95. void test_uint32_format(uint32_t actual, uint32_t expected, const char *format, ...) __printflike(3,4);
  96. void _test_int32(const char* file, long line, const char* desc, int32_t actual, int32_t expected);
  97. #define test_int32(a,b,c) _test_int32(__SOURCE_FILE__, __LINE__, a, b, c)
  98. void test_int32_format(int32_t actual, int32_t expected, const char* format, ...) __printflike(3,4);
  99. void _test_long(const char* file, long line, const char* desc, long actual, long expected);
  100. #define test_long(a,b,c) _test_long(__SOURCE_FILE__, __LINE__, a, b, c)
  101. void test_long_format(long actual, long expected, const char *format, ...) __printflike(3,4);
  102. void _test_sizet(const char* file, long line, const char* desc, size_t actual, size_t expected);
  103. #define test_sizet(a,b,c) _test_sizet(__SOURCE_FILE__, __LINE__, a, b, c)
  104. void test_sizet_format(size_t actual, size_t expected, const char *format, ...) __printflike(3,4);
  105. void _test_uint64(const char* file, long line, const char* desc, uint64_t actual, uint64_t expected);
  106. #define test_uint64(a,b,c) _test_uint64(__SOURCE_FILE__, __LINE__, a, b, c)
  107. void test_uint64_format(uint64_t actual, uint64_t expected, const char* desc, ...);
  108. void _test_int64(const char* file, long line, const char* desc, int64_t actual, int64_t expected);
  109. #define test_int64(a,b,c) _test_uint64(__SOURCE_FILE__, __LINE__, a, b, c)
  110. void test_int64_format(int64_t actual, int64_t expected, const char* desc, ...);
  111. void _test_long_less_than(const char* file, long line, const char* desc, long actual, long max_expected);
  112. #define test_long_less_than(a,b,c) _test_long_less_than(__SOURCE_FILE__, __LINE__, a, b, c)
  113. void test_long_less_than_format(long actual, long max_expected, const char *format, ...) __printflike(3,4);
  114. void _test_long_less_than_or_equal(const char* file, long line, const char* desc, long actual, long max_expected);
  115. #define test_long_less_than_or_equal(a,b,c) _test_long_less_than_or_equal(__SOURCE_FILE__, __LINE__, a, b, c)
  116. void test_long_less_than_or_equal_format(long actual, long max_expected, const char *format, ...) __printflike(3,4);
  117. void _test_long_greater_than_or_equal(const char* file, long line, const char* desc, long actual, long expected_min);
  118. #define test_long_greater_than_or_equal(a,b,c) _test_long_greater_than_or_equal(__SOURCE_FILE__, __LINE__, a, b, c)
  119. void test_long_greater_than_or_equal_format(long actual, long expected_min, const char *format, ...) __printflike(3,4);
  120. void _test_sizet_less_than(const char* file, long line, const char* desc, size_t actual, size_t max_expected);
  121. #define test_sizet_less_than(a,b,c) _test_sizet_less_than(__SOURCE_FILE__, __LINE__, a, b, c)
  122. void test_sizet_less_than_format(size_t actual, size_t max_expected, const char *format, ...) __printflike(3,4);
  123. void _test_sizet_less_than_or_equal(const char* file, long line, const char* desc, size_t actual, size_t max_expected);
  124. #define test_sizet_less_than_or_equal(a,b,c) _test_sizet_less_than_or_equal(__SOURCE_FILE__, __LINE__, a, b, c)
  125. void test_sizet_less_than_or_equal_format(size_t actual, size_t max_expected, const char *format, ...) __printflike(3,4);
  126. void _test_double_less_than_or_equal(const char* file, long line, const char* desc, double val, double max_expected);
  127. #define test_double_less_than_or_equal(d, v, m) _test_double_less_than_or_equal(__SOURCE_FILE__, __LINE__, d, v, m)
  128. void test_double_less_than_or_equal_format(double val, double max_expected, const char *format, ...) __printflike(3,4);
  129. void _test_double_less_than(const char* file, long line, const char* desc, double val, double max_expected);
  130. #define test_double_less_than(d, v, m) _test_double_less_than(__SOURCE_FILE__, __LINE__, d, v, m)
  131. void test_double_less_than_format(double val, double max_expected, const char *format, ...) __printflike(3,4);
  132. void _test_double_equal(const char* file, long line, const char* desc, double val, double expected);
  133. #define test_double_equal(d, v, m) _test_double_equal(__SOURCE_FILE__, __LINE__, d, v, m)
  134. void test_double_equal_format(double val, double expected, const char *format, ...) __printflike(3,4);
  135. void _test_errno(const char* file, long line, const char* desc, int actual, int expected);
  136. #define test_errno(a,b,c) _test_errno(__SOURCE_FILE__, __LINE__, a, b, c)
  137. void test_errno_format(int actual, int expected, const char *format, ...) __printflike(3,4);
  138. #if defined(__APPLE__)
  139. void _test_mach_error(const char* file, long line, const char* desc, mach_error_t actual, mach_error_t expected);
  140. #define test_mach_error(a,b,c) _test_mach_error(__SOURCE_FILE__, __LINE__, a, b, c)
  141. void test_mach_error_format(mach_error_t actual, mach_error_t expected, const char *format, ...) __printflike(3,4);
  142. #endif
  143. #if defined(__APPLE__) || defined(HAVE_COREFOUNDATION)
  144. void test_cferror(const char* desc, CFErrorRef actual, CFIndex expectedCode);
  145. void test_cferror_format(CFErrorRef actual, CFIndex expectedCode, const char *format, ...) __printflike(3,4);
  146. #endif
  147. void _test_skip(const char* file, long line, const char* desc);
  148. #define test_skip(m) _test_skip(__SOURCE_FILE__, __LINE__, m)
  149. #define test_skip2(m) _test_skip("", 0, m)
  150. void test_skip_format(const char *format, ...) __printflike(1,2);
  151. #if defined(__cplusplus)
  152. } /* extern "C" */
  153. #endif
  154. #endif /* __BSD_TEST_H__ */