Updated unit test sim

This commit is contained in:
Mark Herring 2024-09-28 22:16:58 -05:00
parent 95230b06f9
commit 43253a2741
6 changed files with 130 additions and 54 deletions

View File

@ -12,18 +12,35 @@ LIBRARY DEPENDENCY:
#include "trick/trick_type_traits.hh"
/*
In the case that a TrickTypeToString<T> for some type T exist, then this function is valid and will be compiled.
/**
In the case that a TrickTypeToString<T> for some type T exist, then this function is valid and will be compiled.
@details Wrapper around tricks MemoryManager void* declare_var( const char* declaration);) Users can pass constructor
arguments to this function and it will construct type T in the memory return by declare_var
@param Args Parameter pack of arguments pased to the constructor
*/
template<typename T, typename ...Args>
typename std::enable_if<Trick::has_getname<T>::value, T*>::type
tmm_alloc_args(Args&&... args)
{
void* new_alloc = trick_MM->declare_var(TrickTypeToString<T>::getName().c_str());
std::cout << "Allocating: " << TrickTypeToString<T>::getName() << std::endl;
return new (new_alloc) T(std::forward<Args>(args)...);
}
/**
In the case that a TrickTypeToString<T> for some type T exist, then this function is valid and will be compiled.
@details Convenience funcion. No memory allocation, let's user pass in existing valid memory address and construct
the type T in that memory via placement new.
@param Args Parameter pack of arguments pased to the constructor
@param address Memory address the call to T() will be constructed in.
*/
template<typename T, typename ...Args>
typename std::enable_if<Trick::has_getname<T>::value, T*>::type
tmm_alloc_args(Args&&... args, T* address)
{
return new (address) T(std::forward<Args>(args)...);
}
/*
In the case that a TrickTypeToString<T> for some type T does NOT exist, then this function is valid and will be compiled.
*/
@ -31,6 +48,7 @@ template<typename T, typename ...Args>
typename std::enable_if<!Trick::has_getname<T>::value, T*>::type
tmm_alloc_args(Args&&... args)
{
//Compilation will always fail here.
static_assert(Trick::always_false<T>::value,
"You've attempted to call tmm_alloc_args using a type(T) that does not have an implemented specialization for TrickTypeToString.");

View File

@ -5,6 +5,15 @@ trick.sim_control_panel_set_enabled(True)
trick.exec_set_enable_freeze(True)
trick.exec_set_freeze_command(True)
import trick
# Test
EXPECT_EQ(atwargs_1.some_int, 5)
EXPECT_EQ(atwargs_1.some_double, 10.0)
#Use tmm_alloc_args from input file - thanks convert_swig!
alloc_test.atwargs_input_file = trick.AllocTestWithArguments(5, 7.0, TMMName="tmm_alloc_args_input_test_1")

View File

@ -1,37 +1,33 @@
import math
from trick.unit_test import *
print(f"alloc_test.atwargs = {alloc_test}")
def test():
test_suite = "AllocatingUserDefinedTypes"
test_case = "type_AllocTestWithArguments"
alloc_test.atwargs_input_file = trick.AllocTestWithArguments.alloc(5, 7.0)
alloc_test.atwargs_input_file = trick.AllocTestWithArguments(5, 7.0)
TRICK_EXPECT_EQ(alloc_test.atwargs_1.some_int, 0, test_case, test_suite)
TRICK_EXPECT_NEAR(alloc_test.atwargs_1.some_double, 0.0, 1e-6, test_case, test_suite)
trick.add_read(4.0, """TRICK_EXPECT_EQ(alloc_test.atwargs.some_int, 0, test_case, test_suite)""")
trick.add_read(4.0, """TRICK_EXPECT_NEAR(alloc_test.atwargs.some_double, 0, 1e-6, test_case, test_suite)""")
TRICK_EXPECT_EQ(alloc_test.atwargs_2.some_int, 0, test_case, test_suite)
TRICK_EXPECT_NEAR(alloc_test.atwargs_2.some_double, 1.0, 1e-6, test_case, test_suite)
trick.add_read(4.0, """TRICK_EXPECT_EQ(alloc_test.atwargs.some_int, 0, test_case, test_suite)""")
trick.add_read(4.0, """TRICK_EXPECT_NEAR(alloc_test.atwargs.some_double, 0, 1e-6, test_case, test_suite)""")
TRICK_EXPECT_EQ(alloc_test.atwargs_3.some_int, 5, test_case, test_suite)
TRICK_EXPECT_NEAR(alloc_test.atwargs_3.some_double, 10.0, 1e-6, test_case, test_suite)
TRICK_EXPECT_EQ(alloc_test.atwargs_4.some_int, 4, test_case, test_suite)
TRICK_EXPECT_NEAR(alloc_test.atwargs_4.some_double, 8.0, 1e-6, test_case, test_suite)
def main():
trick_utest.unit_tests.enable()
trick_utest.unit_tests.set_file_name( os.getenv("TRICK_HOME") + "/trick_test/SIM_tmm_alloc_args.xml" )
trick_utest.unit_tests.set_test_name( "TMMAllocWithArgsTest" )
trick.add_read(5.0, """test()""")
trick.add_read(2.0, """test()""")
trick.stop(5.0)

View File

@ -12,22 +12,56 @@ PURPOSE:
class AllocTestSimObject : public Trick::SimObject {
public:
AllocTestWithArguments* atwargs;
AllocTestWithArguments* atwargs_1;
AllocTestWithArguments* atwargs_2;
AllocTestWithArguments* atwargs_3;
AllocTestWithArguments* atwargs_4;
AllocTestWithArguments* atwargs_5;
AllocTestWithArguments* atwargs_input_file;
AllocTestSimObject() {
("initialization") init_alloc();
constructor_test();
}
void init_alloc()
~AllocTestSimObject()
{
std::cout << "Entered init_alloc()\n";
//std::cout << TrickTypeToString<AllocTestWithArguments>::getName() << "\n";
atwargs = tmm_alloc_args<AllocTestWithArguments>(0, 1.0);
std::cout << "Called tmm_alloc_args: " << atwargs << "\n";
trick_MM->delete_var(static_cast<void*>(atwargs_1));
trick_MM->delete_var(static_cast<void*>(atwargs_2));
trick_MM->delete_var(static_cast<void*>(atwargs_3));
trick_MM->delete_var(static_cast<void*>(atwargs_4));
}
void constructor_test()
{
atwargs_1 = tmm_alloc_args<AllocTestWithArguments>();
atwargs_2 = tmm_alloc_args<AllocTestWithArguments>(0, 1.0);
//Args: int*, double*, std::string&
int test_int = 5;
double test_double = 10.0;
std::string test_string = "test_string";
atwargs_3 = tmm_alloc_args<AllocTestWithArguments>(&test_int, &test_double, test_string);
//Args: int* double& , std::string
test_int = 4;
test_double = 8.0;
atwargs_4 = tmm_alloc_args<AllocTestWithArguments>(&test_int, test_double, "test_string");
}
private:
};
AllocTestSimObject alloc_test ;

View File

@ -1,5 +1,15 @@
#include "alloc_with_args.hh"
AllocTestWithArguments::AllocTestWithArguments()
:
some_int(0),
some_double(0.0)
{
}
AllocTestWithArguments::AllocTestWithArguments(int* in_int, double& in_double, std::string in_string)
:
some_int(*in_int),
@ -7,4 +17,32 @@ some_double(in_double)
{
std::cout << in_string << "\nn";
}
}
AllocTestWithArguments::AllocTestWithArguments(int* in_int, double *in_double, std::string &a_name)
:
some_int(*in_int),
some_double(*in_double)
{
std::cout << "AllocTestWithArguments constructor with: \n";
std::cout << a_name << std::endl;
std::cout << "in_int: " << in_int << "\n";
std::cout << "in_double: " << in_double << "\n";
}
AllocTestWithArguments::AllocTestWithArguments(int in_int, double in_double)
:
some_int(in_int),
some_double(in_double)
{
std::cout << "AllocTestWithArguments constructor with: \n";
std::cout << "in_int: " << in_int << "\n";
std::cout << "in_double: "
<< in_double << "\n";
}
AllocTestWithArguments::AllocTestWithArguments(Test::SomeStruct in_struct)
{
}

View File

@ -5,6 +5,7 @@ LIBRARY DEPENDENCY:
*******************************************************************************/
#include <iostream>
#include "trick/tmm_alloc_args.hh"
namespace Test {
struct SomeStruct
@ -23,42 +24,22 @@ class AllocTestWithArguments {
public:
AllocTestWithArguments()
:
some_int(0),
some_double(0.0)
{
AllocTestWithArguments();
}
AllocTestWithArguments(int in_int, double in_double)
:
some_int(in_int),
some_double(in_double)
{
std::cout << "AllocTestWithArguments constructor with: \n";
std::cout << "in_int: " << in_int << "\n";
std::cout << "in_double: " << in_double << "\n";
}
AllocTestWithArguments(int in_int, double in_double);
AllocTestWithArguments(int* in_int, double *in_double, std::string &a_name)
:
some_int(*in_int),
some_double(*in_double)
{
std::cout << "AllocTestWithArguments constructor with: \n";
std::cout << a_name << std::endl;
std::cout << "in_int: " << in_int << "\n";
std::cout << "in_double: " << in_double << "\n";
}
AllocTestWithArguments(int* in_int, double *in_double, std::string &a_name);
AllocTestWithArguments(int*, double&, std::string);
AllocTestWithArguments(Test::SomeStruct in_struct);
~AllocTestWithArguments() {
std::cout << "AllocTestWithArguments desctruct.\n";
}
int some_int;
double some_double;
Test::SomeStruct* my_struct;
};