MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mttest.hpp File Reference
#include <chrono>
#include <functional>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <cstring>
#include "mtcore.hpp"
Include dependency graph for mttest.hpp:

Go to the source code of this file.

Classes

class  mttest::IsStreamable< T >
 Checks if a class can be streamd to ostream. More...
 
struct  mttest::CmpRes< L, R >
 Result of comparing left and right (retains value references for debug printing) More...
 
struct  mttest::TestContext
 Context for unit tests. More...
 
struct  mttest::TestContext::test_err
 
struct  mttest::TestContext::test_fail
 
struct  mttest::TestCase
 Test case metadata. More...
 
struct  mttest::TestSuites
 Test suite metadata and runner. More...
 

Namespaces

namespace  mttest
 MT Unit testing framework.
 

Macros

#define TEST_IMPL_NAME1(X, Y, S, N, SEP)
 
#define TEST_IMPL_NAME(X, Y, S, N)
 
#define TEST_SHORT_NAME(X, Y)
 
#define TEST_STRINGIFY(...)
 
#define TEST_CASE(SUITE, NAME)
 Defines a new test case.
 
#define MT_TEST_NARGS(...)
 
#define MT_TEST_NARGS_(_11, _10, _9, _8, _7, _6, _5, _4, _3, _2, _1, N, ...)
 
#define MT_TEST_CONC(A, B)
 
#define MT_TEST_CONC_(A, B)
 
#define MT_TEST_GET_ELEMS(N, ...)
 
#define MT_TEST_GET_ELEMS_0(_0)
 
#define MT_TEST_GET_ELEMS_1(_0, _1)
 
#define MT_TEST_GET_ELEMS_2(_0, _1, _2)
 
#define MT_TEST_GET_ELEMS_3(_0, _1, _2, _3)
 
#define MT_TEST_GET_ELEMS_4(_0, _1, _2, _3, _4)
 
#define MT_TEST_GET_ELEMS_5(_0, _1, _2, _3, _4, _5)
 
#define MT_TEST_GET_ELEMS_6(_0, _1, _2, _3, _4, _5, _6)
 
#define MT_TEST_GET_ELEMS_7(_0, _1, _2, _3, _4, _5, _6, _7)
 
#define MT_TEST_GET_ELEMS_8(_0, _1, _2, _3, _4, _5, _6, _7, _8)
 
#define MT_TEST_GET_ELEMS_9(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9)
 
#define MT_TEST_GET_ELEMS_10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10)
 
#define MT_TEST_GET_ELEMS_11(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11)
 
#define MT_TEST_GET_NOT_LAST(...)
 
#define MT_TEST_COND(...)
 
#define CHECK_MESSAGE(...)
 Does a check with a specific error message on failure.
 
#define CHECK(...)
 Does a check.
 
#define MTTEST_CHECK_CMP(COMP, ...)
 
#define CHECK_EQ_APPROX(LEFT, RIGHT, EPSILON)
 Does a check if two values are approximately equal based on an epsilon.
 
#define CHECK_EQ(...)
 Checks if two values are equal.
 
#define CHECK_NE(...)
 Checks if two values are not equal.
 
#define CHECK_LT(...)
 Checks if the left value is less than the right value.
 
#define CHECK_LE(...)
 Checks if the left value is less than or equal to the right value.
 
#define CHECK_GT(...)
 Checks if the left value is greater than the right value.
 
#define CHECK_GE(...)
 Checks if the left value is greater than or equal to the right value.
 

Enumerations

enum  mttest::SuiteStatus { mttest::OK , mttest::FAIL , mttest::ERROR }
 Test suite status. More...
 

Functions

template<mtcore::DefaultFormattable T>
std::ostream & operator<< (std::ostream &os, const T &v)
 
template<typename T>
std::string mttest::as_string (const T &val)
 Gets a value as a string (for debug printing)
 
std::string mttest::op_name (const std::string &op)
 Gets an operation name's output symbol.
 
bool mttest::check (bool l)
 Does a check (used by macros)
 
bool mttest::check (bool l, const std::string &)
 Does a check with a message (drops the message, used by macros)
 
std::string mttest::check_msg (bool, std::string message="")
 Gets the message from a check (used by macros)
 
template<typename L, typename R>
auto mttest::eq (const L &left, const R &right)
 Equality comparison (used by macros)
 
template<typename L, typename R>
auto mttest::ne (const L &left, const R &right)
 Inequality comparison (used by macros)
 
template<typename L, typename R>
auto mttest::lt (const L &left, const R &right)
 Less than comparison (used by macros)
 
template<typename L, typename R>
auto mttest::le (const L &left, const R &right)
 Less than or equal to comparison (used by macros)
 
template<typename L, typename R>
auto mttest::gt (const L &left, const R &right)
 Greater than comparison (used by macros)
 
template<typename L, typename R>
auto mttest::ge (const L &left, const R &right)
 Greater than or equal to comparison (used by macros)
 

Variables

mttest::TestSuites test_suites
 

Macro Definition Documentation

◆ CHECK

#define CHECK ( ...)
Value:
{ \
auto TEST_SHORT_NAME(mttest_res, __LINE__) = __VA_ARGS__; \
if (!TEST_SHORT_NAME(mttest_res, __LINE__)) { \
mttest__context_ctx.add_failure(TEST_STRINGIFY(__VA_ARGS__), __FILE__, \
__LINE__); \
} \
}
#define TEST_STRINGIFY(...)
Definition mttest.hpp:337
#define TEST_SHORT_NAME(X, Y)
Definition mttest.hpp:335

Does a check.

Definition at line 506 of file mttest.hpp.

506#define CHECK(...) \
507 { \
508 auto TEST_SHORT_NAME(mttest_res, __LINE__) = __VA_ARGS__; \
509 if (!TEST_SHORT_NAME(mttest_res, __LINE__)) { \
510 mttest__context_ctx.add_failure(TEST_STRINGIFY(__VA_ARGS__), __FILE__, \
511 __LINE__); \
512 } \
513 }

◆ CHECK_EQ_APPROX

#define CHECK_EQ_APPROX ( LEFT,
RIGHT,
EPSILON )
Value:
{ \
auto TEST_IMPL_NAME(mttest_check_res_left, __LINE__) = LEFT; \
auto TEST_IMPL_NAME(mttest_check_res_left_minus, __LINE__) = \
TEST_IMPL_NAME(mttest_check_res_left, __LINE__) - EPSILON; \
auto TEST_IMPL_NAME(mttest_check_res_left_plus, __LINE__) = \
TEST_IMPL_NAME(mttest_check_res_left, __LINE__) + EPSILON; \
auto TEST_IMPL_NAME(mttest_check_res_right, __LINE__) = RIGHT; \
if (TEST_IMPL_NAME(mttest_check_res_left_minus, __LINE__) > \
TEST_IMPL_NAME(mttest_check_res_right, __LINE__) || \
TEST_IMPL_NAME(mttest_check_res_right, __LINE__) > \
TEST_IMPL_NAME(mttest_check_res_left_plus, __LINE__)) { \
mttest__context_ctx.add_failure( \
TEST_STRINGIFY((LEFT - EPSILON) <= (RIGHT) <= (LEFT + EPSILON)), \
std::string{"Got "} + \
mttest::as_string( \
TEST_IMPL_NAME(mttest_check_res_left_minus, __LINE__)) + \
" <= " + \
mttest::as_string( \
TEST_IMPL_NAME(mttest_check_res_right, __LINE__)) + " <= " + \
mttest::as_string( \
TEST_IMPL_NAME(mttest_check_res_left_plus, __LINE__)), \
__FILE__, __LINE__); \
} \
}
#define TEST_IMPL_NAME(X, Y, S, N)
Definition mttest.hpp:334

Does a check if two values are approximately equal based on an epsilon.

Definition at line 528 of file mttest.hpp.

528#define CHECK_EQ_APPROX(LEFT, RIGHT, EPSILON) \
529 { \
530 auto TEST_IMPL_NAME(mttest_check_res_left, __LINE__) = LEFT; \
531 auto TEST_IMPL_NAME(mttest_check_res_left_minus, __LINE__) = \
532 TEST_IMPL_NAME(mttest_check_res_left, __LINE__) - EPSILON; \
533 auto TEST_IMPL_NAME(mttest_check_res_left_plus, __LINE__) = \
534 TEST_IMPL_NAME(mttest_check_res_left, __LINE__) + EPSILON; \
535 auto TEST_IMPL_NAME(mttest_check_res_right, __LINE__) = RIGHT; \
536 if (TEST_IMPL_NAME(mttest_check_res_left_minus, __LINE__) > \
537 TEST_IMPL_NAME(mttest_check_res_right, __LINE__) || \
538 TEST_IMPL_NAME(mttest_check_res_right, __LINE__) > \
539 TEST_IMPL_NAME(mttest_check_res_left_plus, __LINE__)) { \
540 mttest__context_ctx.add_failure( \
541 TEST_STRINGIFY((LEFT - EPSILON) <= (RIGHT) <= (LEFT + EPSILON)), \
542 std::string{"Got "} + \
543 mttest::as_string( \
544 TEST_IMPL_NAME(mttest_check_res_left_minus, __LINE__)) + \
545 " <= " + \
546 mttest::as_string( \
547 TEST_IMPL_NAME(mttest_check_res_right, __LINE__)) + " <= " + \
548 mttest::as_string( \
549 TEST_IMPL_NAME(mttest_check_res_left_plus, __LINE__)), \
550 __FILE__, __LINE__); \
551 } \
552 }

◆ CHECK_MESSAGE

#define CHECK_MESSAGE ( ...)
Value:
{ \
auto TEST_SHORT_NAME(mttest_res, __LINE__) = mttest::check(__VA_ARGS__); \
if (!TEST_SHORT_NAME(mttest_res, __LINE__)) { \
mttest__context_ctx.add_failure(MT_TEST_COND(__VA_ARGS__), \
mttest::check_msg(__VA_ARGS__), \
__FILE__, __LINE__); \
} \
}
#define MT_TEST_COND(...)
Definition mttest.hpp:387
bool check(bool l)
Does a check (used by macros)
Definition mttest.hpp:294
std::string check_msg(bool, std::string message="")
Gets the message from a check (used by macros)
Definition mttest.hpp:298

Does a check with a specific error message on failure.

Definition at line 493 of file mttest.hpp.

493#define CHECK_MESSAGE(...) \
494 { \
495 auto TEST_SHORT_NAME(mttest_res, __LINE__) = mttest::check(__VA_ARGS__); \
496 if (!TEST_SHORT_NAME(mttest_res, __LINE__)) { \
497 mttest__context_ctx.add_failure(MT_TEST_COND(__VA_ARGS__), \
498 mttest::check_msg(__VA_ARGS__), \
499 __FILE__, __LINE__); \
500 } \
501 }

◆ MT_TEST_CONC

#define MT_TEST_CONC ( A,
B )
Value:
#define MT_TEST_CONC_(A, B)
Definition mttest.hpp:360

Definition at line 359 of file mttest.hpp.

◆ MT_TEST_CONC_

#define MT_TEST_CONC_ ( A,
B )
Value:
A##B

Definition at line 360 of file mttest.hpp.

◆ MT_TEST_COND

#define MT_TEST_COND ( ...)
Value:
#define MT_TEST_GET_NOT_LAST(...)
Definition mttest.hpp:384

Definition at line 387 of file mttest.hpp.

◆ MT_TEST_GET_ELEMS

#define MT_TEST_GET_ELEMS ( N,
... )
Value:
MT_TEST_CONC(MT_TEST_GET_ELEMS_, N)(__VA_ARGS__)
#define MT_TEST_CONC(A, B)
Definition mttest.hpp:359

Definition at line 362 of file mttest.hpp.

362#define MT_TEST_GET_ELEMS(N, ...) \
363 MT_TEST_CONC(MT_TEST_GET_ELEMS_, N)(__VA_ARGS__)

◆ MT_TEST_GET_ELEMS_0

#define MT_TEST_GET_ELEMS_0 ( _0)

Definition at line 364 of file mttest.hpp.

◆ MT_TEST_GET_ELEMS_1

#define MT_TEST_GET_ELEMS_1 ( _0,
_1 )

Definition at line 365 of file mttest.hpp.

◆ MT_TEST_GET_ELEMS_10

#define MT_TEST_GET_ELEMS_10 ( _0,
_1,
_2,
_3,
_4,
_5,
_6,
_7,
_8,
_9,
_10 )
Value:
#_1 #_2 #_3 #_4 #_5 #_6 #_7 #_8 #_9

Definition at line 377 of file mttest.hpp.

377#define MT_TEST_GET_ELEMS_10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10) \
378 #_1 #_2 #_3 #_4 #_5 #_6 #_7 #_8 #_9

◆ MT_TEST_GET_ELEMS_11

#define MT_TEST_GET_ELEMS_11 ( _0,
_1,
_2,
_3,
_4,
_5,
_6,
_7,
_8,
_9,
_10,
_11 )
Value:
#_1 #_2 #_3 #_4 #_5 #_6 #_7 #_8 #_9 #_10

Definition at line 379 of file mttest.hpp.

379#define MT_TEST_GET_ELEMS_11(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11) \
380 #_1 #_2 #_3 #_4 #_5 #_6 #_7 #_8 #_9 #_10

◆ MT_TEST_GET_ELEMS_2

#define MT_TEST_GET_ELEMS_2 ( _0,
_1,
_2 )
Value:
#_1

Definition at line 366 of file mttest.hpp.

◆ MT_TEST_GET_ELEMS_3

#define MT_TEST_GET_ELEMS_3 ( _0,
_1,
_2,
_3 )
Value:
#_1 #_2

Definition at line 367 of file mttest.hpp.

◆ MT_TEST_GET_ELEMS_4

#define MT_TEST_GET_ELEMS_4 ( _0,
_1,
_2,
_3,
_4 )
Value:
#_1 #_2 #_3

Definition at line 368 of file mttest.hpp.

◆ MT_TEST_GET_ELEMS_5

#define MT_TEST_GET_ELEMS_5 ( _0,
_1,
_2,
_3,
_4,
_5 )
Value:
#_1 #_2 #_3 #_4

Definition at line 369 of file mttest.hpp.

◆ MT_TEST_GET_ELEMS_6

#define MT_TEST_GET_ELEMS_6 ( _0,
_1,
_2,
_3,
_4,
_5,
_6 )
Value:
#_1 #_2 #_3 #_4 #_5

Definition at line 370 of file mttest.hpp.

◆ MT_TEST_GET_ELEMS_7

#define MT_TEST_GET_ELEMS_7 ( _0,
_1,
_2,
_3,
_4,
_5,
_6,
_7 )
Value:
#_1 #_2 #_3 #_4 #_5 #_6

Definition at line 371 of file mttest.hpp.

371#define MT_TEST_GET_ELEMS_7(_0, _1, _2, _3, _4, _5, _6, _7) \
372 #_1 #_2 #_3 #_4 #_5 #_6

◆ MT_TEST_GET_ELEMS_8

#define MT_TEST_GET_ELEMS_8 ( _0,
_1,
_2,
_3,
_4,
_5,
_6,
_7,
_8 )
Value:
#_1 #_2 #_3 #_4 #_5 #_6 #_7

Definition at line 373 of file mttest.hpp.

373#define MT_TEST_GET_ELEMS_8(_0, _1, _2, _3, _4, _5, _6, _7, _8) \
374 #_1 #_2 #_3 #_4 #_5 #_6 #_7

◆ MT_TEST_GET_ELEMS_9

#define MT_TEST_GET_ELEMS_9 ( _0,
_1,
_2,
_3,
_4,
_5,
_6,
_7,
_8,
_9 )
Value:
#_1 #_2 #_3 #_4 #_5 #_6 #_7 #_8

Definition at line 375 of file mttest.hpp.

375#define MT_TEST_GET_ELEMS_9(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) \
376 #_1 #_2 #_3 #_4 #_5 #_6 #_7 #_8

◆ MT_TEST_GET_NOT_LAST

#define MT_TEST_GET_NOT_LAST ( ...)
Value:
MT_TEST_GET_ELEMS(MT_TEST_NARGS(__VA_ARGS__), _, __VA_ARGS__)
#define MT_TEST_NARGS(...)
Definition mttest.hpp:355
#define MT_TEST_GET_ELEMS(N,...)
Definition mttest.hpp:362

Definition at line 384 of file mttest.hpp.

384#define MT_TEST_GET_NOT_LAST(...) \
385 MT_TEST_GET_ELEMS(MT_TEST_NARGS(__VA_ARGS__), _, __VA_ARGS__)

◆ MT_TEST_NARGS

#define MT_TEST_NARGS ( ...)
Value:
MT_TEST_NARGS_(__VA_ARGS__, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define MT_TEST_NARGS_(_11, _10, _9, _8, _7, _6, _5, _4, _3, _2, _1, N,...)
Definition mttest.hpp:357

Definition at line 355 of file mttest.hpp.

355#define MT_TEST_NARGS(...) \
356 MT_TEST_NARGS_(__VA_ARGS__, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)

◆ MT_TEST_NARGS_

#define MT_TEST_NARGS_ ( _11,
_10,
_9,
_8,
_7,
_6,
_5,
_4,
_3,
_2,
_1,
N,
... )
Value:
N

Definition at line 357 of file mttest.hpp.

◆ MTTEST_CHECK_CMP

#define MTTEST_CHECK_CMP ( COMP,
... )
Value:
{ \
auto TEST_SHORT_NAME(mttest_res, __LINE__) = mttest::COMP(__VA_ARGS__); \
if (!TEST_SHORT_NAME(mttest_res, __LINE__).res) { \
mttest__context_ctx.add_failure( \
#COMP, #__VA_ARGS__, TEST_SHORT_NAME(mttest_res, __LINE__).left, \
TEST_SHORT_NAME(mttest_res, __LINE__).right, __FILE__, __LINE__); \
} \
}

Definition at line 515 of file mttest.hpp.

515#define MTTEST_CHECK_CMP(COMP, ...) \
516 { \
517 auto TEST_SHORT_NAME(mttest_res, __LINE__) = mttest::COMP(__VA_ARGS__); \
518 if (!TEST_SHORT_NAME(mttest_res, __LINE__).res) { \
519 mttest__context_ctx.add_failure( \
520 #COMP, #__VA_ARGS__, TEST_SHORT_NAME(mttest_res, __LINE__).left, \
521 TEST_SHORT_NAME(mttest_res, __LINE__).right, __FILE__, __LINE__); \
522 } \
523 }

◆ TEST_IMPL_NAME

#define TEST_IMPL_NAME ( X,
Y,
S,
N )
Value:
TEST_IMPL_NAME1(X, Y, S, N, _)
#define TEST_IMPL_NAME1(X, Y, S, N, SEP)
Definition mttest.hpp:333

Definition at line 334 of file mttest.hpp.

◆ TEST_IMPL_NAME1

#define TEST_IMPL_NAME1 ( X,
Y,
S,
N,
SEP )
Value:
X##SEP##Y##SEP##S##SEP##N

Definition at line 333 of file mttest.hpp.

◆ TEST_SHORT_NAME

#define TEST_SHORT_NAME ( X,
Y )
Value:
TEST_IMPL_NAME1(X, Y, S, N, _)

Definition at line 335 of file mttest.hpp.

◆ TEST_STRINGIFY

#define TEST_STRINGIFY ( ...)
Value:
#__VA_ARGS__

Definition at line 337 of file mttest.hpp.

Function Documentation

◆ operator<<()

std::ostream & operator<< ( std::ostream & os,
const T & v )

Definition at line 33 of file mttest.hpp.

33 {
35 mtcore::io::print(writer, "{}", v);
36 return os;
37}
io::Writer< csv::impl::Writer< WI > > writer(io::Writer< WI > &underlying, Options opts={})
Creates a CSV writer which will encode the data before writing it out.
Result< size_t, typename Writer< WI >::ErrType > print(Writer< WI > &writer, const char *fmt, const Args &...args)
Prints arguments using a format string Element insert points are designated by a pair of curly braces...
auto ostream_writer(std::ostream &os)
Creates a writer which passes its output to an ostream Useful for compatibility with the C++ standard...
Here is the call graph for this function:

Variable Documentation

◆ test_suites

mttest::TestSuites test_suites
extern