From 16632e04c20599fd02737f0ad6d8f94d46f038bf Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 24 Nov 2014 10:43:35 +0200 Subject: Test class templates --- common/bulk/driver.cxx | 91 +++++++++++++++++++++++++++++++++++++++++++++++++- common/bulk/test.hxx | 34 +++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/common/bulk/driver.cxx b/common/bulk/driver.cxx index 4b3b9f8..2903ae4 100644 --- a/common/bulk/driver.cxx +++ b/common/bulk/driver.cxx @@ -287,7 +287,7 @@ main (int argc, char* argv[]) #if defined(DATABASE_ORACLE) || defined(DATABASE_MSSQL) // Test database class API with various forms of containers - // and elements. + // and elements (test #6 is a copy). // { using namespace test1; @@ -926,6 +926,95 @@ main (int argc, char* argv[]) } } + // Test API with persistent class template instantiations (copy of + // test #1). + { + using namespace test6; + + // Make sure we can still call the non-bulk API. + // + { + object o (0, "z"); + transaction t (db->begin ()); + db->persist (o); + db->update (o); + db->reload (o); + db->erase (o); + t.commit (); + } + + + // The rest is a copy of test #1. + // + { + object a[2]; + a[0] = object (1, "a"); + a[1] = object (2, "b"); + test (db, a, a + sizeof (a) / sizeof (a[0])); + } + + { + vector v; + v.push_back (object (1, "a")); + v.push_back (object (2, "b")); + test (db, v.begin (), v.end ()); + } + + { + object o1 (1, "a"); + object o2 (2, "b"); + object* a[2] = {&o1, &o2}; + test (db, a, a + sizeof (a) / sizeof (a[0])); + } + + { + object o1 (1, "a"); + object o2 (2, "b"); + vector v; + v.push_back (&o1); + v.push_back (&o2); + test (db, v.begin (), v.end ()); + } + + { + auto_ptr a[2]; + a[0].reset (new auto_object (1, "a")); + a[1].reset (new auto_object (2, "b")); + test (db, a, a + sizeof (a) / sizeof (a[0])); + } + +#ifdef HAVE_CXX11 + { + vector> v; + v.push_back (unique_ptr (new unique_object (1, "a"))); + v.push_back (unique_ptr (new unique_object (2, "b"))); + test (db, v.begin (), v.end ()); + } +#endif + + { + vector v; + v.push_back (object (1, "a")); + v.push_back (object (2, "b")); + persist (db, v.begin (), v.end ()); + + unsigned long id[2] = {v[0].id, v[1].id}; + erase_id (db, id, id + sizeof (id) / sizeof (id[0])); + } + + { + vector v; + v.push_back (object (1, "a")); + v.push_back (object (2, "b")); + persist (db, v.begin (), v.end ()); + + vector id; + id.push_back (v[0].id); + id.push_back (v[1].id); + erase_id (db, id.begin (), id.end ()); + } + } + #endif } catch (const odb::exception& e) diff --git a/common/bulk/test.hxx b/common/bulk/test.hxx index 8497174..526762f 100644 --- a/common/bulk/test.hxx +++ b/common/bulk/test.hxx @@ -155,4 +155,38 @@ namespace test5 }; } +// Test API with persistent class template instantiations. +// +#pragma db namespace table("t6_") +namespace test6 +{ + template + struct object_template + { + object_template (unsigned int n_ = 0, std::string s_ = "") + : id (0), n (n_), s (s_) {} + + unsigned long id; + unsigned int n; + std::string s; + }; + + typedef object_template<1> object; + + #pragma db object(object) bulk(3) + #pragma db member(object::id) id auto + + typedef object_template<2> auto_object; + + #pragma db object(auto_object) bulk(3) pointer(std::auto_ptr) + #pragma db member(auto_object::id) id auto + +#ifdef HAVE_CXX11 + typedef object_template<3> unique_object; + + #pragma db object(unique_object) bulk(3) pointer(std::unique_ptr) + #pragma db member(unique_object::id) id auto +#endif +} + #endif // TEST_HXX -- cgit v1.1