diff --git a/docs/documentation/simulation_capabilities/STL-capabilities.md b/docs/documentation/simulation_capabilities/STL-capabilities.md index 0e99d321..48f902b0 100644 --- a/docs/documentation/simulation_capabilities/STL-capabilities.md +++ b/docs/documentation/simulation_capabilities/STL-capabilities.md @@ -119,12 +119,4 @@ The types in an std::pair cannot be sequence STL types (vector, queue, stack, et std::pair> ``` -The types inside an std::set cannot be any STL types (excluding strings). -``` - // This will fail to compile - std::set> -``` - -These bugs are being worked on. - [Continue to Threads](Threads) diff --git a/include/trick/checkpoint_sequence_stl.hh b/include/trick/checkpoint_sequence_stl.hh index bb6f5918..ce616692 100644 --- a/include/trick/checkpoint_sequence_stl.hh +++ b/include/trick/checkpoint_sequence_stl.hh @@ -111,7 +111,8 @@ int checkpoint_sequence_s(STL & in_stl , std::string object_name , std::string v std::ostringstream index_string ; index_string << ii ; //message_publish(1, "recursive call to checkpoint_stl %s\n", __PRETTY_FUNCTION__) ; - checkpoint_stl( (*it) , object_name + "_" + var_name , index_string.str() ) ; + checkpoint_stl( const_cast(*it) , object_name + "_" + var_name , index_string.str() ) ; + } } } diff --git a/test/SIM_stls/models/STLCheckpoint.cpp b/test/SIM_stls/models/STLCheckpoint.cpp index 45cc88f3..82fd4f06 100644 --- a/test/SIM_stls/models/STLCheckpoint.cpp +++ b/test/SIM_stls/models/STLCheckpoint.cpp @@ -142,13 +142,27 @@ int STLCheckpoint::addData() { string_set.insert("abc") ; string_set.insert("def") ; - // for (int j = 0; j < 5; j++) { - // std::vector temp; - // for (int i = j; i < 10; i++) { - // temp.push_back(i); - // } - // vector_set.insert(temp); - // } + for (int j = 0; j < 5; j++) { + std::vector temp; + for (int i = j; i < 10; i++) { + temp.push_back(i); + } + vector_set.insert(temp); + } + + pair_set.emplace("One", 1); + pair_set.emplace("Two", 2); + pair_set.emplace("Three", 3); + + std::map m; + m[5] = 500.5; + m[1] = 100.1; + std::map m2; + m2[5] = 5000.5; + m2[1] = 1000.1; + + nested_map_set.insert(m); + nested_map_set.insert(m2); for (int j = 0; j < 5; j++) { std::vector temp; @@ -172,6 +186,16 @@ int STLCheckpoint::addData() { string_multiset.insert("abc") ; string_multiset.insert("def") ; + pair_multiset.emplace(5, 5); + pair_multiset.emplace(5, 5); + pair_multiset.emplace(5, 4); + pair_multiset.emplace(4, 4); + + vec_multiset.emplace(std::vector({1,2,3,4})); + vec_multiset.emplace(std::vector({1,2,3,4})); + vec_multiset.emplace(std::vector({1,2,3,4,5})); + + uint_stack.push(1) ; uint_stack.push(2) ; uint_stack.push(3) ; @@ -379,8 +403,6 @@ int STLCheckpoint::addData() { vec_user_defined_ptr.push_back(temp_wrapper); } - - return 0; } @@ -555,6 +577,33 @@ int STLCheckpoint::test() { std::set string_set_copy = {"efg", "abc", "def"}; TRICK_EXPECT_EQ(string_set, string_set_copy, test_suite, "string_set"); + std::set> vector_set_copy; + for (int j = 0; j < 5; j++) { + std::vector temp; + for (int i = j; i < 10; i++) { + temp.push_back(i); + } + vector_set_copy.insert(temp); + } + TRICK_EXPECT_EQ(vector_set, vector_set_copy, test_suite, "vector_set"); + + std::set> pair_set_copy; + pair_set_copy.emplace("One", 1); + pair_set_copy.emplace("Two", 2); + pair_set_copy.emplace("Three", 3); + TRICK_EXPECT_EQ(pair_set, pair_set_copy, test_suite, "pair_set"); + + std::set> nested_map_set_copy; + std::map m; + m[5] = 500.5; + m[1] = 100.1; + std::map m2; + m2[5] = 5000.5; + m2[1] = 1000.1; + nested_map_set_copy.insert(m); + nested_map_set_copy.insert(m2); + TRICK_EXPECT_EQ(nested_map_set, nested_map_set_copy, test_suite, "nested_map_set"); + std::queue> vector_queue_copy; for (int j = 0; j < 5; j++) { std::vector temp; @@ -571,6 +620,19 @@ int STLCheckpoint::test() { std::multiset string_multiset_copy = {"efg", "abc", "def", "efg", "abc", "def"}; TRICK_EXPECT_EQ(string_multiset, string_multiset_copy, test_suite, "string_multiset"); + std::multiset> pair_multiset_copy; + pair_multiset_copy.emplace(5, 5); + pair_multiset_copy.emplace(5, 5); + pair_multiset_copy.emplace(5, 4); + pair_multiset_copy.emplace(4, 4); + TRICK_EXPECT_EQ(pair_multiset, pair_multiset_copy, test_suite, "pair_multiset"); + + std::multiset> vec_multiset_copy; + vec_multiset_copy.emplace(std::vector({1,2,3,4})); + vec_multiset_copy.emplace(std::vector({1,2,3,4})); + vec_multiset_copy.emplace(std::vector({1,2,3,4,5})); + TRICK_EXPECT_EQ(vec_multiset, vec_multiset_copy, test_suite, "vec_multiset"); + std::stack uint_stack_copy; uint_stack_copy.push(1) ; uint_stack_copy.push(2) ; diff --git a/test/SIM_stls/models/STLCheckpoint.hh b/test/SIM_stls/models/STLCheckpoint.hh index 328615fe..be61a8c0 100644 --- a/test/SIM_stls/models/STLCheckpoint.hh +++ b/test/SIM_stls/models/STLCheckpoint.hh @@ -72,13 +72,19 @@ class STLCheckpoint { std::set< int > int_set ; std::set< std::string > string_set ; - // std::set< std::vector > vector_set; + std::set< std::vector > vector_set; + std::set< std::pair > pair_set; + std::set> nested_map_set; + std::queue< std::vector > vector_queue; std::multiset< long > long_multiset ; std::multiset< std::string > string_multiset ; + std::multiset< std::pair< int,int > > pair_multiset ; + std::multiset< std::vector< int > > vec_multiset ; + std::stack< unsigned int > uint_stack ; std::stack< std::string > string_stack ; diff --git a/trick_source/sim_services/MemoryManager/MemoryManager_restore.cpp b/trick_source/sim_services/MemoryManager/MemoryManager_restore.cpp index 51f7106b..c1077b53 100644 --- a/trick_source/sim_services/MemoryManager/MemoryManager_restore.cpp +++ b/trick_source/sim_services/MemoryManager/MemoryManager_restore.cpp @@ -115,7 +115,6 @@ int Trick::MemoryManager::init_from_checkpoint(const char* filename, bool restor read_checkpoint( filename, restore_stls); - if (debug_level) { std::cout << std::endl << "Initialization from checkpoint finished." << std::endl; std::cout.flush(); diff --git a/trick_source/sim_services/MemoryManager/test/MM_stl_checkpoint.cc b/trick_source/sim_services/MemoryManager/test/MM_stl_checkpoint.cc index 2640e63f..a7f1f012 100644 --- a/trick_source/sim_services/MemoryManager/test/MM_stl_checkpoint.cc +++ b/trick_source/sim_services/MemoryManager/test/MM_stl_checkpoint.cc @@ -757,88 +757,284 @@ TEST_F(MM_stl_checkpoint, nested_list_stack ) { // These will fail to compile -TEST_F(MM_stl_checkpoint, DISABLED_i_set ) { +TEST_F(MM_stl_checkpoint, i_set ) { // ARRANGE // Make a testbed - // STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); - // // Likely to be some repeats in here - // std::vector test_data = get_test_data(1000); + // Likely to be some repeats in here + std::vector test_data = get_test_data(1000); - // // Prepare the STL to be tested - // for (int i = 0; i < test_data.size(); i++) { - // testbed->i_set.insert(test_data[i]); - // } + // Prepare the STL to be tested + for (int i = 0; i < test_data.size(); i++) { + testbed->i_set.insert(test_data[i]); + } - // ATTRIBUTES* vec_attr = memmgr->ref_attributes("my_alloc.i_set")->attr; + ATTRIBUTES* vec_attr = memmgr->ref_attributes("my_alloc.i_set")->attr; - // // ACT - // // Call the checkpoint function that is being tested - // (vec_attr->checkpoint_stl)((void *) &testbed->i_set, "my_alloc", vec_attr->name) ; + // ACT + // Call the checkpoint function that is being tested + (vec_attr->checkpoint_stl)((void *) &testbed->i_set, "my_alloc", vec_attr->name) ; - // // ASSERT - // validate_temp_set(memmgr, std::string("my_alloc"), std::string("i_set"), std::set (test_data.begin(), test_data.end())); + // ASSERT + validate_temp_set(memmgr, std::string("my_alloc"), std::string("i_set"), std::set (test_data.begin(), test_data.end())); } -TEST_F(MM_stl_checkpoint, DISABLED_s_set ) { +TEST_F(MM_stl_checkpoint, vector_set ) { // ARRANGE // Make a testbed - // STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); - // std::vector test_first = get_test_data(10); - // std::vector test_second = get_test_data(10); + std::vector test_data = get_test_data(1000); + std::vector sizes = {500, 250, 125, 62, 31, 15, 7, 3, 1, 6}; + // The ordering is confusing on these, so just make a copy and use that + std::set> expected_data; + // Prepare the STL to be tested + int start_index = 0; + for (int i = 0; i < sizes.size(); i++) { + std::vector temp_vec; + for (int j = 0; j < sizes[i]; j++) { + temp_vec.push_back(test_data[start_index+j]); + } + testbed->vector_set.insert(std::vector(temp_vec)); + expected_data.insert(std::vector(temp_vec)); - // // Prepare the STL to be tested - // for (int i = 0; i < test_first.size(); i++) { - // testbed->s_set.insert(std::pair(test_first[i], test_second[i])); - // } + start_index += sizes[i]; + } - // ATTRIBUTES* vec_attr = memmgr->ref_attributes("my_alloc.s_set")->attr; + ATTRIBUTES* vec_attr = memmgr->ref_attributes("my_alloc.vector_set")->attr; - // // ACT - // // Call the checkpoint function that is being tested - // (vec_attr->checkpoint_stl)((void *) &testbed->s_set, "my_alloc", vec_attr->name) ; + // ACT + // Call the checkpoint function that is being tested + (vec_attr->checkpoint_stl)((void *) &testbed->vector_set, "my_alloc", vec_attr->name) ; - // // ASSERT - // for (int i = 0; i < test_first.size(); i++) { - // std::string var_name = "s_set_" + std::to_string(i); - // validate_temp_pair(memmgr, std::string("my_alloc"), var_name, std::pair(test_first[i], test_second[i])); - // } + // ASSERT + int index = 0; + std::vector sizes_in_right_order; + for (std::vector vec : expected_data) { + std::string var_name = "vector_set_" + std::to_string(index++); + validate_temp_sequence(memmgr, std::string("my_alloc"), var_name, vec); + sizes_in_right_order.push_back(vec.size()); + } + validate_links(memmgr, std::string("my_alloc"), std::string("vector_set"), sizes_in_right_order); + // validate_temp_set(memmgr, std::string("my_alloc"), std::string("vector_set"), std::set (test_data.begin(), test_data.end())); +} + +TEST_F(MM_stl_checkpoint, s_set ) { + // ARRANGE + // Make a testbed + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + + std::vector test_first = get_test_data(10); + std::vector test_second = get_test_data(10); + + // make a copy to deal with ordering + std::set> expected; + + // Prepare the STL to be tested + for (int i = 0; i < test_first.size(); i++) { + testbed->s_set.insert(std::pair(test_first[i], test_second[i])); + expected.insert(std::pair(test_first[i], test_second[i])); + } + + ATTRIBUTES* vec_attr = memmgr->ref_attributes("my_alloc.s_set")->attr; + + // ACT + // Call the checkpoint function that is being tested + (vec_attr->checkpoint_stl)((void *) &testbed->s_set, "my_alloc", vec_attr->name) ; + + // ASSERT + int index = 0; + for (std::pair expected_pair : expected) { + std::string var_name = "s_set_" + std::to_string(index++); + validate_temp_pair(memmgr, std::string("my_alloc"), var_name, std::pair(expected_pair.first, expected_pair.second)); + } + validate_links_pairs(memmgr, "my_alloc", "s_set", test_first.size()); } -TEST_F(MM_stl_checkpoint, DISABLED_nested_list_set ) { +TEST_F(MM_stl_checkpoint, nested_map_set ) { // // ARRANGE // // Make a testbed - // STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); - // std::vector test_data = get_test_data(10); - // std::vector list_sizes = {2, 7, 1}; - // // Prepare the STL to be tested + std::vector test_keys = get_test_data(100); + std::vector test_vals = get_test_data(100); + // Prepare the STL to be tested + + // Make a copy to deal with ordering mess + std::set> expected; - // int data_index = 0; - // for (int i = 0; i < list_sizes.size(); i++) { - // testbed->nested_list_stack.push(std::list(test_data.begin() + data_index, test_data.begin() + data_index + list_sizes[i])); - // data_index += list_sizes[i]; - // } + for (int i = 0; i < 10; i++) { + std::map temp_map; + for (int j = 0; j < 10; j++) { + temp_map[test_keys[i*10+j]] = test_vals[i*10+j]; + } + expected.emplace(temp_map); + testbed->nested_map_set.emplace(temp_map); + } - // ATTRIBUTES* vec_attr = memmgr->ref_attributes("my_alloc.nested_list_stack")->attr; + ATTRIBUTES* vec_attr = memmgr->ref_attributes("my_alloc.nested_map_set")->attr; - // // ACT - // // Call the checkpoint function that is being tested - // (vec_attr->checkpoint_stl)((void *) &testbed->nested_list_stack, "my_alloc", vec_attr->name) ; + // ACT + // Call the checkpoint function that is being tested + (vec_attr->checkpoint_stl)((void *) &testbed->nested_map_set, "my_alloc", vec_attr->name) ; - // // ASSERT - // data_index = 0; - // for (int i = 0; i < list_sizes.size(); i++) { - // // reverse data - just go through the lists backwards - // std::string var_name = "nested_list_stack_" + std::to_string(list_sizes.size()-1-i); - // validate_temp_sequence(memmgr, std::string("my_alloc"), var_name, std::vector(test_data.begin() + data_index, test_data.begin() + data_index + list_sizes[i])); - // data_index += list_sizes[i]; - // } + // ASSERT + int index = 0; + for (std::map expected_map : expected) { + std::string var_name = "nested_map_set_" + std::to_string(index++); + validate_temp_map(memmgr, "my_alloc", var_name, expected_map); + } + + // Validate links? +} + +// Multisets + +TEST_F(MM_stl_checkpoint, i_multiset ) { + // ARRANGE + // Make a testbed + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + + // Likely to be some repeats in here + std::vector test_data = get_test_data(1000); + + std::multiset expected; + + // Prepare the STL to be tested + for (int i = 0; i < test_data.size(); i++) { + testbed->i_multiset.insert(test_data[i]); + testbed->i_multiset.insert(test_data[i]); + expected.insert(test_data[i]); + expected.insert(test_data[i]); + } + + ATTRIBUTES* vec_attr = memmgr->ref_attributes("my_alloc.i_multiset")->attr; + + // ACT + // Call the checkpoint function that is being tested + (vec_attr->checkpoint_stl)((void *) &testbed->i_multiset, "my_alloc", vec_attr->name) ; + + // ASSERT + validate_temp_sequence(memmgr, std::string("my_alloc"), std::string("i_multiset"), std::vector (expected.begin(), expected.end())); +} + +TEST_F(MM_stl_checkpoint, vector_multiset ) { + // ARRANGE + // Make a testbed + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + + std::vector test_data = get_test_data(1000); + std::vector sizes = {500, 250, 125, 62, 31, 15, 7, 3, 1, 6}; + // The ordering is confusing on these, so just make a copy and use that + std::multiset> expected_data; + // Prepare the STL to be tested + int start_index = 0; + for (int i = 0; i < sizes.size(); i++) { + std::vector temp_vec; + for (int j = 0; j < sizes[i]; j++) { + temp_vec.push_back(test_data[start_index+j]); + } + // just add 2? + testbed->vector_multiset.insert(std::vector(temp_vec)); + testbed->vector_multiset.insert(std::vector(temp_vec)); + expected_data.insert(std::vector(temp_vec)); + expected_data.insert(std::vector(temp_vec)); + + start_index += sizes[i]; + } + + ATTRIBUTES* vec_attr = memmgr->ref_attributes("my_alloc.vector_multiset")->attr; + + // ACT + // Call the checkpoint function that is being tested + (vec_attr->checkpoint_stl)((void *) &testbed->vector_multiset, "my_alloc", vec_attr->name) ; + + // ASSERT + int index = 0; + std::vector sizes_in_right_order; + for (std::vector vec : expected_data) { + std::string var_name = "vector_multiset_" + std::to_string(index++); + validate_temp_sequence(memmgr, std::string("my_alloc"), var_name, vec); + sizes_in_right_order.push_back(vec.size()); + } + validate_links(memmgr, std::string("my_alloc"), std::string("vector_multiset"), sizes_in_right_order); +} + +TEST_F(MM_stl_checkpoint, s_multiset ) { + // ARRANGE + // Make a testbed + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + + std::vector test_first = get_test_data(10); + std::vector test_second = get_test_data(10); + + // make a copy to deal with ordering + std::multiset> expected; + + // Prepare the STL to be tested + for (int i = 0; i < test_first.size(); i++) { + testbed->s_multiset.insert(std::pair(test_first[i], test_second[i])); + testbed->s_multiset.insert(std::pair(test_first[i], test_second[i])); + expected.insert(std::pair(test_first[i], test_second[i])); + expected.insert(std::pair(test_first[i], test_second[i])); + } + + ATTRIBUTES* vec_attr = memmgr->ref_attributes("my_alloc.s_multiset")->attr; + + // ACT + // Call the checkpoint function that is being tested + (vec_attr->checkpoint_stl)((void *) &testbed->s_multiset, "my_alloc", vec_attr->name) ; + + // ASSERT + int index = 0; + for (std::pair expected_pair : expected) { + std::string var_name = "s_multiset_" + std::to_string(index++); + validate_temp_pair(memmgr, std::string("my_alloc"), var_name, std::pair(expected_pair.first, expected_pair.second)); + } + validate_links_pairs(memmgr, "my_alloc", "s_multiset", expected.size()); } +TEST_F(MM_stl_checkpoint, nested_map_multiset ) { + // // ARRANGE + // // Make a testbed + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + + std::vector test_keys = get_test_data(100); + std::vector test_vals = get_test_data(100); + // Prepare the STL to be tested + + // Make a copy to deal with ordering mess + std::multiset> expected; + + for (int i = 0; i < 10; i++) { + std::map temp_map; + for (int j = 0; j < 10; j++) { + temp_map[test_keys[i*10+j]] = test_vals[i*10+j]; + } + expected.emplace(temp_map); + expected.emplace(temp_map); + testbed->nested_map_multiset.emplace(temp_map); + testbed->nested_map_multiset.emplace(temp_map); + } + + ATTRIBUTES* vec_attr = memmgr->ref_attributes("my_alloc.nested_map_multiset")->attr; + + // ACT + // Call the checkpoint function that is being tested + (vec_attr->checkpoint_stl)((void *) &testbed->nested_map_multiset, "my_alloc", vec_attr->name) ; + + // ASSERT + int index = 0; + for (std::map expected_map : expected) { + std::string var_name = "nested_map_multiset_" + std::to_string(index++); + validate_temp_map(memmgr, "my_alloc", var_name, expected_map); + } + + // Validate links? +} // Arrays diff --git a/trick_source/sim_services/MemoryManager/test/MM_stl_restore.cc b/trick_source/sim_services/MemoryManager/test/MM_stl_restore.cc index fcc7ca3b..8d05d14f 100644 --- a/trick_source/sim_services/MemoryManager/test/MM_stl_restore.cc +++ b/trick_source/sim_services/MemoryManager/test/MM_stl_restore.cc @@ -823,14 +823,322 @@ TEST_F(MM_stl_restore, i_set ) { EXPECT_EQ(memmgr->var_exists("my_alloc_i_set"), 0); } -// These can't be written yet since the data structures will fail at compile time -TEST_F(MM_stl_restore, DISABLED_s_set ) { } +TEST_F(MM_stl_restore, s_set ) { + // ARRANGE + // make a testbed object + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + ATTRIBUTES * attr = memmgr->ref_attributes("my_alloc.s_set")->attr; -TEST_F(MM_stl_restore, DISABLED_nested_map_set ) { } + std::vector test_first= get_test_data(20); + std::vector test_second = get_test_data(20); -TEST_F(MM_stl_restore, DISABLED_i_multiset ) { } + std::set> expected; + std::string * data_links = (std::string *) memmgr->declare_var("std::string my_alloc_s_set[20]"); + + for (int i = 0; i < test_first.size(); i++) { + data_links[i] = "my_alloc_s_set_" + std::to_string(i); + + std::string first_var_name = "int my_alloc_s_set_" + std::to_string(i) + "_first"; + std::string second_var_name = "int my_alloc_s_set_" + std::to_string(i) + "_second"; + + int * temp_first = (int *)memmgr->declare_var(first_var_name.c_str()); + *temp_first = test_first[i]; + int * temp_second = (int *)memmgr->declare_var(second_var_name.c_str()); + *temp_second = test_second[i]; + + // Build a copy of the expected data + expected.insert(std::pair(test_first[i], test_second[i])); + } + + // ACT + (attr->restore_stl)((void *) &testbed->s_set, "my_alloc", attr->name) ; + + // ASSERT + // Make sure the STL has been populated + EXPECT_EQ(testbed->s_set, expected); + + // Check that all the temporary variables have been deleted + EXPECT_EQ(memmgr->var_exists("my_alloc_s_set"), 0); + for (int i = 0; i < test_first.size(); i++) { + EXPECT_EQ(memmgr->var_exists("my_alloc_s_set_" + std::to_string(i) + "_first"), 0); + EXPECT_EQ(memmgr->var_exists("my_alloc_s_set_" + std::to_string(i) + "_second"), 0); + } +} + +TEST_F(MM_stl_restore, vector_set ) { + // ARRANGE + // make a testbed object + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + ATTRIBUTES* attr = memmgr->ref_attributes("my_alloc.vector_set")->attr; + + std::vector test_data = get_test_data(50); + // Make sure we can handle lists of different sizes + std::vector lengths = {20, 4, 1, 1, 14, 10}; + + // Build a copy of the expected data + std::set> expected; + + // Register the expected temporary variables with the memory manager + int start_index = 0; + std::string * nested_list_links = (std::string *) memmgr->declare_var( "std::string my_alloc_vector_set[6]"); + for (int i = 0; i < lengths.size(); i++) { + nested_list_links[i] = "my_alloc_vector_set_" + std::to_string(i); + + std::string temp_var_name = "int my_alloc_vector_set_" + std::to_string(i) + "[" + std::to_string(lengths[i]) + "]"; + std::vector temp_expected; + int * temp_arr = (int *)memmgr->declare_var(temp_var_name.c_str()); + for (int j = 0; j < lengths[i]; j++) { + temp_arr[j] = test_data[start_index + j]; + temp_expected.push_back(test_data[start_index + j]); + } + + expected.insert(temp_expected); + + start_index += lengths[i]; + } + + // ACT + (attr->restore_stl)((void *) &testbed->vector_set, "my_alloc", attr->name) ; + + // ASSERT + // Make sure the STL has been populated + ASSERT_EQ(testbed->vector_set, expected); + + // Check that all the temporary variables have been deleted + EXPECT_EQ(memmgr->var_exists("my_alloc_vector_set"), 0); + for (int i = 0; i < test_data.size()/2; i++) { + EXPECT_EQ(memmgr->var_exists("my_alloc_vector_set_" + std::to_string(i)), 0); + } +} + +TEST_F(MM_stl_restore, nested_map_set ) { + // ARRANGE + // make a testbed object + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + ATTRIBUTES* attr = memmgr->ref_attributes("my_alloc.nested_map_set")->attr; + + std::vector test_keys = get_test_data(60); + std::vector test_vals = get_test_data(60); + + std::set> expected; + + std::string * nested_map_links = (std::string *) memmgr->declare_var( "std::string my_alloc_nested_map_set[6]"); + for (int i = 0; i < 6; i++) { + nested_map_links[i] = "my_alloc_nested_map_set_" + std::to_string(i); + + std::string keys_var_name = "short my_alloc_nested_map_set_" + std::to_string(i) + "_keys[10]"; + std::string vals_var_name = "double my_alloc_nested_map_set_" + std::to_string(i) + "_data[10]"; + + short * map_keys_temp = (short *) memmgr->declare_var( keys_var_name.c_str() ); + double * map_vals_temp = (double *) memmgr->declare_var( vals_var_name.c_str() ); + + std::map temp_map; + for (int j = 0; j < 10; j++) { + map_keys_temp[j] = test_keys[(i*10) + j]; + map_vals_temp[j] = test_vals[(i*10) + j]; + + temp_map[test_keys[(i*10) + j]] = test_vals[(i*10) + j]; + } + + expected.emplace(temp_map); + } + + // ACT + (attr->restore_stl)((void *) &testbed->nested_map_set, "my_alloc", attr->name) ; + + // ASSERT + ASSERT_EQ(testbed->nested_map_set, expected); + + + // Check that all the temporary variables have been deleted + EXPECT_EQ(memmgr->var_exists("my_alloc_nested_map_set"), 0); + for (int i = 0; i < expected.size(); i++) { + EXPECT_EQ(memmgr->var_exists("my_alloc_nested_map_set_" + std::to_string(i) + "_data"), 0); + EXPECT_EQ(memmgr->var_exists("my_alloc_nested_map_set_" + std::to_string(i) + "_vals"), 0); + } +} + +// Multiset + +TEST_F(MM_stl_restore, i_multiset ) { + // ARRANGE + // make a testbed object + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + ATTRIBUTES* attr = memmgr->ref_attributes("my_alloc.i_multiset")->attr; + + + std::vector test_data = get_test_data(100); + std::multiset expected; + + // Make the expected structure first + for (int val : test_data) { + expected.insert(val); + expected.insert(val); + } + + // Register the expected temporary variables with the memory manager + std::string var_name = "int my_alloc_i_multiset[" + std::to_string(expected.size()) + "]"; + int * temp_data = (int *) memmgr->declare_var(var_name.c_str()); + int index = 0; + for (const int val : expected) { + temp_data[index++] = val; + } + + // ACT + (attr->restore_stl)((void *) &testbed->i_multiset, "my_alloc", attr->name) ; + + // ASSERT + // Make sure the STL has been populated + EXPECT_EQ(testbed->i_multiset, expected); + + // Check that all the temporary variables have been deleted + EXPECT_EQ(memmgr->var_exists("my_alloc_i_multiset"), 0); +} + + +TEST_F(MM_stl_restore, s_multiset ) { + // ARRANGE + // make a testbed object + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + ATTRIBUTES * attr = memmgr->ref_attributes("my_alloc.s_multiset")->attr; + + std::vector test_first= get_test_data(20); + std::vector test_second = get_test_data(20); + + std::multiset> expected; + + std::string * data_links = (std::string *) memmgr->declare_var("std::string my_alloc_s_multiset[40]"); + + for (int i = 0; i < test_first.size(); i++) { + // just insert the same thing twice + for (int j = 0; j < 2; j++) { + data_links[i*2+j] = "my_alloc_s_multiset_" + std::to_string(i*2+j); + + std::string first_var_name = "int my_alloc_s_multiset_" + std::to_string(i*2+j) + "_first"; + std::string second_var_name = "int my_alloc_s_multiset_" + std::to_string(i*2+j) + "_second"; + + int * temp_first = (int *)memmgr->declare_var(first_var_name.c_str()); + *temp_first = test_first[i]; + int * temp_second = (int *)memmgr->declare_var(second_var_name.c_str()); + *temp_second = test_second[i]; + + // Build a copy of the expected data + expected.insert(std::pair(test_first[i], test_second[i])); + } + } + + // ACT + (attr->restore_stl)((void *) &testbed->s_multiset, "my_alloc", attr->name) ; + + // ASSERT + // Make sure the STL has been populated + EXPECT_EQ(testbed->s_multiset, expected); + + // Check that all the temporary variables have been deleted + EXPECT_EQ(memmgr->var_exists("my_alloc_s_multiset"), 0); + for (int i = 0; i < expected.size(); i++) { + EXPECT_EQ(memmgr->var_exists("my_alloc_s_multiset_" + std::to_string(i) + "_first"), 0); + EXPECT_EQ(memmgr->var_exists("my_alloc_s_multiset_" + std::to_string(i) + "_second"), 0); + } +} + +TEST_F(MM_stl_restore, vector_multiset ) { + // ARRANGE + // make a testbed object + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + ATTRIBUTES* attr = memmgr->ref_attributes("my_alloc.vector_multiset")->attr; + + std::vector test_data = get_test_data(50); + // Make sure we can handle lists of different sizes + std::vector lengths = {20, 4, 1, 1, 14, 10}; + + // Build a copy of the expected data + std::multiset> expected; + + // Register the expected temporary variables with the memory manager + int start_index = 0; + std::string * nested_list_links = (std::string *) memmgr->declare_var( "std::string my_alloc_vector_multiset[12]"); + for (int i = 0; i < lengths.size(); i++) { + for (int j = 0; j < 2; j++) { + nested_list_links[i*2+j] = "my_alloc_vector_multiset_" + std::to_string(i*2+j); + + std::string temp_var_name = "int my_alloc_vector_multiset_" + std::to_string(i*2+j) + "[" + std::to_string(lengths[i]) + "]"; + std::vector temp_expected; + int * temp_arr = (int *)memmgr->declare_var(temp_var_name.c_str()); + for (int k = 0; k < lengths[i]; k++) { + temp_arr[k] = test_data[start_index + k]; + temp_expected.push_back(test_data[start_index + k]); + } + + expected.insert(temp_expected); + + } + start_index += lengths[i]; + } + + // ACT + (attr->restore_stl)((void *) &testbed->vector_multiset, "my_alloc", attr->name) ; + + // ASSERT + // Make sure the STL has been populated + ASSERT_EQ(testbed->vector_multiset, expected); + + // Check that all the temporary variables have been deleted + EXPECT_EQ(memmgr->var_exists("my_alloc_vector_multiset"), 0); + for (int i = 0; i < expected.size(); i++) { + EXPECT_EQ(memmgr->var_exists("my_alloc_vector_multiset_" + std::to_string(i)), 0); + } +} + +TEST_F(MM_stl_restore, nested_map_multiset ) { + // ARRANGE + // make a testbed object + STLTestbed * testbed = (STLTestbed *) memmgr->declare_var("STLTestbed my_alloc"); + ATTRIBUTES* attr = memmgr->ref_attributes("my_alloc.nested_map_multiset")->attr; + + std::vector test_keys = get_test_data(60); + std::vector test_vals = get_test_data(60); + + std::multiset> expected; + + std::string * nested_map_links = (std::string *) memmgr->declare_var( "std::string my_alloc_nested_map_multiset[12]"); + for (int i = 0; i < 6; i++) { + for (int k = 0; k < 2; k++) { + nested_map_links[i] = "my_alloc_nested_map_multiset_" + std::to_string(i*2+k); + + std::string keys_var_name = "short my_alloc_nested_map_multiset_" + std::to_string(i*2+k) + "_keys[10]"; + std::string vals_var_name = "double my_alloc_nested_map_multiset_" + std::to_string(i*2+k) + "_data[10]"; + + short * map_keys_temp = (short *) memmgr->declare_var( keys_var_name.c_str() ); + double * map_vals_temp = (double *) memmgr->declare_var( vals_var_name.c_str() ); + + std::map temp_map; + for (int j = 0; j < 10; j++) { + map_keys_temp[j] = test_keys[(i*10) + j]; + map_vals_temp[j] = test_vals[(i*10) + j]; + + temp_map[test_keys[(i*10) + j]] = test_vals[(i*10) + j]; + } + + expected.emplace(temp_map); + } + } + + // ACT + (attr->restore_stl)((void *) &testbed->nested_map_multiset, "my_alloc", attr->name) ; + + // ASSERT + ASSERT_EQ(testbed->nested_map_multiset, expected); + + // Check that all the temporary variables have been deleted + EXPECT_EQ(memmgr->var_exists("my_alloc_nested_map_multiset"), 0); + for (int i = 0; i < expected.size(); i++) { + EXPECT_EQ(memmgr->var_exists("my_alloc_nested_map_multiset_" + std::to_string(i) + "_data"), 0); + EXPECT_EQ(memmgr->var_exists("my_alloc_nested_map_multiset_" + std::to_string(i) + "_vals"), 0); + } +} TEST_F(MM_stl_restore, i_array ) { // ARRANGE @@ -884,7 +1192,7 @@ TEST_F(MM_stl_restore, pair_array ) { *temp_second = test_second[i]; // Build a copy of the expected data - expected[i] = std::pair(test_first[i], test_second[i]); + expected[i] = std::pair(test_first[i], test_second[i]); } // ACT diff --git a/trick_source/sim_services/MemoryManager/test/MM_stl_testbed.hh b/trick_source/sim_services/MemoryManager/test/MM_stl_testbed.hh index 5e87785f..6a4a72ef 100644 --- a/trick_source/sim_services/MemoryManager/test/MM_stl_testbed.hh +++ b/trick_source/sim_services/MemoryManager/test/MM_stl_testbed.hh @@ -8,8 +8,6 @@ #include #include -#include - class UserClassStl { public: int a; @@ -61,6 +59,7 @@ public: std::pair,std::stack> s_s_pair; std::pair> i_v_pair; std::pair, int> pair_pair; + std::pair< int , std::pair< int, int > > int_pair_pair; std::map i_i_map; std::map> i_s_map; @@ -76,12 +75,15 @@ public: std::stack> nested_list_stack; std::set i_set; - // These fail in CP right now - // std::set > s_set; - // std::set> nested_map_set; + std::set> vector_set; + std::set > s_set; + std::set> nested_map_set; // this one will as well, if we put any STL containers in there std::multiset i_multiset; + std::multiset> vector_multiset; + std::multiset > s_multiset; + std::multiset> nested_map_multiset; std::array i_array; std::array, 10> pair_array; diff --git a/trick_source/sim_services/MemoryManager/test/MM_test.hh b/trick_source/sim_services/MemoryManager/test/MM_test.hh index a62c9770..2319e383 100644 --- a/trick_source/sim_services/MemoryManager/test/MM_test.hh +++ b/trick_source/sim_services/MemoryManager/test/MM_test.hh @@ -102,7 +102,7 @@ void validate_alloc_info(Trick::MemoryManager *memmgr, // char, short, int, long, long long template ::value>::type* = nullptr> T random() { - return (T) (rand() % 1000); + return (T) (rand()); } // This should just catch bools