lumiera_/tests/library/hash-indexed-test.cpp

160 lines
4.7 KiB
C++
Raw Permalink Normal View History

/*
HashIndexed(Test) - proof-of-concept test for a hash based and typed ID
2010-12-17 23:28:49 +01:00
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
Copyright (C)
2009, Hermann Vosseler <Ichthyostega@web.de>
2010-12-17 23:28:49 +01:00
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
  **Lumiera** is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version. See the file COPYING for further details.
2010-12-17 23:28:49 +01:00
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
* *****************************************************************/
/** @file hash-indexed-test.cpp
** unit test \ref HashIndexed_test
*/
#include "lib/test/run.hpp"
#include "lib/hash-indexed.hpp"
2010-03-21 23:45:56 +01:00
#include "lib/util.hpp"
#include <unordered_map>
2010-03-28 05:14:57 +02:00
using util::isSameObject;
namespace lib {
namespace test{
2009-05-24 01:46:00 +02:00
/* == a hierarchy of test-dummy objects to use the HashIndexed::ID == */
struct DummyAncestor
{
long xyz_;
};
struct TestB; ///< Base class to mix in the hash ID facility
typedef HashIndexed<TestB, hash::LuidH> Mixin; ///< actual configuration of the mixin
struct TestB : DummyAncestor, Mixin
{
TestB () {}
TestB (ID const& refID) : Mixin (refID) {}
bool operator== (TestB const& o) const { return this->getID() == o.getID(); }
};
struct TestDA : TestB {};
struct TestDB : TestB {};
/***********************************************************************//**
* @test proof-of-concept test for a generic hash based and typed ID struct.
2025-06-07 23:59:57 +02:00
* - check the various ctors
* - check default assignment works properly
* - check assumptions about memory layout
* - check equality comparison
* - extract LUID and then cast LUID back into ID
* - use the embedded hash ID (LUID) as hashtable key
2025-06-07 23:59:57 +02:00
*
* @see lib::HashIndexed::Id
2025-06-07 23:59:57 +02:00
* @see mobject::Placement real world usage example
*/
class HashIndexed_test : public Test
{
virtual void
run (Arg)
{
checkBasicProperties();
checkLUID_passing();
2025-06-07 23:59:57 +02:00
// ---key-type-------+-value-+-hash-function---
buildHashtable<TestB::Id<TestDB>, TestDB, TestB::UseHashID> ();
buildHashtable<TestDB, TestDB, TestB::UseEmbeddedHash>();
}
void
checkBasicProperties ()
{
TestB::Id<TestDA> idDA;
TestB bb (idDA);
TestB::Id<TestDB> idDB1 ;
TestB::Id<TestDB> idDB2 (idDB1);
CHECK (sizeof (idDB1) == sizeof (idDA) );
CHECK (sizeof (TestB::ID) == sizeof (hash::LuidH));
CHECK (sizeof (TestDA) == sizeof (hash::LuidH) + sizeof (DummyAncestor));
CHECK (idDA == bb.getID() );
CHECK (idDB1 == idDB2 ); // equality handled by the hash impl (here LuidH)
TestDA d1;
TestDA d2;
CHECK (d1.getID() != d2.getID()); // should be different because LUIDs are random
2025-06-07 23:59:57 +02:00
d2 = d1;
CHECK (d1.getID() == d2.getID()); // default assignment operator works as expected
}
void
checkLUID_passing ()
{
TestB::Id<TestDA> idOrig;
lumiera_uid plainLUID;
lumiera_uid_copy (&plainLUID, idOrig.get());
// now, maybe after passing it through a Layer barrier...
TestB::ID const& idCopy = reinterpret_cast<TestB::ID & > (plainLUID);
CHECK (idOrig == idCopy);
}
template<class KEY, class VAL, class HashFunc>
void
buildHashtable ()
{
typedef std::unordered_map<KEY, VAL, HashFunc> Hashtable;
Hashtable tab;
VAL o1; KEY key1 (o1);
VAL o2; KEY key2 (o2);
VAL o3; KEY key3 (o3);
tab[key1] = o1; // store copy into hashtable
tab[key2] = o2;
tab[key3] = o3;
CHECK (!isSameObject (o1, tab[key1])); // indeed a copy...
CHECK (!isSameObject (o2, tab[key2]));
CHECK (!isSameObject (o3, tab[key3]));
CHECK (o1.getID() == tab[key1].getID()); // but "equal" by ID
CHECK (o2.getID() == tab[key2].getID());
CHECK (o3.getID() == tab[key3].getID());
CHECK (o1.getID() != tab[key2].getID());
CHECK (o1.getID() != tab[key3].getID());
CHECK (o2.getID() != tab[key3].getID());
}
};
/** Register this test class... */
LAUNCHER (HashIndexed_test, "unit common");
}} // namespace lib::test