Lumiera  0.pre.03
»edityourfreedom«
functor-util-test.cpp
Go to the documentation of this file.
1 /*
2  FunctorUtil(Test) - verifying function object and signal utilities
3 
4  Copyright (C) Lumiera.org
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
29 #include "lib/test/run.hpp"
30 #include "lib/functor-util.hpp"
31 
32 #include <functional>
33 #include <boost/functional/hash.hpp>
34 #include <iostream>
35 
36 using lib::HashVal;
37 using std::cout;
38 using std::function;
39 using boost::hash; // note: only boost::hash allows for easy defining of custom hash functions
40 
41 
42 namespace util {
43 namespace test {
44 
45 
46  namespace {
47 
48  void fun1 (int i) { cout << "fun1 (" << i << ")\n"; }
49  void fun2 (int i) { cout << "fun2 (" << i << ")\n"; }
50 
51  struct Dummy
52  {
53  void gummi (int i) { cout << "gummi (" << i << ")\n"; }
54  };
55  }
56 
57 
58 
59 
60 
61  /*****************************************************************/
65  class FunctorUtil_test : public Test
66  {
67  virtual void
68  run (Arg)
69  {
70  verifyBruteForceComparison();
71  verifyHashThroughBackdoor();
72  }
73 
74  typedef function<void(int)> Fvi;
75  typedef function<int(void)> Fiv;
76 
77  typedef function<void(void)> Fvv;
78 
79 
81  void
83  {
84  Fvi f0;
85  Fvi f1 (fun1);
86  Fvi f2 (fun2);
87 
88  CHECK (!rawComparison(f0, f1));
89  CHECK (!rawComparison(f1, f2));
90  CHECK (!rawComparison(f0, f2));
91 
92  Fvi f22 (f2);
93  CHECK ( rawComparison(f2, f22));
94 
95  f1 = f2;
96  CHECK ( rawComparison(f1, f2));
97 
98  CHECK (!rawComparison(f0, Fvi())); // note: can't detect they are equivalent
99  CHECK (!rawComparison(f0, Fiv()));
100 
101  f1 = bind (fun2, _1);
102  CHECK (!rawComparison(f1, f2));
103 
104  Dummy dum1, dum2;
105  Fvi fm1 = bind (&Dummy::gummi, dum1, _1);
106  Fvi fm2 = bind (&Dummy::gummi, dum2, _1);
107  Fvv fm3 = bind (&Dummy::gummi, dum1, 23);
108  Fvv fm4 = bind (&Dummy::gummi, dum1, 24);
109  Fvv fm5 = bind (&Dummy::gummi, dum2, 24);
110  Fvv fm6 = bind (&Dummy::gummi, dum2, 24);
111 
112  CHECK (!rawComparison(f1, fm1));
113 
114  CHECK (!rawComparison(fm1, fm2));
115  CHECK (!rawComparison(fm1, fm3));
116  CHECK (!rawComparison(fm1, fm4));
117  CHECK (!rawComparison(fm1, fm5));
118  CHECK (!rawComparison(fm1, fm6));
119  CHECK (!rawComparison(fm2, fm3));
120  CHECK (!rawComparison(fm2, fm4));
121  CHECK (!rawComparison(fm2, fm5));
122  CHECK (!rawComparison(fm2, fm6));
123  CHECK (!rawComparison(fm3, fm4));
124  CHECK (!rawComparison(fm3, fm5));
125  CHECK (!rawComparison(fm3, fm6));
126  CHECK (!rawComparison(fm4, fm5)); // note: same argument but different functor instance
127  CHECK (!rawComparison(fm4, fm6));
128  CHECK (!rawComparison(fm5, fm6)); // again: can't detect they are equivalent
129  }
130 
131 
136  void
138  {
139  Fvi f0;
140  Fvi f1 (fun1);
141  Fvi f2 (fun2);
142  Fvi f22 (f2);
143 
144  hash<Fvi> calculateHash;
145  CHECK (calculateHash (f0));
146  CHECK (calculateHash (f1));
147  CHECK (calculateHash (f2));
148  CHECK (calculateHash (f22));
149 
150  HashVal h0 = calculateHash (f0);
151  HashVal h1 = calculateHash (f1);
152  HashVal h2 = calculateHash (f2);
153  HashVal h22 = calculateHash (f22);
154 
155  CHECK (h0 != h1);
156  CHECK (h0 != h2);
157  CHECK (h1 != h2);
158 
159  CHECK (h2 == h22);
160 
161  f1 = f2;
162  h1 = calculateHash (f1);
163  CHECK (h1 == h2);
164  CHECK (h1 != h0);
165 
166  CHECK (h0 != calculateHash (Fvi())); // note: equivalence not detected
167 
168  // checking functors based on member function(s)
169  Dummy dum1, dum2;
170  Fvi fm1 = bind (&Dummy::gummi, dum1, _1);
171  Fvi fm2 = bind (&Dummy::gummi, dum2, _1);
172  Fvv fm3 = bind (&Dummy::gummi, dum1, 23);
173  Fvv fm4 = bind (&Dummy::gummi, dum1, 24);
174  Fvv fm5 = bind (&Dummy::gummi, dum2, 24);
175  Fvv fm6 = bind (&Dummy::gummi, dum2, 24);
176 
177  HashVal hm1 = calculateHash (fm1);
178  HashVal hm2 = calculateHash (fm2);
179 
180  hash<Fvv> calculateHashVV;
181  HashVal hm3 = calculateHashVV (fm3);
182  HashVal hm4 = calculateHashVV (fm4);
183  HashVal hm5 = calculateHashVV (fm5);
184  HashVal hm6 = calculateHashVV (fm6);
185 
186  CHECK (h1 != hm1);
187 
188  CHECK (hm1 != hm2);
189  CHECK (hm1 != hm3);
190  CHECK (hm1 != hm4);
191  CHECK (hm1 != hm5);
192  CHECK (hm1 != hm6);
193  CHECK (hm2 != hm3);
194  CHECK (hm2 != hm4);
195  CHECK (hm2 != hm5);
196  CHECK (hm2 != hm6);
197  CHECK (hm3 != hm4);
198  CHECK (hm3 != hm5);
199  CHECK (hm3 != hm6);
200  CHECK (hm4 != hm5);
201  CHECK (hm4 != hm6);
202  CHECK (hm5 != hm6); // again: unable to detect the equivalence
203  }
204  };
205 
206 
208  LAUNCHER (FunctorUtil_test, "unit common");
209 
210 
211 
212 }} // namespace util::test
#define hash
Definition: run.hpp:49
std::vector< string > & Arg
Definition: run.hpp:54
Simple test class runner.
bool rawComparison(function< SIG > const &f1, function< SIG > const &f2)
temporary workaround: tr1/functional should define public comparison operators for functor objects...
LAUNCHER(FormatCOUT_test, "unit common")
Collection of small helpers and utilities related to function objects.
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:56