libcruft-util/test/introspection.cpp

84 lines
2.6 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;
};
}
///////////////////////////////////////////////////////////////////////////////
// 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";
}
//-----------------------------------------------------------------------------
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", "namespaced struct type_name");
return tap.status ();
}