/** * Tests serialization and deserialization of @c AbstractSerializable null pointers. */ void testBugAbstractSerializableNullPointerSerialization() { Abstract* a = 0; std::stringstream stream; AbstractFactory factory; XmlSerializer s; s.setUseAttributes(true); s.registerFactory(&factory); s.serialize("Abstract", a); s.write(stream); XmlDeserializer d; d.setUseAttributes(true); d.registerFactory(&factory); d.read(stream); try { d.deserialize("Abstract", a); } catch (XmlSerializationMemoryAllocationException&) { test(false, "bug occured, since memory allocation exception is thrown for 0 pointer"); } }
/** * Tests serialization and deserialization of polymorphic classes. */ void testPolymorphism() { Parent p; p.pdata = 1; Child c; c.pdata = 2; c.cdata = 3; Parent* pp = &p; Parent* pc = &c; Parent* child1 = new Child(); child1->pdata = 4; dynamic_cast<Child*>(child1)->cdata = 5; Child* child2 = new Child(); child2->pdata = 6; child2->cdata = 7; Factory factory; std::stringstream stream; try { XmlSerializer s; s.registerFactory(&factory); s.serialize("p", p); s.serialize("c", c); s.serialize("pp", pp); s.serialize("pc", pc); s.serialize("child1", child1); s.serialize("child2", child2); s.write(stream); delete child1; delete child2; } catch (...) { delete child1; delete child2; throw; } // Reset all variables to default values... p.pdata = 0; c.pdata = 0; c.cdata = 0; pp = 0; pc = 0; child1 = 0; child2 = 0; XmlDeserializer d; d.registerFactory(&factory); d.read(stream); d.deserialize("p", p); d.deserialize("c", c); d.deserialize("pp", pp); d.deserialize("pc", pc); d.deserialize("child1", child1); d.deserialize("child2", child2); test(p.pdata, 1, "p.pdata incorrect deserialized"); test(c.pdata, 2, "c.pdata incorrect deserialized"); test(c.cdata, 3, "c.cdata incorrect deserialized"); test(pp != 0, "pp still null"); test(pp == &p, "pp does not point to p"); test(pc != 0, "pc still null"); test(pc == &c, "pc does not point to c"); test(child1 != 0, "child1 still null"); test(child1->pdata, 4, "child1.pdata incorrect deserialized"); test(dynamic_cast<Child*>(child1) != 0, "child1 deserialized without using correct polymorphic type"); test(dynamic_cast<Child*>(child1)->cdata, 5, "child2.cdata incorrect deserialized"); test(child2 != 0, "child2 still null"); test(child2->pdata, 6, "child2.pdata incorrect deserialized"); test(child2->cdata, 7, "child2.cdata incorrect deserialized"); delete child1; delete child2; }