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

Enumerates the values of a type in increasing order. More...

Detailed Description

template<typename T>
struct tts::enumeration< T >

Enumerates the values of a type in increasing order.

Gives each value of T its rank, its position among the values of T in increasing order. tts::sweep and tts::sample take the values of an argument from the ranks of its type.

Specialized for every integer type of at most 8 bytes, and for every floating-point type of 4 or 8 bytes. A specialization for another type, with last, index, and value, makes that type usable with tts::sweep and tts::sample.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
#include <array>
#include <cstdint>
TTS_CASE("Ranks of the values of an integer")
{
// The 256 values of std::int8_t, from -128 to 127
TTS_EQUAL(ranks::last, 255ULL);
TTS_EQUAL(ranks::index(-128), 0ULL);
TTS_EQUAL(ranks::value(255), std::int8_t {127});
};
TTS_CASE("Ranks of the values of a float")
{
using ranks = tts::enumeration<float>;
// -0 and +0 take two neighboring ranks
TTS_EQUAL(ranks::index(0.f), ranks::index(-0.f) + 1);
TTS_LESS(ranks::index(-1.f), ranks::index(1.f));
TTS_EQUAL(ranks::value(ranks::index(1.5f)), 1.5f);
};
// A type of three values
enum class rounding
{
down,
nearest,
up
};
double shift(rounding r)
{
return static_cast<double>(r) - 1.;
}
namespace tts
{
// The name of a rounding in the report of a check
template<> struct display<rounding>
{
static text render(rounding r)
{
std::array names {"down", "nearest", "up"};
return text {names[ static_cast<std::size_t>(r) ]};
}
};
// The three values of rounding, in the order of their declaration
template<> struct enumeration<rounding>
{
static constexpr std::uint64_t last = 2;
static constexpr std::uint64_t index(rounding r) noexcept
{
return static_cast<std::uint64_t>(r);
}
static constexpr rounding value(std::uint64_t i) noexcept
{
return static_cast<rounding>(i);
}
};
}
TTS_CASE("Sweep over the values of a user-defined type")
{
// Calls shift on down, nearest, and up
TTS_ULP_RANGE_CHECK(tts::sweep, rounding, rounding, shift, shift, 0.);
};
Template Parameters
TType whose values are enumerated

Static Public Member Functions

static constexpr std::uint64_t index (T v) noexcept
 Returns the rank of a value.
static constexpr T value (std::uint64_t i) noexcept
 Returns the value of a rank.

Static Public Attributes

static constexpr std::uint64_t last
 Largest rank.

Member Function Documentation

◆ index()

template<typename T>
constexpr std::uint64_t tts::enumeration< T >::index ( T v)
staticconstexprnoexcept

Returns the rank of a value.

The rank of a value is the number of values of T that precede it in increasing order. The smallest value of T has rank 0, the largest has rank last, and the value that follows v has rank index(v) + 1.

Parameters
vValue of T, any but a NaN, which has no rank
Returns
The rank of v, from 0 to last

◆ value()

template<typename T>
constexpr T tts::enumeration< T >::value ( std::uint64_t i)
staticconstexprnoexcept

Returns the value of a rank.

The inverse of index: value(index(v)) is v, and index(value(i)) is i. value(0) is the smallest value of T, value(last) is the largest, and value(i + 1) is the value that follows value(i).

Parameters
iRank, from 0 to last
Returns
The value of T whose rank is i