Lumiera  0.pre.03
»edit your freedom«
hash-indexed-test.cpp
Go to the documentation of this file.
1 /*
2  HashIndexed(Test) - proof-of-concept test for a hash based and typed ID
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 
30 #include "lib/hash-indexed.hpp"
31 #include "lib/util.hpp"
32 
33 #include <unordered_map>
34 
35 using util::isSameObject;
36 
37 
38 namespace lib {
39 namespace test{
40 
41  /* == a hierarchy of test-dummy objects to use the HashIndexed::ID == */
42 
44  {
45  long xyz_;
46  };
47 
48  struct TestB;
50 
51  struct TestB : DummyAncestor, Mixin
52  {
53  TestB () {}
54  TestB (ID const& refID) : Mixin (refID) {}
55 
56  bool operator== (TestB const& o) const { return this->getID() == o.getID(); }
57  };
58  struct TestDA : TestB {};
59  struct TestDB : TestB {};
60 
61 
62 
63 
64  /***********************************************************************/
76  class HashIndexed_test : public Test
77  {
78 
79  virtual void
80  run (Arg)
81  {
82  checkBasicProperties();
83  checkLUID_passing();
84 
85  // ---key-type-------+-value-+-hash-function---
86  buildHashtable<TestB::Id<TestDB>, TestDB, TestB::UseHashID> ();
87  buildHashtable<TestDB, TestDB, TestB::UseEmbeddedHash>();
88  }
89 
90 
91  void
92  checkBasicProperties ()
93  {
94  TestB::Id<TestDA> idDA;
95 
96  TestB bb (idDA);
97 
98  TestB::Id<TestDB> idDB1 ;
99  TestB::Id<TestDB> idDB2 (idDB1);
100 
101  CHECK (sizeof (idDB1) == sizeof (idDA) );
102  CHECK (sizeof (TestB::ID) == sizeof (hash::LuidH));
103  CHECK (sizeof (TestDA) == sizeof (hash::LuidH) + sizeof (DummyAncestor));
104 
105  CHECK (idDA == bb.getID() );
106  CHECK (idDB1 == idDB2 ); // equality handled by the hash impl (here LuidH)
107 
108  TestDA d1;
109  TestDA d2;
110  CHECK (d1.getID() != d2.getID()); // should be different because LUIDs are random
111 
112  d2 = d1;
113  CHECK (d1.getID() == d2.getID()); // default assignment operator works as expected
114  }
115 
116 
117  void
118  checkLUID_passing ()
119  {
120  TestB::Id<TestDA> idOrig;
121 
122  lumiera_uid plainLUID;
123  lumiera_uid_copy (&plainLUID, idOrig.get());
124 
125  // now, maybe after passing it through a Layer barrier...
126  TestB::ID const& idCopy = reinterpret_cast<TestB::ID & > (plainLUID);
127 
128  CHECK (idOrig == idCopy);
129  }
130 
131 
132  template<class KEY, class VAL, class HashFunc>
133  void
134  buildHashtable ()
135  {
136  typedef std::unordered_map<KEY, VAL, HashFunc> Hashtable;
137 
138  Hashtable tab;
139 
140  VAL o1; KEY key1 (o1);
141  VAL o2; KEY key2 (o2);
142  VAL o3; KEY key3 (o3);
143 
144  tab[key1] = o1; // store copy into hashtable
145  tab[key2] = o2;
146  tab[key3] = o3;
147 
148  CHECK (!isSameObject (o1, tab[key1])); // indeed a copy...
149  CHECK (!isSameObject (o2, tab[key2]));
150  CHECK (!isSameObject (o3, tab[key3]));
151 
152  CHECK (o1.getID() == tab[key1].getID()); // but "equal" by ID
153  CHECK (o2.getID() == tab[key2].getID());
154  CHECK (o3.getID() == tab[key3].getID());
155 
156  CHECK (o1.getID() != tab[key2].getID());
157  CHECK (o1.getID() != tab[key3].getID());
158  CHECK (o2.getID() != tab[key3].getID());
159  }
160 
161  };
162 
163 
165  LAUNCHER (HashIndexed_test, "unit common");
166 
167 
168 }} // namespace lib::test
Hash based ID, typed to a specific subclass of BA.
generic hash based ID, corresponding to the base class BA
Definition: run.hpp:49
A Mixin to add a private ID type to the target class, together with storage to hold an instance of th...
bool operator==(PtrDerefIter< I1 > const &il, PtrDerefIter< I2 > const &ir)
Supporting equality comparisons...
Implementation namespace for support and library code.
Simple test class runner.
trivial hash functor using the ID as hash
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
void lumiera_uid_copy(lumiera_uid *dest, lumiera_uid *src)
Copy an luid.
Definition: luid.c:99
A template for generating hash based ID tags carrying compile-time type info.
Hash implementation based on a lumiera unique object id (LUID) When invoking the default ctor...
unsigned char lumiera_uid[16]
storage for a Lumiera unique ID, based on a 128bit random number
Definition: hash-value.h:45
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:347