libcruft-util/test/introspection.cpp

161 lines
4.4 KiB
C++

#include "introspection.hpp"
#include "std.hpp"
#include "tap.hpp"
#include <iostream>
///////////////////////////////////////////////////////////////////////////////
// simple test struct of scalars
struct foo
{
int a;
float b;
};
//-----------------------------------------------------------------------------
namespace bar {
struct qux {
short c;
};
template <typename>
struct templated_qux { };
}
///////////////////////////////////////////////////////////////////////////////
template <typename ...ValueT>
struct templated_with_type { };
template <int ValueV>
struct templated_with_value { };
enum enumeration_t { FOO };
template <enumeration_t EnumV>
struct templated_with_enum { };
///////////////////////////////////////////////////////////////////////////////
// define introspection data
namespace cruft
{
template <>
struct type<foo>
{
typedef std::tuple<
field<foo,int,&foo::a>,
field<foo,float,&foo::b>
> fields;
};
template <> const std::string field<foo,int,&foo::a>::name = "a";
template <> const std::string field<foo,float,&foo::b>::name = "b";
}
//-----------------------------------------------------------------------------
template <typename A, typename B>
static
bool
equal (A &&a, B &&b)
{
return std::equal (
std::begin (a), std::end (a),
std::begin (b), std::end (b)
);
}
//-----------------------------------------------------------------------------
int main ()
{
cruft::TAP::logger tap;
// Ensure tuples are mapped to themselves with type_tuple::type
{
using src_t = std::tuple<int>;
using dst_t = typename cruft::type_tuple<src_t>::type;
tap.expect (std::is_same<src_t, dst_t>::value, "static identity type_tuple");
}
// Check member extraction from a simple POD structure.
{
foo d_foo { 7, 42.0 };
auto f_tuple = cruft::as_tuple (d_foo);
tap.expect (cruft::equal (d_foo.a, std::get<0> (f_tuple)) &&
cruft::equal (d_foo.b, std::get<1> (f_tuple)),
"dynamic member access after conversion to tuple");
}
tap.expect_eq (cruft::view (cruft::type_name<i08> ()), "i08", "i08 type_name");
tap.expect_eq (cruft::view (cruft::type_name<i16> ()), "i16", "i16 type_name");
tap.expect_eq (cruft::view (cruft::type_name<i32> ()), "i32", "i32 type_name");
tap.expect_eq (cruft::view (cruft::type_name<i64> ()), "i64", "i64 type_name");
tap.expect_eq (cruft::view (cruft::type_name<u08> ()), "u08", "u08 type_name");
tap.expect_eq (cruft::view (cruft::type_name<u16> ()), "u16", "u16 type_name");
tap.expect_eq (cruft::view (cruft::type_name<u32> ()), "u32", "u32 type_name");
tap.expect_eq (cruft::view (cruft::type_name<u64> ()), "u64", "u64 type_name");
tap.expect_eq (cruft::view (cruft::type_name<f32> ()), "f32", "f32 type_name");
tap.expect_eq (cruft::view (cruft::type_name<f64> ()), "f64", "f64 type_name");
tap.expect_eq (cruft::view (cruft::type_name<foo> ()), "foo", "struct type_name");
tap.expect_eq (cruft::view (cruft::type_name<bar::qux> ()), "qux", "de-namespaced struct type_name");
tap.expect (
equal (
cruft::type_name_with_namespace<bar::qux> (),
std::string_view ("bar::qux")
),
"namespaced struct"
);
tap.expect (
equal (
cruft::type_name_with_namespace<bar::templated_qux<int>> (),
std::string_view ("bar::templated_qux<int>")
),
"namespaced templated struct"
);
tap.expect_eq (
cruft::type_name<templated_with_type<int>> (),
std::string_view {"templated_with_type<int>"},
"templated_with_type"
);
tap.expect_eq (
cruft::type_name<templated_with_type<int, int>> (),
std::string_view {"templated_with_type<int, int>"},
"templated_with_type"
);
tap.expect_eq (
cruft::type_name<templated_with_type<templated_with_type<int>>> (),
std::string_view {"templated_with_type<templated_with_type<int> >"},
"templated_with_type"
);
tap.expect_eq (
cruft::type_name<templated_with_value<-1>> (),
std::string_view {"templated_with_value<-1>"},
"templated_with_value"
);
tap.expect_eq (
cruft::type_name<templated_with_enum<FOO>> (),
std::string_view {"templated_with_enum<FOO>"},
"templated_with_enum"
);
return tap.status ();
}