0

I have a complex struct eg.

struct Data{

    std::unordered_map<string, int> map1;
    shared_ptr<ClassA> ptrA;
    shared_ptr<ClassB> ptrB;

    ...
};

Inside the code, at a particular iteration, I want to store the state of the struct. This will be done once and that is it.

I want to use this state in my test case as the input.

eg. test.cpp

Data StoredState;
UnitTest(){
   ...
   
   // do something with StoredState

   ...
}

Question - What is the best way to store the data and use it in my test file.

The only way that comes to my mind is, write each fields of the struct to files, and manually fill in the StoredState, which I think is cumbersome. Is there a better way to achieve this.

Nitron_707
  • 307
  • 4
  • 10
  • 1
    No, there is no automatic serialization of objects in C++. Writing to a file from unit test seems suspicious? – Quimby Jul 26 '23 at 09:18
  • 1
    Perhaps look at the original object as a [*prototype*](https://en.wikipedia.org/wiki/Prototype_pattern) that can be cloned (deep copy) for the "frozen" state? – Some programmer dude Jul 26 '23 at 09:18
  • 1
    https://isocpp.org/wiki/faq/serialization – Mat Jul 26 '23 at 09:19
  • @Quimby Writing to file from the library once and then fill that data manually to the object in unit test case, thats the plan for now – Nitron_707 Jul 26 '23 at 11:19

0 Answers0