Lumiera  0.pre.03
»edit your freedom«
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) 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 
28 #include "lib/test/run.hpp"
29 #include "lib/util.hpp"
30 #include "lib/util-foreach.hpp"
31 #include "lib/format-cout.hpp"
32 
33 #include "lib/sub-id.hpp"
34 
35 #include <boost/functional/hash.hpp>
36 #include <unordered_map>
37 #include <functional>
38 #include <vector>
39 #include <string>
40 
41 
42 
43 namespace lib {
44 namespace test{
45 
46  using util::for_each;
47  using std::bind;
48  using std::placeholders::_1;
49  using boost::hash;
50  using std::vector;
51  using std::string;
52 
53 
54 
55  namespace { // test data
56 
57  enum Colour { R,G,B };
58 
59 
60  inline string
61  toString (Colour c)
62  {
63  static string sym("RGB");
64  return sym.substr(c,1);
65  }
66 
67  }
68 
69 
70 
71 
72  /************************************************************************/
80  class SubID_test : public Test
81  {
82 
83  virtual void
84  run (Arg)
85  {
86  checkBaseType();
87  checkExtension();
88  checkSubIDHash();
89  }
90 
91 
92  void
93  checkBaseType ()
94  {
95  typedef SubId<Colour> CID;
96  CID c1 (R);
97  CID c2 (G);
98  CID c3 (B);
99 
100  cout << "...." << c1 << c2 << c3 << endl;
101  }
102 
103 
104  void
105  checkExtension ()
106  {
107  typedef SubId<uint> UID;
108 
109  typedef ExtendedSubId<Colour, UID> CUID;
110 
111  SubID const& id1 = CUID(R, 12);
112  SubID const& id2 = CUID(G, 13);
113 
114  cout << "id1=" << id1 << endl;
115  cout << "id2=" << id2 << endl;
116  }
117 
118 
119  void
120  checkSubIDHash()
121  {
122  typedef SubId<Colour> CID;
123  typedef SubId<uint> UID;
124  typedef ExtendedSubId<Colour, UID> CUID;
125 
126  vector<CID> simpleIDs;
127  simpleIDs.push_back(CID(R));
128  simpleIDs.push_back(CID(R));
129  simpleIDs.push_back(CID(G));
130  simpleIDs.push_back(CID(B));
131 
132  vector<CUID> extendedIDs;
133  extendedIDs.push_back(CUID(R,22));
134  extendedIDs.push_back(CUID(R,22)); // note the duplicates get dropped
135  extendedIDs.push_back(CUID(R,23));
136  extendedIDs.push_back(CUID(R,24));
137  extendedIDs.push_back(CUID(G,24));
138  extendedIDs.push_back(CUID(B,25));
139 
140  buildHashtable<CID> (simpleIDs);
141  buildHashtable<CUID> (extendedIDs);
142  }
143 
144 
145 
146  template<class KEY>
147  struct HashTable
148  : std::unordered_map<KEY, string, hash<KEY>>
149  {
150  void
151  add (KEY key)
152  {
153  (*this)[key] = string(key);
154  }
155 
156  void
157  verify (KEY key)
158  {
159  cout << "verify....." << key << endl;
160  CHECK (string(key) == (*this)[key]);
161  }
162  };
163 
164 
165  template<class KEY>
166  void
167  buildHashtable (vector<KEY> keys)
168  {
169 
170  typedef HashTable<KEY> HTab;
171  HTab tab;
172 
173  for_each (keys, bind (&HTab::add, ref(tab), _1 ));
174  for_each (keys, bind (&HTab::verify, ref(tab), _1 ));
175 
176  cout << "Elements in hashtable: " << tab.size() << endl;
177  }
178 
179 
180  };
181 
182 
184  LAUNCHER (SubID_test, "unit common");
185 
186 
187 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:49
Implementation namespace for support and library code.
Simple 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.