BAN: Better ASSERT()

This commit is contained in:
Bananymous 2023-01-10 17:43:18 +02:00
parent 73cd08fa51
commit 1a65ea977d
5 changed files with 21 additions and 20 deletions

View File

@ -67,7 +67,7 @@ namespace BAN
ErrorOr<void> String::Insert(char ch, size_type index) ErrorOr<void> String::Insert(char ch, size_type index)
{ {
assert(index <= m_size); ASSERT(index <= m_size);
TRY(EnsureCapasity(m_size + 2)); TRY(EnsureCapasity(m_size + 2));
memmove(m_data + index + 1, m_data + index, m_size - index); memmove(m_data + index + 1, m_data + index, m_size - index);
m_data[index] = ch; m_data[index] = ch;
@ -94,14 +94,14 @@ namespace BAN
void String::PopBack() void String::PopBack()
{ {
assert(m_size > 0); ASSERT(m_size > 0);
m_data[m_size - 1] = '\0'; m_data[m_size - 1] = '\0';
m_size--; m_size--;
} }
void String::Remove(size_type index) 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); memmove(m_data + index, m_data + index + 1, m_size - index - 1);
m_data[m_size - 1] = '\0'; m_data[m_size - 1] = '\0';
m_size--; m_size--;
@ -115,13 +115,13 @@ namespace BAN
char String::operator[](size_type index) const char String::operator[](size_type index) const
{ {
assert(index < m_size); ASSERT(index < m_size);
return m_data[index]; return m_data[index];
} }
char& String::operator[](size_type index) char& String::operator[](size_type index)
{ {
assert(index < m_size); ASSERT(index < m_size);
return m_data[index]; return m_data[index];
} }

View File

@ -25,7 +25,7 @@ namespace BAN
char StringView::operator[](size_type index) const char StringView::operator[](size_type index) const
{ {
assert(index < m_size); ASSERT(index < m_size);
return m_data[index]; return m_data[index];
} }
@ -52,10 +52,10 @@ namespace BAN
StringView StringView::Substring(size_type index, size_type len) const StringView StringView::Substring(size_type index, size_type len) const
{ {
assert(index <= m_size); ASSERT(index <= m_size);
if (len == size_type(-1)) if (len == size_type(-1))
len = m_size - index; 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; StringView result;
result.m_data = m_data + index; result.m_data = m_data + index;
result.m_size = len; result.m_size = len;

View File

@ -7,6 +7,7 @@
#if defined(__is_kernel) #if defined(__is_kernel)
#include <kernel/Panic.h> #include <kernel/Panic.h>
#define MUST(error) ({ auto e = error; if (e.IsError()) Kernel::Panic("{}", e.GetError()); e.Value(); }) #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 #else
#error "NOT IMPLEMENTED" #error "NOT IMPLEMENTED"
#endif #endif

View File

@ -59,7 +59,7 @@ namespace BAN
template<typename T> template<typename T>
void Queue<T>::Pop() void Queue<T>::Pop()
{ {
assert(m_size > 0); ASSERT(m_size > 0);
m_data->~T(); m_data->~T();
memmove(m_data, m_data + 1, sizeof(T) * (--m_size)); memmove(m_data, m_data + 1, sizeof(T) * (--m_size));
} }
@ -79,14 +79,14 @@ namespace BAN
template<typename T> template<typename T>
const T& Queue<T>::Front() const const T& Queue<T>::Front() const
{ {
assert(m_size > 0); ASSERT(m_size > 0);
return *m_data; return *m_data;
} }
template<typename T> template<typename T>
T& Queue<T>::Front() T& Queue<T>::Front()
{ {
assert(m_size > 0); ASSERT(m_size > 0);
return *m_data; return *m_data;
} }

View File

@ -83,7 +83,7 @@ namespace BAN
template<typename T> template<typename T>
ErrorOr<void> Vector<T>::Insert(const T& value, size_type index) ErrorOr<void> Vector<T>::Insert(const T& value, size_type index)
{ {
assert(index <= m_size); ASSERT(index <= m_size);
TRY(EnsureCapasity(m_size + 1)); TRY(EnsureCapasity(m_size + 1));
memmove(m_data + index + 1, m_data + index, (m_size - index) * sizeof(T)); memmove(m_data + index + 1, m_data + index, (m_size - index) * sizeof(T));
m_data[index] = value; m_data[index] = value;
@ -94,7 +94,7 @@ namespace BAN
template<typename T> template<typename T>
void Vector<T>::PopBack() void Vector<T>::PopBack()
{ {
assert(m_size > 0); ASSERT(m_size > 0);
m_data[m_size - 1].~T(); m_data[m_size - 1].~T();
m_size--; m_size--;
} }
@ -102,7 +102,7 @@ namespace BAN
template<typename T> template<typename T>
void Vector<T>::Remove(size_type index) void Vector<T>::Remove(size_type index)
{ {
assert(index < m_size); ASSERT(index < m_size);
m_data[index].~T(); m_data[index].~T();
memmove(m_data + index, m_data + index + 1, (m_size - index - 1) * sizeof(T)); memmove(m_data + index, m_data + index + 1, (m_size - index - 1) * sizeof(T));
m_size--; m_size--;
@ -120,41 +120,41 @@ namespace BAN
template<typename T> template<typename T>
const T& Vector<T>::operator[](size_type index) const const T& Vector<T>::operator[](size_type index) const
{ {
assert(index < m_size); ASSERT(index < m_size);
return m_data[index]; return m_data[index];
} }
template<typename T> template<typename T>
T& Vector<T>::operator[](size_type index) T& Vector<T>::operator[](size_type index)
{ {
assert(index < m_size); ASSERT(index < m_size);
return m_data[index]; return m_data[index];
} }
template<typename T> template<typename T>
const T& Vector<T>::Back() const const T& Vector<T>::Back() const
{ {
assert(m_size > 0); ASSERT(m_size > 0);
return m_data[m_size - 1]; return m_data[m_size - 1];
} }
template<typename T> template<typename T>
T& Vector<T>::Back() T& Vector<T>::Back()
{ {
assert(m_size > 0); ASSERT(m_size > 0);
return m_data[m_size - 1]; return m_data[m_size - 1];
} }
template<typename T> template<typename T>
const T& Vector<T>::Front() const const T& Vector<T>::Front() const
{ {
assert(m_size > 0); ASSERT(m_size > 0);
return m_data[0]; return m_data[0];
} }
template<typename T> template<typename T>
T& Vector<T>::Front() T& Vector<T>::Front()
{ {
assert(m_size > 0); ASSERT(m_size > 0);
return m_data[0]; return m_data[0];
} }