#include "object.h" #include "object.h" // Include the assert functionality which can be used for very simple // testing. Think of this as a more "stupid" version of CHECK from // CATCH. If all test passes then this program should not produce any // output, otherwise it specifies which testcase failed. #include int main() { std::string original { "Original string" }; std::string updated { "Updated string" }; Object object { original }; // No references currently exist assert( object.count() == 0 ); assert( object.data() == original ); // Use the get function Reference ref1 { object.get() }; // This has updated the number of references assert( object.count() == 1 ); assert( ref1.data() == original ); { // copy constructor Reference ref2 { ref1 }; assert( object.count() == 2 ); // change the object through the reference ref2.data() = updated; } // Check that there is one reference (ref1) and that // the objects' data has been updated assert( object.count() == 1 ); assert( object.data() == updated ); assert( ref1.data() == updated ); // create an empty reference Reference ref3; // default initialization should not update count assert( object.count() == 1 ); // copy assignment ref3 = ref1; assert( object.count() == 2 ); assert( object.data() == updated ); { Reference ref4; // move assignment ref4 = std::move(ref3); } // we moved ref3 into ref4 which was then destroyed assert( object.count() == 1 ); { Reference ref5; ref1 = std::move(ref5); } // Since we moved ref5 into ref1 we no longer have any references // to the object assert( object.count() == 0 ); Object const const_object { "" }; assert( const_object.count() == 0 ); }