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)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 #include "lib/util.hpp"
21 #include "lib/util-foreach.hpp"
22 #include "lib/query-util.hpp"
23 #include "lib/cmdline.hpp"
25 #include "lib/format-cout.hpp"
26 
27 #include <functional>
28 
29 using lib::Cmdline;
30 using util::isnil;
31 using util::contains;
32 using util::for_each;
33 
34 using std::placeholders::_1;
35 using std::bind;
36 using std::string;
37 
38 
39 
40 namespace lib {
41 namespace query {
42 namespace test{
43 
44 
45  struct Thing
46  {
47  virtual ~Thing() {} // add RTTI for Query.asKey();
48  };
49 
50 
51 
52  /********************************************************************/
56  class QueryUtils_test : public Test
57  {
58 
59  virtual void
60  run (Arg arg)
61  {
62  if (isnil(arg)) arg = Cmdline ("normaliseID extractID removeTerm countPred");
63 
64  if (contains (arg, "normaliseID")) check_normaliseID();
65  if (contains (arg, "extractID" )) check_extractID ();
66  if (contains (arg, "removeTerm" )) check_removeTerm ();
67  if (contains (arg, "countPred" )) check_countPred ();
68  }
69 
70 
71 
73  void
75  {
76  Cmdline tokens ("a A AA dufte 1a _1 A_A BÄH");
77  tokens.push_back ("");
78  tokens.push_back (" White \t space ");
79  tokens.push_back ("§&Ω%€GΩ%€ar ☠☠☠ baäääääge!!!!! ");
80 
81  cout << "..original : "<<tokens<<" :"<<endl;
82 
83  for_each (tokens, normaliseID, _1 );
84 
85  cout << "normalised : "<<tokens<<" :"<<endl;
86  }
87 
88 
89 
91  void
93  {
94  CHECK ("tok" == extractID ("pred", "pred(tok)." ));
95  CHECK ("tok" == extractID ("pred", " pred( tok )" ));
96  CHECK ("tok" == extractID ("pred", "pred(tok), pred(tux)." ));
97  CHECK ("tok" == extractID ("pred", "other(xyz) pred(tok) pred(tux)" ));
98  CHECK ("tok" == extractID ("pred", "some( pred(tok)" ));
99 
100  CHECK (isnil (extractID ("pred", "pred (tok)")));
101  CHECK (isnil (extractID ("pred", "pred tok)" )));
102  CHECK (isnil (extractID ("pred", "pred(tok " )));
103  }
104 
105 
106 
108  void
110  {
111  // successful------Symbol---input-string----------------------extracted------remaining-------------
112  CHECK_removeTerm ("pred", "pred(tok).", "pred(tok)", "." );
113  CHECK_removeTerm ("pred", " pred( tok )", "pred(tok)", " " );
114  CHECK_removeTerm ("pred", "pred(tok), pred(tux).", "pred(tok)", "pred(tux)." );
115  CHECK_removeTerm ("pred", "other(xyz) pred(tok) pred(tux)", "pred(tok)", "other(xyz) pred(tux)" );
116  CHECK_removeTerm ("pred", "some( pred(tok)", "pred(tok)", "some( " );
117 
118  // not successful
119  CHECK_removeTerm ("pred", "pred (tok", "", "pred (tok" );
120  CHECK_removeTerm ("pred", "pred tok)", "", "pred tok)" );
121  CHECK_removeTerm ("pred", "pred(tok", "", "pred(tok" );
122  }
123 
124  void
125  CHECK_removeTerm (Symbol sym, string input, string extracted, string modified)
126  {
127  CHECK (extracted == removeTerm (sym, input));
128  CHECK (modified == input);
129  }
130 
131 
132 
136  void
138  {
139  for (uint i=1; i <= 30; ++i)
140  CHECK ( i == countPred (garbage_query (i)));
141  }
142  };
143 
144 
146  LAUNCHER (QueryUtils_test, "unit query");
147 
148 
149 
150 }}} // 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:137
void normaliseID(string &id)
ensure standard format for a given id string.
Definition: query-util.cpp:52
Definition: run.hpp:40
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:92
Token or Atom with distinct identity.
Definition: symbol.hpp:117
Class to encapsulate the typical C-style commandline definition.
string garbage_query(int degree=0)
Simplistic 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:109
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:48
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255