]> git.saurik.com Git - apple/hfs.git/blob - tests/test-utils.h
hfs-366.1.1.tar.gz
[apple/hfs.git] / tests / test-utils.h
1 /*
2 * Copyright (c) 2014-2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef TEST_UTILS_H_
30 #define TEST_UTILS_H_
31
32 #include <mach-o/dyld.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <sys/syslimits.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/errno.h>
39 #include <libgen.h>
40
41 __BEGIN_DECLS
42
43 #if HAVE_ASSERT_FAIL
44
45 void __attribute__((format (printf, 3, 4), noreturn))
46 assert_fail_(const char *file, int line, const char *assertion, ...);
47
48 #else // !HAVE_ASSERT_FAIL
49
50 static inline void __attribute__((format (printf, 3, 4), noreturn))
51 assert_fail_(const char *file, int line, const char *assertion, ...)
52 {
53 va_list args;
54 va_start(args, assertion);
55 char *msg;
56 vasprintf(&msg, assertion, args);
57 va_end(args);
58 printf("\n%s:%u: error: %s\n", file, line, msg);
59 exit(1);
60 }
61
62 #endif // !HAVE_ASSERT_FAIL
63
64 int test_cleanup(bool (^)(void));
65 void do_cleanups(void);
66
67 #define assert_fail(str, ...) \
68 assert_fail_(__FILE__, __LINE__, str, ## __VA_ARGS__)
69
70 #undef assert
71 #define assert(condition) \
72 do { \
73 if (!(condition)) \
74 assert_fail_(__FILE__, __LINE__, \
75 "assertion failed: %s", #condition); \
76 } while (0)
77
78 #define assert_with_errno_(condition, condition_str) \
79 do { \
80 if (!(condition)) \
81 assert_fail_(__FILE__, __LINE__, "%s failed: %s", \
82 condition_str, strerror(errno)); \
83 } while (0)
84
85 #define assert_with_errno(condition) \
86 assert_with_errno_((condition), #condition)
87
88 #define assert_no_err(condition) \
89 assert_with_errno_(!(condition), #condition)
90
91 #define assert_equal(lhs, rhs, fmt) \
92 do { \
93 typeof (lhs) lhs_ = (lhs); \
94 typeof (lhs) rhs_ = (rhs); \
95 if (lhs_ != rhs_) \
96 assert_fail(#lhs " (" fmt ") != " #rhs " (" fmt ")", \
97 lhs_, rhs_); \
98 } while (0)
99
100 #define assert_equal_int(lhs, rhs) assert_equal(lhs, rhs, "%d")
101 #define assert_equal_ll(lhs, rhs) assert_equal(lhs, rhs, "%lld");
102
103 #define check_io(fn, len) check_io_((fn), len, __FILE__, __LINE__, #fn)
104
105 static inline ssize_t check_io_(ssize_t res, ssize_t len, const char *file,
106 int line, const char *fn_str)
107 {
108 if (res < 0)
109 assert_fail_(file, line, "%s failed: %s", fn_str, strerror(errno));
110 else if (len != -1 && res != len)
111 assert_fail_(file, line, "%s != %ld", fn_str, len);
112 return res;
113 }
114
115 #define ignore_eintr(x, error_val) \
116 ({ \
117 typeof(x) eintr_ret_; \
118 do { \
119 eintr_ret_ = (x); \
120 } while (eintr_ret_ == (error_val) && errno == EINTR); \
121 eintr_ret_; \
122 })
123
124 #define lengthof(x) (sizeof(x) / sizeof(*x))
125
126 __END_DECLS
127
128 #endif // TEST_UTILS_H_