Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
test.h File Reference

极简C++测试框架,基于宏与异常实现断言与结果统计。 支持单元测试的注册、执行、结果输出与汇总报告。 More...

#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include "../core/csexc.h"
#include "../io/colorful.h"
#include "../text/fmt.h"
#include "../text/repr.h"
#include "./time.h"

Go to the source code of this file.

Namespaces

namespace  console
 本库所有组件所在的顶层命名空间。

Macros

#define TEST(test_name)
#define EXPR_WARP(expression)
#define ASSERT_TRUE(condition)
 断言条件为真。
#define ASSERT_FALSE(condition)
 断言条件为假。
#define ASSERT_EQ(expected, actual)
 断言两个值相等。
#define ASSERT_NE(expected, actual)
 断言两个值不相等。
#define ASSERT_LT(a, b)
 断言第一个值小于第二个值。
#define ASSERT_GT(a, b)
 断言第一个值大于第二个值。
#define ASSERT_LE(a, b)
 断言第一个值小于等于第二个值。
#define ASSERT_GE(a, b)
 断言第一个值大于等于第二个值。
#define ASSERT_NEAR(expected, actual, eps)
 断言两个浮点数接近相等。
#define ASSERT_NULL(ptr)
 断言指针为null。
#define ASSERT_NOTNULL(ptr)
 断言指针不为null。
#define ASSERT_STREQ(expected, actual)
 断言C风格字符串相等。
#define ASSERT_STRNE(expected, actual)
 断言C风格字符串不相等。
#define ASSERT_STRCASEEQ(a, b)
 断言C风格字符串相等(不区分大小写)。
#define ASSERT_STRCONTAINS(str, substr)
 断言C风格字符串包含子串。
#define ASSERT_STRNOTCONTAINS(str, substr)
 断言字符串不包含指定子串。
#define ASSERT_THROWS(expression, exception_type)
 断言表达式抛出指定类型的异常。
#define ASSERT_NO_THROW(expression)
 断言表达式不抛出异常。
#define ASSERT_THROWS_ANY(expression)
 断言表达式抛出任意类型的异常。
#define ASSERT_CONTAINS(container, element)
 断言容器包含指定元素。
#define ASSERT_SIZE_EQ(container, n)
 断言容器大小等于指定值。
#define ASSERT_EMPTY(container)
 断言容器为空。
#define ASSERT_NOT_EMPTY(container)
 断言容器非空。
#define ASSERT_TIMEOUT(expr, ms)
 断言超时。
#define ASSERT_PERFORMANCE(expr, ops, ms)
 断言性能。
#define TEST_MAIN   int main() {}
 测试主函数(其实就是个空壳,没有任何逻辑)。

Functions

int & console::passed_count ()
 获取通过的测试用例数量。
int & console::failed_count ()
 获取失败的测试用例数量。
void console::pre_summary ()
 构造守卫对象,程序结束时输出摘要。
std::string console::ptr_to_str (const char *s)
 辅助函数:安全地将 C 字符串转换为显示字符串。

Detailed Description

极简C++测试框架,基于宏与异常实现断言与结果统计。 支持单元测试的注册、执行、结果输出与汇总报告。

Author
MrXie1109
Date
2026

Macro Definition Documentation

◆ ASSERT_CONTAINS

#define ASSERT_CONTAINS ( container,
element )
Value:
do { \
const auto &_c = (container); \
const auto &_e = (element); \
bool _found = false; \
for (const auto &_item : _c) \
if (_item == _e) { \
_found = true; \
break; \
} \
if (!_found) \
EXPR_WARP(container) " contains " EXPR_WARP(element)); \
} while (false)
表示断言失败的错误。
Definition csexc.h:200
#define EXPR_WARP(expression)
Definition test.h:164

断言容器包含指定元素。

Parameters
container容器。
element元素。
Exceptions
AssertError容器不包含元素时抛出异常。

◆ ASSERT_EMPTY

#define ASSERT_EMPTY ( container)
Value:
do { \
if (!(container).empty()) \
console::format("{} is empty\n\tsize: {}", \
EXPR_WARP(container), \
(container).size())); \
} while (false)
std::string format(const std::string &fmt, const Args &...args)
格式化字符串(char 版)。
Definition fmt.h:494

断言容器为空。

Parameters
container容器。
Exceptions
AssertError容器不为空时抛出异常。

◆ ASSERT_EQ

#define ASSERT_EQ ( expected,
actual )
Value:
do { \
if ((expected) != (actual)) \
console::format("{} == {}\n\texpected: {}\n\tactual: {}", \
EXPR_WARP(expected), \
EXPR_WARP(actual), \
expected, \
actual)); \
} while (false)

断言两个值相等。

Parameters
expected预期值。
actual实际值。
Exceptions
AssertError值不相等时抛出异常。
Note
不要用这个比较浮点数,用ASSERT_NEAR。
Examples
以下是测试用例的格式。.

◆ ASSERT_FALSE

#define ASSERT_FALSE ( condition)
Value:
do { \
if (condition) throw console::AssertError("!(" #condition ")"); \
} while (false)

断言条件为假。

Parameters
condition要断言的条件。
Exceptions
AssertError条件满足时抛出异常。

◆ ASSERT_GE

#define ASSERT_GE ( a,
b )
Value:
do { \
if ((a) < (b)) \
console::format("{} >= {}\n\tleft: {}\n\tright: {}", \
EXPR_WARP(a), \
EXPR_WARP(b), \
a, \
b)); \
} while (false)

断言第一个值大于等于第二个值。

Parameters
a第一个值。
b第二个值。
Exceptions
AssertError第一个值不大于等于第二个值时抛出异常。

◆ ASSERT_GT

#define ASSERT_GT ( a,
b )
Value:
do { \
if ((a) <= (b)) \
console::format("{} > {}\n\tleft: {}\n\tright: {}", \
EXPR_WARP(a), \
EXPR_WARP(b), \
a, \
b)); \
} while (false)

断言第一个值大于第二个值。

Parameters
a第一个值。
b第二个值。
Exceptions
AssertError第一个值不大于第二个值时抛出异常。

◆ ASSERT_LE

#define ASSERT_LE ( a,
b )
Value:
do { \
if ((a) > (b)) \
console::format("{} <= {}\n\tleft: {}\n\tright: {}", \
EXPR_WARP(a), \
EXPR_WARP(b), \
a, \
b)); \
} while (false)

断言第一个值小于等于第二个值。

Parameters
a第一个值。
b第二个值。
Exceptions
AssertError第一个值不小于等于第二个值时抛出异常。

◆ ASSERT_LT

#define ASSERT_LT ( a,
b )
Value:
do { \
if ((a) >= (b)) \
console::format("{} < {}\n\tleft: {}\n\tright: {}", \
EXPR_WARP(a), \
EXPR_WARP(b), \
a, \
b)); \
} while (false)

断言第一个值小于第二个值。

Parameters
a第一个值。
b第二个值。
Exceptions
AssertError第一个值不小于第二个值时抛出异常。

◆ ASSERT_NE

#define ASSERT_NE ( expected,
actual )
Value:
do { \
if ((expected) == (actual)) \
console::format("{} != {}\n\texpected: {}\n\tactual: {}", \
EXPR_WARP(expected), \
EXPR_WARP(actual), \
expected, \
actual)); \
} while (false)

断言两个值不相等。

Parameters
expected预期值。
actual实际值。
Exceptions
AssertError值相等时抛出异常。

◆ ASSERT_NEAR

#define ASSERT_NEAR ( expected,
actual,
eps )
Value:
do { \
auto _e = (expected); \
auto _a = (actual); \
if ((_e - _a) > (eps) || (_a - _e) > (eps)) \
"{} \u2248 {} [\u00B1{}]\n\texpected: " \
"{:.20f}\n\tactual: {:.20f}\n\tepsilon: {:.20f}", \
EXPR_WARP(expected), \
EXPR_WARP(actual), \
eps, \
_e, \
_a, \
eps)); \
} while (false)

断言两个浮点数接近相等。

Parameters
expected期望值。
actual实际值。
eps允许的误差范围。
Exceptions
AssertError两个浮点数不接近相等时抛出异常。

◆ ASSERT_NO_THROW

#define ASSERT_NO_THROW ( expression)
Value:
do { \
try { \
expression; \
} catch (...) { \
EXPR_WARP(expression) " should not throw"); \
} \
} while (false)

断言表达式不抛出异常。

Parameters
expression表达式。
Exceptions
AssertError表达式抛出异常时抛出异常。

◆ ASSERT_NOT_EMPTY

#define ASSERT_NOT_EMPTY ( container)
Value:
do { \
if ((container).empty()) \
EXPR_WARP(container) " is not empty\n\tsize: 0"); \
} while (false)

断言容器非空。

Parameters
container容器。
Exceptions
AssertError容器为空时抛出异常。

◆ ASSERT_NOTNULL

#define ASSERT_NOTNULL ( ptr)
Value:
do { \
if ((ptr) == nullptr) \
EXPR_WARP(ptr) " == nullptr\n\tptr: nullptr"); \
} while (false)

断言指针不为null。

Parameters
ptr指针。
Exceptions
AssertError指针为null时抛出异常。

◆ ASSERT_NULL

#define ASSERT_NULL ( ptr)
Value:
do { \
if ((ptr) != nullptr) \
console::format("{} == nullptr\n\tptr: {:#x}", \
EXPR_WARP(ptr), \
reinterpret_cast<uintptr_t>(ptr))); \
} while (false)

断言指针为null。

Parameters
ptr指针。
Exceptions
AssertError指针不为null时抛出异常。

◆ ASSERT_PERFORMANCE

#define ASSERT_PERFORMANCE ( expr,
ops,
ms )
Value:
do { \
auto _duration = timer([&] { \
for (auto i = 0ULL; i < ops; ++i) expr; \
}); \
auto _limit = milliseconds(ms); \
if (_duration > _limit) \
"{} times {} timed out after {}\n\telapsed: {}\n\t" \
"limit: {}\n\tops: {}", \
EXPR_WARP(ops), \
EXPR_WARP(expr), \
_limit, \
_duration, \
_limit, \
ops)); \
} while (false)

断言性能。

Parameters
expr表达式。
ops操作次数。
ms超时时间(毫秒)。
Exceptions
AssertError表达式执行时间超过指定时间时抛出异常。

◆ ASSERT_SIZE_EQ

#define ASSERT_SIZE_EQ ( container,
n )
Value:
do { \
if ((container).size() != static_cast<std::size_t>(n)) \
console::format("{}.size() == {}\n\tsize: {}\n\texpected: {}", \
EXPR_WARP(container), \
EXPR_WARP(n), \
(container).size(), \
static_cast<std::size_t>(n))); \
} while (false)

断言容器大小等于指定值。

Parameters
container容器。
n期望大小。
Exceptions
AssertError容器大小不等于指定值时抛出异常。

◆ ASSERT_STRCASEEQ

#define ASSERT_STRCASEEQ ( a,
b )

断言C风格字符串相等(不区分大小写)。

Parameters
a字符串A。
b字符串B。
Exceptions
AssertError字符串不相等时抛出异常。

◆ ASSERT_STRCONTAINS

#define ASSERT_STRCONTAINS ( str,
substr )
Value:
do { \
const char *_str = (str); \
const char *_substr = (substr); \
if (_str == nullptr || _substr == nullptr \
|| std::strstr(_str, _substr) == nullptr) { \
console::format("{} contains {}\n\tstr: {}\n\tsubstr: {}", \
EXPR_WARP(str), \
EXPR_WARP(substr), \
console::ptr_to_str(_substr))); \
} \
} while (false)
std::string ptr_to_str(const char *s)
辅助函数:安全地将 C 字符串转换为显示字符串。
Definition test.h:341

断言C风格字符串包含子串。

Parameters
str字符串。
substr子串。
Exceptions
AssertError字符串不包含子串时抛出异常。

◆ ASSERT_STREQ

#define ASSERT_STREQ ( expected,
actual )
Value:
do { \
const char *_e = (expected); \
const char *_a = (actual); \
if (_e == nullptr || _a == nullptr) { \
if (_e != _a) \
"{} == {}\n\texpected: {}\n\tactual: {}", \
EXPR_WARP(expected), \
EXPR_WARP(actual), \
} else if (std::strcmp(_e, _a) != 0) { \
console::format("{} == {}\n\texpected: {}\n\tactual: {}", \
EXPR_WARP(expected), \
EXPR_WARP(actual), \
} \
} while (false)

断言C风格字符串相等。

Parameters
expected预期字符串。
actual实际字符串。
Exceptions
AssertError字符串不相等时抛出异常。

◆ ASSERT_STRNE

#define ASSERT_STRNE ( expected,
actual )
Value:
do { \
const char *_e = (expected); \
const char *_a = (actual); \
if (_e == nullptr || _a == nullptr) { \
if (_e != _a) \
"{} != {}\n\texpected: {}\n\tactual: {}", \
EXPR_WARP(expected), \
EXPR_WARP(actual), \
} else if (std::strcmp(_e, _a) == 0) { \
console::format("{} != {}\n\texpected: {}\n\tactual: {}", \
EXPR_WARP(expected), \
EXPR_WARP(actual), \
} \
} while (false)

断言C风格字符串不相等。

Parameters
expected预期字符串。
actual实际字符串。
Exceptions
AssertError字符串相等时抛出异常。

◆ ASSERT_STRNOTCONTAINS

#define ASSERT_STRNOTCONTAINS ( str,
substr )
Value:
do { \
const char *_str = (str); \
const char *_substr = (substr); \
if (_str == nullptr || _substr == nullptr \
|| std::strstr(_str, _substr) != nullptr) { \
console::format("{} does not contain {}\n\tstr: {}\n\t" \
"substr: {}", \
EXPR_WARP(str), \
EXPR_WARP(substr), \
console::ptr_to_str(_substr))); \
} \
} while (false)

断言字符串不包含指定子串。

Parameters
str字符串。
substr子串。

◆ ASSERT_THROWS

#define ASSERT_THROWS ( expression,
exception_type )
Value:
do { \
try { \
expression; \
EXPR_WARP(expression) " should throw " #exception_type); \
} catch (const exception_type &) { \
} catch (...) { \
throw console::AssertError( \
EXPR_WARP(expression) " should throw " #exception_type); \
} \
} while (false)

断言表达式抛出指定类型的异常。

Parameters
expression表达式。
exception_type异常类型。
Exceptions
AssertError表达式未抛出指定类型的异常时抛出异常。

◆ ASSERT_THROWS_ANY

#define ASSERT_THROWS_ANY ( expression)
Value:
do { \
try { \
expression; \
} catch (...) { \
break; \
} \
throw console::AssertError(EXPR_WARP(expression) " should throw"); \
} while (false)

断言表达式抛出任意类型的异常。

Parameters
expression表达式。
Exceptions
AssertError表达式未抛出异常时抛出异常。

◆ ASSERT_TIMEOUT

#define ASSERT_TIMEOUT ( expr,
ms )
Value:
do { \
auto _duration = timer([&] { expr; }); \
auto _limit = milliseconds(ms); \
if (_duration > _limit) \
console::format("{} timed out after {}\n\telapsed: " \
"{}\n\tlimit: {}", \
EXPR_WARP(expr), \
_limit, \
_duration, \
_limit)); \
} while (false)

断言超时。

Parameters
expr表达式。
ms超时时间(毫秒)。
Exceptions
AssertError表达式执行时间超过指定时间时抛出异常。

◆ ASSERT_TRUE

#define ASSERT_TRUE ( condition)
Value:
do { \
if (!(condition)) throw console::AssertError(#condition); \
} while (false)

断言条件为真。

Parameters
condition要断言的条件。
Exceptions
AssertError条件不满足时抛出异常。

◆ EXPR_WARP

#define EXPR_WARP ( expression)
Value:
"`" #expression "`"

◆ TEST

#define TEST ( test_name)

◆ TEST_MAIN

#define TEST_MAIN   int main() {}

测试主函数(其实就是个空壳,没有任何逻辑)。