summaryrefslogtreecommitdiff
path: root/tests/test_accumulated.cc
diff options
context:
space:
mode:
authorMartin Schulze <MHL.Schulze@t-online.de>2003-03-05 17:34:24 +0000
committerMartin Schulze <teebaum@src.gnome.org>2003-03-05 17:34:24 +0000
commite95491c7aab5ed98b0566901a4b5ca0b6a9110e5 (patch)
tree6ae5a0086ca968638aab52df8557154dfac22968 /tests/test_accumulated.cc
parent6a418adf5bb48eacbbb47a8cbf151fe74b1a158c (diff)
downloadsigc++-e95491c7aab5ed98b0566901a4b5ca0b6a9110e5.tar.gz
Add two test cases. test_size is showing the size of public and internal
2003-03-05 Martin Schulze <MHL.Schulze@t-online.de> * tests/Makefile.am, tests/test_size.cc, tests/test_accumulated.cc: Add two test cases. test_size is showing the size of public and internal structures. (Which apart from empty signals are smaller than the sizes of the equivalent libsigc++-1.2 structures.) test_accumulated is a test for the template signal<>::accumulated<> at the same time showing the use of accumulators in libsigc++2.
Diffstat (limited to 'tests/test_accumulated.cc')
-rw-r--r--tests/test_accumulated.cc49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/test_accumulated.cc b/tests/test_accumulated.cc
new file mode 100644
index 0000000..7791fb2
--- /dev/null
+++ b/tests/test_accumulated.cc
@@ -0,0 +1,49 @@
+// -*- c++ -*-
+/* Copyright 2002, The libsigc++ Development Team
+ * Assigned to public domain. Use as you wish without restriction.
+ */
+
+#include <sigc++/trackable.h>
+#include <sigc++/signal.h>
+#include <sigc++/functors/ptr_fun.h>
+#include <sigc++/functors/mem_fun.h>
+#include <iostream>
+
+using namespace std;
+using namespace sigc::functor;
+using sigc::signal;
+
+struct arithmetic_mean_accumulator
+{
+ typedef double result_type;
+ template<typename T_iterator>
+ double operator()(T_iterator first, T_iterator last) const
+ {
+ double value_ = 0;
+ int n_ = 0;
+ for (; first != last; ++first, ++n_)
+ value_ += *first;
+ return value_ / n_;
+ }
+};
+
+int foo(int i) { cout << "foo: " << 3*i+1 << endl; return 3*i+1;}
+int bar(float i) { cout << "bar: " << 5*(int)i-3 << endl; return 5*(int)i-3;}
+
+struct A : public sigc::trackable
+{
+ int foo(int i) { cout << "A::foo: " << 20*i-14 << endl; return 20*i-14;}
+};
+
+int main()
+{
+ signal<int,int>::accumulated<arithmetic_mean_accumulator> sig;
+
+ A a;
+ sig.connect(ptr_fun1(&foo));
+ sig.connect(mem_fun1(&a, &A::foo));
+ sig.connect(ptr_fun1(&bar));
+
+ cout << "Result (i=1): " << sig(1) << endl;
+ cout << "Result (i=11): " << sig(11) << endl;
+}