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)
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 
21 #include "lib/hash-indexed.hpp"
22 #include "lib/util.hpp"
23 
24 #include <unordered_map>
25 
26 using util::isSameObject;
27 
28 
29 namespace lib {
30 namespace test{
31 
32  /* == a hierarchy of test-dummy objects to use the HashIndexed::ID == */
33 
35  {
36  long xyz_;
37  };
38 
39  struct TestB;
41 
42  struct TestB : DummyAncestor, Mixin
43  {
44  TestB () {}
45  TestB (ID const& refID) : Mixin (refID) {}
46 
47  bool operator== (TestB const& o) const { return this->getID() == o.getID(); }
48  };
49  struct TestDA : TestB {};
50  struct TestDB : TestB {};
51 
52 
53 
54 
55  /***********************************************************************/
67  class HashIndexed_test : public Test
68  {
69 
70  virtual void
71  run (Arg)
72  {
73  checkBasicProperties();
74  checkLUID_passing();
75 
76  // ---key-type-------+-value-+-hash-function---
77  buildHashtable<TestB::Id<TestDB>, TestDB, TestB::UseHashID> ();
78  buildHashtable<TestDB, TestDB, TestB::UseEmbeddedHash>();
79  }
80 
81 
82  void
83  checkBasicProperties ()
84  {
85  TestB::Id<TestDA> idDA;
86 
87  TestB bb (idDA);
88 
89  TestB::Id<TestDB> idDB1 ;
90  TestB::Id<TestDB> idDB2 (idDB1);
91 
92  CHECK (sizeof (idDB1) == sizeof (idDA) );
93  CHECK (sizeof (TestB::ID) == sizeof (hash::LuidH));
94  CHECK (sizeof (TestDA) == sizeof (hash::LuidH) + sizeof (DummyAncestor));
95 
96  CHECK (idDA == bb.getID() );
97  CHECK (idDB1 == idDB2 ); // equality handled by the hash impl (here LuidH)
98 
99  TestDA d1;
100  TestDA d2;
101  CHECK (d1.getID() != d2.getID()); // should be different because LUIDs are random
102 
103  d2 = d1;
104  CHECK (d1.getID() == d2.getID()); // default assignment operator works as expected
105  }
106 
107 
108  void
109  checkLUID_passing ()
110  {
111  TestB::Id<TestDA> idOrig;
112 
113  lumiera_uid plainLUID;
114  lumiera_uid_copy (&plainLUID, idOrig.get());
115 
116  // now, maybe after passing it through a Layer barrier...
117  TestB::ID const& idCopy = reinterpret_cast<TestB::ID & > (plainLUID);
118 
119  CHECK (idOrig == idCopy);
120  }
121 
122 
123  template<class KEY, class VAL, class HashFunc>
124  void
125  buildHashtable ()
126  {
127  typedef std::unordered_map<KEY, VAL, HashFunc> Hashtable;
128 
129  Hashtable tab;
130 
131  VAL o1; KEY key1 (o1);
132  VAL o2; KEY key2 (o2);
133  VAL o3; KEY key3 (o3);
134 
135  tab[key1] = o1; // store copy into hashtable
136  tab[key2] = o2;
137  tab[key3] = o3;
138 
139  CHECK (!isSameObject (o1, tab[key1])); // indeed a copy...
140  CHECK (!isSameObject (o2, tab[key2]));
141  CHECK (!isSameObject (o3, tab[key3]));
142 
143  CHECK (o1.getID() == tab[key1].getID()); // but "equal" by ID
144  CHECK (o2.getID() == tab[key2].getID());
145  CHECK (o3.getID() == tab[key3].getID());
146 
147  CHECK (o1.getID() != tab[key2].getID());
148  CHECK (o1.getID() != tab[key3].getID());
149  CHECK (o2.getID() != tab[key3].getID());
150  }
151 
152  };
153 
154 
156  LAUNCHER (HashIndexed_test, "unit common");
157 
158 
159 }} // 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:40
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.
Simplistic 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:90
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:40
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee&#39;s memory identities. ...
Definition: util.hpp:421