Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
entry-id-test.cpp
Go to the documentation of this file.
1 /*
2  EntryID(Test) - proof-of-concept test for a combined hash+symbolic ID
3 
4  Copyright (C) Lumiera.org
5  2010, 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/test/test-helper.hpp"
30 
31 #include "lib/idi/entry-id.hpp"
36 #include "lib/util-foreach.hpp"
37 #include "lib/format-cout.hpp"
38 #include "lib/symbol.hpp"
39 
40 #include <unordered_map>
41 #include <string>
42 
44 using lib::test::randStr;
45 using util::isSameObject;
46 using util::contains;
47 using util::and_all;
48 using lib::Literal;
49 using std::string;
50 
51 
52 
53 namespace lib {
54 namespace idi {
55 namespace test{
56 
57  using lumiera::error::LERR_(WRONG_TYPE);
58 
59  namespace { // Test definitions...
60 
61  struct Dummy { };
62 
63  }
64 
68 
69 
70 
71 
72  /***********************************************************************/
83  class EntryID_test : public Test
84  {
85 
86  virtual void
87  run (Arg)
88  {
89  checkCreation();
92  checkErasure();
94  }
95 
96 
97 
98  void
100  {
101  DummyID dID1;
102  DummyID dID2("strange");
103  DummyID dID3;
104  CHECK (dID1.isValid());
105  CHECK (dID2.isValid());
106  CHECK (dID3.isValid());
107  CHECK (dID1 != dID2); CHECK (dID2 != dID1);
108  CHECK (dID2 != dID3); CHECK (dID3 != dID2);
109  CHECK (dID1 != dID3); CHECK (dID3 != dID1);
110 
111  ForkID tID1;
112  ForkID tID2;
113  ForkID tID3("special");
114  CHECK (tID1.isValid());
115  CHECK (tID2.isValid());
116  CHECK (tID3.isValid());
117  CHECK (tID1 != tID2); CHECK (tID2 != tID1);
118  CHECK (tID2 != tID3); CHECK (tID3 != tID2);
119  CHECK (tID1 != tID3); CHECK (tID3 != tID1);
120 
121  cout << dID1 << endl;
122  cout << dID2 << endl;
123  cout << dID3 << endl;
124 
125  cout << tID1 << endl;
126  cout << tID2 << endl;
127  cout << tID3 << endl;
128 
129  DummyID x (dID2); // copy ctor
130  CHECK (x == dID2);
131  CHECK (!isSameObject (x, dID2));
132  }
133 
134 
135  void
137  {
138  using proc::asset::Asset;
139  using proc::asset::STRUCT;
140  using proc::asset::Category;
142 
143  ForkID tID(" test ⚡ ☠ ☭ ⚡ track ");
144 
145  // Symbol-ID will be "sanitised"
146  CHECK ("test_track" == tID.getSym());
147  CHECK (tID == ForkID("☢ test ☢ track ☢"));
148  CHECK (tID == ForkID(string{"☢ test ☢ track ☢"}));
149 
150  // but: there is a pass-through for internal symbols
151  CHECK (tID != ForkID(Symbol{"☢ test ☢ track ☢"}));
152 
153  CHECK (tID.getHash() == ForkID("☢ test ☢ track ☢").getHash());
154 
155  CHECK (getAssetIdent(tID) == Asset::Ident("test_track", Category(STRUCT,"forks"), "lumi", 0));
156 
157  CHECK (tID.getSym() == getAssetIdent(tID).name);
158  CHECK (getAssetIdent(ForkID()).category == Category (STRUCT,"forks"));
159  CHECK (getAssetIdent(ClipID()).category == Category (STRUCT,"clips"));
160 
161  ClipID cID2,cID3;
162  CHECK (cID2.getSym() < cID3.getSym());
163  CHECK (ClipID("x").getSym() == ClipID(" x ").getSym());
164 
165  for (uint i=0; i<10000; ++i)
166  {
167  ForkID arbitrary(randStr(30));
168  CHECK (0 < arbitrary.getHash());
169  CHECK (tID.getHash() != arbitrary.getHash());
170  tID = arbitrary;
171  CHECK (tID.getHash() == arbitrary.getHash());
172  CHECK (tID.getSym() == arbitrary.getSym());
173  CHECK (getAssetIdent(tID)== getAssetIdent(arbitrary));
174  }
175 
176  cout << showSizeof<ForkID>() << endl;
177  cout << showSizeof<BareEntryID>() << endl;
178  CHECK (sizeof(ForkID) == sizeof(BareEntryID));
179  CHECK (sizeof(ForkID) == sizeof(lumiera_uid) + sizeof(void*));
180  }
181 
182 
183  void
185  {
186  ForkID tID1("a1");
187  ForkID tID2("a1");
188  ForkID tID3("a2");
189  ForkID tID4("b");
190  CHECK (tID1 == tID2);
191 
192  CHECK (tID2 < tID3);
193  CHECK (tID2 <= tID3);
194  CHECK (tID3 >= tID2);
195  CHECK (tID3 > tID2);
196 
197  CHECK (tID3 < tID4);
198  CHECK (tID3 <= tID4);
199  CHECK (tID4 >= tID3);
200  CHECK (tID4 > tID3);
201 
202  ForkID trackID1, trackID2;
203  CHECK (trackID1 < trackID2); // auto generated IDs are prefix + running counter
204  }
205 
206 
207 
216  void
218  {
219  ForkID fID("suspicious");
220  ClipID cID("suspicious");
221 
222  CHECK (fID.getHash() != cID.getHash());
223  CHECK (fID.getSym() == cID.getSym());
224 
225  BareEntryID bIDf (fID);
226  BareEntryID bIDc (cID);
227 
228  CHECK (bIDf != bIDc);
229  CHECK (bIDf.getHash() != bIDc.getHash());
230  CHECK (bIDf.getSym() == bIDc.getSym());
231  CHECK ("suspicious" == bIDc.getSym());
232 
235  ForkID tIDnew = bIDf.recast<Fork>();
236  ClipID cIDnew = bIDc.recast<Clip>();
237  CHECK (tIDnew == fID);
238  CHECK (cIDnew == cID);
239 
240  VERIFY_ERROR (WRONG_TYPE, bIDf.recast<Clip>());
241  VERIFY_ERROR (WRONG_TYPE, bIDc.recast<Fork>());
242  VERIFY_ERROR (WRONG_TYPE, bIDc.recast<Dummy>());
243  VERIFY_ERROR (WRONG_TYPE, bIDf.recast<Dummy>());
244 
245  CHECK (fID == ForkID::recast (bIDf)); // equivalent static API on typed subclass
246  VERIFY_ERROR (WRONG_TYPE, ForkID::recast(bIDc));
247  VERIFY_ERROR (WRONG_TYPE, ClipID::recast(bIDf));
248  VERIFY_ERROR (WRONG_TYPE, DummyID::recast(bIDc));
249  VERIFY_ERROR (WRONG_TYPE, DummyID::recast(bIDf));
250 
251  // mixed equality comparisons (based on the hash)
252  BareEntryID bIDt_copy (bIDf);
253  CHECK (bIDf == bIDt_copy);
254  CHECK (!isSameObject (bIDf, bIDt_copy));
255 
256  CHECK (fID != bIDc);
257  CHECK (cID != bIDt_copy);
258  CHECK (fID == bIDt_copy);
259 
260  CHECK (bIDf == ForkID ("suspicious"));
261  CHECK (bIDf != ClipID ("suspicious"));
262  CHECK (bIDc == ClipID ("suspicious"));
263  CHECK (ForkID ("suspicious") != ClipID ("suspicious"));
264  }
265 
266 
267 
268  //---key--+-value-+-hash-function---
269  typedef std::unordered_map<DummyID, string, DummyID::UseEmbeddedHash> Hashtable;
270 
278  void
280  {
281  Hashtable tab;
282 
283  for (uint i=0; i<100000; ++i)
284  {
285  DummyID dummy;
286  tab[dummy] = string(dummy);
287  }
288 
289  CHECK (and_all (tab, verifyEntry));
290  CHECK (100000 == tab.size());
291  }
292 
293 
294  static bool
295  verifyEntry (Hashtable::value_type entry)
296  {
297  return checkForHashCollision(string(entry.first), entry.second);
298  }
299 
300  static bool
301  checkForHashCollision(string const& key, string const& val)
302  {
303  if (key != val) cout << "Hash collision: "<<key<<" != "<<val<< endl;
304  return key == val;
305  }
306 
307  };
308 
309 
310 
312  LAUNCHER (EntryID_test, "unit common");
313 
314 
315 }}} // namespace lib::idi::test
type erased baseclass for building a combined hash and symbolic ID.
Definition: entry-id.hpp:135
static EntryID const & recast(BareEntryID const &bID)
Definition: entry-id.hpp:259
string name
element ID, comprehensible but sanitised.
Definition: asset.hpp:162
A user visible/editable Clip is a reference to a contiguous sequence of media data loaded as Asset in...
Automatically use custom string conversion in C++ stream output.
string showSizeof(size_t siz, string name)
for printing sizeof().
Definition: test-helper.cpp:48
string randStr(size_t len)
create garbage string of given length
Definition: test-helper.cpp:61
Definition: run.hpp:49
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:75
typed symbolic and hash ID for asset-like position accounting.
Definition: entry-id.hpp:128
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify a statement indeed raises an exception.
LAUNCHER(EntryID_test,"unit common")
Register this test class...
Organisational grouping device within the Session model ("Track" / "Media Bin").
Implementation namespace for support and library code.
string const & getSym() const
Definition: entry-id.hpp:165
Superinterface describing especially bookkeeping properties.
Definition: asset.hpp:146
EntryID< proc::mobject::session::Clip > ClipID
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:337
Token or Atom with distinct identity.
Definition: symbol.hpp:116
MObject in the Session to represent a clip on the timeline.
bool contains(MAP &map, typename MAP::key_type const &key)
shortcut for containment test on a map
Definition: util.hpp:205
std::vector< string > & Arg
Definition: run.hpp:54
EntryID< TAR > const & recast() const
try to upcast this BareEntryID to a fully typed EntryID.
Definition: entry-id.hpp:297
#define LERR_(_NAME_)
Definition: error.hpp:58
Marker types to indicate a literal string and a Symbol.
Simple test class runner.
static bool checkForHashCollision(string const &key, string const &val)
static bool verifyEntry(Hashtable::value_type entry)
Tree like classification of Assets.
Definition: category.hpp:75
A collection of frequently used helper functions to support unit testing.
bool and_all(IT i, IT end, FUN predicate)
All quantification: check if all elements of a collection satisfy the given predicate.
Rarely used specialisations of otherwise heavily used generic code.
std::unordered_map< DummyID, string, DummyID::UseEmbeddedHash > Hashtable
bool isValid() const
Definition: entry-id.hpp:159
Bare symbolic and hash ID used for accounting of asset like entries.
unsigned char lumiera_uid[16]
storage for a Lumiera unique ID, based on a 128bit random number
Definition: hash-value.h:45
A Fork serves as grouping device within the Session.
Definition: fork.hpp:103
Perform operations "for each element" of a collection.
LuidH const & getHash() const
Definition: entry-id.hpp:171
Asset::Ident getAssetIdent(lib::idi::EntryID< TY > const &entryID)
generate an Asset identification tuple based on this EntryID's symbolic ID and type information...
Naming and labelling scheme for structural assets.
EntryID< proc::mobject::session::Fork > ForkID