From e39f1cf9f0c3f520c61e0b832cca78df715d658d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 19 Oct 2011 10:58:44 +0200 Subject: Rename common/const test to common/const-object --- common/const-object/driver.cxx | 214 +++++++++++++++++++++++++++++++++++++++++ common/const-object/makefile | 109 +++++++++++++++++++++ common/const-object/test.hxx | 52 ++++++++++ common/const-object/test.std | 0 common/const/driver.cxx | 214 ----------------------------------------- common/const/makefile | 109 --------------------- common/const/test.hxx | 52 ---------- common/const/test.std | 0 common/makefile | 2 +- 9 files changed, 376 insertions(+), 376 deletions(-) create mode 100644 common/const-object/driver.cxx create mode 100644 common/const-object/makefile create mode 100644 common/const-object/test.hxx create mode 100644 common/const-object/test.std delete mode 100644 common/const/driver.cxx delete mode 100644 common/const/makefile delete mode 100644 common/const/test.hxx delete mode 100644 common/const/test.std (limited to 'common') diff --git a/common/const-object/driver.cxx b/common/const-object/driver.cxx new file mode 100644 index 0000000..6fb4cde --- /dev/null +++ b/common/const-object/driver.cxx @@ -0,0 +1,214 @@ +// file : common/const-object/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test database operations with const objects. +// + +#include // std::auto_ptr +#include +#include + +#include +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + aggr a (1); + aggr ca_ (2); // o1 and o2 are NULL + const aggr& ca (ca_); + + obj1* o1 (new obj1 (1)); + obj1* co1_ (new obj1 (2)); + const obj1* co1 (co1_); + a.o1 = co1; + + auto_ptr o2 (new obj2 (1)); + obj2* co2_ (new obj2 (2)); + a.o2.reset (co2_); + auto_ptr& co2 (a.o2); + + // persist via references + // + { + transaction t (db->begin ()); + db->persist (*o1); + db->persist (*co1); + db->persist (*o2); + db->persist (*co2); + db->persist (a); + db->persist (ca); + t.commit (); + } + + // persist via pointers + // + o1->id += 2; + co1_->id += 2; + o2->id += 2; + co2_->id += 2; + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (co1); + db->persist (o2); + db->persist (co2); + t.commit (); + } + + // load & compare + // + { + transaction t (db->begin ()); + auto_ptr a (db->load (1)); + auto_ptr ca (db->load (2)); + t.commit (); + + assert (a->o1->id == 2); + assert (a->o2->id == 2); + + assert (ca->o1 == 0); + assert (ca->o2.get () == 0); + } + + // update via references + // + { + transaction t (db->begin ()); + db->update (*o1); + db->update (*co1); + db->update (*o2); + db->update (*co2); + db->update (a); + db->update (ca); + t.commit (); + } + + // update via pointers + // + { + transaction t (db->begin ()); + db->update (o1); + db->update (co1); + db->update (o2); + db->update (co2); + t.commit (); + } + + // query + // + typedef odb::query query1; + typedef odb::query query2; + + typedef odb::result result1; + typedef odb::result result2; + + { + transaction t (db->begin ()); + result1 r1 (db->query (query1::id < 3)); + // odb::result ur (r1); // error + size_t n1 (0); + + for (result1::iterator i (r1.begin ()); i != r1.end (); ++i) + { + // i->f (); // error + i->cf (); + // obj1* p (i.load ()); // error + const obj1* p (i.load ()); + obj1 o (0); + i.load (o); + assert (p->id == o.id); + delete p; + n1++; + } + + assert (n1 == 2); + + result2 r2 (db->query (query2::id < 3)); + size_t n2 (0); + + for (result2::iterator i (r2.begin ()); i != r2.end (); ++i) + { + // i->f (); // error + i->cf (); + // auto_ptr p (i.load ()); // error + auto_ptr p (i.load ()); + obj2 o (0); + i.load (o); + assert (p->id == o.id); + n2++; + } + + assert (n2 == 2); + + t.commit (); + } + + // erase via references + // + { + transaction t (db->begin ()); + db->erase (*o1); + db->erase (*co1); + db->erase (*o2); + db->erase (*co2); + db->erase (a); + db->erase (ca); + t.commit (); + } + + // erase via pointers + // + o1->id -= 2; + co1_->id -= 2; + o2->id -= 2; + co2_->id -= 2; + + { + transaction t (db->begin ()); + db->erase (o1); + db->erase (co1); + db->erase (o2); + db->erase (co2); + t.commit (); + } + + // Test session and const/non-const object handling + // + { + session s; + transaction t (db->begin ()); + + obj1 o1 (1); + const obj1& co1 (o1); + db->persist (co1); + + assert (db->load (1) == &o1); + + t.commit (); + } + + delete o1; + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/const-object/makefile b/common/const-object/makefile new file mode 100644 index 0000000..768bd98 --- /dev/null +++ b/common/const-object/makefile @@ -0,0 +1,109 @@ +# file : common/const-object/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ +--generate-query --table-prefix const_object_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/common/,,$(src_base))) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(odb_rules)) +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/common/const-object/test.hxx b/common/const-object/test.hxx new file mode 100644 index 0000000..82635f0 --- /dev/null +++ b/common/const-object/test.hxx @@ -0,0 +1,52 @@ +// file : common/const-object/test.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include +#include + +#pragma db object pointer (obj1*) +struct obj1 +{ + obj1 () {} + obj1 (int i): id (i) {} + + #pragma db id + int id; + + void f () {} + void cf () const {} +}; + +#pragma db object pointer (std::auto_ptr) +struct obj2 +{ + obj2 () {} + obj2 (int i): id (i) {} + + #pragma db id + int id; + + void f () {} + void cf () const {} +}; + +#pragma db object +struct aggr +{ + aggr (int i): id (i), o1 (0) {} + aggr (): o1 (0) {} + ~aggr () {delete o1;} + + #pragma db id + int id; + + const obj1* o1; + std::auto_ptr o2; +}; + +#endif // TEST_HXX diff --git a/common/const-object/test.std b/common/const-object/test.std new file mode 100644 index 0000000..e69de29 diff --git a/common/const/driver.cxx b/common/const/driver.cxx deleted file mode 100644 index dd0a2b6..0000000 --- a/common/const/driver.cxx +++ /dev/null @@ -1,214 +0,0 @@ -// file : common/const/driver.cxx -// author : Boris Kolpackov -// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -// Test database operations with const objects. -// - -#include // std::auto_ptr -#include -#include - -#include -#include -#include - -#include - -#include "test.hxx" -#include "test-odb.hxx" - -using namespace std; -using namespace odb::core; - -int -main (int argc, char* argv[]) -{ - try - { - auto_ptr db (create_database (argc, argv)); - - aggr a (1); - aggr ca_ (2); // o1 and o2 are NULL - const aggr& ca (ca_); - - obj1* o1 (new obj1 (1)); - obj1* co1_ (new obj1 (2)); - const obj1* co1 (co1_); - a.o1 = co1; - - auto_ptr o2 (new obj2 (1)); - obj2* co2_ (new obj2 (2)); - a.o2.reset (co2_); - auto_ptr& co2 (a.o2); - - // persist via references - // - { - transaction t (db->begin ()); - db->persist (*o1); - db->persist (*co1); - db->persist (*o2); - db->persist (*co2); - db->persist (a); - db->persist (ca); - t.commit (); - } - - // persist via pointers - // - o1->id += 2; - co1_->id += 2; - o2->id += 2; - co2_->id += 2; - - { - transaction t (db->begin ()); - db->persist (o1); - db->persist (co1); - db->persist (o2); - db->persist (co2); - t.commit (); - } - - // load & compare - // - { - transaction t (db->begin ()); - auto_ptr a (db->load (1)); - auto_ptr ca (db->load (2)); - t.commit (); - - assert (a->o1->id == 2); - assert (a->o2->id == 2); - - assert (ca->o1 == 0); - assert (ca->o2.get () == 0); - } - - // update via references - // - { - transaction t (db->begin ()); - db->update (*o1); - db->update (*co1); - db->update (*o2); - db->update (*co2); - db->update (a); - db->update (ca); - t.commit (); - } - - // update via pointers - // - { - transaction t (db->begin ()); - db->update (o1); - db->update (co1); - db->update (o2); - db->update (co2); - t.commit (); - } - - // query - // - typedef odb::query query1; - typedef odb::query query2; - - typedef odb::result result1; - typedef odb::result result2; - - { - transaction t (db->begin ()); - result1 r1 (db->query (query1::id < 3)); - // odb::result ur (r1); // error - size_t n1 (0); - - for (result1::iterator i (r1.begin ()); i != r1.end (); ++i) - { - // i->f (); // error - i->cf (); - // obj1* p (i.load ()); // error - const obj1* p (i.load ()); - obj1 o (0); - i.load (o); - assert (p->id == o.id); - delete p; - n1++; - } - - assert (n1 == 2); - - result2 r2 (db->query (query2::id < 3)); - size_t n2 (0); - - for (result2::iterator i (r2.begin ()); i != r2.end (); ++i) - { - // i->f (); // error - i->cf (); - // auto_ptr p (i.load ()); // error - auto_ptr p (i.load ()); - obj2 o (0); - i.load (o); - assert (p->id == o.id); - n2++; - } - - assert (n2 == 2); - - t.commit (); - } - - // erase via references - // - { - transaction t (db->begin ()); - db->erase (*o1); - db->erase (*co1); - db->erase (*o2); - db->erase (*co2); - db->erase (a); - db->erase (ca); - t.commit (); - } - - // erase via pointers - // - o1->id -= 2; - co1_->id -= 2; - o2->id -= 2; - co2_->id -= 2; - - { - transaction t (db->begin ()); - db->erase (o1); - db->erase (co1); - db->erase (o2); - db->erase (co2); - t.commit (); - } - - // Test session and const/non-const object handling - // - { - session s; - transaction t (db->begin ()); - - obj1 o1 (1); - const obj1& co1 (o1); - db->persist (co1); - - assert (db->load (1) == &o1); - - t.commit (); - } - - delete o1; - } - catch (const odb::exception& e) - { - cerr << e.what () << endl; - return 1; - } -} diff --git a/common/const/makefile b/common/const/makefile deleted file mode 100644 index fd55611..0000000 --- a/common/const/makefile +++ /dev/null @@ -1,109 +0,0 @@ -# file : common/const/makefile -# author : Boris Kolpackov -# copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC -# license : GNU GPL v2; see accompanying LICENSE file - -include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make - -cxx_tun := driver.cxx -odb_hdr := test.hxx -cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) -cxx_od := $(cxx_obj:.o=.o.d) - -common.l := $(out_root)/libcommon/common/common.l -common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options - -driver := $(out_base)/driver -dist := $(out_base)/.dist -test := $(out_base)/.test -clean := $(out_base)/.clean - -# Import. -# -$(call import,\ - $(scf_root)/import/odb/stub.make,\ - odb: odb,odb-rules: odb_rules) - -# Build. -# -$(driver): $(cxx_obj) $(common.l) -$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) -$(cxx_obj) $(cxx_od): $(common.l.cpp-options) - -genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) -gen := $(addprefix $(out_base)/,$(genf)) - -$(gen): $(odb) -$(gen): odb := $(odb) -$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ ---generate-query --table-prefix const_ -$(gen): cpp_options := -I$(src_base) -$(gen): $(common.l.cpp-options) - -$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) - -# Alias for default target. -# -$(out_base)/: $(driver) - -# Dist -# -name := $(subst /,-,$(subst $(src_root)/common/,,$(src_base))) - -$(dist): db_id := @database@ -$(dist): sources := $(cxx_tun) -$(dist): headers := $(odb_hdr) -$(dist): data_dist := test.std -$(dist): export name := $(name) -$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ -$(call vc10projs,$(name)) -$(dist): - $(call dist-data,$(sources) $(headers) $(data_dist)) - $(call meta-automake,../template/Makefile.am) - $(call meta-vc9projs,../template/template,$(name)) - $(call meta-vc10projs,../template/template,$(name)) - -# Test. -# -$(test): $(driver) $(src_base)/test.std - $(call schema) - $(call message,test $<,$< --options-file $(dcf_root)/db.options \ ->$(out_base)/test.out) - $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) - $(call message,,rm -f $(out_base)/test.out) - -# Clean. -# -$(clean): \ - $(driver).o.clean \ - $(addsuffix .cxx.clean,$(cxx_obj)) \ - $(addsuffix .cxx.clean,$(cxx_od)) \ - $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) - $(call message,,rm -f $(out_base)/test.out) - -# Generated .gitignore. -# -ifeq ($(out_base),$(src_base)) -$(driver): | $(out_base)/.gitignore - -$(out_base)/.gitignore: files := driver $(genf) -$(clean): $(out_base)/.gitignore.clean - -$(call include,$(bld_root)/git/gitignore.make) -endif - -# How to. -# -$(call include,$(bld_root)/dist.make) -$(call include,$(bld_root)/meta/vc9proj.make) -$(call include,$(bld_root)/meta/vc10proj.make) -$(call include,$(bld_root)/meta/automake.make) - -$(call include,$(odb_rules)) -$(call include,$(bld_root)/cxx/cxx-d.make) -$(call include,$(bld_root)/cxx/cxx-o.make) -$(call include,$(bld_root)/cxx/o-e.make) - -# Dependencies. -# -$(call import,$(src_root)/libcommon/makefile) diff --git a/common/const/test.hxx b/common/const/test.hxx deleted file mode 100644 index 341acb8..0000000 --- a/common/const/test.hxx +++ /dev/null @@ -1,52 +0,0 @@ -// file : common/const/test.hxx -// author : Boris Kolpackov -// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST_HXX -#define TEST_HXX - -#include -#include - -#pragma db object pointer (obj1*) -struct obj1 -{ - obj1 () {} - obj1 (int i): id (i) {} - - #pragma db id - int id; - - void f () {} - void cf () const {} -}; - -#pragma db object pointer (std::auto_ptr) -struct obj2 -{ - obj2 () {} - obj2 (int i): id (i) {} - - #pragma db id - int id; - - void f () {} - void cf () const {} -}; - -#pragma db object -struct aggr -{ - aggr (int i): id (i), o1 (0) {} - aggr (): o1 (0) {} - ~aggr () {delete o1;} - - #pragma db id - int id; - - const obj1* o1; - std::auto_ptr o2; -}; - -#endif // TEST_HXX diff --git a/common/const/test.std b/common/const/test.std deleted file mode 100644 index e69de29..0000000 diff --git a/common/makefile b/common/makefile index e5065cd..c066ce1 100644 --- a/common/makefile +++ b/common/makefile @@ -9,7 +9,7 @@ tests := \ auto \ callback \ composite \ -const \ +const-object \ const-member \ container \ ctor \ -- cgit v1.1