diff options
author | Constantin Michael <constantin@codesynthesis.com> | 2011-04-19 09:48:20 +0200 |
---|---|---|
committer | Constantin Michael <constantin@codesynthesis.com> | 2011-04-22 18:52:24 +0200 |
commit | cb7359cbe30940debf8137508cd987220c038081 (patch) | |
tree | fee8fbd0ee426942a28536bc7b74d98ee6750630 /qt/common | |
parent | e08eeb9e5da87a729992c5a1d14bfd62f4ed2205 (diff) |
Update qt/smart-ptr implementation test
Diffstat (limited to 'qt/common')
-rw-r--r-- | qt/common/smart-ptr/driver.cxx | 180 | ||||
-rw-r--r-- | qt/common/smart-ptr/makefile | 2 | ||||
-rw-r--r-- | qt/common/smart-ptr/test.hxx | 2 |
3 files changed, 176 insertions, 8 deletions
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 <cassert> #include <iostream> +#include <QSharedPointer> + #include <odb/database.hxx> #include <odb/transaction.hxx> @@ -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<cont>; +template class QLazyWeakPointer<cont>; + int main (int argc, char* argv[]) { @@ -29,20 +37,180 @@ main (int argc, char* argv[]) auto_ptr<database> db (create_database (argc, argv)); QSharedPointer<cont> c1 (new cont (1)); + QSharedPointer<cont> c2 (new cont (2)); + + // Test boolean conversion operator. + // + { + assert (!QLazySharedPointer<cont> ()); + assert (!QLazyWeakPointer<cont> ()); + assert (QLazySharedPointer<cont> (c1)); + assert (QLazySharedPointer<cont> (*db, 1)); + assert (QLazyWeakPointer<cont> (c1)); + assert (QLazyWeakPointer<cont> (*db, 1)); + } + + // Test loaded () implementation. + // + { + assert (QLazySharedPointer<cont> ().loaded ()); + assert (!QLazySharedPointer<cont> (c1).loaded ()); + assert (!QLazySharedPointer<cont> (*db, 1).loaded ()); + assert (QLazySharedPointer<cont> (*db, c1).loaded ()); + assert (QLazyWeakPointer<cont> ().loaded ()); + assert (!QLazyWeakPointer<cont> (c1).loaded ()); + assert (!QLazyWeakPointer<cont> (*db, 1).loaded ()); + assert (QLazyWeakPointer<cont> (*db, c1).loaded ()); + } // Test comparison operators. // { + // Transient QLazySharedPointer. + // assert (QLazySharedPointer<cont> () == QLazySharedPointer<cont> ()); assert (QLazySharedPointer<cont> () != QLazySharedPointer<cont> (c1)); - assert (QLazySharedPointer<cont> (c1) == QLazySharedPointer<cont> (c1)); + assert (QLazySharedPointer<cont> (c1) != QLazySharedPointer<cont> (c2)); + assert (QLazySharedPointer<cont> (c2) == QLazySharedPointer<cont> (c2)); + + // Persistent QLazySharedPointer. + // + QLazySharedPointer<cont> ls1 (*db, 1), ls2 (*db, c2); + assert (ls1 != QLazySharedPointer<cont> ()); + assert (ls1 != QLazySharedPointer<cont> (c1)); + assert (ls1 == QLazySharedPointer<cont> (*db, c1)); + assert (ls1 != ls2); + assert (ls2 == QLazySharedPointer<cont> (c2)); + + // Transient QLazyWeakPointer. + // + assert (QLazyWeakPointer<cont> () == QLazyWeakPointer<cont> ()); + assert (QLazyWeakPointer<cont> () != QLazyWeakPointer<cont> (c1)); + assert (QLazyWeakPointer<cont> (c1) != QLazyWeakPointer<cont> (c2)); + assert (QLazyWeakPointer<cont> (c2) == QLazyWeakPointer<cont> (c2)); + assert (QLazyWeakPointer<cont> () == QLazySharedPointer<cont> ()); + assert (QLazyWeakPointer<cont> () != QLazySharedPointer<cont> (c1)); + assert (QLazyWeakPointer<cont> (c1) != QLazySharedPointer<cont> (c2)); + assert (QLazyWeakPointer<cont> (c2) == QLazySharedPointer<cont> (c2)); + + // Persistent QLazyWeakPointer. + // + QLazyWeakPointer<cont> lw1 (*db, 1), lw2 (*db, c2); + assert (lw1 != QLazyWeakPointer<cont> ()); + assert (lw1 != QLazyWeakPointer<cont> (c1)); + assert (lw1 == QLazyWeakPointer<cont> (*db, c1)); + assert (lw1 != lw2); + assert (lw2 == QLazyWeakPointer<cont> (c2)); + assert (ls1 == lw1); + assert (ls1 != QLazyWeakPointer<cont> (c1)); + assert (ls1 == QLazyWeakPointer<cont> (*db, c1)); + assert (ls1 != lw2); + assert (ls2 == QLazyWeakPointer<cont> (c2)); + } + + // Test swap. + // + { + QLazySharedPointer<cont> lx (*db, 1), ly; + swap (lx, ly); + + assert (lx.isNull ()); + assert (ly == QLazySharedPointer<cont> (*db, c1)); + } + + // Persist. + // + QSharedPointer<obj> o1 (new obj (1)); + QSharedPointer<obj> o2 (new obj (2)); + QSharedPointer<obj> o3 (new obj (3)); + QSharedPointer<obj> 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<cont> c (db->load<cont> (1)); + QSharedPointer<obj> o (db->load<obj> (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<obj> () == 1); + assert (c->o[1].objectId<obj> () == 2); + assert (o->c.objectId<obj> () == 1); + + // Load the lazy pointer targets ensuring that the loaded + // targets correspond to the cached session objects. + // + QSharedPointer<cont> cl (o->c.load ()); + QSharedPointer<obj> 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<cont> c (db->load<cont> (1)); + + // Lock. + // + assert (!c->o[1].loaded ()); + QLazySharedPointer<obj> l (c->o[1].toStrongRef ()); + assert (!l.loaded ()); + assert (l.objectId<obj> () == c->o[1].objectId<obj> ()); + + // Reload. + // + assert (!c->o[1].loaded ()); + + QSharedPointer<obj> ol (c->o[1].load ()); + assert (c->o[1].loaded ()); + + ol.clear (); + assert (!c->o[1].loaded ()); - QLazySharedPointer<cont> lc1 (*db, 1); - assert (lc1 != QLazySharedPointer<cont> ()); - assert (lc1 == QLazySharedPointer<cont> (*db, c1)); + ol = c->o[1].load (); + assert (c->o[1].loaded ()); - QSharedPointer<cont> c2 (new cont (2)); - assert (lc1 != QLazySharedPointer<cont> (*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<QLazySharedPointer<obj> > obj_list; + typedef std::vector<QLazyWeakPointer<obj> > obj_list; #pragma db inverse(c) not_null obj_list o; |