1 | /* |
2 | * librdkafka - Apache Kafka C library |
3 | * |
4 | * Copyright (c) 2017 Magnus Edenhill |
5 | * All rights reserved. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions are met: |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | * this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
13 | * this list of conditions and the following disclaimer in the documentation |
14 | * and/or other materials provided with the distribution. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #ifndef _RD_UNITTEST_H |
30 | #define _RD_UNITTEST_H |
31 | |
32 | #include <stdio.h> |
33 | |
34 | |
35 | extern int rd_unittest_assert_on_failure; |
36 | |
37 | /** |
38 | * @brief Fail the current unit-test function. |
39 | */ |
40 | #define RD_UT_FAIL(...) do { \ |
41 | fprintf(stderr, "\033[31mRDUT: FAIL: %s:%d: %s: ", \ |
42 | __FILE__, __LINE__, __FUNCTION__); \ |
43 | fprintf(stderr, __VA_ARGS__); \ |
44 | fprintf(stderr, "\033[0m\n"); \ |
45 | if (rd_unittest_assert_on_failure) \ |
46 | rd_assert(!*"unittest failure"); \ |
47 | return 1; \ |
48 | } while (0) |
49 | |
50 | /** |
51 | * @brief Pass the current unit-test function |
52 | */ |
53 | #define RD_UT_PASS() do { \ |
54 | fprintf(stderr, "\033[32mRDUT: PASS: %s:%d: %s\033[0m\n", \ |
55 | __FILE__, __LINE__, __FUNCTION__); \ |
56 | return 0; \ |
57 | } while (0) |
58 | |
59 | /** |
60 | * @brief Fail unit-test if \p expr is false |
61 | */ |
62 | #define RD_UT_ASSERT(expr,...) do { \ |
63 | if (!(expr)) { \ |
64 | fprintf(stderr, \ |
65 | "\033[31mRDUT: FAIL: %s:%d: %s: assert failed: " # expr ": ", \ |
66 | __FILE__, __LINE__, __FUNCTION__); \ |
67 | fprintf(stderr, __VA_ARGS__); \ |
68 | fprintf(stderr, "\033[0m\n"); \ |
69 | if (rd_unittest_assert_on_failure) \ |
70 | rd_assert(expr); \ |
71 | return 1; \ |
72 | } \ |
73 | } while (0) |
74 | |
75 | |
76 | /** |
77 | * @brief Check that value \p V is within inclusive range \p VMIN .. \p VMAX, |
78 | * else asserts. |
79 | * |
80 | * @param VFMT is the printf formatter for \p V's type |
81 | */ |
82 | #define RD_UT_ASSERT_RANGE(V,VMIN,VMAX,VFMT) \ |
83 | RD_UT_ASSERT((VMIN) <= (V) && (VMAX) >= (V), \ |
84 | VFMT" out of range "VFMT" .. "VFMT, \ |
85 | (V), (VMIN), (VMAX)) |
86 | |
87 | |
88 | /** |
89 | * @brief Log something from a unit-test |
90 | */ |
91 | #define RD_UT_SAY(...) do { \ |
92 | fprintf(stderr, "RDUT: INFO: %s:%d: %s: ", \ |
93 | __FILE__, __LINE__, __FUNCTION__); \ |
94 | fprintf(stderr, __VA_ARGS__); \ |
95 | fprintf(stderr, "\n"); \ |
96 | } while (0) |
97 | |
98 | |
99 | /** |
100 | * @brief Warn about something from a unit-test |
101 | */ |
102 | #define RD_UT_WARN(...) do { \ |
103 | fprintf(stderr, "\033[33mRDUT: WARN: %s:%d: %s: ", \ |
104 | __FILE__, __LINE__, __FUNCTION__); \ |
105 | fprintf(stderr, __VA_ARGS__); \ |
106 | fprintf(stderr, "\033[0m\n"); \ |
107 | } while (0) |
108 | |
109 | |
110 | int rd_unittest (void); |
111 | |
112 | #endif /* _RD_UNITTEST_H */ |
113 | |