Lumiera  0.pre.03
»edit your freedom«
query-utils-test.cpp
Go to the documentation of this file.
1 /*
2  QueryUtils(Test) - checking various utils provided for dealing with config queries
3 
4  Copyright (C) Lumiera.org
5  2008, 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/util.hpp"
30 #include "lib/util-foreach.hpp"
31 #include "lib/query-util.hpp"
32 #include "lib/cmdline.hpp"
34 #include "lib/format-cout.hpp"
35 
36 #include <functional>
37 
38 using lib::Cmdline;
39 using util::isnil;
40 using util::contains;
41 using util::for_each;
42 
43 using std::placeholders::_1;
44 using std::bind;
45 using std::string;
46 
47 
48 
49 namespace lib {
50 namespace query {
51 namespace test{
52 
53 
54  struct Thing
55  {
56  virtual ~Thing() {} // add RTTI for Query.asKey();
57  };
58 
59 
60 
61  /********************************************************************/
65  class QueryUtils_test : public Test
66  {
67 
68  virtual void
69  run (Arg arg)
70  {
71  if (isnil(arg)) arg = Cmdline ("normaliseID extractID removeTerm countPred");
72 
73  if (contains (arg, "normaliseID")) check_normaliseID();
74  if (contains (arg, "extractID" )) check_extractID ();
75  if (contains (arg, "removeTerm" )) check_removeTerm ();
76  if (contains (arg, "countPred" )) check_countPred ();
77  }
78 
79 
80 
82  void
84  {
85  Cmdline tokens ("a A AA dufte 1a _1 A_A BÄH");
86  tokens.push_back ("");
87  tokens.push_back (" White \t space ");
88  tokens.push_back ("§&Ω%€GΩ%€ar ☠☠☠ baäääääge!!!!! ");
89 
90  cout << "..original : "<<tokens<<" :"<<endl;
91 
92  for_each (tokens, normaliseID, _1 );
93 
94  cout << "normalised : "<<tokens<<" :"<<endl;
95  }
96 
97 
98 
100  void
102  {
103  CHECK ("tok" == extractID ("pred", "pred(tok)." ));
104  CHECK ("tok" == extractID ("pred", " pred( tok )" ));
105  CHECK ("tok" == extractID ("pred", "pred(tok), pred(tux)." ));
106  CHECK ("tok" == extractID ("pred", "other(xyz) pred(tok) pred(tux)" ));
107  CHECK ("tok" == extractID ("pred", "some( pred(tok)" ));
108 
109  CHECK (isnil (extractID ("pred", "pred (tok)")));
110  CHECK (isnil (extractID ("pred", "pred tok)" )));
111  CHECK (isnil (extractID ("pred", "pred(tok " )));
112  }
113 
114 
115 
117  void
119  {
120  // successful------Symbol---input-string----------------------extracted------remaining-------------
121  CHECK_removeTerm ("pred", "pred(tok).", "pred(tok)", "." );
122  CHECK_removeTerm ("pred", " pred( tok )", "pred(tok)", " " );
123  CHECK_removeTerm ("pred", "pred(tok), pred(tux).", "pred(tok)", "pred(tux)." );
124  CHECK_removeTerm ("pred", "other(xyz) pred(tok) pred(tux)", "pred(tok)", "other(xyz) pred(tux)" );
125  CHECK_removeTerm ("pred", "some( pred(tok)", "pred(tok)", "some( " );
126 
127  // not successful
128  CHECK_removeTerm ("pred", "pred (tok", "", "pred (tok" );
129  CHECK_removeTerm ("pred", "pred tok)", "", "pred tok)" );
130  CHECK_removeTerm ("pred", "pred(tok", "", "pred(tok" );
131  }
132 
133  void
134  CHECK_removeTerm (Symbol sym, string input, string extracted, string modified)
135  {
136  CHECK (extracted == removeTerm (sym, input));
137  CHECK (modified == input);
138  }
139 
140 
141 
145  void
147  {
148  for (uint i=1; i <= 30; ++i)
149  CHECK ( i == countPred (garbage_query (i)));
150  }
151  };
152 
153 
155  LAUNCHER (QueryUtils_test, "unit query");
156 
157 
158 
159 }}} // namespace lib::query::test
Utilities to support working with predicate queries.
Automatically use custom string conversion in C++ stream output.
uint countPred(const string &q)
count the top-level predicates in the query string.
Definition: query-util.cpp:146
void normaliseID(string &id)
ensure standard format for a given id string.
Definition: query-util.cpp:61
Definition: run.hpp:49
Implementation namespace for support and library code.
string extractID(Symbol sym, const string &termString)
(preliminary) helper: instead of really parsing and evaluating the terms, just do a regular expressio...
Definition: query-util.cpp:101
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Class to encapsulate the typical C-style commandline definition.
string garbage_query(int degree=0)
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
string removeTerm(Symbol sym, string &queryString)
(preliminary) helper: cut a term with the given symbol.
Definition: query-util.cpp:118
diagnostic helpers to support test related to predicate queries
void for_each(CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
Accept binding for arbitrary function arguments.
Perform operations "for each element" of a collection.
Abstraction of the usual int argc, int** argv-Commandline, to be able to treat it as a vector of stri...
Definition: cmdline.hpp:57
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255