From cb9ea47e7825b5073d4d645afb94f6326cb7cf4d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 6 Sep 2009 12:52:53 +0200 Subject: Start the libcutl repository --- tests/shared-ptr/driver.cxx | 186 ++++++++++++++++++++++++++++++++++++++++++++ tests/shared-ptr/makefile | 70 +++++++++++++++++ 2 files changed, 256 insertions(+) create mode 100644 tests/shared-ptr/driver.cxx create mode 100644 tests/shared-ptr/makefile (limited to 'tests/shared-ptr') diff --git a/tests/shared-ptr/driver.cxx b/tests/shared-ptr/driver.cxx new file mode 100644 index 0000000..c32192e --- /dev/null +++ b/tests/shared-ptr/driver.cxx @@ -0,0 +1,186 @@ +// file : tests/shared-ptr/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include +#include + +#include + +using namespace cutl; + +struct type +{ + type (int x, char const* y) : x_ (x), y_ (y) {} + + int x_; + std::string y_; +}; + +struct base1 +{ + virtual + ~base1 () {} + base1 (int x) : x_ (x) {} + + int x_; +}; + +struct base2 +{ + virtual + ~base2 () {} + base2 (char const* y) : y_ (y) {} + + std::string y_; +}; + +struct derived: base1, base2 +{ + derived (int x, char const* y) : base1 (x), base2 (y) {} +}; + +struct shared_type: shared_base +{ + shared_type (int x, char const* y) + : x_ (x), y_ (y) + { + assert (ref_count (this) == 1); + } + + int x_; + std::string y_; +}; + +int +main () +{ + // + // inc_ref, dec_ref, ref_count + // + + // Non-polymorphic type. + // + { + type* x (new (shared) type (5, "foo")); + assert (ref_count (x) == 1); + inc_ref (x); + assert (ref_count (x) == 2); + dec_ref (x); + assert (ref_count (x) == 1); + dec_ref (x); + } + + // Polymorphic type. + // + { + base2* x (new (shared) derived (5, "foo")); + assert (ref_count (x) == 1); + inc_ref (x); + assert (ref_count (x) == 2); + dec_ref (x); + assert (ref_count (x) == 1); + dec_ref (x); + } + + // Shared type. + // + { + shared_type* x (new (shared) shared_type (5, "foo")); + assert (ref_count (x) == 1); + inc_ref (x); + assert (ref_count (x) == 2); + dec_ref (x); + assert (ref_count (x) == 1); + dec_ref (x); + } + + // Error handling (this theoretically can segfault). + // + { + type* x (new type (5, "foo")); + + try + { + inc_ref (x); + assert (false); + } + catch (not_shared const&) + { + } + + delete x; + } + + // + // shared_ptr + // + + // Non-polymorphic type. + // + { + shared_ptr x (new (shared) type (5, "foo")); + assert (x.count () == 1); + assert (x); + assert (x->x_ == 5); + assert ((*x).y_ == "foo"); + { + shared_ptr y (x); + assert (y.count () == 2); + } + { + shared_ptr y; + y = x; + assert (y.count () == 2); + } + assert (x.count () == 1); + shared_ptr y (x.release ()); + assert (y.count () == 1); + } + + // Polymorphic type. + // + { + shared_ptr x (new (shared) derived (5, "foo")); + assert (x.count () == 1); + { + shared_ptr y (x); + assert (y.count () == 2); + assert (y->y_ == "foo"); + } + { + shared_ptr y; + y = x; + assert (y.count () == 2); + } + assert (x.count () == 1); + } + + // Non-polymorphic type. + // + { + shared_ptr x (new (shared) shared_type (5, "foo")); + assert (x.count () == 1); + assert (x); + assert (x->x_ == 5); + assert ((*x).y_ == "foo"); + assert (x->_ref_count () == 1); + x->_inc_ref (); + assert (x.count () == 2); + x->_dec_ref (); + assert (x.count () == 1); + { + shared_ptr y (x); + assert (y.count () == 2); + } + { + shared_ptr y; + y = x; + assert (y.count () == 2); + } + assert (x.count () == 1); + shared_ptr y (x.release ()); + assert (y.count () == 1); + } +} diff --git a/tests/shared-ptr/makefile b/tests/shared-ptr/makefile new file mode 100644 index 0000000..43e9b53 --- /dev/null +++ b/tests/shared-ptr/makefile @@ -0,0 +1,70 @@ +# file : tests/shared-ptr/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2009 Code Synthesis Tools CC +# license : MIT; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx + +# +# +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +cutl.l := $(out_root)/cutl/cutl.l +cutl.l.cpp-options := $(out_root)/cutl/cutl.l.cpp-options + +driver := $(out_base)/driver +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Build. +# +$(driver): $(cxx_obj) $(cutl.l) +$(cxx_obj) $(cxx_od): $(cutl.l.cpp-options) + + +$(call include-dep,$(cxx_od)) + + +# Alias for default target. +# +$(out_base)/: $(driver) + + +# Test. +# +$(test): $(driver) + $(call message,test $<,$<) + + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) + + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + + +# How to. +# +$(call include,$(bld_root)/cxx/o-e.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/cxx-d.make) + +# Dependencies. +# +$(call import,$(src_root)/cutl/makefile) -- cgit v1.1