Added reasons to assertions

This commit is contained in:
2023-07-23 10:15:37 -07:00
parent ef6b269141
commit 5b01eb904d
78 changed files with 357 additions and 289 deletions

View File

@ -10,31 +10,61 @@
#if ASSERTS_ENABLED == 0
#elif ASSERTS_ENABLED == 1
void assertTrue(bool_t x) {
void assertTrue(bool_t x, const char message[]) {
if(x != true) {
throw "Assertion Failed";
std::cout << message << std::endl;
throw message;
free(0);
}
assert(x == true);
}
void assertTrue(bool_t x, std::string message) {
assertTrue(x, message.c_str());
}
void assertFalse(bool_t x) {
assertTrue(!x);
void assertFalse(bool_t x, const char message[]) {
assertTrue(!x, message);
}
void assertUnreachable() {
assertTrue(false);
void assertFalse(bool_t x, std::string message) {
assertFalse(x, message.c_str());
}
void assertNotNull(void *pointer) {
assertTrue(pointer != nullptr && pointer != NULL);
void assertUnreachable(const char message[]) {
assertTrue(false, message);
}
void assertNull(void *pointer) {
assertTrue(pointer == NULL || pointer == nullptr);
void assertUnreachable(std::string message) {
assertUnreachable(message.c_str());
}
void assertDeprecated() {
assertUnreachable();
void assertNotNull(void *pointer, const char message[]) {
assertTrue(pointer != nullptr && pointer != NULL, message);
}
void assertNotNull(void *pointer, std::string message) {
assertNotNull(pointer, message.c_str());
}
void assertNull(void *pointer, const char message[]) {
assertTrue(pointer == NULL || pointer == nullptr, message);
}
void assertNull(void *pointer, std::string message) {
assertNull(pointer, message.c_str());
}
void assertDeprecated(const char message[]) {
assertUnreachable(message);
}
void assertDeprecated(std::string message) {
assertDeprecated(message.c_str());
}
#endif

View File

@ -19,47 +19,72 @@ static inline void assertTrue(bool_t x) {}
/**
* Assert a given value to be true.
* @param x Value to assert as true.
* @param message Message to throw against assertion failure.
*/
void assertTrue(bool_t x);
void assertTrue(bool_t x, const char message[]);
void assertTrue(bool_t x, std::string message);
/**
* Asserts a given statement to be false.
* @param x Value to assert as false.
* @param message Message to throw against assertion failure.
*/
void assertFalse(bool_t x);
void assertFalse(bool_t x, const char message[]);
void assertFalse(bool_t x, std::string message);
/**
* Asserts that a given line of code is unreachable. Essentially a forced
* assertion failure, good for "edge cases"
* @param message Message to throw against assertion failure.
*/
void assertUnreachable();
void assertUnreachable(const char message[]);
void assertUnreachable(std::string message);
/**
* Assert a given pointer to not point to a null pointer.
* @param pointer Pointer to assert is not a null pointer.
* @param message Message to throw against assertion failure.
*/
void assertNotNull(void *pointer);
void assertNotNull(void *pointer, const char message[]);
void assertNotNull(void *pointer, std::string message);
/**
* Asserts a given pointer to be a nullptr.
* @param pointer Pointer to assert is nullptr.
* @param message Message to throw against assertion failure.
*/
void assertNull(void *pointer);
void assertNull(void *pointer, const char message[]);
void assertNull(void *pointer, std::string message);
/**
* Asserts a function as being deprecated.
* @param message Message to throw against assertion failure.
*/
void assertDeprecated();
void assertDeprecated(const char message[]);
void assertDeprecated(std::string message);
/**
* Asserts that a given map has a key.
*
* @param map Map to check.
* @param key Key to try and assert exists.
* @param message Message to throw against assertion failure.
*/
template<typename K, typename V>
void assertMapHasKey(std::map<K,V> map, K key) {
assertTrue(map.find(key) != map.end());
void assertMapHasKey(std::map<K,V> map, K key, const char message[]) {
assertTrue(map.find(key) != map.end(), message);
}
/**
* Asserts that a given map has a key.
*
* @param map Map to check.
* @param key Key to try and assert exists.
* @param message Message to throw against assertion failure.
*/
template<typename K, typename V>
void assertMapHasKey(std::map<K,V> map, K key, std::string message) {
assertMapHasKey(map, key, message.c_str());
}
#else

View File

@ -35,6 +35,6 @@ struct Color Color::fromString(std::string str) {
}
// TODO: Parse other kinds of colors
assertUnreachable();
assertUnreachable("Failed to find a color match for " + str);
return {};
}

View File

@ -11,7 +11,7 @@ UsageLock::UsageLock() : UsageLock(&this->internalPool) {
}
UsageLock::UsageLock(usagelockid_t *lockPool) {
assertNotNull(lockPool);
assertNotNull(lockPool, "Lock pool cannot be null");
this->pool = lockPool;
this->onEmpty = []() {};
this->onFirstLock = []() {};

View File

@ -199,7 +199,7 @@ void Xml::load(Xml *xml, std::string data, size_t *j) {
while(c = data[i++]) {
xml->innerXml += c;
if(c == ';') break;
if(c == '<') assertUnreachable();//Invalid XML
if(c == '<') assertUnreachable("Error decrypting XML/HTML Special Char, encountered end of Node?");//Invalid XML
sc += c;
}
@ -221,8 +221,7 @@ void Xml::load(Xml *xml, std::string data, size_t *j) {
int code = std::stoi(sc.substr(1));
buffer += (char)code;
} else {
std::cout << "Unknown Special character: " << sc << std::endl;
assertUnreachable();
assertUnreachable("Unknown Special character: " + sc);
}
}
} else {

View File

@ -16,8 +16,8 @@ namespace Dawn {
*/
template<typename T>
void vectorAppend(std::vector<T> *list, std::vector<T> *append) {
assertNotNull(list);
assertNotNull(append);
assertNotNull(list, "vectorAppend: list cannot be null");
assertNotNull(append, "vectorAppend: append cannot be null");
auto it = append->begin();
while(it != append->end()) {
@ -34,7 +34,7 @@ namespace Dawn {
*/
template<typename T>
void vectorAppend(std::vector<T> *list, std::vector<T> append) {
assertNotNull(list);
assertNotNull(list, "vectorAppend: list cannot be null");
auto it = append.begin();
while(it != append.end()) {

View File

@ -21,12 +21,12 @@
* @return Pointer to the space in memory to use.
*/
static inline void * memoryAllocate(const size_t size) {
assertTrue(size > 0);
assertTrue(size > 0, "memoryAllocate: size must be greater than 0");
#if DAWN_DEBUG_BUILD
dawnAllocatedItemCount++;
#endif
auto x = (void *)malloc(size);
assertNotNull(x);
assertNotNull(x, "memoryAllocate: Failed to allocate memory");
return x;
}
@ -37,12 +37,12 @@ static inline void * memoryAllocate(const size_t size) {
* @return Pointer to the space in memory to use.
*/
static inline void * memoryFillWithZero(const size_t size) {
assertTrue(size > 0);
assertTrue(size > 0, "memoryFillWithZero: size must be greater than 0");
#if DAWN_DEBUG_BUILD
dawnAllocatedItemCount++;
#endif
auto x =(void *)calloc(1, size);
assertNotNull(x);
auto x = (void*)calloc(1, size);
assertNotNull(x, "memoryFillWithZero: Failed to allocate memory");
return x;
}
@ -52,7 +52,7 @@ static inline void * memoryFillWithZero(const size_t size) {
* @param pointer Pointer in memory to free.
*/
static inline void memoryFree(void *pointer) {
assertNotNull(pointer);
assertNotNull(pointer, "memoryFree: pointer must not be null");
#if DAWN_DEBUG_BUILD
dawnAllocatedItemCount--;
#endif
@ -71,10 +71,10 @@ static inline void memoryCopy(
void *destination,
size_t size
) {
assertNotNull(source);
assertNotNull(destination);
assertTrue(destination != source);
assertTrue(size > 0);
assertNotNull(source, "memoryCopy: source must not be null");
assertNotNull(destination, "memoryCopy: destination must not be null");
assertTrue(destination != source, "memoryCopy: destination must not be source");
assertTrue(size > 0, "memoryCopy: size must be greater than 0");
memcpy(destination, source, size);
}
@ -91,8 +91,8 @@ static inline int32_t memoryCompare(
const void *right,
const size_t size
) {
assertTrue(left != right);
assertTrue(size > 0);
assertTrue(left != right, "memoryCompare: left must not be right");
assertTrue(size > 0, "memoryCompare: size must be greater than 0");
return memcmp(left, right, size);
}
@ -108,8 +108,8 @@ static inline void memorySet(
uint8_t data,
size_t length
) {
assertNotNull(dest);
assertTrue(length > 0);
assertNotNull(dest, "memorySet: dest must not be null");
assertTrue(length > 0, "memorySet: length must be greater than 0");
memset(dest, data, length);
}

View File

@ -23,12 +23,12 @@ static inline char * stringFindNext(
) {
char *p;
assertNotNull(haystack);
assertTrue(limit > 0);
assertNotNull(haystack, "String find haystack cannot be null");
assertTrue(limit > 0, "String find limit must be greater than 0");
for(p = haystack; (size_t)(p - haystack) < limit; p++) {
if(*p == needle) return p;
assertFalse(*p == '\0');// We don't allow you to have a limit > strlen
assertFalse(*p == '\0', "String find limit reached");
}
return NULL;