Skip to content

Commit c98a47a

Browse files
committed
Use raylib-assert - fixes #163
1 parent dc393b3 commit c98a47a

File tree

4 files changed

+175
-22
lines changed

4 files changed

+175
-22
lines changed

include/Image.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,13 @@ class Image : public ::Image {
391391
return *this;
392392
}
393393

394+
/**
395+
* Crop an image to a new given width and height.
396+
*/
397+
inline Image& Crop(int newWidth, int newHeight) {
398+
return Crop(0, 0, newWidth, newHeight);
399+
}
400+
394401
/**
395402
* Crop an image to a new given width and height based on a vector.
396403
*/

tests/raylib-assert.h

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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

tests/raylib_cpp_test.cpp

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include <cassert>
21
#include <string>
32
#include <vector>
43
#include "raylib-cpp.hpp"
4+
#include "raylib-assert.h"
55

66
int main(int argc, char *argv[]) {
7+
TraceLog(LOG_INFO, "---------------------");
78
TraceLog(LOG_INFO, "TEST: raylib-cpp test");
89

910
// Get a path to where the executable is so file loading is relative.
@@ -15,74 +16,88 @@ int main(int argc, char *argv[]) {
1516
// Vector
1617
{
1718
raylib::Vector2 position(50, 100);
18-
assert(position.GetX() == position.x);
19+
AssertEqual(position.GetX(), position.x);
1920
position.x = 150;
20-
assert(position.GetX() == 150);
21+
AssertEqual(position.GetX(), 150);
2122
position.SetX(300);
22-
assert(position.GetX() == 300);
23+
AssertEqual(position.GetX(), 300);
2324

2425
raylib::Vector2 speed(10, 10);
2526
position += speed;
26-
assert(position.x == 310);
27-
assert(raylib::Window::IsReady() == false);
27+
AssertEqual(position.x, 310);
28+
AssertEqual(raylib::Window::IsReady(), false);
2829

2930
raylib::Vector2 size{50,50};
3031
raylib::Vector2 halfsize = size / 2.0f;
3132

32-
assert(size.x == 50);
33-
assert(halfsize.x == 25);
33+
AssertEqual(size.x, 50);
34+
AssertEqual(halfsize.x, 25);
3435

3536
raylib::Vector2 doublesize = size * 2.0f;
36-
assert(size.x == 50);
37-
assert(doublesize.x == 100);
37+
AssertEqual(size.x, 50);
38+
AssertEqual(doublesize.x, 100);
3839
}
3940

4041
// Color
4142
{
4243
raylib::Color color = RED;
43-
assert(color.ToInt() == ::ColorToInt(RED));
44+
AssertEqual(color.ToInt(), ::ColorToInt(RED));
4445
color = RAYWHITE;
45-
assert(color.r == RAYWHITE.r);
46+
::Color raylibColor = RAYWHITE;
47+
AssertEqual(color.r, raylibColor.r);
4648
}
4749

4850
// Math
4951
{
5052
raylib::Vector2 direction(50, 50);
5153
raylib::Vector2 newDirection = direction.Rotate(30);
52-
assert(((int)newDirection.x) == 57);
54+
AssertEqual((int)newDirection.x, 57);
55+
}
56+
57+
// Image
58+
{
59+
// Loading
60+
raylib::Image image(path + "/resources/feynman.png");
61+
Assert(image.IsReady());
62+
63+
// Chaining
64+
image.Crop(100, 100)
65+
.Resize(50, 50);
66+
AssertEqual(image.GetWidth(), 50);
67+
AssertEqual(image.GetHeight(), 50);
5368
}
5469

5570
// raylib::GetDirectoryFiles()
5671
{
5772
std::vector<std::string> files = raylib::GetDirectoryFiles(::GetWorkingDirectory());
58-
assert(files.size() > 3);
73+
Assert(files.size() > 3);
5974
}
6075

6176
// raylib::TextReplace()
6277
{
6378
std::string input = "Hello World!";
6479
std::string output = raylib::TextReplace(input, "World", "Moon");
65-
assert(output == "Hello Moon!");
80+
AssertEqual(output, "Hello Moon!");
6681
}
6782

6883
// raylib::TextInsert()
6984
{
7085
std::string input = "Hello World!";
7186
std::string output = raylib::TextInsert(input, "Good!", 0);
72-
assert(output == "Good! World!");
87+
AssertEqual(output, "Good! World!");
7388
}
7489

7590
// raylib::TextSubtext()
7691
{
7792
std::string input = "Hello World!";
7893
std::string output = raylib::TextSubtext(input, 6, 5);
79-
assert(output == "World");
94+
AssertEqual(output, "World");
8095
}
8196

82-
// Sound
97+
// Wave
8398
{
8499
raylib::Wave wave(path + "/resources/weird.wav");
85-
assert(wave.IsReady());
100+
Assert(wave.IsReady(), "Expected wave to be loaded correctly");
86101
}
87102

88103
// RaylibException
@@ -95,10 +110,10 @@ int main(int argc, char *argv[]) {
95110
error.TraceLog(LOG_INFO);
96111
passed = true;
97112
}
98-
99-
assert(passed);
113+
Assert(passed, "Expected to have a RaylibException to be thrown");
100114
}
101115

102-
TraceLog(LOG_INFO, "TEST: raylib-cpp test complete");
116+
TraceLog(LOG_INFO, "TEST: raylib-cpp test");
117+
TraceLog(LOG_INFO, "---------------------");
103118
return 0;
104119
}

tests/resources/feynman.png

41.3 KB
Loading

0 commit comments

Comments
 (0)