Lumiera  0.pre.03
»edit your freedom«
advice-binding-pattern-test.cpp
Go to the documentation of this file.
1 /*
2  AdviceBindingPattern(Test) - cover pattern matching used to dispatch Advice
3 
4  Copyright (C) Lumiera.org
5  2010, 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 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
30 #include "lib/format-cout.hpp"
31 
32 #include "lib/time/timevalue.hpp"
33 #include "common/advice.hpp"
34 
35 using lib::time::Time;
36 
37 
38 
39 namespace lumiera {
40 namespace advice {
41 namespace test {
42 
43  namespace {
44  class DummyAdvice { };
45  }
46 
47 
48 
49  /***************************************************************************/
66  class AdviceBindingPattern_test : public Test
67  {
68 
69  virtual void
70  run (Arg)
71  {
72  verifyPatternSyntax();
73  verifyPatternNormalisation();
74  verifyStaticMatch();
75  verifyPreparedMatch();
76  verifyDynamicMatch();
77  }
78 
79 
80  void
81  verifyPatternSyntax()
82  {
83 #define _PARSE_AND_SHOW(_STR_) \
84  cout << _STR_ << "\t--->" << Binding(_STR_) << endl;
85 
86  _PARSE_AND_SHOW ("");
87  _PARSE_AND_SHOW ("aSymbol");
88  _PARSE_AND_SHOW ("a.compound_Symbol-with-various.parts");
89  _PARSE_AND_SHOW ("trailing Garbage allowed. ☢☢ eat ☠☠☠ atomic ☠☠☠ waste ☢☢");
90  _PARSE_AND_SHOW ("a, list , of ,symbols.");
91  _PARSE_AND_SHOW ("nullary().");
92  _PARSE_AND_SHOW ("nullary( )");
93  _PARSE_AND_SHOW ("nullary .");
94  _PARSE_AND_SHOW ("predicate( with-argument )");
95 
96  VERIFY_ERROR (BINDING_PATTERN_SYNTAX, Binding("no (valid definition here)"));
97  VERIFY_ERROR (BINDING_PATTERN_SYNTAX, Binding("predicate(with ☠☠☠ Garbage ☠☠☠"));
98  VERIFY_ERROR (BINDING_PATTERN_SYNTAX, Binding("§&Ω%€GΩ%€ar☠☠☠baäääääge"));
99 
100  Binding testBinding;
101  testBinding.addTypeGuard<DummyAdvice>();
102  testBinding.addPredicate("one two(), three( four ).");
103 
104  cout << testBinding << endl;
105  }
106 
107 
108  void
109  verifyPatternNormalisation()
110  {
111  Binding b0, b00;
112  Binding b1 ("cat1(), cat2().");
113  Binding b2 (" cat2 cat1 ....");
114 
115  cout << "b0==" << b0 << endl;
116  cout << "b1==" << b1 << endl;
117  cout << "b2==" << b2 << endl;
118 
119 
120  CHECK (b0 == b00); CHECK (b00 == b0);
121  CHECK (b1 == b2); CHECK (b2 == b1);
122 
123  CHECK (b0 != b1); CHECK (b1 != b0);
124  CHECK (b0 != b2); CHECK (b2 != b0);
125 
126  b2.addPredicate("cat1()"); // adding the same predicate multiple times has no effect
127  b2.addPredicate(" cat1 ");
128  CHECK (b1 == b2);
129  b2.addPredicate("cat3(zzz)");
130  CHECK (b1 != b2);
131 
132  b1.addTypeGuard<Time>();
133  CHECK (b1 != b2);
134  b1.addPredicate(" cat3( zzz ) ");
135  CHECK (b1 != b2);
136  b2.addTypeGuard<Time>();
137  CHECK (b1 == b2);
138 
139  cout << "b2==" << b2 << endl;
140  }
141 
142 
143  void
144  verifyStaticMatch()
145  {
146  CHECK ( matches (Binding(), Binding()));
147  CHECK ( matches (Binding("pred()"), Binding("pred( ) ")));
148 
149  CHECK ( matches (Binding("pred(x)"), Binding("pred(x)")));
150  CHECK (!matches (Binding("pred()"), Binding("pred(x)")));
151  CHECK (!matches (Binding("pred(x)"), Binding("pred(y)")));
152 
153  CHECK ( matches (Binding("pred(x), pred(y)"), Binding("pred(y), pred(x)")));
154  CHECK (!matches (Binding("pred(x), pred(y)"), Binding("pred(y), pred(y)")));
155  }
156 
157 
158  void
159  verifyPreparedMatch()
160  {
161  Binding b1 ("pred()");
162  Binding b2 ("pred");
163  Binding b3 ("pred, pred(x)");
164  Binding b4 ("pred( x ) , pred().");
165  CHECK ( matches (b1,b2));
166  CHECK ( matches (b3,b4));
167 
168  Binding::Matcher bm1 (b1.buildMatcher());
169  Binding::Matcher bm2 (b2.buildMatcher());
170  Binding::Matcher bm3 (b3.buildMatcher());
171  Binding::Matcher bm4 (b4.buildMatcher());
172 
173  CHECK (hash_value(b1) == hash_value(bm1));
174  CHECK (hash_value(b2) == hash_value(bm2));
175  CHECK (hash_value(b3) == hash_value(bm3));
176  CHECK (hash_value(b4) == hash_value(bm4));
177 
178  CHECK (hash_value(b1) != hash_value(b3));
179 
180  CHECK ( matches (bm1,bm2));
181  CHECK ( matches (bm3,bm4));
182  CHECK (!matches (bm1,bm3));
183  CHECK (!matches (bm2,bm4));
184  }
185 
186 
193  void
195  {
196 #if false
197  CHECK ( matches (Binding("pred(u)"), Binding("pred(X)")));
198  CHECK ( matches (Binding("pred(f(u))"), Binding("pred(f(X))")));
199  CHECK ( matches (Binding("pred(f(u,Y))"), Binding("pred(f(X,v))")));
200  CHECK ( matches (Binding("pred(f(u,X))"), Binding("pred(f(X,v))"))); // the so called "standardisation apart"
201 
202  CHECK (!matches (Binding("pred(u,v)"), Binding("pred(X)")));
203  CHECK (!matches (Binding("pred(f(u))"), Binding("pred(f(v))")));
204  CHECK (!matches (Binding("pred(f(u))"), Binding("pred(g(X))")));
205  CHECK (!matches (Binding("pred(f(u,v))"), Binding("pred(f(X,X))")));
206 
208 
209 #endif
210  }
211  };
212 
213 
214 
216  LAUNCHER (AdviceBindingPattern_test, "unit common");
217 
218 
219 }}} // namespace lumiera::advice::test
Conjunction of predicates to be matched against a collaboration partner for establishing an Advice co...
Automatically use custom string conversion in C++ stream output.
Functor object for matching against another Binding.
Definition: run.hpp:49
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Lumiera&#39;s internal time value datatype.
Definition: timevalue.hpp:308
Expecting Advice and giving Advice: a cross-cutting collaboration of loosely coupled participants...
Simple test class runner.
A collection of frequently used helper functions to support unit testing.
void addPredicate(Literal spec)
extend the definition of this binding by adding a predicate according to the given textual definition...
Lumiera public interface.
Definition: advice.cpp:113
a family of time value like entities and their relationships.