]>
git.saurik.com Git - apple/hfs.git/blob - tests/test-utils.h
2 * Copyright (c) 2014-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 #include <mach-o/dyld.h>
35 #include <sys/syslimits.h>
38 #include <sys/errno.h>
45 void __attribute__((format (printf
, 3, 4), noreturn
))
46 assert_fail_(const char *file
, int line
, const char *assertion
, ...);
48 #else // !HAVE_ASSERT_FAIL
50 static inline void __attribute__((format (printf
, 3, 4), noreturn
))
51 assert_fail_(const char *file
, int line
, const char *assertion
, ...)
54 va_start(args
, assertion
);
56 vasprintf(&msg
, assertion
, args
);
58 printf("\n%s:%u: error: %s\n", file
, line
, msg
);
62 #endif // !HAVE_ASSERT_FAIL
64 int test_cleanup(bool (^)(void));
65 void do_cleanups(void);
67 #define assert_fail(str, ...) \
68 assert_fail_(__FILE__, __LINE__, str, ## __VA_ARGS__)
71 #define assert(condition) \
74 assert_fail_(__FILE__, __LINE__, \
75 "assertion failed: %s", #condition); \
78 #define assert_with_errno_(condition, condition_str) \
81 assert_fail_(__FILE__, __LINE__, "%s failed: %s", \
82 condition_str, strerror(errno)); \
85 #define assert_with_errno(condition) \
86 assert_with_errno_((condition), #condition)
88 #define assert_no_err(condition) \
89 assert_with_errno_(!(condition), #condition)
91 #define assert_equal(lhs, rhs, fmt) \
93 typeof (lhs) lhs_ = (lhs); \
94 typeof (lhs) rhs_ = (rhs); \
96 assert_fail(#lhs " (" fmt ") != " #rhs " (" fmt ")", \
100 #define assert_equal_int(lhs, rhs) assert_equal(lhs, rhs, "%d")
101 #define assert_equal_ll(lhs, rhs) assert_equal(lhs, rhs, "%lld");
103 #define check_io(fn, len) check_io_((fn), len, __FILE__, __LINE__, #fn)
105 static inline ssize_t
check_io_(ssize_t res
, ssize_t len
, const char *file
,
106 int line
, const char *fn_str
)
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
);
115 #define ignore_eintr(x, error_val) \
117 typeof(x) eintr_ret_; \
120 } while (eintr_ret_ == (error_val) && errno == EINTR); \
124 #define lengthof(x) (sizeof(x) / sizeof(*x))
128 #endif // TEST_UTILS_H_