TTS v4.1.0
The Tiny Test System
Loading...
Searching...
No Matches
Tests definition macros

Macros for defining tests scenarios. More...

Variables

constexpr tts_implementation_defined tts::constant = {}
 Context running a case body in a constant evaluation.
constexpr tts_implementation_defined tts::runtime = {}
 Context running a case body as ordinary code.

Scenarios Definitions

#define TTS_CASE(ID)
 Defines a test case and registers it with the test driver.
#define TTS_CONTEXT(...)
 Declares the parameter of a TTS_CASE_TPL body run in a named context.
#define TTS_CASE_IN(ID, ...)
 Defines a test case run in each of the given contexts, and registers it with the test driver.
#define TTS_CASE_TPL_IN(ID, TYPES, ...)
 Defines a test case run for each type of a list, in each of the given contexts, and registers it with the test driver.
#define TTS_CONSTEXPR(ID)
 Runs a test case as ordinary code and in constant evaluation.
#define TTS_CONSTEVAL(ID)
 Runs a test case in constant evaluation alone.
#define TTS_CASE_TPL(ID, ...)
 Defines a test case run for each type of a list, and registers it with the test driver.
#define TTS_CASE_WITH(ID, TYPES, ...)
 Defines a test case run for each type of a list on generated values, and registers it with the test driver.
#define TTS_XFAIL(ID)
 Marks a test case as expected to fail.
#define TTS_MAYFAIL(ID)
 Marks a test case as allowed to fail.
#define TTS_XINVALID(ID)
 Marks a test case as expected to run no check.
#define TTS_TIMEOUT(MS, ID)
 Gives a test case a deadline of its own.

Scoped scenarios

#define TTS_WHEN(STORY)
 Starts a block whose code runs again before each of its sections.
#define TTS_AND_THEN(MESSAGE)
 Adds a section to a TTS_WHEN block.

Macro Definition Documentation

◆ TTS_AND_THEN

#define TTS_AND_THEN ( MESSAGE)

#include <tts/test/when.hpp>

Adds a section to a TTS_WHEN block.

A change that a section makes to a variable of the block does not reach the next section.

Parameters
MESSAGEText printed under --verbose when the section starts.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
#include <iostream>
TTS_CASE("Check test with sub-test")
{
TTS_WHEN("We start some sub-test")
{
int i = 99;
TTS_AND_THEN("We increment a variable")
{
std::cout << ">> Sub-test 1: i = " << i << "\n";
TTS_EQUAL(i, 99);
i++;
TTS_EQUAL(i, 100);
}
TTS_AND_THEN("We decrement a variable")
{
std::cout << ">> Sub-test 2: i = " << i << "\n";
TTS_EQUAL(i, 99);
i--;
TTS_EQUAL(i, 98);
}
}
};
See also
TTS_WHEN

◆ TTS_CASE

#define TTS_CASE ( ID)

#include <tts/engine/case.hpp>

Defines a test case and registers it with the test driver.

The block after the macro is the body of the case. A case that runs no check is reported as invalid.

The case runs in tts::runtime when its title names no context. A title wrapped by TTS_CONSTEXPR or TTS_CONSTEVAL names the contexts the case runs in, and TTS_CASE_IN names any list of them.

Parameters
IDTitle of the case, or a title wrapped by a tag.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
TTS_CASE("Check basic arithmetic")
{
TTS_EQUAL(2 + 2, 4);
};

◆ TTS_CASE_IN

#define TTS_CASE_IN ( ID,
... )

#include <tts/engine/case.hpp>

Defines a test case run in each of the given contexts, and registers it with the test driver.

The contexts follow the title, among tts::runtime and tts::constant. The body runs once in each, in the order they are given.

A TTS_CASE whose title is wrapped by TTS_CONSTEXPR runs in both contexts, and one wrapped by TTS_CONSTEVAL in tts::constant alone.

Parameters
IDTitle of the case, or a title wrapped by TTS_XFAIL, TTS_MAYFAIL, TTS_XINVALID, or TTS_TIMEOUT.
...The contexts.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
constexpr int twice(int x)
{
return 2 * x;
}
// The constant context first, then the runtime one
TTS_CASE_IN("Check twice in a chosen order of contexts", tts::constant, tts::runtime)
{
TTS_EQUAL(twice(21), 42);
};
// A tag wrapper on the title
TTS_CASE_IN(TTS_XFAIL("Check a wrong expectation in constant evaluation"), tts::constant)
{
TTS_EQUAL(twice(21), 41);
};
// A template case, its types in parentheses, in constant evaluation alone
TTS_CASE_TPL_IN("Check twice for each type",
(int, long),
{
TTS_EQUAL(twice(T(21)), T(42));
};
See also
TTS_CONSTEXPR
TTS_CONSTEVAL

◆ TTS_CASE_TPL

#define TTS_CASE_TPL ( ID,
... )

#include <tts/engine/case.hpp>

Defines a test case run for each type of a list, and registers it with the test driver.

The block after the macro is a generic lambda, <typename T>(tts::type<T>) { ... }, run once per type. The types are given as:

  • types separated by commas,
  • a tts::types list,
  • a type whose public member type types_list is the list.

A case that runs no check is reported as invalid.

A title wrapped by TTS_CONSTEXPR or TTS_CONSTEVAL names the contexts each type runs in, and TTS_CASE_TPL_IN names any list of them. The body then declares its type through TTS_CONTEXT, as in <typename T>(TTS_CONTEXT(tts::type<T>)).

Parameters
IDTitle of the case, or a title wrapped by a tag.
...Types to run the case for.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
#include <array>
TTS_CASE_TPL("Check types using variadic list", char, short, int, double, void*)
<typename T>(tts::type<T>)
{
TTS_GREATER_EQUAL(sizeof(T), 1UL);
};
TTS_CASE_TPL("Check types using a types list", tts::types<float, double>)
<typename T>(tts::type<T>)
{
TTS_EQUAL(1 / T {2}, static_cast<T>(0.5));
};
// A Types Generator is any type exposing a types_list internal type
// In this example we use such a type to generate the list of types:
//
// tts::types<std::array<std::byte,1>,...,std::array<std::byte,N>>>;
template<int N, typename Indexes = std::make_index_sequence<N>> struct sizes;
template<int N, std::size_t... I> struct sizes<N, std::index_sequence<I...>>
{
using types_list = tts::types<std::array<std::byte, I + 1>...>;
};
TTS_CASE_TPL("Check types using a types list generator", sizes<5>)
<typename T>(tts::type<T>)
{
T x;
TTS_EQUAL(sizeof(x), x.size());
};

◆ TTS_CASE_TPL_IN

#define TTS_CASE_TPL_IN ( ID,
TYPES,
... )

#include <tts/engine/case.hpp>

Defines a test case run for each type of a list, in each of the given contexts, and registers it with the test driver.

The types follow the title, between parentheses, in any form TTS_CASE_TPL accepts. The contexts come last, among tts::runtime and tts::constant. For each type, the body runs once in each context, in the order they are given.

The body declares its type with TTS_CONTEXT, as in <typename T>(TTS_CONTEXT(tts::type<T>)).

A TTS_CASE_TPL whose title is wrapped by TTS_CONSTEXPR runs in both contexts, and one wrapped by TTS_CONSTEVAL in tts::constant alone.

Parameters
IDTitle of the case, or a title wrapped by TTS_XFAIL, TTS_MAYFAIL, TTS_XINVALID, or TTS_TIMEOUT.
TYPESTypes to run the case for, between parentheses.
...The contexts.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
constexpr int twice(int x)
{
return 2 * x;
}
// The constant context first, then the runtime one
TTS_CASE_IN("Check twice in a chosen order of contexts", tts::constant, tts::runtime)
{
TTS_EQUAL(twice(21), 42);
};
// A tag wrapper on the title
TTS_CASE_IN(TTS_XFAIL("Check a wrong expectation in constant evaluation"), tts::constant)
{
TTS_EQUAL(twice(21), 41);
};
// A template case, its types in parentheses, in constant evaluation alone
TTS_CASE_TPL_IN("Check twice for each type",
(int, long),
{
TTS_EQUAL(twice(T(21)), T(42));
};
See also
TTS_CASE_IN
TTS_CONTEXT

◆ TTS_CASE_WITH

#define TTS_CASE_WITH ( ID,
TYPES,
... )

#include <tts/engine/case.hpp>

Defines a test case run for each type of a list on generated values, and registers it with the test driver.

The block after the macro is a generic lambda with one argument per generator. The lambda runs once per type of the list, each generator building its argument for that type. The types are given as:

  • types separated by commas, between parentheses,
  • a tts::types list,
  • a type whose public member type types_list is the list.

A case that runs no check is reported as invalid.

The case runs at run time alone, where its generators produce their values, so a title wrapped by TTS_CONSTEXPR or TTS_CONSTEVAL does not compile.

Parameters
IDTitle of the case, a string literal.
TYPESTypes to run the case for.
...Generators, one per argument of the body.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
TTS_CASE_WITH("Check behavior for scalar types",
tts::value {37},
tts::between {0, 100},
tts::randoms {0., 10.})
<typename V, typename B, typename R>(V v, B b, R const& r)
{
TTS_EQUAL(v, V {37});
TTS_GREATER_EQUAL(b, B {0});
TTS_LESS_EQUAL(b, B {100});
TTS_GREATER_EQUAL(r, R {0});
TTS_LESS_EQUAL(r, R {10});
};

◆ TTS_CONSTEVAL

#define TTS_CONSTEVAL ( ID)

#include <tts/engine/case.hpp>

Runs a test case in constant evaluation alone.

Wraps the title of a TTS_CASE or a TTS_CASE_TPL and names the context tts::constant. The body runs as a constant expression, and its checks are reported after the evaluation, each failure with its expression and its position. A body that calls a consteval function needs this tag, since such a function is callable in constant evaluation alone.

Parameters
IDTitle of the case, or a title already wrapped by TTS_XFAIL, TTS_MAYFAIL, TTS_XINVALID, or TTS_TIMEOUT.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
consteval int table_size(int n)
{
return n * n + 1;
}
// The body runs as one constant expression, the only context a consteval function allows
TTS_CASE(TTS_CONSTEVAL("Check table_size in constant evaluation"))
{
TTS_EQUAL(table_size(0), 1);
TTS_EQUAL(table_size(4), 17);
};
See also
TTS_CONSTEXPR
TTS_CONTEXT
tts::constant

◆ TTS_CONSTEXPR

#define TTS_CONSTEXPR ( ID)

#include <tts/engine/case.hpp>

Runs a test case as ordinary code and in constant evaluation.

Wraps the title of a TTS_CASE or a TTS_CASE_TPL and names the contexts tts::runtime and tts::constant. The body runs as ordinary code, then as a constant expression, and the checks of both runs are counted and reported.

The body uses the ordinary checks. In constant evaluation, a failure is reported with its expression, its position, and the values the check compares at run time. A body that calls a function the language refuses in a constant expression does not compile.

Parameters
IDTitle of the case, or a title already wrapped by TTS_XFAIL, TTS_MAYFAIL, TTS_XINVALID, or TTS_TIMEOUT.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
#include <array>
constexpr int square(int x)
{
return x * x;
}
// The body runs as ordinary code, then as one constant expression
TTS_CASE(TTS_CONSTEXPR("Check square in both contexts"))
{
std::array<int, 3> values = {1, 2, 3};
int sum = 0;
for(auto v: values)
sum += square(v);
TTS_EQUAL(sum, 14);
TTS_EQUAL(square(-3), 9);
};
See also
TTS_CONSTEVAL
TTS_CONTEXT
tts::constant

◆ TTS_CONTEXT

#define TTS_CONTEXT ( ...)

#include <tts/engine/case.hpp>

Declares the parameter of a TTS_CASE_TPL body run in a named context.

Wraps the parameter that declares the type, TTS_CONTEXT(tts::type<T>), and adds the context in front of it. A TTS_CASE body declares no parameter, and needs no wrapper.

Parameters
...The parameter the body declares.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
// A template case run in both contexts declares its type through TTS_CONTEXT
TTS_CASE_TPL(TTS_CONSTEXPR("Check the sum of two units for each type"), int, float, double)
<typename T>(TTS_CONTEXT(tts::type<T>))
{
TTS_EQUAL(T(1) + T(1), T(2));
};
// A template case run in constant evaluation alone
TTS_CASE_TPL(TTS_CONSTEVAL("Check the product of two units for each type"), int, float)
<typename T>(TTS_CONTEXT(tts::type<T>))
{
TTS_EQUAL(T(2) * T(2), T(4));
};

◆ TTS_MAYFAIL

#define TTS_MAYFAIL ( ID)

#include <tts/engine/case.hpp>

Marks a test case as allowed to fail.

Wraps the title of a TTS_CASE, a TTS_CASE_TPL, or a TTS_CASE_WITH. Nests with TTS_TIMEOUT, TTS_CONSTEXPR, and TTS_CONSTEVAL in either order.

The case is accepted whether its checks pass or fail, and neither outcome is reported as a failure.

A case that runs no check is still reported as invalid, as for a case without the tag.

Parameters
IDTitle of the case, a string literal.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
TTS_CASE(TTS_MAYFAIL("Flaky under load on some CI runners"))
{
TTS_EXPECT(true); // may occasionally fail elsewhere - either outcome is accepted here
};
See also
TTS_XFAIL
TTS_XINVALID

◆ TTS_TIMEOUT

#define TTS_TIMEOUT ( MS,
ID )

#include <tts/engine/case.hpp>

Gives a test case a deadline of its own.

Wraps the title of a TTS_CASE, a TTS_CASE_TPL, or a TTS_CASE_WITH. Kills the case once it has run for MS milliseconds, and ends the run there. 0 removes, for that case, the deadline --timeout sets.

Nests with TTS_XFAIL, TTS_MAYFAIL, TTS_XINVALID, TTS_CONSTEXPR, and TTS_CONSTEVAL in either order.

Parameters
MSDeadline in milliseconds.
IDTitle of the case, a string literal.

Example

TTS_CASE(TTS_TIMEOUT(5000, "a collective that answers in time"))
{
TTS_EXPECT(true);
};
TTS_CASE(TTS_XFAIL(TTS_TIMEOUT(0, "known to sit there, and exempt from --timeout")))
{
TTS_EXPECT(1 == 2);
};
#if !defined(__EMSCRIPTEN__) // no process timer there, so nothing would end the case below
// The deadline ends the run, so the tag turns that end into the expected one and the exit code
// into 0.
TTS_CASE(TTS_XFAIL(TTS_TIMEOUT(250, "a case that outlives its own deadline")))
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
};
#endif
See also
TTS_XFAIL

◆ TTS_WHEN

#define TTS_WHEN ( STORY)

#include <tts/test/when.hpp>

Starts a block whose code runs again before each of its sections.

A section is a TTS_AND_THEN block inside it.

Parameters
STORYText printed under --verbose when the block starts.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
#include <iostream>
TTS_CASE("Check test with sub-test")
{
TTS_WHEN("We start some sub-test")
{
int i = 99;
TTS_AND_THEN("We increment a variable")
{
std::cout << ">> Sub-test 1: i = " << i << "\n";
TTS_EQUAL(i, 99);
i++;
TTS_EQUAL(i, 100);
}
TTS_AND_THEN("We decrement a variable")
{
std::cout << ">> Sub-test 2: i = " << i << "\n";
TTS_EQUAL(i, 99);
i--;
TTS_EQUAL(i, 98);
}
}
};
See also
TTS_AND_THEN

◆ TTS_XFAIL

#define TTS_XFAIL ( ID)

#include <tts/engine/case.hpp>

Marks a test case as expected to fail.

Wraps the title of a TTS_CASE, a TTS_CASE_TPL, or a TTS_CASE_WITH, as in TTS_CASE(TTS_XFAIL("...")). Nests with TTS_TIMEOUT, TTS_CONSTEXPR, and TTS_CONSTEVAL in either order.

The case passes when at least one of its checks fails, and fails when every check passes.

A case that runs no check fails too. TTS_XINVALID marks a case expected to run none.

Parameters
IDTitle of the case, a string literal.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
TTS_CASE(TTS_XFAIL("Known, not-yet-fixed rounding bug"))
{
TTS_EQUAL(1 + 1, 3); // stands in for the real, currently broken behavior being tracked
};
See also
TTS_MAYFAIL
TTS_XINVALID

◆ TTS_XINVALID

#define TTS_XINVALID ( ID)

#include <tts/engine/case.hpp>

Marks a test case as expected to run no check.

Wraps the title of a TTS_CASE, a TTS_CASE_TPL, or a TTS_CASE_WITH. Nests with TTS_TIMEOUT, TTS_CONSTEXPR, and TTS_CONSTEVAL in either order.

The case fails when it runs any check, passing or failing.

Parameters
IDTitle of the case, a string literal.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
TTS_CASE(TTS_XINVALID("Placeholder for a scenario not written yet"))
{
// Intentionally empty - stays a visible reminder in the suite instead of silently vanishing
};
See also
TTS_XFAIL
TTS_MAYFAIL

Variable Documentation

◆ constant

tts_implementation_defined tts::constant = {}
inlineconstexpr

#include <tts/engine/context.hpp>

Context running a case body in a constant evaluation.

TTS_CASE_IN names the context, alone or with tts::runtime, and TTS_CONSTEXPR or TTS_CONSTEVAL on the title of a case does the same. The body is evaluated in one constant expression. The checks are reported afterward: each pass counts, each failure names its file, its line, and its expression. If the case runs in tts::runtime too, the body runs once more, silently, so that the message adds the values the failed check compares at run time. A body that calls a function the language refuses in a constant expression does not compile. The diagnostic names the call.

The checks accepted in this context:

The precision, sequence, range, and exception checks run at run time alone, and so does a TTS_CASE_WITH, whose values the generators produce at run time.

◆ runtime

tts_implementation_defined tts::runtime = {}
inlineconstexpr

#include <tts/engine/context.hpp>

Context running a case body as ordinary code.

A plain TTS_CASE runs in this context. TTS_CASE_IN names the context, alone or with tts::constant. The body is called like a function. The checks of the body count and report like those of a plain case.