From 43253a2741161fdfb39e3c32c5bd196b10872834 Mon Sep 17 00:00:00 2001 From: Mark Herring Date: Sat, 28 Sep 2024 22:16:58 -0500 Subject: [PATCH] Updated unit test sim --- include/trick/tmm_alloc_args.hh | 24 +++++++-- test/SIM_tmm_with_args/RUN_test/input.py | 9 ++++ test/SIM_tmm_with_args/RUN_test/unit_test.py | 26 ++++------ test/SIM_tmm_with_args/S_define | 50 ++++++++++++++++--- .../models/alloc_with_args.cc | 40 ++++++++++++++- .../models/alloc_with_args.hh | 35 +++---------- 6 files changed, 130 insertions(+), 54 deletions(-) diff --git a/include/trick/tmm_alloc_args.hh b/include/trick/tmm_alloc_args.hh index 80189207..a6e4963e 100644 --- a/include/trick/tmm_alloc_args.hh +++ b/include/trick/tmm_alloc_args.hh @@ -12,18 +12,35 @@ LIBRARY DEPENDENCY: #include "trick/trick_type_traits.hh" -/* -In the case that a TrickTypeToString for some type T exist, then this function is valid and will be compiled. +/** + In the case that a TrickTypeToString 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 std::enable_if::value, T*>::type tmm_alloc_args(Args&&... args) { void* new_alloc = trick_MM->declare_var(TrickTypeToString::getName().c_str()); - std::cout << "Allocating: " << TrickTypeToString::getName() << std::endl; return new (new_alloc) T(std::forward(args)...); } +/** +In the case that a TrickTypeToString 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 std::enable_if::value, T*>::type +tmm_alloc_args(Args&&... args, T* address) +{ + return new (address) T(std::forward(args)...); +} + /* In the case that a TrickTypeToString for some type T does NOT exist, then this function is valid and will be compiled. */ @@ -31,6 +48,7 @@ template typename std::enable_if::value, T*>::type tmm_alloc_args(Args&&... args) { + //Compilation will always fail here. static_assert(Trick::always_false::value, "You've attempted to call tmm_alloc_args using a type(T) that does not have an implemented specialization for TrickTypeToString."); diff --git a/test/SIM_tmm_with_args/RUN_test/input.py b/test/SIM_tmm_with_args/RUN_test/input.py index 575a3b8d..1368a5cc 100644 --- a/test/SIM_tmm_with_args/RUN_test/input.py +++ b/test/SIM_tmm_with_args/RUN_test/input.py @@ -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") diff --git a/test/SIM_tmm_with_args/RUN_test/unit_test.py b/test/SIM_tmm_with_args/RUN_test/unit_test.py index ab3b177e..9e2d497c 100644 --- a/test/SIM_tmm_with_args/RUN_test/unit_test.py +++ b/test/SIM_tmm_with_args/RUN_test/unit_test.py @@ -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) diff --git a/test/SIM_tmm_with_args/S_define b/test/SIM_tmm_with_args/S_define index 193da70d..5ba4b381 100644 --- a/test/SIM_tmm_with_args/S_define +++ b/test/SIM_tmm_with_args/S_define @@ -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::getName() << "\n"; - atwargs = tmm_alloc_args(0, 1.0); - std::cout << "Called tmm_alloc_args: " << atwargs << "\n"; + trick_MM->delete_var(static_cast(atwargs_1)); + trick_MM->delete_var(static_cast(atwargs_2)); + trick_MM->delete_var(static_cast(atwargs_3)); + trick_MM->delete_var(static_cast(atwargs_4)); } + + + void constructor_test() + { + atwargs_1 = tmm_alloc_args(); + + atwargs_2 = tmm_alloc_args(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(&test_int, &test_double, test_string); + + //Args: int* double& , std::string + test_int = 4; + test_double = 8.0; + + atwargs_4 = tmm_alloc_args(&test_int, test_double, "test_string"); + + } + + + + + + + + private: }; AllocTestSimObject alloc_test ; \ No newline at end of file diff --git a/test/SIM_tmm_with_args/models/alloc_with_args.cc b/test/SIM_tmm_with_args/models/alloc_with_args.cc index 48cc127e..c2ba5b5c 100644 --- a/test/SIM_tmm_with_args/models/alloc_with_args.cc +++ b/test/SIM_tmm_with_args/models/alloc_with_args.cc @@ -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"; -} \ No newline at end of file +} + +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) +{ + +} \ No newline at end of file diff --git a/test/SIM_tmm_with_args/models/alloc_with_args.hh b/test/SIM_tmm_with_args/models/alloc_with_args.hh index b987bc32..2ccc429b 100644 --- a/test/SIM_tmm_with_args/models/alloc_with_args.hh +++ b/test/SIM_tmm_with_args/models/alloc_with_args.hh @@ -5,6 +5,7 @@ LIBRARY DEPENDENCY: *******************************************************************************/ #include +#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; }; \ No newline at end of file