Set checkpointing fix ()

* Fix set checkpointing, add tests
* Documentation update
This commit is contained in:
Jacqueline Deans 2022-10-07 14:24:21 -05:00 committed by GitHub
parent f19ba7df78
commit 3d2e8e52ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 650 additions and 84 deletions
docs/documentation/simulation_capabilities
include/trick
test/SIM_stls/models
trick_source/sim_services/MemoryManager

View File

@ -119,12 +119,4 @@ The types in an std::pair cannot be sequence STL types (vector, queue, stack, et
std::pair<int, std::vector<int>>
```
The types inside an std::set cannot be any STL types (excluding strings).
```
// This will fail to compile
std::set<std::vector<int>>
```
These bugs are being worked on.
[Continue to Threads](Threads)

View File

@ -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<typename STL::value_type &>(*it) , object_name + "_" + var_name , index_string.str() ) ;
}
}
}

View File

@ -142,13 +142,27 @@ int STLCheckpoint::addData() {
string_set.insert("abc") ;
string_set.insert("def") ;
// for (int j = 0; j < 5; j++) {
// std::vector<int> 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<int> 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<short,double> m;
m[5] = 500.5;
m[1] = 100.1;
std::map<short,double> 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<int> 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<int>({1,2,3,4}));
vec_multiset.emplace(std::vector<int>({1,2,3,4}));
vec_multiset.emplace(std::vector<int>({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<std::string> string_set_copy = {"efg", "abc", "def"};
TRICK_EXPECT_EQ(string_set, string_set_copy, test_suite, "string_set");
std::set<std::vector<int>> vector_set_copy;
for (int j = 0; j < 5; j++) {
std::vector<int> 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<std::pair<std::string, int>> 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<std::map<short,double>> nested_map_set_copy;
std::map<short,double> m;
m[5] = 500.5;
m[1] = 100.1;
std::map<short,double> 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<std::vector<int>> vector_queue_copy;
for (int j = 0; j < 5; j++) {
std::vector<int> temp;
@ -571,6 +620,19 @@ int STLCheckpoint::test() {
std::multiset<std::string> string_multiset_copy = {"efg", "abc", "def", "efg", "abc", "def"};
TRICK_EXPECT_EQ(string_multiset, string_multiset_copy, test_suite, "string_multiset");
std::multiset<std::pair<int,int>> 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<std::vector<int>> vec_multiset_copy;
vec_multiset_copy.emplace(std::vector<int>({1,2,3,4}));
vec_multiset_copy.emplace(std::vector<int>({1,2,3,4}));
vec_multiset_copy.emplace(std::vector<int>({1,2,3,4,5}));
TRICK_EXPECT_EQ(vec_multiset, vec_multiset_copy, test_suite, "vec_multiset");
std::stack<unsigned int> uint_stack_copy;
uint_stack_copy.push(1) ;
uint_stack_copy.push(2) ;

View File

@ -72,13 +72,19 @@ class STLCheckpoint {
std::set< int > int_set ;
std::set< std::string > string_set ;
// std::set< std::vector<int> > vector_set;
std::set< std::vector<int> > vector_set;
std::set< std::pair<std::string, int> > pair_set;
std::set<std::map<short,double>> nested_map_set;
std::queue< std::vector<int> > 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 ;

View File

@ -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();

View File

@ -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<char> test_data = get_test_data<char>(1000);
// Likely to be some repeats in here
std::vector<char> test_data = get_test_data<char>(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<char>(memmgr, std::string("my_alloc"), std::string("i_set"), std::set<char> (test_data.begin(), test_data.end()));
// ASSERT
validate_temp_set<char>(memmgr, std::string("my_alloc"), std::string("i_set"), std::set<char> (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<short> test_first = get_test_data<short>(10);
// std::vector<double> test_second = get_test_data<double>(10);
std::vector<char> test_data = get_test_data<char>(1000);
std::vector<int> 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<std::vector<int>> expected_data;
// Prepare the STL to be tested
int start_index = 0;
for (int i = 0; i < sizes.size(); i++) {
std::vector<int> 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<int>(temp_vec));
expected_data.insert(std::vector<int>(temp_vec));
// // Prepare the STL to be tested
// for (int i = 0; i < test_first.size(); i++) {
// testbed->s_set.insert(std::pair<short,double>(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<short,double>(test_first[i], test_second[i]));
// }
// ASSERT
int index = 0;
std::vector<int> sizes_in_right_order;
for (std::vector<int> vec : expected_data) {
std::string var_name = "vector_set_" + std::to_string(index++);
validate_temp_sequence<int>(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<char>(memmgr, std::string("my_alloc"), std::string("vector_set"), std::set<char> (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<int> test_first = get_test_data<int>(10);
std::vector<int> test_second = get_test_data<int>(10);
// make a copy to deal with ordering
std::set<std::pair<int,int>> expected;
// Prepare the STL to be tested
for (int i = 0; i < test_first.size(); i++) {
testbed->s_set.insert(std::pair<int,int>(test_first[i], test_second[i]));
expected.insert(std::pair<int,int>(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<int,int> 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<int,int>(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<float> test_data = get_test_data<float>(10);
// std::vector<int> list_sizes = {2, 7, 1};
// // Prepare the STL to be tested
std::vector<short> test_keys = get_test_data<short>(100);
std::vector<double> test_vals = get_test_data<double>(100);
// Prepare the STL to be tested
// Make a copy to deal with ordering mess
std::set<std::map<short,double>> expected;
// int data_index = 0;
// for (int i = 0; i < list_sizes.size(); i++) {
// testbed->nested_list_stack.push(std::list<float>(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<short,double> 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<float>(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<short,double> 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<int> test_data = get_test_data<int>(1000);
std::multiset<int> 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<int>(memmgr, std::string("my_alloc"), std::string("i_multiset"), std::vector<int> (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<int> test_data = get_test_data<int>(1000);
std::vector<int> 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<std::vector<int>> expected_data;
// Prepare the STL to be tested
int start_index = 0;
for (int i = 0; i < sizes.size(); i++) {
std::vector<int> 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<int>(temp_vec));
testbed->vector_multiset.insert(std::vector<int>(temp_vec));
expected_data.insert(std::vector<int>(temp_vec));
expected_data.insert(std::vector<int>(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<int> sizes_in_right_order;
for (std::vector<int> vec : expected_data) {
std::string var_name = "vector_multiset_" + std::to_string(index++);
validate_temp_sequence<int>(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<int> test_first = get_test_data<int>(10);
std::vector<int> test_second = get_test_data<int>(10);
// make a copy to deal with ordering
std::multiset<std::pair<int,int>> expected;
// Prepare the STL to be tested
for (int i = 0; i < test_first.size(); i++) {
testbed->s_multiset.insert(std::pair<int,int>(test_first[i], test_second[i]));
testbed->s_multiset.insert(std::pair<int,int>(test_first[i], test_second[i]));
expected.insert(std::pair<int,int>(test_first[i], test_second[i]));
expected.insert(std::pair<int,int>(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<int,int> 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<int,int>(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<short> test_keys = get_test_data<short>(100);
std::vector<double> test_vals = get_test_data<double>(100);
// Prepare the STL to be tested
// Make a copy to deal with ordering mess
std::multiset<std::map<short,double>> expected;
for (int i = 0; i < 10; i++) {
std::map<short,double> 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<short,double> 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

View File

@ -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<int> test_first= get_test_data<int>(20);
std::vector<int> test_second = get_test_data<int>(20);
TEST_F(MM_stl_restore, DISABLED_i_multiset ) { }
std::set<std::pair<int,int>> 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<int,double>(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<int> test_data = get_test_data<int>(50);
// Make sure we can handle lists of different sizes
std::vector<int> lengths = {20, 4, 1, 1, 14, 10};
// Build a copy of the expected data
std::set<std::vector<int>> 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<int> 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<short> test_keys = get_test_data<short>(60);
std::vector<double> test_vals = get_test_data<double>(60);
std::set<std::map<short,double>> 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<short,double> 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<int> test_data = get_test_data<int>(100);
std::multiset<int> 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<int> test_first= get_test_data<int>(20);
std::vector<int> test_second = get_test_data<int>(20);
std::multiset<std::pair<int,int>> 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<int,double>(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<int> test_data = get_test_data<int>(50);
// Make sure we can handle lists of different sizes
std::vector<int> lengths = {20, 4, 1, 1, 14, 10};
// Build a copy of the expected data
std::multiset<std::vector<int>> 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<int> 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<short> test_keys = get_test_data<short>(60);
std::vector<double> test_vals = get_test_data<double>(60);
std::multiset<std::map<short,double>> 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<short,double> 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<short,double>(test_first[i], test_second[i]);
expected[i] = std::pair<int,int>(test_first[i], test_second[i]);
}
// ACT

View File

@ -8,8 +8,6 @@
#include <array>
#include <string>
#include <iostream>
class UserClassStl {
public:
int a;
@ -61,6 +59,7 @@ public:
std::pair<std::vector<int>,std::stack<float>> s_s_pair;
std::pair<double,std::vector<bool>> i_v_pair;
std::pair<std::pair<int,int>, int> pair_pair;
std::pair< int , std::pair< int, int > > int_pair_pair;
std::map<int, double> i_i_map;
std::map<int, std::stack<std::string>> i_s_map;
@ -76,12 +75,15 @@ public:
std::stack<std::list<float>> nested_list_stack;
std::set<char> i_set;
// These fail in CP right now
// std::set <std::pair<int,int>> s_set;
// std::set<std::map<int,int>> nested_map_set;
std::set<std::vector<int>> vector_set;
std::set <std::pair<int,int>> s_set;
std::set<std::map<short,double>> nested_map_set;
// this one will as well, if we put any STL containers in there
std::multiset<int> i_multiset;
std::multiset<std::vector<int>> vector_multiset;
std::multiset <std::pair<int,int>> s_multiset;
std::multiset<std::map<short,double>> nested_map_multiset;
std::array<char, 10> i_array;
std::array<std::pair<int,int>, 10> pair_array;

View File

@ -102,7 +102,7 @@ void validate_alloc_info(Trick::MemoryManager *memmgr,
// char, short, int, long, long long
template <typename T, typename std::enable_if<!std::is_floating_point<T>::value>::type* = nullptr>
T random() {
return (T) (rand() % 1000);
return (T) (rand());
}
// This should just catch bools