From 1a65ea977dfd8cf5df4dc71e578f9a0369786a11 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 10 Jan 2023 17:43:18 +0200 Subject: [PATCH] BAN: Better ASSERT() --- BAN/BAN/String.cpp | 10 +++++----- BAN/BAN/StringView.cpp | 6 +++--- BAN/include/BAN/Errors.h | 1 + BAN/include/BAN/Queue.h | 6 +++--- BAN/include/BAN/Vector.h | 18 +++++++++--------- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/BAN/BAN/String.cpp b/BAN/BAN/String.cpp index 23ef447c..54eee434 100644 --- a/BAN/BAN/String.cpp +++ b/BAN/BAN/String.cpp @@ -67,7 +67,7 @@ namespace BAN ErrorOr String::Insert(char ch, size_type index) { - assert(index <= m_size); + ASSERT(index <= m_size); TRY(EnsureCapasity(m_size + 2)); memmove(m_data + index + 1, m_data + index, m_size - index); m_data[index] = ch; @@ -94,14 +94,14 @@ namespace BAN void String::PopBack() { - assert(m_size > 0); + ASSERT(m_size > 0); m_data[m_size - 1] = '\0'; m_size--; } void String::Remove(size_type index) { - assert(index < m_size); + ASSERT(index < m_size); memmove(m_data + index, m_data + index + 1, m_size - index - 1); m_data[m_size - 1] = '\0'; m_size--; @@ -115,13 +115,13 @@ namespace BAN char String::operator[](size_type index) const { - assert(index < m_size); + ASSERT(index < m_size); return m_data[index]; } char& String::operator[](size_type index) { - assert(index < m_size); + ASSERT(index < m_size); return m_data[index]; } diff --git a/BAN/BAN/StringView.cpp b/BAN/BAN/StringView.cpp index 14ae5b3a..53306b80 100644 --- a/BAN/BAN/StringView.cpp +++ b/BAN/BAN/StringView.cpp @@ -25,7 +25,7 @@ namespace BAN char StringView::operator[](size_type index) const { - assert(index < m_size); + ASSERT(index < m_size); return m_data[index]; } @@ -52,10 +52,10 @@ namespace BAN StringView StringView::Substring(size_type index, size_type len) const { - assert(index <= m_size); + ASSERT(index <= m_size); if (len == size_type(-1)) len = m_size - index; - assert(len <= m_size - index); // weird order to avoid overflow + ASSERT(len <= m_size - index); // weird order to avoid overflow StringView result; result.m_data = m_data + index; result.m_size = len; diff --git a/BAN/include/BAN/Errors.h b/BAN/include/BAN/Errors.h index 02225d98..31da467c 100644 --- a/BAN/include/BAN/Errors.h +++ b/BAN/include/BAN/Errors.h @@ -7,6 +7,7 @@ #if defined(__is_kernel) #include #define MUST(error) ({ auto e = error; if (e.IsError()) Kernel::Panic("{}", e.GetError()); e.Value(); }) + #define ASSERT(cond) do { if (!(cond)) Kernel::Panic("ASSERT() failed"); } while(false) #else #error "NOT IMPLEMENTED" #endif diff --git a/BAN/include/BAN/Queue.h b/BAN/include/BAN/Queue.h index b08bf9a6..a43617d3 100644 --- a/BAN/include/BAN/Queue.h +++ b/BAN/include/BAN/Queue.h @@ -59,7 +59,7 @@ namespace BAN template void Queue::Pop() { - assert(m_size > 0); + ASSERT(m_size > 0); m_data->~T(); memmove(m_data, m_data + 1, sizeof(T) * (--m_size)); } @@ -79,14 +79,14 @@ namespace BAN template const T& Queue::Front() const { - assert(m_size > 0); + ASSERT(m_size > 0); return *m_data; } template T& Queue::Front() { - assert(m_size > 0); + ASSERT(m_size > 0); return *m_data; } diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index 3f26f83b..a0b3bd71 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -83,7 +83,7 @@ namespace BAN template ErrorOr Vector::Insert(const T& value, size_type index) { - assert(index <= m_size); + ASSERT(index <= m_size); TRY(EnsureCapasity(m_size + 1)); memmove(m_data + index + 1, m_data + index, (m_size - index) * sizeof(T)); m_data[index] = value; @@ -94,7 +94,7 @@ namespace BAN template void Vector::PopBack() { - assert(m_size > 0); + ASSERT(m_size > 0); m_data[m_size - 1].~T(); m_size--; } @@ -102,7 +102,7 @@ namespace BAN template void Vector::Remove(size_type index) { - assert(index < m_size); + ASSERT(index < m_size); m_data[index].~T(); memmove(m_data + index, m_data + index + 1, (m_size - index - 1) * sizeof(T)); m_size--; @@ -120,41 +120,41 @@ namespace BAN template const T& Vector::operator[](size_type index) const { - assert(index < m_size); + ASSERT(index < m_size); return m_data[index]; } template T& Vector::operator[](size_type index) { - assert(index < m_size); + ASSERT(index < m_size); return m_data[index]; } template const T& Vector::Back() const { - assert(m_size > 0); + ASSERT(m_size > 0); return m_data[m_size - 1]; } template T& Vector::Back() { - assert(m_size > 0); + ASSERT(m_size > 0); return m_data[m_size - 1]; } template const T& Vector::Front() const { - assert(m_size > 0); + ASSERT(m_size > 0); return m_data[0]; } template T& Vector::Front() { - assert(m_size > 0); + ASSERT(m_size > 0); return m_data[0]; }