diff options
Diffstat (limited to 'odb-tests/boost/common/smart-ptr')
-rw-r--r-- | odb-tests/boost/common/smart-ptr/buildfile | 44 | ||||
-rw-r--r-- | odb-tests/boost/common/smart-ptr/driver.cxx | 205 | ||||
-rw-r--r-- | odb-tests/boost/common/smart-ptr/test.hxx | 82 | ||||
-rw-r--r-- | odb-tests/boost/common/smart-ptr/testscript | 53 |
4 files changed, 384 insertions, 0 deletions
diff --git a/odb-tests/boost/common/smart-ptr/buildfile b/odb-tests/boost/common/smart-ptr/buildfile new file mode 100644 index 0000000..9dfafeb --- /dev/null +++ b/odb-tests/boost/common/smart-ptr/buildfile @@ -0,0 +1,44 @@ +# file : boost/common/smart-ptr/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +import meta_libs = libodb%lib{odb} +import meta_libs += libodb-boost%lib{odb-boost} +import meta_libs += libboost-smart-ptr%lib{boost_smart_ptr} + +libs = + +for db: $databases + import libs += libodb-$db%lib{odb-$db} + +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -test-odb -test-odb-*} {hxx ixx cxx}{test-odb} testscript + +# Introduce the metadata library target to make sure the libodb library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $meta_libs + +<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta} + +for db: $databases +{ + exe{driver}: {hxx ixx cxx}{test-odb-$db}: include = $multi + <{hxx ixx cxx}{test-odb-$db}>: hxx{test} libue{test-meta} +} + +exe{driver}: libue{test-meta} $libs + +# Specify the ODB custom options to be used by the odb_compile ad hoc rule +# (see build/root.build for details). +# +odb_options = --table-prefix boost_smart_ptr_ \ + --profile boost/smart-ptr \ + --generate-schema \ + --generate-session + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../../alias{database-client}: include = adhoc diff --git a/odb-tests/boost/common/smart-ptr/driver.cxx b/odb-tests/boost/common/smart-ptr/driver.cxx new file mode 100644 index 0000000..c7697c5 --- /dev/null +++ b/odb-tests/boost/common/smart-ptr/driver.cxx @@ -0,0 +1,205 @@ +// file : boost/common/smart-ptr/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test boost smart pointers. +// + +#include <memory> // std::unique_ptr +#include <utility> // std::move() +#include <iostream> + +#include <odb/database.hxx> +#include <odb/session.hxx> +#include <odb/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; + +using namespace odb::boost; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + using boost::shared_ptr; + + try + { + unique_ptr<database> db (create_database (argc, argv)); + + shared_ptr<cont> c1 (new cont (1)); + { + transaction t (db->begin ()); + db->persist (c1); + t.commit (); + } + + // Test comparison operators. + // + { + assert (lazy_shared_ptr<cont> () == lazy_shared_ptr<cont> ()); + assert (lazy_shared_ptr<cont> () != lazy_shared_ptr<cont> (c1)); + assert (lazy_shared_ptr<cont> (c1) == lazy_shared_ptr<cont> (c1)); + + lazy_shared_ptr<cont> lc1 (*db, 1); + assert (lc1 != lazy_shared_ptr<cont> ()); + assert (lc1 == lazy_shared_ptr<cont> (*db, c1)); + + shared_ptr<cont> c2 (new cont (2)); + assert (lc1 != lazy_shared_ptr<cont> (*db, c2)); + } + + // Test swap. + // + { + lazy_shared_ptr<cont> lx (*db, 1), ly; + assert (lx == lazy_shared_ptr<cont> (*db, c1)); + + swap (lx, ly); + assert (lx == lazy_shared_ptr<cont> ()); + assert (ly == lazy_shared_ptr<cont> (*db, c1)); + } + + // Test assignment from unique_ptr. + // + { + cont* p = new cont (3); + unique_ptr<cont> a (p); + lazy_shared_ptr<cont> l; + l = move (a); + + assert (l.get() == p); + assert (!a.get ()); + } + + shared_ptr<obj> o1 (new obj (1)); + shared_ptr<obj> o2 (new obj (2)); + shared_ptr<obj> o3 (new obj (3)); + shared_ptr<obj> o4 (new obj (4)); + shared_ptr<cont> c2 (new cont (2)); + + o1->c = c1; + o2->c = c1; + o3->c = c2; + o4->c = c2; + + // Persist. + // + { + transaction t (db->begin ()); + + db->persist (o1); + db->persist (o2); + db->persist (o3); + db->persist (o4); + db->persist (c2); + + t.commit (); + } + + // Load. + // + { + session s; + transaction t (db->begin ()); + + shared_ptr<cont> c (db->load<cont> (1)); + shared_ptr<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].object_id<obj> () == 1); + assert (c->o[1].object_id<obj> () == 2); + assert (o->c.object_id<obj> () == 1); + + // Load the lazy pointer targets ensuring that the loaded + // targets correspond to the cached session objects. + // + shared_ptr<cont> cl (o->c.load ()); + shared_ptr<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 ()); + shared_ptr<cont> c (db->load<cont> (1)); + + // Lock. + // + assert (!c->o[1].loaded ()); + lazy_shared_ptr<obj> l (c->o[1].lock ()); + assert (!l.loaded ()); + assert (l.object_id<obj> () == c->o[1].object_id<obj> ()); + + // Reload. + // + assert (!c->o[1].loaded ()); + + shared_ptr<obj> ol (c->o[1].load ()); + assert (c->o[1].loaded ()); + + ol.reset (); + assert (!c->o[1].loaded ()); + + ol = c->o[1].load (); + assert (c->o[1].loaded ()); + + t.commit (); + } + + // + // Test shared_ptr as a value wrapper. + // + + { + obj2 o1 (1); + obj2 o2 (2); + o2.str.reset (new string ("abc")); + + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + shared_ptr<obj2> o1 (db->load<obj2> (1)); + shared_ptr<obj2> o2 (db->load<obj2> (2)); + t.commit (); + + assert (!o1->str); + assert (o2->str && *o2->str == "abc"); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/boost/common/smart-ptr/test.hxx b/odb-tests/boost/common/smart-ptr/test.hxx new file mode 100644 index 0000000..26d7109 --- /dev/null +++ b/odb-tests/boost/common/smart-ptr/test.hxx @@ -0,0 +1,82 @@ +// file : boost/common/smart-ptr/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <boost/shared_ptr.hpp> + +#include <odb/core.hxx> +#include <odb/boost/smart-ptr/lazy-ptr.hxx> + +struct obj; + +using boost::shared_ptr; +using odb::boost::lazy_shared_ptr; +using odb::boost::lazy_weak_ptr; + +#pragma db object +struct cont +{ + cont () + { + } + + cont (unsigned long id) + : id (id) + { + } + + #pragma db id + unsigned long id; + + typedef std::vector<lazy_weak_ptr<obj> > obj_list; + + #pragma db inverse(c) value_not_null + obj_list o; +}; + +#pragma db object +struct obj +{ + obj () + { + } + + obj (unsigned long id) + : id (id) + { + } + + #pragma db id + unsigned long id; + + #pragma db not_null + lazy_shared_ptr<cont> c; +}; + +// Test shared_ptr as a value wrapper. +// +#pragma db object +struct obj2 +{ + obj2 () + { + } + + obj2 (unsigned long id) + : id (id) + { + } + + #pragma db id + unsigned long id; + + #pragma db null + shared_ptr<std::string> str; +}; + +#endif // TEST_HXX diff --git a/odb-tests/boost/common/smart-ptr/testscript b/odb-tests/boost/common/smart-ptr/testscript new file mode 100644 index 0000000..095e2e9 --- /dev/null +++ b/odb-tests/boost/common/smart-ptr/testscript @@ -0,0 +1,53 @@ +# file : boost/common/smart-ptr/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../../database-options.testscript + +: mysql +: +if $mysql +{ + .include ../../../mysql.testscript + + $create_schema; + $* +} + +: sqlite +: +if $sqlite +{ + .include ../../../sqlite.testscript + + $* +} + +: pgsql +: +if $pgsql +{ + .include ../../../pgsql.testscript + + $create_schema; + $* +} + +: oracle +: +if $oracle +{ + .include ../../../oracle.testscript + + $create_schema; + $* +} + +: mssql +: +if $mssql +{ + .include ../../../mssql.testscript + + $create_schema; + $* +} |