RingRacers/src/tests/notnull.cpp
Eidolon 6af003771d Add SRB2_ASSERT, srb2::NotNull<T>
Add SRB2_ASSERT, superceding I_Assert

This assertion macro always expands to a call of srb2::do_assert, which
is overloaded with two templates: one which applies if the provided
Level is less than or equal to the SRB2_ASSERTION_LEVEL, and one which
is a no-op. When optimizations are enabled, this will verifiably remove
the evaluation of the expression in all cases, instead of evaluating the
expression and doing nothing with it.

Add srb2::NotNull wrapper utility

This is meant to be used in places where pointers are used as
parameters. It can be used with any pointer-like type, not just raw
pointers. During construction of NotNull, the pointer will be asserted
not-null in debug and paranoia builds, and in release optimizations with
no assertions, the code decays gracefully to standard pointer-passing.
2022-12-30 23:49:29 -06:00

31 lines
631 B
C++

#include "testbase.hpp"
#include <catch2/catch_test_macros.hpp>
#include "../cxxutil.hpp"
namespace {
class A {
public:
virtual bool foo() = 0;
};
class B : public A {
public:
virtual bool foo() override final { return true; };
};
} // namespace
TEST_CASE("NotNull<int*> is constructible from int*") {
int a = 0;
REQUIRE(srb2::NotNull(static_cast<int*>(&a)));
}
TEST_CASE("NotNull<A*> is constructible from B* where B inherits from A") {
B b;
REQUIRE(srb2::NotNull(static_cast<B*>(&b)));
}
TEST_CASE("NotNull<A*> dereferences to B& to call foo") {
B b;
srb2::NotNull<A*> a {static_cast<B*>(&b)};
REQUIRE(a->foo());
}