aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2009-09-06 12:52:53 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2009-09-06 12:52:53 +0200
commitcb9ea47e7825b5073d4d645afb94f6326cb7cf4d (patch)
tree201c215b08df918924153a60520046c294438a6b /tests
Start the libcutl repository
Diffstat (limited to 'tests')
-rw-r--r--tests/compiler/makefile18
-rw-r--r--tests/compiler/traversal/driver.cxx135
-rw-r--r--tests/compiler/traversal/makefile71
-rw-r--r--tests/compiler/traversal/output.std16
-rw-r--r--tests/makefile18
-rw-r--r--tests/shared-ptr/driver.cxx186
-rw-r--r--tests/shared-ptr/makefile70
7 files changed, 514 insertions, 0 deletions
diff --git a/tests/compiler/makefile b/tests/compiler/makefile
new file mode 100644
index 0000000..5a63fc8
--- /dev/null
+++ b/tests/compiler/makefile
@@ -0,0 +1,18 @@
+# file : tests/compiler/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make
+
+tests := traversal
+
+default := $(out_base)/
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+$(default): $(addprefix $(out_base)/,$(addsuffix /,$(tests)))
+$(test): $(addprefix $(out_base)/,$(addsuffix /.test,$(tests)))
+$(clean): $(addprefix $(out_base)/,$(addsuffix /.clean,$(tests)))
+
+$(foreach t,$(tests),$(call import,$(src_base)/$t/makefile))
diff --git a/tests/compiler/traversal/driver.cxx b/tests/compiler/traversal/driver.cxx
new file mode 100644
index 0000000..8d49b6f
--- /dev/null
+++ b/tests/compiler/traversal/driver.cxx
@@ -0,0 +1,135 @@
+// file : tests/compiler/traversal/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#include <vector>
+#include <iostream>
+
+#include <cutl/shared-ptr.hxx>
+
+#include <cutl/compiler/type-info.hxx>
+#include <cutl/compiler/traversal.hxx>
+
+using namespace std;
+using namespace cutl;
+
+// Data types.
+//
+struct base
+{
+ virtual ~base () {}
+};
+
+struct derived1: base {};
+struct derived2: base {};
+
+typedef vector<shared_ptr<base> > objects;
+
+struct init
+{
+ init ()
+ {
+ using compiler::type_info;
+
+ {
+ type_info ti (typeid (base));
+ insert (ti);
+ }
+
+ {
+ type_info ti (typeid (derived1));
+ ti.add_base (typeid (base));
+ insert (ti);
+ }
+
+ {
+ type_info ti (typeid (derived2));
+ ti.add_base (typeid (base));
+ insert (ti);
+ }
+ }
+} init_;
+
+// Traversers.
+//
+template <typename X>
+struct traverser: compiler::traverser_impl<X, base>,
+ virtual compiler::dispatcher<base>
+{
+};
+
+typedef traverser<base> base_trav;
+typedef traverser<derived1> derived1_trav;
+typedef traverser<derived2> derived2_trav;
+
+struct base_impl: base_trav
+{
+ virtual void
+ traverse (type&)
+ {
+ cout << "base_impl: base" << endl;
+ }
+};
+
+struct derived1_impl: derived1_trav
+{
+ virtual void
+ traverse (type&)
+ {
+ cout << "derived1_impl: derived1" << endl;
+ }
+};
+
+struct combined_impl: derived1_trav, derived2_trav
+{
+ virtual void
+ traverse (derived1&)
+ {
+ cout << "combined_impl: derived1" << endl;
+ }
+
+ virtual void
+ traverse (derived2&)
+ {
+ cout << "combined_impl: derived2" << endl;
+ }
+};
+
+int
+main ()
+{
+ objects o;
+ o.push_back (shared_ptr<base> (new (shared) base));
+ o.push_back (shared_ptr<base> (new (shared) derived1));
+ o.push_back (shared_ptr<base> (new (shared) derived2));
+
+ base_impl base;
+ derived1_impl derived1;
+ combined_impl combined;
+
+ for (objects::iterator i (o.begin ()); i != o.end (); ++i)
+ base.dispatch (**i);
+
+ cout << endl;
+
+ for (objects::iterator i (o.begin ()); i != o.end (); ++i)
+ derived1.dispatch (**i);
+
+ cout << endl;
+
+ for (objects::iterator i (o.begin ()); i != o.end (); ++i)
+ combined.dispatch (**i);
+
+ cout << endl;
+
+ base.traverser (derived1);
+ for (objects::iterator i (o.begin ()); i != o.end (); ++i)
+ base.dispatch (**i);
+
+ cout << endl;
+
+ derived1.traverser (combined);
+ for (objects::iterator i (o.begin ()); i != o.end (); ++i)
+ derived1.dispatch (**i);
+}
diff --git a/tests/compiler/traversal/makefile b/tests/compiler/traversal/makefile
new file mode 100644
index 0000000..bb5513a
--- /dev/null
+++ b/tests/compiler/traversal/makefile
@@ -0,0 +1,71 @@
+# file : tests/compiler/traversal/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# 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 := $(driver)
+$(test): $(driver) $(src_base)/output.std
+ $(call message,test $$1,$$1 | diff -u $(src_base)/output.std -,$(driver))
+
+
+# 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)
diff --git a/tests/compiler/traversal/output.std b/tests/compiler/traversal/output.std
new file mode 100644
index 0000000..095739c
--- /dev/null
+++ b/tests/compiler/traversal/output.std
@@ -0,0 +1,16 @@
+base_impl: base
+base_impl: base
+base_impl: base
+
+derived1_impl: derived1
+
+combined_impl: derived1
+combined_impl: derived2
+
+base_impl: base
+derived1_impl: derived1
+base_impl: base
+
+derived1_impl: derived1
+combined_impl: derived1
+combined_impl: derived2
diff --git a/tests/makefile b/tests/makefile
new file mode 100644
index 0000000..ffbd8bd
--- /dev/null
+++ b/tests/makefile
@@ -0,0 +1,18 @@
+# file : tests/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make
+
+tests := compiler shared-ptr
+
+default := $(out_base)/
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+$(default): $(addprefix $(out_base)/,$(addsuffix /,$(tests)))
+$(test): $(addprefix $(out_base)/,$(addsuffix /.test,$(tests)))
+$(clean): $(addprefix $(out_base)/,$(addsuffix /.clean,$(tests)))
+
+$(foreach t,$(tests),$(call import,$(src_base)/$t/makefile))
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 <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#include <string>
+#include <cassert>
+
+#include <cutl/shared-ptr.hxx>
+
+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<type> x (new (shared) type (5, "foo"));
+ assert (x.count () == 1);
+ assert (x);
+ assert (x->x_ == 5);
+ assert ((*x).y_ == "foo");
+ {
+ shared_ptr<type> y (x);
+ assert (y.count () == 2);
+ }
+ {
+ shared_ptr<type> y;
+ y = x;
+ assert (y.count () == 2);
+ }
+ assert (x.count () == 1);
+ shared_ptr<type> y (x.release ());
+ assert (y.count () == 1);
+ }
+
+ // Polymorphic type.
+ //
+ {
+ shared_ptr<derived> x (new (shared) derived (5, "foo"));
+ assert (x.count () == 1);
+ {
+ shared_ptr<base2> y (x);
+ assert (y.count () == 2);
+ assert (y->y_ == "foo");
+ }
+ {
+ shared_ptr<base2> y;
+ y = x;
+ assert (y.count () == 2);
+ }
+ assert (x.count () == 1);
+ }
+
+ // Non-polymorphic type.
+ //
+ {
+ shared_ptr<shared_type> 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<shared_type> y (x);
+ assert (y.count () == 2);
+ }
+ {
+ shared_ptr<shared_type> y;
+ y = x;
+ assert (y.count () == 2);
+ }
+ assert (x.count () == 1);
+ shared_ptr<shared_type> 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 <boris@codesynthesis.com>
+# 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)