summaryrefslogtreecommitdiff
path: root/odb-tests/common/container/basics
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/common/container/basics')
-rw-r--r--odb-tests/common/container/basics/buildfile40
-rw-r--r--odb-tests/common/container/basics/driver.cxx523
-rw-r--r--odb-tests/common/container/basics/test.hxx245
-rw-r--r--odb-tests/common/container/basics/testscript53
4 files changed, 861 insertions, 0 deletions
diff --git a/odb-tests/common/container/basics/buildfile b/odb-tests/common/container/basics/buildfile
new file mode 100644
index 0000000..f83444e
--- /dev/null
+++ b/odb-tests/common/container/basics/buildfile
@@ -0,0 +1,40 @@
+# file : common/container/basics/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 t_cont_bs_ \
+ --generate-schema
+
+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/container/basics/driver.cxx b/odb-tests/common/container/basics/driver.cxx
new file mode 100644
index 0000000..14e1984
--- /dev/null
+++ b/odb-tests/common/container/basics/driver.cxx
@@ -0,0 +1,523 @@
+// file : common/container/basics/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test basic container persistence.
+//
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/database.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;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv));
+
+ for (unsigned short i (0); i < 2; ++i)
+ {
+ object empty ("empty"), med ("medium"), full ("full");
+
+ //
+ // empty
+ //
+
+ empty.num = 0;
+ empty.str = "";
+
+ // array
+ //
+ empty.na[0] = 123;
+ empty.na[1] = 234;
+ empty.na[2] = 345;
+
+ empty.sa[0] = "aaa";
+ empty.sa[1] = "bbbb";
+ empty.sa[2] = "ccccc";
+
+ empty.ca[0] = comp (123, "aaa");
+ empty.ca[1] = comp (234, "bbbb");
+ empty.ca[2] = comp (345, "ccccc");
+
+
+ //
+ // med
+ //
+
+ med.num = 999;
+ med.str = "xxx";
+
+ // vector
+ //
+ med.nv.push_back (123);
+ med.nv.push_back (234);
+
+ med.sv.push_back ("aaa");
+ med.sv.push_back ("bbbb");
+
+ med.cv.push_back (comp (123, "aaa"));
+ med.cv.push_back (comp (234, "bbbb"));
+
+ med.uv.push_back (123);
+ med.uv.push_back (234);
+
+ // list
+ //
+ med.sl.push_back ("aaa");
+ med.sl.push_back ("bbbb");
+
+ // deque
+ //
+ med.nd.push_back (123);
+ med.nd.push_back (234);
+
+ // set
+ //
+ med.ns.insert (123);
+ med.ns.insert (234);
+
+ med.ss.insert ("aaa");
+ med.ss.insert ("bbbb");
+
+ med.cs.insert (comp (123, "aaa"));
+ med.cs.insert (comp (234, "bbbb"));
+
+ // map
+ //
+ med.nsm[123] = "aaa";
+ med.nsm[234] = "bbbb";
+
+ med.snm["aaa"] = 123;
+ med.snm["bbbb"] = 234;
+
+ med.ncm[123] = comp (123, "aaa");
+ med.ncm[234] = comp (234, "bbbb");
+
+ med.csm[comp (123, "aaa")] = "aaa";
+ med.csm[comp (234, "bbbb")] = "bbbb";
+
+ // array
+ //
+ med.na[0] = 123;
+ med.na[1] = 234;
+ med.na[2] = 345;
+
+ med.sa[0] = "aaa";
+ med.sa[1] = "bbbb";
+ med.sa[2] = "ccccc";
+
+ med.ca[0] = comp (123, "aaa");
+ med.ca[1] = comp (234, "bbbb");
+ med.ca[2] = comp (345, "ccccc");
+
+ // forward_list
+ //
+ med.nfl.push_front (234);
+ med.nfl.push_front (123);
+
+ med.sfl.push_front ("bbbb");
+ med.sfl.push_front ("aaa");
+
+ med.cfl.push_front (comp (234, "bbbb"));
+ med.cfl.push_front (comp (123, "aaa"));
+
+ // unordered_set
+ //
+ med.nus.insert (123);
+ med.nus.insert (234);
+
+ med.sus.insert ("aaa");
+ med.sus.insert ("bbbb");
+
+ med.cus.insert (comp (123, "aaa"));
+ med.cus.insert (comp (234, "bbbb"));
+
+ // unordered_map
+ //
+ med.nsum[123] = "aaa";
+ med.nsum[234] = "bbbb";
+
+ med.snum["aaa"] = 123;
+ med.snum["bbbb"] = 234;
+
+ med.ncum[123] = comp (123, "aaa");
+ med.ncum[234] = comp (234, "bbbb");
+
+ med.csum[comp (123, "aaa")] = "aaa";
+ med.csum[comp (234, "bbbb")] = "bbbb";
+
+ //
+ // full
+ //
+
+ full.num = 9999;
+ full.str = "xxxx";
+
+ // vector
+ //
+ full.nv.push_back (1234);
+ full.nv.push_back (2345);
+ full.nv.push_back (3456);
+
+ full.sv.push_back ("aaaa");
+ full.sv.push_back ("bbbbb");
+ full.sv.push_back ("cccccc");
+
+ full.cv.push_back (comp (1234, "aaaa"));
+ full.cv.push_back (comp (2345, "bbbbb"));
+ full.cv.push_back (comp (3456, "cccccc"));
+
+ full.uv.push_back (1234);
+ full.uv.push_back (2345);
+ full.uv.push_back (3456);
+
+ // list
+ //
+ full.sl.push_back ("aaaa");
+ full.sl.push_back ("bbbbb");
+ full.sl.push_back ("cccccc");
+
+ // deque
+ //
+ full.nd.push_back (1234);
+ full.nd.push_back (2345);
+ full.nd.push_back (3456);
+
+ // set
+ //
+ full.ns.insert (1234);
+ full.ns.insert (2345);
+ full.ns.insert (3456);
+
+ full.ss.insert ("aaaa");
+ full.ss.insert ("bbbbb");
+ full.ss.insert ("cccccc");
+
+ full.cs.insert (comp (1234, "aaaa"));
+ full.cs.insert (comp (2345, "bbbbb"));
+ full.cs.insert (comp (3456, "cccccc"));
+
+ // map
+ //
+ full.nsm[1234] = "aaaa";
+ full.nsm[2345] = "bbbbb";
+ full.nsm[3456] = "cccccc";
+
+ full.snm["aaaa"] = 1234;
+ full.snm["bbbbb"] = 2345;
+ full.snm["cccccc"] = 3456;
+
+ full.ncm[1234] = comp (1234, "aaaa");
+ full.ncm[2345] = comp (2345, "bbbbb");
+ full.ncm[3456] = comp (3456, "cccccc");
+
+ full.csm[comp (1234, "aaaa")] = "aaaa";
+ full.csm[comp (2345, "bbbbb")] = "bbbbb";
+ full.csm[comp (3456, "cccccc")] = "cccccc";
+
+ // array
+ //
+ full.na[0] = 123;
+ full.na[1] = 234;
+ full.na[2] = 345;
+
+ full.sa[0] = "aaa";
+ full.sa[1] = "bbbb";
+ full.sa[2] = "ccccc";
+
+ full.ca[0] = comp (123, "aaa");
+ full.ca[1] = comp (234, "bbbb");
+ full.ca[2] = comp (345, "ccccc");
+
+ // forward_list
+ //
+ full.nfl.push_front (345);
+ full.nfl.push_front (234);
+ full.nfl.push_front (123);
+
+ full.sfl.push_front ("ccccc");
+ full.sfl.push_front ("bbbb");
+ full.sfl.push_front ("aaa");
+
+ full.cfl.push_front (comp (345, "ccccc"));
+ full.cfl.push_front (comp (234, "bbbb"));
+ full.cfl.push_front (comp (123, "aaa"));
+
+ // unordered_set
+ //
+ full.nus.insert (1234);
+ full.nus.insert (2345);
+ full.nus.insert (3456);
+
+ full.sus.insert ("aaaa");
+ full.sus.insert ("bbbbb");
+ full.sus.insert ("cccccc");
+
+ full.cus.insert (comp (1234, "aaaa"));
+ full.cus.insert (comp (2345, "bbbbb"));
+ full.cus.insert (comp (3456, "cccccc"));
+
+ // unordered_map
+ //
+ full.nsum[1234] = "aaaa";
+ full.nsum[2345] = "bbbbb";
+ full.nsum[3456] = "cccccc";
+
+ full.snum["aaaa"] = 1234;
+ full.snum["bbbbb"] = 2345;
+ full.snum["cccccc"] = 3456;
+
+ full.ncum[1234] = comp (1234, "aaaa");
+ full.ncum[2345] = comp (2345, "bbbbb");
+ full.ncum[3456] = comp (3456, "cccccc");
+
+ full.csum[comp (1234, "aaaa")] = "aaaa";
+ full.csum[comp (2345, "bbbbb")] = "bbbbb";
+ full.csum[comp (3456, "cccccc")] = "cccccc";
+
+ // persist
+ //
+ {
+ transaction t (db->begin ());
+ db->persist (empty);
+ db->persist (med);
+ db->persist (full);
+ t.commit ();
+ }
+
+ // load & check
+ //
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> e (db->load<object> ("empty"));
+ unique_ptr<object> m (db->load<object> ("medium"));
+ unique_ptr<object> f (db->load<object> ("full"));
+ t.commit ();
+
+ assert (empty == *e);
+ assert (med == *m);
+ assert (full == *f);
+ }
+
+ //
+ // empty
+ //
+
+ empty.num = 99;
+ empty.str = "xx";
+
+ empty.nv.push_back (12);
+ empty.sv.push_back ("aa");
+ empty.cv.push_back (comp (12, "aa"));
+ empty.uv.push_back (12);
+ empty.sl.push_back ("aa");
+ empty.nd.push_back (12);
+
+ empty.ns.insert (12);
+ empty.ss.insert ("aa");
+ empty.cs.insert (comp (12, "aa"));
+
+ empty.nsm[12] = "aa";
+ empty.snm["aa"] = 12;
+ empty.ncm[12] = comp (12, "aa");
+ empty.csm[comp (12, "aa")] = "aa";
+
+ empty.nfl.push_front (12);
+ empty.sfl.push_front ("aa");
+ empty.cfl.push_front (comp (12, "aa"));
+
+ empty.nus.insert (12);
+ empty.sus.insert ("aa");
+ empty.cus.insert (comp (12, "aa"));
+
+ empty.nsum[12] = "aa";
+ empty.snum["aa"] = 12;
+ empty.ncum[12] = comp (12, "aa");
+ empty.csum[comp (12, "aa")] = "aa";
+
+ //
+ // med
+ //
+
+ med.num = 0;
+ med.str = "";
+
+ med.nv.clear ();
+ med.sv.clear ();
+ med.cv.clear ();
+ med.uv.clear ();
+
+ med.sl.clear ();
+
+ med.nd.clear ();
+
+ med.ns.clear ();
+ med.ss.clear ();
+ med.cs.clear ();
+
+ med.nsm.clear ();
+ med.snm.clear ();
+ med.ncm.clear ();
+ med.csm.clear ();
+
+ med.nfl.clear ();
+ med.sfl.clear ();
+ med.cfl.clear ();
+
+ med.nus.clear ();
+ med.sus.clear ();
+ med.cus.clear ();
+
+ med.nsum.clear ();
+ med.snum.clear ();
+ med.ncum.clear ();
+ med.csum.clear ();
+
+ //
+ // full
+ //
+
+ full.num++;
+ full.str += "x";
+
+ // vector
+ //
+ full.nv.back ()++;
+ full.nv.push_back (4567);
+
+ full.sv.back () += "c";
+ full.sv.push_back ("ddddddd");
+
+ full.cv.back ().num++;
+ full.cv.back ().str += "c";
+ full.cv.push_back (comp (4567, "ddddddd"));
+
+ full.uv.back ()++;
+ full.uv.push_back (4567);
+
+ // list
+ //
+ full.sl.back () += "c";
+ full.sl.push_back ("ddddddd");
+
+ // deque
+ //
+ full.nd.push_front (456);
+
+ // set
+ //
+ full.ns.insert (4567);
+ full.ss.insert ("ddddddd");
+ full.cs.insert (comp (4567, "ddddddd"));
+
+ // map
+ //
+ full.nsm[3456] += 'c';
+ full.nsm[4567] = "ddddddd";
+
+ full.snm["cccccc"]++;
+ full.snm["ddddddd"] = 4567;
+
+ full.ncm[3456].num++;
+ full.ncm[3456].str += 'c';
+ full.ncm[4567] = comp (4567, "ddddddd");
+
+ full.csm[comp (3456, "cccccc")] += "c";
+ full.csm[comp (4567, "ddddddd")] = "ddddddd";
+
+ // array
+ //
+ full.na[0]++;
+ full.sa[0] += 'a';
+ full.ca[0].num++;
+ full.ca[0].str += 'a';
+
+ // forward_list
+ //
+ full.nfl.front ()++;
+ full.nfl.push_front (4567);
+
+ full.sfl.front () += 'a';
+ full.sfl.push_front ("ddddddd");
+
+ full.cfl.front ().num++;
+ full.cfl.front ().str += 'a';
+ full.cfl.push_front (comp (4567, "ddddddd"));
+
+ // unordered_set
+ //
+ full.nus.insert (4567);
+ full.sus.insert ("ddddddd1"); // 1 is to preserve order in VC++ 10.
+ full.cus.insert (comp (4567, "ddddddd1"));
+
+ // unordered_map
+ //
+ full.nsum[3456] += 'c';
+ full.nsum[4567] = "ddddddd";
+
+ full.snum["cccccc"]++;
+ full.snum["ddddddd1"] = 4567;
+
+ full.ncum[3456].num++;
+ full.ncum[3456].str += 'c';
+ full.ncum[4567] = comp (4567, "ddddddd");
+
+ full.csum[comp (3456, "cccccc")] += "c";
+ full.csum[comp (4567, "ddddddd1")] = "ddddddd";
+
+ // update
+ //
+ {
+ transaction t (db->begin ());
+ db->update (empty);
+ db->update (med);
+ db->update (full);
+ t.commit ();
+ }
+
+ // load & check
+ //
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> e (db->load<object> ("empty"));
+ unique_ptr<object> m (db->load<object> ("medium"));
+ unique_ptr<object> f (db->load<object> ("full"));
+ t.commit ();
+
+ assert (empty == *e);
+ assert (med == *m);
+ assert (full == *f);
+ }
+
+ // erase
+ //
+ if (i == 0)
+ {
+ transaction t (db->begin ());
+ db->erase<object> ("empty");
+ db->erase<object> ("medium");
+ db->erase<object> ("full");
+ t.commit ();
+ }
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/common/container/basics/test.hxx b/odb-tests/common/container/basics/test.hxx
new file mode 100644
index 0000000..e8e329e
--- /dev/null
+++ b/odb-tests/common/container/basics/test.hxx
@@ -0,0 +1,245 @@
+// file : common/container/basics/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <map>
+#include <set>
+#include <list>
+#include <vector>
+#include <deque>
+#include <array>
+#include <string>
+#include <forward_list>
+#include <unordered_map>
+#include <unordered_set>
+
+#include <odb/core.hxx>
+
+#pragma db value
+struct comp
+{
+ comp () {}
+ comp (int n, const std::string& s) : num (n), str (s) {}
+
+ #pragma db column("number")
+ int num = 0;
+ std::string str;
+};
+
+inline bool
+operator== (const comp& x, const comp& y)
+{
+ return x.num == y.num && x.str == y.str;
+}
+
+inline bool
+operator!= (const comp& x, const comp& y)
+{
+ return !(x == y);
+}
+
+inline bool
+operator< (const comp& x, const comp& y)
+{
+ return x.num != y.num ? x.num < y.num : x.str < y.str;
+}
+
+typedef std::list<std::string> str_list;
+typedef std::deque<int> num_deque;
+
+typedef std::vector<int> num_vector;
+typedef std::vector<std::string> str_vector;
+
+typedef std::set<int> num_set;
+typedef std::set<std::string> str_set;
+typedef std::set<comp> comp_set;
+
+typedef std::map<int, std::string> num_str_map;
+typedef std::map<std::string, int> str_num_map;
+typedef std::map<int, comp> num_comp_map;
+typedef std::map<comp, std::string> comp_str_map;
+
+struct comp_hash
+{
+ std::size_t
+ operator() (const comp& x) const {return nh (x.num) + sh (x.str);}
+
+ std::hash<int> nh;
+ std::hash<std::string> sh;
+};
+
+typedef std::array<int, 3> num_array;
+typedef std::array<std::string, 3> str_array;
+typedef std::array<comp, 3> comp_array;
+
+typedef std::forward_list<int> num_flist;
+typedef std::forward_list<std::string> str_flist;
+typedef std::forward_list<comp> comp_flist;
+
+typedef std::unordered_set<int> num_uset;
+typedef std::unordered_set<std::string> str_uset;
+typedef std::unordered_set<comp, comp_hash> comp_uset;
+
+typedef std::unordered_map<int, std::string> num_str_umap;
+typedef std::unordered_map<std::string, int> str_num_umap;
+typedef std::unordered_map<int, comp> num_comp_umap;
+typedef std::unordered_map<comp, std::string, comp_hash> comp_str_umap;
+
+#pragma db value
+struct cont_comp1
+{
+ // This composite value does not have any columns.
+ //
+ num_vector sv; // Have the name "conflic" with the one in the object.
+};
+
+#pragma db value
+struct cont_comp2
+{
+ cont_comp2 (): num (777), str ("ggg") {}
+
+ int num;
+ str_list sl;
+ std::string str;
+};
+
+#pragma db object
+struct object
+{
+ object (): nv (comp1_.sv), sl (comp2_.sl) {}
+ object (const std::string& id) : id_ (id), nv (comp1_.sv), sl (comp2_.sl) {}
+
+ #pragma db id
+ std::string id_;
+
+ int num;
+
+ cont_comp1 comp1_;
+ cont_comp2 comp2_;
+
+ // vector
+ //
+ #pragma db transient
+ num_vector& nv;
+
+ #pragma db table("object_strings") id_column ("obj_id")
+ str_vector sv;
+
+ #pragma db value_column("")
+ std::vector<comp> cv;
+
+ #pragma db unordered
+ num_vector uv;
+
+ // list
+ //
+ #pragma db transient
+ str_list& sl;
+
+ // deque
+ //
+ num_deque nd;
+
+ // set
+ //
+ num_set ns;
+ str_set ss;
+ comp_set cs;
+
+ // map
+ //
+ num_str_map nsm;
+ str_num_map snm;
+ num_comp_map ncm;
+ comp_str_map csm;
+
+ // array
+ //
+ num_array na;
+ str_array sa;
+ comp_array ca;
+
+ // forward_list
+ //
+ num_flist nfl;
+ str_flist sfl;
+ comp_flist cfl;
+
+ // unordered_set
+ //
+ num_uset nus;
+ str_uset sus;
+ comp_uset cus;
+
+ // unordered_map
+ //
+ num_str_umap nsum;
+ str_num_umap snum;
+ num_comp_umap ncum;
+ comp_str_umap csum;
+
+ std::string str;
+};
+
+inline bool
+operator== (const object& x, const object& y)
+{
+ if (x.uv.size () != y.uv.size ())
+ return false;
+
+ int xs (0), ys (0);
+
+ for (num_vector::size_type i (0); i < x.uv.size (); ++i)
+ {
+ xs += x.uv[i];
+ ys += y.uv[i];
+ }
+
+ return
+ x.id_ == y.id_ &&
+ x.num == y.num &&
+
+ x.comp2_.num == y.comp2_.num &&
+ x.comp2_.str == y.comp2_.str &&
+
+ x.nv == y.nv &&
+ x.sv == y.sv &&
+ x.cv == y.cv &&
+ xs == ys &&
+
+ x.sl == y.sl &&
+
+ x.nd == y.nd &&
+
+ x.ns == y.ns &&
+ x.ss == y.ss &&
+ x.cs == y.cs &&
+
+ x.nsm == y.nsm &&
+ x.snm == y.snm &&
+ x.ncm == y.ncm &&
+ x.csm == y.csm &&
+
+ x.na == y.na &&
+ x.sa == y.sa &&
+ x.ca == y.ca &&
+
+ x.nfl == y.nfl &&
+ x.sfl == y.sfl &&
+ x.cfl == y.cfl &&
+
+ x.nus == y.nus &&
+ x.sus == y.sus &&
+ x.cus == y.cus &&
+
+ x.nsum == y.nsum &&
+ x.snum == y.snum &&
+ x.ncum == y.ncum &&
+ x.csum == y.csum &&
+
+ x.str == y.str;
+}
+
+#endif // TEST_HXX
diff --git a/odb-tests/common/container/basics/testscript b/odb-tests/common/container/basics/testscript
new file mode 100644
index 0000000..9e6bfb9
--- /dev/null
+++ b/odb-tests/common/container/basics/testscript
@@ -0,0 +1,53 @@
+# file : common/container/basics/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;
+ $*
+}