forked from Alepha/Alepha
Table testing created.
Haven't tested `VectorCases` yet.
This commit is contained in:
117
Testing/TableTest.h
Normal file
117
Testing/TableTest.h
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
static_assert( __cplusplus > 201700, "C++17 Required" );
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Alepha/Alepha.h>
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <Alepha/Meta/is_vector.h>
|
||||||
|
#include <Alepha/Utility/evaluation.h>
|
||||||
|
|
||||||
|
namespace Alepha::Hydrogen::Testing
|
||||||
|
{
|
||||||
|
inline namespace exports { inline namespace table_test {} }
|
||||||
|
|
||||||
|
namespace detail::table_test
|
||||||
|
{
|
||||||
|
inline namespace exports
|
||||||
|
{
|
||||||
|
template< auto > struct TableTest;
|
||||||
|
}
|
||||||
|
|
||||||
|
using namespace Utility::exports::evaluation;
|
||||||
|
|
||||||
|
template< typename RetVal, typename ... Args, RetVal (*function)( Args... ) >
|
||||||
|
struct exports::TableTest< function >
|
||||||
|
{
|
||||||
|
using args_type= std::tuple< Args... >;
|
||||||
|
|
||||||
|
struct Cases
|
||||||
|
{
|
||||||
|
using TestDescription= std::tuple< std::string, args_type, RetVal >;
|
||||||
|
|
||||||
|
std::vector< TestDescription > tests;
|
||||||
|
|
||||||
|
explicit
|
||||||
|
Cases( std::initializer_list< TestDescription > initList )
|
||||||
|
: tests( initList ) {}
|
||||||
|
|
||||||
|
int
|
||||||
|
operator() () const
|
||||||
|
{
|
||||||
|
int failureCount= 0;
|
||||||
|
for( const auto &[ comment, params, expected ]: tests )
|
||||||
|
{
|
||||||
|
if( std::apply( function, params ) != expected )
|
||||||
|
{
|
||||||
|
std::cout << " FAILURE: " << comment << std::endl;
|
||||||
|
++failureCount;
|
||||||
|
}
|
||||||
|
else std::cout << " SUCCESS: " << comment << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return failureCount;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VectorCases;
|
||||||
|
};
|
||||||
|
|
||||||
|
template< typename RetVal, typename ... Args, RetVal (*function)( Args... ) >
|
||||||
|
struct TableTest< function >::VectorCases
|
||||||
|
{
|
||||||
|
static_assert( sizeof...( Args ) == 1 );
|
||||||
|
static_assert( Meta::is_vector_v< RetVal > );
|
||||||
|
static_assert( Meta::is_vector_v< std::tuple_element_t< 0, std::tuple< Args... > > > );
|
||||||
|
|
||||||
|
using TestDescription= std::tuple< std::string,
|
||||||
|
std::vector< std::pair< typename std::tuple_element_t< 0, std::tuple< Args... > >::value_type, typename RetVal::value_type > > >;
|
||||||
|
|
||||||
|
std::vector< TestDescription > tests;
|
||||||
|
|
||||||
|
explicit
|
||||||
|
VectorCases( std::initializer_list< TestDescription > initList )
|
||||||
|
: tests( initList ) {}
|
||||||
|
|
||||||
|
int
|
||||||
|
operator() () const
|
||||||
|
{
|
||||||
|
int failureCount= 0;
|
||||||
|
for( const auto &[ comment, productions ]: tests )
|
||||||
|
{
|
||||||
|
const auto expected= evaluate <=[&]
|
||||||
|
{
|
||||||
|
std::vector< RetVal > rv;
|
||||||
|
std::transform( begin( productions ), end( productions ), back_inserter( rv ),
|
||||||
|
[]( const auto &prod ) { return prod.second; } );
|
||||||
|
return rv;
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto params= evaluate <=[&]
|
||||||
|
{
|
||||||
|
std::vector< RetVal > rv;
|
||||||
|
std::transform( begin( productions ), end( productions ), back_inserter( rv ),
|
||||||
|
[]( const auto &prod ) { return prod.first; } );
|
||||||
|
return rv;
|
||||||
|
};
|
||||||
|
|
||||||
|
if( std::apply( function, std::tuple{ params } ) != expected )
|
||||||
|
{
|
||||||
|
std::cout << " FAILURE: " << comment << std::endl;
|
||||||
|
++failureCount;
|
||||||
|
}
|
||||||
|
else std::cout << " SUCCESS: " << comment << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return failureCount;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace exports::table_test
|
||||||
|
{
|
||||||
|
using namespace detail::table_test::exports;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,9 +1,12 @@
|
|||||||
|
static_assert( __cplusplus > 201700, "C++17 Required" );
|
||||||
|
|
||||||
#include <Alepha/Testing/test.h>
|
#include <Alepha/Testing/test.h>
|
||||||
|
#include <Alepha/Testing/TableTest.h>
|
||||||
#include <Alepha/Utility/evaluation.h>
|
#include <Alepha/Utility/evaluation.h>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
namespace UnitTest= Alepha::Testing::exports::testing;
|
namespace UnitTest= Alepha::Testing::exports;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -14,8 +17,9 @@ main( const int argcnt, const char *const *const argvec )
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
using namespace UnitTest::literals;
|
|
||||||
using namespace Alepha::Utility::exports::evaluation;
|
using namespace Alepha::Utility::exports::evaluation;
|
||||||
|
using namespace UnitTest::literals;
|
||||||
|
using UnitTest::TableTest;
|
||||||
|
|
||||||
auto registration= enroll <=[]
|
auto registration= enroll <=[]
|
||||||
{
|
{
|
||||||
@ -25,4 +29,11 @@ namespace
|
|||||||
|
|
||||||
auto named1= "named.basic.success"_test <= []{};
|
auto named1= "named.basic.success"_test <= []{};
|
||||||
auto named2= -"named.basic.failure"_test <=[]{ return 1; };
|
auto named2= -"named.basic.failure"_test <=[]{ return 1; };
|
||||||
|
|
||||||
|
int identity( int a ){ return a; }
|
||||||
|
auto namedTable1= "named.table.success"_test <=TableTest< identity >::Cases
|
||||||
|
{
|
||||||
|
{ "smoke", { 1 }, 1 },
|
||||||
|
//{ "fail", { 2 }, 1 },
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user