summaryrefslogtreecommitdiff
path: root/odb-tests/common/lazy-ptr
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2024-01-25 20:32:06 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2024-01-25 20:32:06 +0300
commit0d49ea1fe08cf1eab41a00149393a291c65a59d7 (patch)
tree0391eb09309ca95282e200516937e64d89f3e1bb /odb-tests/common/lazy-ptr
parentfc3fb39c90ab7fe5fccbe3f3bc0eb2645157bb96 (diff)
Turn odb-tests repository into package for muti-package repositoryodb-tests
Diffstat (limited to 'odb-tests/common/lazy-ptr')
-rw-r--r--odb-tests/common/lazy-ptr/buildfile41
-rw-r--r--odb-tests/common/lazy-ptr/driver.cxx360
-rw-r--r--odb-tests/common/lazy-ptr/test.hxx147
-rw-r--r--odb-tests/common/lazy-ptr/testscript33
4 files changed, 581 insertions, 0 deletions
diff --git a/odb-tests/common/lazy-ptr/buildfile b/odb-tests/common/lazy-ptr/buildfile
new file mode 100644
index 0000000..d495d2f
--- /dev/null
+++ b/odb-tests/common/lazy-ptr/buildfile
@@ -0,0 +1,41 @@
+# file : common/lazy-ptr/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+import libodb = libodb%lib{odb}
+
+libs =
+
+for db: $databases
+ import libs += libodb-$db%lib{odb-$db}
+
+import libs += lib{common}
+
+exe{driver}: {hxx cxx}{* -*-odb -*-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}: $libodb
+
+<{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 lazy_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/common/lazy-ptr/driver.cxx b/odb-tests/common/lazy-ptr/driver.cxx
new file mode 100644
index 0000000..9a3b324
--- /dev/null
+++ b/odb-tests/common/lazy-ptr/driver.cxx
@@ -0,0 +1,360 @@
+// file : common/lazy-ptr/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test lazy object 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::core;
+
+namespace test2
+{
+ cont::cont (unsigned long i)
+ : id (i)
+ {
+ }
+
+ obj_ptr
+ create (unsigned int id)
+ {
+ obj_ptr r (new obj (id));
+ return r;
+ }
+
+ lazy_obj_ptr
+ create (database& db, unsigned int id)
+ {
+ lazy_obj_ptr r (db, id);
+ return r;
+ }
+}
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv));
+
+ // Raw.
+ //
+ {
+ using namespace test1;
+
+ // persist
+ //
+ obj* o1 (new obj (1));
+
+ {
+ transaction t (db->begin ());
+ db->persist (o1);
+ t.commit ();
+ }
+
+ unique_ptr<cont> c1 (new cont (1));
+ unique_ptr<cont> c2 (new cont (2));
+
+ lazy_ptr<obj> lo1 (*db, 1);
+ obj* o2 (new obj (2));
+
+ obj* o3 (new obj (3));
+ obj* o4 (new obj (4));
+
+ c1->o.push_back (lo1);
+ c1->o.push_back (o2);
+ c2->o.push_back (o3);
+ c2->o.push_back (o4);
+
+ // Test pointer comparison.
+ //
+ assert (lazy_ptr<obj> () == lazy_ptr<obj> ());
+ assert (lazy_ptr<obj> (o1) != lazy_ptr<obj> ());
+ assert (lo1 != lazy_ptr<obj> ());
+ assert (lazy_ptr<obj> (o1) == lazy_ptr<obj> (o1));
+ assert (lo1 == lazy_ptr<obj> (*db, o1));
+ assert (lo1 != lazy_ptr<obj> (*db, o2));
+
+ delete o1;
+
+ {
+ transaction t (db->begin ());
+
+ db->persist (o2);
+ db->persist (o3);
+ db->persist (o4);
+
+ db->persist (*c1);
+ db->persist (*c2);
+
+ t.commit ();
+ }
+
+ // load
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+ unique_ptr<cont> c (db->load<cont> (1));
+ obj* o (db->load<obj> (1));
+
+ // Not loaded.
+ //
+ assert (c->o.size () == 2);
+ assert (!c->o[0].loaded ());
+ assert (!c->o[1].loaded ());
+
+ assert (!o->c.loaded ());
+
+ // Correct object ids.
+ //
+ assert (c->o[0].object_id () == o->id);
+ assert (o->c.object_id () == c->id);
+
+ // Load.
+ //
+ cont* cl (o->c.load ());
+ obj* ol (c->o[0].load ());
+
+ assert (cl == c.get ());
+ assert (ol == o);
+
+ // Test unload/reload.
+ //
+ o->c.unload ();
+ assert (!o->c.loaded ());
+ o->c.load ();
+ assert (o->c.loaded ());
+
+ t.commit ();
+ }
+ }
+
+ // std::unique_ptr
+ //
+ {
+ using namespace test2;
+
+ // persist
+ //
+ {
+ obj_ptr o1 (new obj (1));
+ transaction t (db->begin ());
+ db->persist (*o1);
+ t.commit ();
+ }
+
+ cont_ptr c1 (new cont (1));
+ cont_ptr c2 (new cont (2));
+
+ lazy_obj_ptr lo1 = create (*db, 1);
+ lo1 = create (*db, 1);
+
+ c1->o = std::move (lo1);
+ c2->o = create (2);
+
+ {
+ transaction t (db->begin ());
+
+ db->persist (*c2->o);
+
+ db->persist (*c1);
+ db->persist (*c2);
+
+ t.commit ();
+ }
+
+ // load
+ //
+ {
+ session s;
+ transaction t (db->begin ());
+ cont_ptr c (db->load<cont> (1));
+ obj* o (db->load<obj> (1));
+
+ // Not loaded.
+ //
+ assert (!c->o.loaded ());
+ assert (!o->c.loaded ());
+
+ // Correct object ids.
+ //
+ assert (c->o.object_id () == o->id);
+ assert (o->c.object_id () == c->id);
+
+ // Load.
+ //
+ cont* cl (o->c.load ());
+ const obj_ptr& ol (c->o.load ());
+
+ assert (cl == c.get ());
+ assert (ol.get () == o);
+
+ t.commit ();
+ }
+
+ // unload/reload
+ //
+ {
+ // No session.
+ transaction t (db->begin ());
+ cont_ptr c (db->load<cont> (1));
+
+ assert (!c->o.loaded ());
+ c->o.load ();
+ assert (c->o.loaded ());
+ c->o.unload ();
+ assert (!c->o.loaded ());
+ c->o.load ();
+ assert (c->o.loaded ());
+
+ t.commit ();
+ }
+ }
+
+ // Shared pointer from C++11 or TR1.
+ //
+ {
+ using namespace test3;
+
+ // persist
+ //
+ shared_ptr<cont> c1 (new cont (1));
+
+ {
+ transaction t (db->begin ());
+ db->persist (c1);
+ t.commit ();
+ }
+
+ lazy_shared_ptr<cont> lc1 (*db, 1);
+ shared_ptr<cont> c2 (new cont (2));
+
+ 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));
+
+ o1->c = lc1;
+ o2->c = lc1;
+ o3->c = c2;
+ o4->c = c2;
+
+ // Test pointer comparison.
+ //
+ assert (lazy_shared_ptr<cont> () == lazy_shared_ptr<cont> ());
+ assert (lazy_shared_ptr<cont> (c1) != lazy_shared_ptr<cont> ());
+ assert (lc1 != lazy_shared_ptr<cont> ());
+ assert (lazy_shared_ptr<cont> (c1) == lazy_shared_ptr<cont> (c1));
+ assert (lc1 == lazy_shared_ptr<cont> (*db, c1));
+ assert (lc1 != lazy_shared_ptr<cont> (*db, c2));
+
+ // Test move constructors.
+ //
+ {
+ lazy_shared_ptr<cont> tmp (*db, 1);
+ lazy_shared_ptr<cont> l (std::move (tmp));
+ assert (lc1 == l);
+ }
+
+ {
+ shared_ptr<cont> tmp (c1);
+ lazy_shared_ptr<cont> l (*db, std::move (tmp));
+ assert (lc1 == l);
+ }
+
+ {
+ 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));
+
+ // Not loaded.
+ //
+ assert (c->o.size () == 2);
+ assert (!c->o[0].loaded ());
+ assert (!c->o[1].loaded ());
+
+ assert (!o->c.loaded ());
+
+ // Correct object ids.
+ //
+ assert (c->o[0].object_id () == o->id);
+ assert (o->c.object_id () == c->id);
+
+ // Load.
+ //
+ shared_ptr<cont> cl (o->c.load ());
+ shared_ptr<obj> ol (c->o[0].load ());
+
+ 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 () == c->o[1].object_id ());
+
+ // 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 ();
+ }
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/common/lazy-ptr/test.hxx b/odb-tests/common/lazy-ptr/test.hxx
new file mode 100644
index 0000000..f946029
--- /dev/null
+++ b/odb-tests/common/lazy-ptr/test.hxx
@@ -0,0 +1,147 @@
+// file : common/lazy-ptr/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <vector>
+#include <string>
+#include <memory>
+
+#include <odb/core.hxx>
+#include <odb/lazy-ptr.hxx>
+
+// Raw pointer.
+//
+#pragma db namespace table("t1_")
+namespace test1
+{
+ using odb::lazy_ptr;
+ class obj;
+
+ #pragma db object
+ class cont
+ {
+ public:
+ cont () {}
+ cont (unsigned long i): id (i) {}
+ ~cont ();
+
+ #pragma db id
+ unsigned long id;
+
+ typedef std::vector<lazy_ptr<obj> > obj_list;
+
+ #pragma db value_not_null
+ obj_list o;
+ };
+
+ #pragma db object
+ class obj
+ {
+ public:
+ obj () {}
+ obj (unsigned long i): id (i) {}
+
+ #pragma db id
+ unsigned long id;
+
+ #pragma db inverse(o) not_null
+ lazy_ptr<cont> c; // weak
+ };
+
+ inline cont::
+ ~cont ()
+ {
+ for (obj_list::iterator i (o.begin ()); i != o.end (); ++i)
+ if (obj* p = i->get ())
+ delete p;
+ }
+}
+
+// std::auto_ptr/std::unique_ptr
+//
+#pragma db namespace table("t2_")
+namespace test2
+{
+ using odb::lazy_ptr;
+
+ class obj;
+ class cont;
+
+ typedef std::unique_ptr<obj> obj_ptr;
+ typedef std::unique_ptr<cont> cont_ptr;
+ typedef odb::lazy_unique_ptr<obj> lazy_obj_ptr;
+
+ #pragma db object
+ class cont
+ {
+ public:
+ cont () = default;
+ cont (unsigned long id);
+
+ #pragma db id
+ unsigned long id;
+
+ #pragma db not_null
+ lazy_obj_ptr o;
+ };
+
+ #pragma db object
+ class obj
+ {
+ public:
+ obj () = default;
+ obj (unsigned long i): id (i) {}
+
+ #pragma db id
+ unsigned long id;
+
+ #pragma db inverse(o) not_null
+ lazy_ptr<cont> c; // weak
+ };
+}
+
+// shared_ptr
+//
+#pragma db namespace table("t3_")
+namespace test3
+{
+ using std::shared_ptr;
+ using odb::lazy_shared_ptr;
+ using odb::lazy_weak_ptr;
+
+ class obj;
+
+ #pragma db object pointer(shared_ptr<cont>)
+ class cont
+ {
+ public:
+ cont () {}
+ cont (unsigned long i): id (i) {}
+
+ #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 pointer(shared_ptr<obj>)
+ class obj
+ {
+ public:
+ obj () {}
+ obj (unsigned long i): id (i) {}
+
+ #pragma db id
+ unsigned long id;
+
+ #pragma db not_null
+ lazy_shared_ptr<cont> c;
+ };
+}
+
+#endif // TEST_HXX
diff --git a/odb-tests/common/lazy-ptr/testscript b/odb-tests/common/lazy-ptr/testscript
new file mode 100644
index 0000000..736fa4c
--- /dev/null
+++ b/odb-tests/common/lazy-ptr/testscript
@@ -0,0 +1,33 @@
+# file : common/lazy-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;
+ $*
+}