From cb7359cbe30940debf8137508cd987220c038081 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Tue, 19 Apr 2011 09:48:20 +0200 Subject: Update qt/smart-ptr implementation test --- qt/common/smart-ptr/driver.cxx | 180 +++++++++++++++++++++++++++++++++++++++-- qt/common/smart-ptr/makefile | 2 +- qt/common/smart-ptr/test.hxx | 2 +- 3 files changed, 176 insertions(+), 8 deletions(-) (limited to 'qt/common') diff --git a/qt/common/smart-ptr/driver.cxx b/qt/common/smart-ptr/driver.cxx index a7ca263..56eb6bf 100644 --- a/qt/common/smart-ptr/driver.cxx +++ b/qt/common/smart-ptr/driver.cxx @@ -10,6 +10,8 @@ #include #include +#include + #include #include @@ -21,6 +23,12 @@ using namespace std; using namespace odb::core; +// Force generation of code for all QLazySharedPointer and QLazyWeakPointer +// class template members. +// +template class QLazySharedPointer; +template class QLazyWeakPointer; + int main (int argc, char* argv[]) { @@ -29,20 +37,180 @@ main (int argc, char* argv[]) auto_ptr db (create_database (argc, argv)); QSharedPointer c1 (new cont (1)); + QSharedPointer c2 (new cont (2)); + + // Test boolean conversion operator. + // + { + assert (!QLazySharedPointer ()); + assert (!QLazyWeakPointer ()); + assert (QLazySharedPointer (c1)); + assert (QLazySharedPointer (*db, 1)); + assert (QLazyWeakPointer (c1)); + assert (QLazyWeakPointer (*db, 1)); + } + + // Test loaded () implementation. + // + { + assert (QLazySharedPointer ().loaded ()); + assert (!QLazySharedPointer (c1).loaded ()); + assert (!QLazySharedPointer (*db, 1).loaded ()); + assert (QLazySharedPointer (*db, c1).loaded ()); + assert (QLazyWeakPointer ().loaded ()); + assert (!QLazyWeakPointer (c1).loaded ()); + assert (!QLazyWeakPointer (*db, 1).loaded ()); + assert (QLazyWeakPointer (*db, c1).loaded ()); + } // Test comparison operators. // { + // Transient QLazySharedPointer. + // assert (QLazySharedPointer () == QLazySharedPointer ()); assert (QLazySharedPointer () != QLazySharedPointer (c1)); - assert (QLazySharedPointer (c1) == QLazySharedPointer (c1)); + assert (QLazySharedPointer (c1) != QLazySharedPointer (c2)); + assert (QLazySharedPointer (c2) == QLazySharedPointer (c2)); + + // Persistent QLazySharedPointer. + // + QLazySharedPointer ls1 (*db, 1), ls2 (*db, c2); + assert (ls1 != QLazySharedPointer ()); + assert (ls1 != QLazySharedPointer (c1)); + assert (ls1 == QLazySharedPointer (*db, c1)); + assert (ls1 != ls2); + assert (ls2 == QLazySharedPointer (c2)); + + // Transient QLazyWeakPointer. + // + assert (QLazyWeakPointer () == QLazyWeakPointer ()); + assert (QLazyWeakPointer () != QLazyWeakPointer (c1)); + assert (QLazyWeakPointer (c1) != QLazyWeakPointer (c2)); + assert (QLazyWeakPointer (c2) == QLazyWeakPointer (c2)); + assert (QLazyWeakPointer () == QLazySharedPointer ()); + assert (QLazyWeakPointer () != QLazySharedPointer (c1)); + assert (QLazyWeakPointer (c1) != QLazySharedPointer (c2)); + assert (QLazyWeakPointer (c2) == QLazySharedPointer (c2)); + + // Persistent QLazyWeakPointer. + // + QLazyWeakPointer lw1 (*db, 1), lw2 (*db, c2); + assert (lw1 != QLazyWeakPointer ()); + assert (lw1 != QLazyWeakPointer (c1)); + assert (lw1 == QLazyWeakPointer (*db, c1)); + assert (lw1 != lw2); + assert (lw2 == QLazyWeakPointer (c2)); + assert (ls1 == lw1); + assert (ls1 != QLazyWeakPointer (c1)); + assert (ls1 == QLazyWeakPointer (*db, c1)); + assert (ls1 != lw2); + assert (ls2 == QLazyWeakPointer (c2)); + } + + // Test swap. + // + { + QLazySharedPointer lx (*db, 1), ly; + swap (lx, ly); + + assert (lx.isNull ()); + assert (ly == QLazySharedPointer (*db, c1)); + } + + // Persist. + // + QSharedPointer o1 (new obj (1)); + QSharedPointer o2 (new obj (2)); + QSharedPointer o3 (new obj (3)); + QSharedPointer o4 (new obj (4)); + + o1->c = c1; + o2->c = c1; + o3->c = c2; + o4->c = c2; + + { + transaction t (db->begin ()); + + db->persist (c1); + + db->persist (o1); + db->persist (o2); + db->persist (o3); + db->persist (o4); + + db->persist (c2); + + t.commit (); + } + + // Load. + // + { + session s; + transaction t (db->begin ()); + + QSharedPointer c (db->load (1)); + QSharedPointer o (db->load (1)); + + // Ensure that lazy pointers are present but not loaded. + // + assert (c->o.size () == 2); + assert (!c->o[0].loaded ()); + assert (!c->o[1].loaded ()); + assert (!o->c.loaded ()); + + // Ensure that the correct object IDs were loaded. + // + assert (c->o[0].objectId () == 1); + assert (c->o[1].objectId () == 2); + assert (o->c.objectId () == 1); + + // Load the lazy pointer targets ensuring that the loaded + // targets correspond to the cached session objects. + // + QSharedPointer cl (o->c.load ()); + QSharedPointer ol (c->o[0].load ()); + + assert (c->o[0].loaded ()); + assert (o->c.loaded ()); + + assert (cl == c); + assert (ol == o); + + t.commit (); + } + + // Test lazy weak locking and reloading. + // + { + // No session. + // + transaction t (db->begin ()); + QSharedPointer c (db->load (1)); + + // Lock. + // + assert (!c->o[1].loaded ()); + QLazySharedPointer l (c->o[1].toStrongRef ()); + assert (!l.loaded ()); + assert (l.objectId () == c->o[1].objectId ()); + + // Reload. + // + assert (!c->o[1].loaded ()); + + QSharedPointer ol (c->o[1].load ()); + assert (c->o[1].loaded ()); + + ol.clear (); + assert (!c->o[1].loaded ()); - QLazySharedPointer lc1 (*db, 1); - assert (lc1 != QLazySharedPointer ()); - assert (lc1 == QLazySharedPointer (*db, c1)); + ol = c->o[1].load (); + assert (c->o[1].loaded ()); - QSharedPointer c2 (new cont (2)); - assert (lc1 != QLazySharedPointer (*db, c2)); + t.commit (); } } catch (const odb::exception& e) diff --git a/qt/common/smart-ptr/makefile b/qt/common/smart-ptr/makefile index f8e75d4..a7742f2 100644 --- a/qt/common/smart-ptr/makefile +++ b/qt/common/smart-ptr/makefile @@ -35,7 +35,7 @@ $(call import,\ # Build. # $(driver): $(cxx_obj) $(odb_qt.l) $(common.l) $(qt_core.l) -$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) -DQWEAKPOINTER_ENABLE_ARROW $(cxx_obj) $(cxx_od): $(common.l.cpp-options) $(odb_qt.l.cpp-options) \ $(qt_core.l.cpp-options) diff --git a/qt/common/smart-ptr/test.hxx b/qt/common/smart-ptr/test.hxx index 6b376b0..06c05fa 100644 --- a/qt/common/smart-ptr/test.hxx +++ b/qt/common/smart-ptr/test.hxx @@ -28,7 +28,7 @@ struct cont #pragma db id unsigned long id; - typedef std::vector > obj_list; + typedef std::vector > obj_list; #pragma db inverse(c) not_null obj_list o; -- cgit v1.1