Newer
Older
libj / modules / j / base / src / assert.cpp
#include <iostream>
#include <sstream>
#include <cstring>
#include <j/lang/assertion_error.hpp>
#include <j/cppunit/assert.hpp>

namespace j
{
    namespace cppunit
    {
        namespace Assert
        {

            // オーバーロード: const char*
            void _assertEqualsImpl(const char *expected, const char *actual,
                                   const char *file, const char *func, int line)
            {
                if (std::strcmp(expected, actual) != 0)
                {
                    std::ostringstream msg;
                    msg << "expected <" << expected << "> but was: <" << actual << ">";
                    throw lang::AssertionError(msg.str(), file, func, line);
                }
            }

            // float, double 用
            void _assertEqualsFloatImpl(const double &expected, const double &actual, const double &delta,
                                        const char *file, const char *func, int line)
            {
                bool isSuccess = (std::fabs(expected - actual) < delta);
                if (!isSuccess)
                {
                    std::ostringstream msg;
                    msg << "expected <" << expected << "> but was: <" << actual << "> (delta = " << delta << ")";
                    throw lang::AssertionError(msg.str(), file, func, line);
                }
            }

            void _assertTrueImpl(bool condition, const char *file, const char *func, int line)
            {
                if (!condition)
                {
                    throw lang::AssertionError("expected <true> but was: <false>", file, func, line);
                }
            }

            void _assertFalseImpl(bool condition, const char *file, const char *func, int line)
            {
                if (condition)
                {
                    throw lang::AssertionError("expected <false> but was: <true>", file, func, line);
                }
            }
            void _assertNullImpl(void *obj, const char *file, const char *func, int line)
            {
                if (obj != nullptr)
                {
                    throw lang::AssertionError("expected <nullptr> but was: <not nullptr>", file, func, line);
                }
            }

            void _assertNotNullImpl(void *obj, const char *file, const char *func, int line)
            {
                if (obj == nullptr)
                {
                    throw lang::AssertionError("expected <not null> but was: <nullptr>", file, func, line);
                }
            }

            void _failImpl(const char *file, const char *func, int line)
            {
                throw lang::AssertionError("fail()", file, func, line);
            }

        } // namespace Assert
    } // namespace cppunit
} // namespace j