|
| 1 | +/********************************************************************************************** |
| 2 | +* |
| 3 | +* raylib-assert - Assertion library for raylib. |
| 4 | +* https://github.com/robloach/raylib-assert |
| 5 | +* |
| 6 | +* Copyright 2021 Rob Loach (@RobLoach) |
| 7 | +* |
| 8 | +* DEPENDENCIES: |
| 9 | +* raylib https://www.raylib.com/ |
| 10 | +* |
| 11 | +* LICENSE: zlib/libpng |
| 12 | +* |
| 13 | +* raylib-assert is licensed under an unmodified zlib/libpng license, which is an OSI-certified, |
| 14 | +* BSD-like license that allows static linking with closed source software: |
| 15 | +* |
| 16 | +* This software is provided "as-is", without any express or implied warranty. In no event |
| 17 | +* will the authors be held liable for any damages arising from the use of this software. |
| 18 | +* |
| 19 | +* Permission is granted to anyone to use this software for any purpose, including commercial |
| 20 | +* applications, and to alter it and redistribute it freely, subject to the following restrictions: |
| 21 | +* |
| 22 | +* 1. The origin of this software must not be misrepresented; you must not claim that you |
| 23 | +* wrote the original software. If you use this software in a product, an acknowledgment |
| 24 | +* in the product documentation would be appreciated but is not required. |
| 25 | +* |
| 26 | +* 2. Altered source versions must be plainly marked as such, and must not be misrepresented |
| 27 | +* as being the original software. |
| 28 | +* |
| 29 | +* 3. This notice may not be removed or altered from any source distribution. |
| 30 | +* |
| 31 | +**********************************************************************************************/ |
| 32 | + |
| 33 | +#ifndef RAYLIB_ASSERT_H |
| 34 | +#define RAYLIB_ASSERT_H |
| 35 | + |
| 36 | +#ifdef __cplusplus |
| 37 | +extern "C" { |
| 38 | +#endif |
| 39 | + |
| 40 | +#include "raylib.h" // NOLINT |
| 41 | + |
| 42 | +// How to report failed assertions |
| 43 | +#ifndef RAYLIB_ASSERT_LOG |
| 44 | +#define RAYLIB_ASSERT_LOG LOG_FATAL |
| 45 | +#endif |
| 46 | + |
| 47 | +// Define NDEBUG or RAYLIB_ASSERT_NDEBUG to skip assertions |
| 48 | +#ifdef NDEBUG |
| 49 | +#ifndef RAYLIB_ASSERT_NDEBUG |
| 50 | +#define RAYLIB_ASSERT_NDEBUG |
| 51 | +#endif |
| 52 | +#endif |
| 53 | + |
| 54 | +// Variadic Arguments |
| 55 | +#define RAYLIB_ASSERT_CAT( A, B ) A ## B |
| 56 | +#define RAYLIB_ASSERT_SELECT( NAME, NUM ) RAYLIB_ASSERT_CAT( NAME ## _, NUM ) |
| 57 | +#define RAYLIB_ASSERT_GET_COUNT( _1, _2, _3, _4, _5, _6, _7, RAYLIB_ASSERT_COUNT, ... ) RAYLIB_ASSERT_COUNT |
| 58 | +#define RAYLIB_ASSERT_VA_SIZE( ... ) RAYLIB_ASSERT_GET_COUNT( __VA_ARGS__, 7, 6, 5, 4, 3, 2, 1 ) |
| 59 | +#define RAYLIB_ASSERT_VA_SELECT( NAME, ... ) RAYLIB_ASSERT_SELECT( NAME, RAYLIB_ASSERT_VA_SIZE(__VA_ARGS__) )(__VA_ARGS__) |
| 60 | + |
| 61 | +#define Assert(...) RAYLIB_ASSERT_VA_SELECT( Assert, __VA_ARGS__ ) |
| 62 | +#define AssertEqual(...) RAYLIB_ASSERT_VA_SELECT( AssertEqual, __VA_ARGS__ ) |
| 63 | +#define AssertNot(...) RAYLIB_ASSERT_VA_SELECT( AssertNot, __VA_ARGS__ ) |
| 64 | +#define AssertFail(...) RAYLIB_ASSERT_VA_SELECT( AssertFail, __VA_ARGS__ ) |
| 65 | + |
| 66 | +// Assert() |
| 67 | +#ifdef RAYLIB_ASSERT_NDEBUG |
| 68 | +#define Assert_1(condition) |
| 69 | +#define Assert_2(condition, message) |
| 70 | +#define Assert_3(condition, message, p1) |
| 71 | +#define Assert_4(condition, message, p1, p2) |
| 72 | +#define Assert_5(condition, message, p1, p2, p3) |
| 73 | +#define Assert_6(condition, message, p1, p2, p3, p4) |
| 74 | +#define Assert_7(condition, message, p1, p2, p3, p4, p5) |
| 75 | +#else |
| 76 | +#define Assert_1(condition) Assert_2(condition, #condition) |
| 77 | +#define Assert_2(condition, message) if (!((bool)(condition))) { TraceLog(RAYLIB_ASSERT_LOG, "ASSERT: %s (%s:%i)", message, __FILE__, __LINE__); } |
| 78 | +#define Assert_3(condition, message, p1) Assert_2(condition, TextFormat(message, p1)) |
| 79 | +#define Assert_4(condition, message, p1, p2) Assert_2(condition, TextFormat(message, p1, p2)) |
| 80 | +#define Assert_5(condition, message, p1, p2, p3) Assert_2(condition, TextFormat(message, p1, p2, p3)) |
| 81 | +#define Assert_6(condition, message, p1, p2, p3, p4) Assert_2(condition, TextFormat(message, p1, p2, p3, p4)) |
| 82 | +#define Assert_7(condition, message, p1, p2, p3, p4, p5) Assert_2(condition, TextFormat(message, p1, p2, p3, p4, p5)) |
| 83 | +#endif |
| 84 | + |
| 85 | +// AssertEqual() |
| 86 | +#define AssertEqual_1(condition) Assert_2(condition, #condition) |
| 87 | +#define AssertEqual_2(actual, expected) Assert_4((actual) == (expected), "%s == %s", #actual, #expected) |
| 88 | +#define AssertEqual_3(actual, expected, message) Assert_2((actual) == (expected), message) |
| 89 | +#define AssertEqual_4(actual, expected, message, p1) Assert_3((actual) == (expected), message, p1) |
| 90 | +#define AssertEqual_5(actual, expected, message, p1, p2) Assert_4((actual) == (expected), message, p1, p2) |
| 91 | +#define AssertEqual_6(actual, expected, message, p1, p2, p3) Assert_5((actual) == (expected), message, p1, p2, p3) |
| 92 | +#define AssertEqual_7(actual, expected, message, p1, p2, p3, p4) Assert_6((actual) == (expected), message, p1, p2, p3, p4) |
| 93 | + |
| 94 | +// AssertNot() |
| 95 | +#define AssertNot_1(condition) Assert_2(!(bool)(condition), #condition) |
| 96 | +#define AssertNot_2(condition, message) Assert_2(!(bool)(condition), message) |
| 97 | +#define AssertNot_3(condition, message, p1) Assert_3(!(bool)(condition), message, p1) |
| 98 | +#define AssertNot_4(condition, message, p1, p2) Assert_4(!(bool)(condition), message, p1, p2) |
| 99 | +#define AssertNot_5(condition, message, p1, p2, p3) Assert_5(!(bool)(condition), message, p1, p2, p3) |
| 100 | +#define AssertNot_6(condition, message, p1, p2, p3, p4) Assert_6(!(bool)(condition), message, p1, p2, p3, p4) |
| 101 | +#define AssertNot_7(condition, message, p1, p2, p3, p4, p5) Assert_7(!(bool)(condition), message, p1, p2, p3, p4, p5) |
| 102 | + |
| 103 | +// AssertFail() |
| 104 | +#ifdef RAYLIB_ASSERT_NDEBUG |
| 105 | +#define AssertFail_0() |
| 106 | +#define AssertFail_1(message) |
| 107 | +#define AssertFail_2(message, p1) |
| 108 | +#define AssertFail_3(message, p1, p2) |
| 109 | +#define AssertFail_4(message, p1, p2, p3) |
| 110 | +#define AssertFail_5(message, p1, p2, p3, p4) |
| 111 | +#define AssertFail_6(message, p1, p2, p3, p4, p5) |
| 112 | +#define AssertFail_7(message, p1, p2, p3, p4, p5, p6) |
| 113 | +#else |
| 114 | +#define AssertFail_0() TraceLog(RAYLIB_ASSERT_LOG, "ASSERT: AssertFail() (%s:%i)", __FILE__, __LINE__) |
| 115 | +#define AssertFail_1(message) TraceLog(RAYLIB_ASSERT_LOG, "ASSERT: %s (%s:%i)", message, __FILE__, __LINE__) |
| 116 | +#define AssertFail_2(message, p1) AssertFail_1(TextFormat(message, p1)) |
| 117 | +#define AssertFail_3(message, p1, p2) AssertFail_1(TextFormat(message, p1, p2)) |
| 118 | +#define AssertFail_4(message, p1, p2, p3) AssertFail_1(TextFormat(message, p1, p2, p3)) |
| 119 | +#define AssertFail_5(message, p1, p2, p3, p4) AssertFail_1(TextFormat(message, p1, p2, p3, p4)) |
| 120 | +#define AssertFail_6(message, p1, p2, p3, p4, p5) AssertFail_1(TextFormat(message, p1, p2, p3, p4, p5)) |
| 121 | +#define AssertFail_7(message, p1, p2, p3, p4, p5, p6) AssertFail_1(TextFormat(message, p1, p2, p3, p4, p5, p6)) |
| 122 | +#endif |
| 123 | + |
| 124 | +// AssertBreakpoint() |
| 125 | +#define AssertBreakpoint() AssertFail_1("AssertBreakpoint()") |
| 126 | + |
| 127 | +#ifdef __cplusplus |
| 128 | +} |
| 129 | +#endif |
| 130 | + |
| 131 | +#endif // RAYLIB_ASSERT_H |
0 commit comments