Lumiera  0.pre.03
»edit your freedom«
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sub-id-test.cpp
Go to the documentation of this file.
1 /*
2  SubID(Test) - exploring possible properties of an extensible symbolic identifier
3 
4  Copyright (C)
5  2009, 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/format-cout.hpp"
23 
24 #include "lib/sub-id.hpp"
25 
26 #include <boost/functional/hash.hpp>
27 #include <unordered_map>
28 #include <functional>
29 #include <vector>
30 #include <string>
31 
32 
33 
34 namespace lib {
35 namespace test{
36 
37  using util::for_each;
38  using std::bind;
39  using std::placeholders::_1;
40  using boost::hash;
41  using std::vector;
42  using std::string;
43 
44 
45 
46  namespace { // test data
47 
48  enum Colour { R,G,B };
49 
50 
51  inline string
52  toString (Colour c)
53  {
54  static string sym("RGB");
55  return sym.substr(c,1);
56  }
57 
58  }
59 
60 
61 
62 
63  /************************************************************************/
71  class SubID_test : public Test
72  {
73 
74  virtual void
75  run (Arg)
76  {
77  checkBaseType();
78  checkExtension();
79  checkSubIDHash();
80  }
81 
82 
83  void
84  checkBaseType ()
85  {
86  typedef SubId<Colour> CID;
87  CID c1 (R);
88  CID c2 (G);
89  CID c3 (B);
90 
91  cout << "...." << c1 << c2 << c3 << endl;
92  }
93 
94 
95  void
96  checkExtension ()
97  {
98  typedef SubId<uint> UID;
99 
100  typedef ExtendedSubId<Colour, UID> CUID;
101 
102  SubID const& id1 = CUID(R, 12);
103  SubID const& id2 = CUID(G, 13);
104 
105  cout << "id1=" << id1 << endl;
106  cout << "id2=" << id2 << endl;
107  }
108 
109 
110  void
111  checkSubIDHash()
112  {
113  typedef SubId<Colour> CID;
114  typedef SubId<uint> UID;
115  typedef ExtendedSubId<Colour, UID> CUID;
116 
117  vector<CID> simpleIDs;
118  simpleIDs.push_back(CID(R));
119  simpleIDs.push_back(CID(R));
120  simpleIDs.push_back(CID(G));
121  simpleIDs.push_back(CID(B));
122 
123  vector<CUID> extendedIDs;
124  extendedIDs.push_back(CUID(R,22));
125  extendedIDs.push_back(CUID(R,22)); // note the duplicates get dropped
126  extendedIDs.push_back(CUID(R,23));
127  extendedIDs.push_back(CUID(R,24));
128  extendedIDs.push_back(CUID(G,24));
129  extendedIDs.push_back(CUID(B,25));
130 
131  buildHashtable<CID> (simpleIDs);
132  buildHashtable<CUID> (extendedIDs);
133  }
134 
135 
136 
137  template<class KEY>
138  struct HashTable
139  : std::unordered_map<KEY, string, hash<KEY>>
140  {
141  void
142  add (KEY key)
143  {
144  (*this)[key] = string(key);
145  }
146 
147  void
148  verify (KEY key)
149  {
150  cout << "verify....." << key << endl;
151  CHECK (string(key) == (*this)[key]);
152  }
153  };
154 
155 
156  template<class KEY>
157  void
158  buildHashtable (vector<KEY> keys)
159  {
160 
161  typedef HashTable<KEY> HTab;
162  HTab tab;
163 
164  for_each (keys, bind (&HTab::add, ref(tab), _1 ));
165  for_each (keys, bind (&HTab::verify, ref(tab), _1 ));
166 
167  cout << "Elements in hashtable: " << tab.size() << endl;
168  }
169 
170 
171  };
172 
173 
175  LAUNCHER (SubID_test, "unit common");
176 
177 
178 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:40
Implementation namespace for support and library code.
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
void for_each(CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
Accept binding for arbitrary function arguments.
Extensible symbolic ID type.
Perform operations "for each element" of a collection.