1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391/**********************************************************************************************
*
* raylib-assert - Assertion library for raylib.
* https://github.com/robloach/raylib-assert
*
* Version: v2.0.0
*
* Copyright 2023 Rob Loach (@RobLoach)
*
* DEPENDENCIES:
* raylib 4.5+ https://www.raylib.com
*
* LICENSE: zlib/libpng
*
* raylib-assert is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software:
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/
#ifndef RAYLIB_ASSERT_H
#define RAYLIB_ASSERT_H
#ifdef __cplusplus
extern "C" {
#endif
// How to report failed assertions
#ifndef RAYLIB_ASSERT_LOG
/**
* The Trace Log Level used to report to TraceLog() on failed assertions. Defaults to LOG_FATAL.
*
* @example
* #define RAYLIB_ASSERT_LOG LOG_WARNING
*
* @see TraceLogLevel
*/
#define RAYLIB_ASSERT_LOG LOG_FATAL
#endif
// Define NDEBUG or RAYLIB_ASSERT_NDEBUG to skip assertions
#ifdef NDEBUG
#ifndef RAYLIB_ASSERT_NDEBUG
#define RAYLIB_ASSERT_NDEBUG
#endif
#endif
#ifndef RAYLIB_ASSERT_TRACELOG
/**
* The TraceLog() function to use.
*
* @see TraceLog()
*/
#define RAYLIB_ASSERT_TRACELOG TraceLog
#endif
#ifndef RAYLIB_ASSERT_TEXTFORMAT
/**
* The TextFormat() function to use when formating text.
*
* @see TextFormat()
*/
#define RAYLIB_ASSERT_TEXTFORMAT TextFormat
#endif
// Variadic Arguments
#define RAYLIB_ASSERT_CAT( A, B ) A ## B
#define RAYLIB_ASSERT_SELECT( NAME, NUM ) RAYLIB_ASSERT_CAT( NAME ## _, NUM )
#define RAYLIB_ASSERT_GET_COUNT( _1, _2, _3, _4, _5, _6, _7, RAYLIB_ASSERT_COUNT, ... ) RAYLIB_ASSERT_COUNT
#define RAYLIB_ASSERT_VA_SIZE( ... ) RAYLIB_ASSERT_GET_COUNT( __VA_ARGS__, 7, 6, 5, 4, 3, 2, 1 )
#define RAYLIB_ASSERT_VA_SELECT( NAME, ... ) RAYLIB_ASSERT_SELECT( NAME, RAYLIB_ASSERT_VA_SIZE(__VA_ARGS__) )(__VA_ARGS__)
/**
* Assert whether the given condition is true.
*
* @param condition The condition that is expected to be true.
* @param message (Optional) The message to provide on failed assertions.
* @param p1 (Optional) The first parameter in the message.
* @param p2 (Optional) The second parameter in the message.
* @param p3 (Optional) The third parameter in the message.
* @param p4 (Optional) The fourth parameter in the message.
* @param p5 (Optional) The fifth parameter in the message.
*/
#define Assert(...) RAYLIB_ASSERT_VA_SELECT(Assert, __VA_ARGS__)
/**
* Assert whether the two given parameters are equal.
*
* @param actual The actual value.
* @param expected The expected value.
* @param message (Optional) The message to provide on failed assertions.
* @param p1 (Optional) The first parameter in the message.
* @param p2 (Optional) The second parameter in the message.
* @param p3 (Optional) The third parameter in the message.
* @param p4 (Optional) The fourth parameter in the message.
*/
#define AssertEqual(...) RAYLIB_ASSERT_VA_SELECT(AssertEqual, __VA_ARGS__)
/**
* Assert whether the given condition is false.
*
* @param condition The condition that is expected to be false.
* @param message (Optional) The message to provide on failed assertions.
* @param p1 (Optional) The first parameter in the message.
* @param p2 (Optional) The second parameter in the message.
* @param p3 (Optional) The third parameter in the message.
* @param p4 (Optional) The fourth parameter in the message.
* @param p5 (Optional) The fifth parameter in the message.
*/
#define AssertNot(...) RAYLIB_ASSERT_VA_SELECT(AssertNot, __VA_ARGS__)
/**
* Assert whether the two given parameters are not equal.
*
* @param actual The actual value.
* @param notexpected The expected value that shouldn't equal the actual value.
* @param message (Optional) The message to provide on failed assertions.
* @param p1 (Optional) The first parameter in the message.
* @param p2 (Optional) The second parameter in the message.
* @param p3 (Optional) The third parameter in the message.
* @param p4 (Optional) The fourth parameter in the message.
*/
#define AssertNotEqual(...) RAYLIB_ASSERT_VA_SELECT(AssertNotEqual, __VA_ARGS__)
/**
* Sets a failed assertion, with the given message.
*
* @param message (Optional) The message to provide for the failed assertion.
* @param p1 (Optional) The first parameter in the message.
* @param p2 (Optional) The second parameter in the message.
* @param p3 (Optional) The third parameter in the message.
* @param p4 (Optional) The fourth parameter in the message.
* @param p5 (Optional) The fifth parameter in the message.
* @param p6 (Optional) The sixth parameter in the message.
*/
#define AssertFail(...) RAYLIB_ASSERT_VA_SELECT(AssertFail, __VA_ARGS__)
/**
* Assert whether an image is loaded.
*
* @param image The image to check for valid data.
* @param message (Optional) The message to provide on failed assertions.
* @param p1 (Optional) The first parameter in the message.
* @param p2 (Optional) The second parameter in the message.
* @param p3 (Optional) The third parameter in the message.
* @param p4 (Optional) The fourth parameter in the message.
* @param p5 (Optional) The fifth parameter in the message.
*/
#define AssertImage(...) RAYLIB_ASSERT_VA_SELECT(AssertImage, __VA_ARGS__)
/**
* Assert whether two images are the same.
*
* @param image1 The first image to check is equal to the second.
* @param image2 The second image to check is equal to the first.
* @param message (Optional) The message to provide on failed assertions.
* @param p1 (Optional) The first parameter in the message.
* @param p2 (Optional) The second parameter in the message.
* @param p3 (Optional) The third parameter in the message.
* @param p4 (Optional) The fourth parameter in the message.
*/
#define AssertImageSame(...) RAYLIB_ASSERT_VA_SELECT(AssertImageSame, __VA_ARGS__)
/**
* Assert whether two colors are the same.
*
* @param color1 The first color to check.
* @param color2 The second color to check.
* @param message (Optional) The message to provide on failed assertions.
* @param p1 (Optional) The first parameter in the message.
* @param p2 (Optional) The second parameter in the message.
* @param p3 (Optional) The third parameter in the message.
* @param p4 (Optional) The fourth parameter in the message.
*/
#define AssertColorSame(...) RAYLIB_ASSERT_VA_SELECT(AssertColorSame, __VA_ARGS__)
/**
* Assert whether two Vector2s are the same.
*
* @param vector1 The first Vector2 to check.
* @param vector2 The second Vector2 to check.
* @param message (Optional) The message to provide on failed assertions.
* @param p1 (Optional) The first parameter in the message.
* @param p2 (Optional) The second parameter in the message.
* @param p3 (Optional) The third parameter in the message.
* @param p4 (Optional) The fourth parameter in the message.
*/
#define AssertVector2Same(...) RAYLIB_ASSERT_VA_SELECT(AssertVector2Same, __VA_ARGS__)
// Assert()
#ifdef RAYLIB_ASSERT_NDEBUG
#define Assert_0()
#define Assert_1(condition)
#define Assert_2(condition, message)
#define Assert_3(condition, message, p1)
#define Assert_4(condition, message, p1, p2)
#define Assert_5(condition, message, p1, p2, p3)
#define Assert_6(condition, message, p1, p2, p3, p4)
#define Assert_7(condition, message, p1, p2, p3, p4, p5)
#else
#define Assert_0() AssertFail_1("No condition provided for Assert()")
#define Assert_1(condition) Assert_2(condition, #condition)
#define Assert_2(condition, message) do { if (!((bool)(condition))) { RAYLIB_ASSERT_TRACELOG(RAYLIB_ASSERT_LOG, "ASSERT: %s (%s:%i)", message, __FILE__, __LINE__); } } while(0)
#define Assert_3(condition, message, p1) Assert_2(condition, RAYLIB_ASSERT_TEXTFORMAT(message, p1))
#define Assert_4(condition, message, p1, p2) Assert_2(condition, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2))
#define Assert_5(condition, message, p1, p2, p3) Assert_2(condition, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3))
#define Assert_6(condition, message, p1, p2, p3, p4) Assert_2(condition, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3, p4))
#define Assert_7(condition, message, p1, p2, p3, p4, p5) Assert_2(condition, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3, p4, p5))
#endif
// AssertEqual()
#define AssertEqual_0() AssertFail_1("No condition provided for AssertEqual()")
#define AssertEqual_1(condition) Assert_2(condition, #condition)
#define AssertEqual_2(actual, expected) Assert_4((actual) == (expected), "AssertEqual(%s, %s) - Provided arguments are not equal", #actual, #expected)
#define AssertEqual_3(actual, expected, message) Assert_2((actual) == (expected), message)
#define AssertEqual_4(actual, expected, message, p1) Assert_3((actual) == (expected), message, p1)
#define AssertEqual_5(actual, expected, message, p1, p2) Assert_4((actual) == (expected), message, p1, p2)
#define AssertEqual_6(actual, expected, message, p1, p2, p3) Assert_5((actual) == (expected), message, p1, p2, p3)
#define AssertEqual_7(actual, expected, message, p1, p2, p3, p4) Assert_6((actual) == (expected), message, p1, p2, p3, p4)
// AssertNotEqual()
#define AssertNotEqual_0() AssertFail_1("No condition provided for AssertNotEqual()")
#define AssertNotEqual_1(condition) AssertNot_2(condition, #condition)
#define AssertNotEqual_2(actual, expected) Assert_4((actual) != (expected), "AssertNotEqual(%s, %s) - Provided arguments are equal", #actual, #expected)
#define AssertNotEqual_3(actual, expected, message) Assert_2((actual) != (expected), message)
#define AssertNotEqual_4(actual, expected, message, p1) Assert_3((actual) != (expected), message, p1)
#define AssertNotEqual_5(actual, expected, message, p1, p2) Assert_4((actual) != (expected), message, p1, p2)
#define AssertNotEqual_6(actual, expected, message, p1, p2, p3) Assert_5((actual) != (expected), message, p1, p2, p3)
#define AssertNotEqual_7(actual, expected, message, p1, p2, p3, p4) Assert_6((actual) != (expected), message, p1, p2, p3, p4)
// AssertNot()
#define AssertNot_0() AssertFail_1("No condition provided for AssertNot()")
#define AssertNot_1(condition) Assert_2(!(bool)(condition), #condition)
#define AssertNot_2(condition, message) Assert_2(!(bool)(condition), message)
#define AssertNot_3(condition, message, p1) Assert_3(!(bool)(condition), message, p1)
#define AssertNot_4(condition, message, p1, p2) Assert_4(!(bool)(condition), message, p1, p2)
#define AssertNot_5(condition, message, p1, p2, p3) Assert_5(!(bool)(condition), message, p1, p2, p3)
#define AssertNot_6(condition, message, p1, p2, p3, p4) Assert_6(!(bool)(condition), message, p1, p2, p3, p4)
#define AssertNot_7(condition, message, p1, p2, p3, p4, p5) Assert_7(!(bool)(condition), message, p1, p2, p3, p4, p5)
// AssertFail()
#ifdef RAYLIB_ASSERT_NDEBUG
#define AssertFail_0()
#define AssertFail_1(message)
#define AssertFail_2(message, p1)
#define AssertFail_3(message, p1, p2)
#define AssertFail_4(message, p1, p2, p3)
#define AssertFail_5(message, p1, p2, p3, p4)
#define AssertFail_6(message, p1, p2, p3, p4, p5)
#define AssertFail_7(message, p1, p2, p3, p4, p5, p6)
#else
#define AssertFail_0() RAYLIB_ASSERT_TRACELOG(RAYLIB_ASSERT_LOG, "ASSERT: AssertFail() (%s:%i)", __FILE__, __LINE__)
#define AssertFail_1(message) RAYLIB_ASSERT_TRACELOG(RAYLIB_ASSERT_LOG, "ASSERT: %s (%s:%i)", message, __FILE__, __LINE__)
#define AssertFail_2(message, p1) AssertFail_1(RAYLIB_ASSERT_TEXTFORMAT(message, p1))
#define AssertFail_3(message, p1, p2) AssertFail_1(RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2))
#define AssertFail_4(message, p1, p2, p3) AssertFail_1(RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3))
#define AssertFail_5(message, p1, p2, p3, p4) AssertFail_1(RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3, p4))
#define AssertFail_6(message, p1, p2, p3, p4, p5) AssertFail_1(RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3, p4, p5))
#define AssertFail_7(message, p1, p2, p3, p4, p5, p6) AssertFail_1(RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3, p4, p5, p6))
#endif
// AssertImage()
#define AssertImage_0() AssertFail_1("No image provided for AssertImage()")
#define AssertImage_1(image) Assert_3(IsImageReady(image), "AssertImage(%s) - Image not loaded", #image)
#define AssertImage_2(image, message) Assert_2(IsImageReady(image), message)
#define AssertImage_3(image, message, p1) Assert_3(IsImageReady(image), message, p1)
#define AssertImage_4(image, message, p1, p2) Assert_4(IsImageReady(image), message, p1, p2)
#define AssertImage_5(image, message, p1, p2, p3) Assert_5(IsImageReady(image), message, p1, p2, p3)
#define AssertImage_6(image, message, p1, p2, p3, p4) Assert_6(IsImageReady(image), message, p1, p2, p3, p4)
// AssertTexture()
#define AssertTexture_0() AssertFail_1("No texture provided for AssertTexture()")
#define AssertTexture_1(texture) Assert_3(IsTextureReady(texture), "AssertTexture(%s) - Texture not loaded", #texture)
#define AssertTexture_2(texture, message) Assert_2(IsTextureReady(texture), message)
#define AssertTexture_3(texture, message, p1) Assert_3(IsTextureReady(texture), message, p1)
#define AssertTexture_4(texture, message, p1, p2) Assert_4(IsTextureReady(texture), message, p1, p2)
#define AssertTexture_5(texture, message, p1, p2, p3) Assert_5(IsTextureReady(texture), message, p1, p2, p3)
#define AssertTexture_6(texture, message, p1, p2, p3, p4) Assert_6(IsTextureReady(texture), message, p1, p2, p3, p4)
// AssertImageSame()
#ifdef RAYLIB_ASSERT_NDEBUG
#define AssertImageSame_0()
#define AssertImageSame_1(image)
#define AssertImageSame_2(image1, image2)
#define AssertImageSame_3(image1, image2, message)
#define AssertImageSame_4(image1, image2, message, p1)
#define AssertImageSame_5(image1, image2, message, p1, p2)
#define AssertImageSame_6(image1, image2, message, p1, p2, p3)
#define AssertImageSame_7(image1, image2, message, p1, p2, p3, p4)
#else
#define AssertImageSame_0() AssertFail_1("AssertImageSame(): No images provided to AssertImageSame(), expected 2")
#define AssertImageSame_1(image) AssertFail_1("Only one image provided for AssertImageSame()")
#define AssertImageSame_2(image1, image2) AssertImageSame_5(image1, image2, "AssertImageSame(%s, %s) - Images do not match", #image1, #image2)
#define AssertImageSame_3(image1, image2, message) do { \
if (image1.width != image2.width || image1.height != image2.height || image1.format != image2.format) { \
AssertFail_1(message); \
break; \
} \
Color* colors1 = LoadImageColors(image1); \
Color* colors2 = LoadImageColors(image2); \
bool failure = false; \
for (int i = 0; i < image1.width * image1.height; i++) { \
Color color1 = colors1[i]; \
Color color2 = colors2[i]; \
if (color1.r != color2.r || color1.g != color2.g || color1.b != color2.b || color1.a != color2.a) { \
failure = true; \
break; \
} \
} \
UnloadImageColors(colors1); \
UnloadImageColors(colors2); \
if (failure) { \
AssertFail_1(message); \
} \
} while(0)
#define AssertImageSame_4(image1, image2, message, p1) AssertImageSame_3(image1, image2, RAYLIB_ASSERT_TEXTFORMAT(message, p1))
#define AssertImageSame_5(image1, image2, message, p1, p2) AssertImageSame_3(image1, image2, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2))
#define AssertImageSame_6(image1, image2, message, p1, p2, p3) AssertImageSame_3(image1, image2, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3))
#define AssertImageSame_7(image1, image2, message, p1, p2, p3, p4) AssertImageSame_3(image1, image2, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3, p4))
#endif
// AssertColorSame()
#ifdef RAYLIB_ASSERT_NDEBUG
#define AssertColorSame_0()
#define AssertColorSame_1(color)
#define AssertColorSame_2(color1, color2)
#define AssertColorSame_3(color1, color2, message)
#define AssertColorSame_4(color1, color2, message, p1)
#define AssertColorSame_5(color1, color2, message, p1, p2)
#define AssertColorSame_6(color1, color2, message, p1, p2, p3)
#define AssertColorSame_7(color1, color2, message, p1, p2, p3, p4)
#else
#define AssertColorSame_0() AssertFail_1("Colors not provided to AssertColorSame()")
#define AssertColorSame_1(color) AssertFail_1("Expected two colors for AssertColorSame()")
#define AssertColorSame_2(color1, color2) AssertColorSame_5(color1, color2, "AssertColorSame(%s, %s) - Colors do not match", #color1, #color2)
#define AssertColorSame_3(color1, color2, message) do { \
if (color1.r != color2.r || color1.g != color2.g || color1.b != color2.b || color1.a != color2.a) { \
AssertFail_1(message); \
}\
} while (0)
#define AssertColorSame_4(color1, color2, message, p1) AssertColorSame_3(color1, color2, RAYLIB_ASSERT_TEXTFORMAT(message, p1))
#define AssertColorSame_5(color1, color2, message, p1, p2) AssertColorSame_3(color1, color2, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2))
#define AssertColorSame_6(color1, color2, message, p1, p2, p3) AssertColorSame_3(color1, color2, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3))
#define AssertColorSame_7(color1, color2, message, p1, p2, p3, p4) AssertColorSame_3(color1, color2, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3, p4))
#endif
// AssertVector2Same()
#ifdef RAYLIB_ASSERT_NDEBUG
#define AssertVector2Same_0()
#define AssertVector2Same_1(vector)
#define AssertVector2Same_2(vector1, vector2)
#define AssertVector2Same_3(vector1, vector2, message)
#define AssertVector2Same_4(vector1, vector2, message, p1)
#define AssertVector2Same_5(vector1, vector2, message, p1, p2)
#define AssertVector2Same_6(vector1, vector2, message, p1, p2, p3)
#define AssertVector2Same_7(vector1, vector2, message, p1, p2, p3, p4)
#else
#define AssertVector2Same_0() AssertFail_1("Vectors not provided to AssertVector2Same()")
#define AssertVector2Same_1(vector) AssertFail_1("Expected two vectors for AssertVector2Same()")
#define AssertVector2Same_2(vector1, vector2) AssertVector2Same_5(vector1, vector2, "AssertVector2Same(%s, %s) - vectors do not match", #vector1, #vector2)
#define AssertVector2Same_3(vector1, vector2, message) do { \
if (vector1.x != vector2.x || vector1.y != vector2.y) { \
AssertFail_1(message); \
}\
} while (0)
#define AssertVector2Same_4(vector1, vector2, message, p1) AssertVector2Same_3(vector1, vector2, RAYLIB_ASSERT_TEXTFORMAT(message, p1))
#define AssertVector2Same_5(vector1, vector2, message, p1, p2) AssertVector2Same_3(vector1, vector2, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2))
#define AssertVector2Same_6(vector1, vector2, message, p1, p2, p3) AssertVector2Same_3(vector1, vector2, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3))
#define AssertVector2Same_7(vector1, vector2, message, p1, p2, p3, p4) AssertVector2Same_3(vector1, vector2, RAYLIB_ASSERT_TEXTFORMAT(message, p1, p2, p3, p4))
#endif
#ifdef __cplusplus
}
#endif
#endif // RAYLIB_ASSERT_H