2009-09-25 01:10:20 +02:00
|
|
|
|
/*
|
|
|
|
|
|
SymbolHashtable(Test) - building a hashtable with Symbol (id) keys
|
2010-12-10 02:55:40 +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-10 02:55:40 +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-10 02:55:40 +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
|
|
|
|
* *****************************************************************/
|
2009-09-25 01:10:20 +02:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file symbol-hashtable-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref SymbolHashtable_test
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2009-09-25 01:10:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2009-10-30 05:15:26 +01:00
|
|
|
|
#include "lib/test/test-helper.hpp"
|
2009-09-25 01:10:20 +02:00
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/symbol.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <boost/functional/hash.hpp>
|
2014-04-03 22:42:48 +02:00
|
|
|
|
#include <unordered_map>
|
2009-10-30 05:15:26 +01:00
|
|
|
|
#include <cstring>
|
2009-09-25 01:10:20 +02:00
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
|
|
using util::contains;
|
2009-10-30 05:15:26 +01:00
|
|
|
|
using util::isnil;
|
2009-09-25 01:10:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
using boost::hash;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace { // test data
|
|
|
|
|
|
Symbol KEY1 ("Key1");
|
|
|
|
|
|
Symbol KEY2 ("Key2");
|
|
|
|
|
|
Symbol KEY3 ("Key3");
|
|
|
|
|
|
Symbol KEY4 ("Key4");
|
|
|
|
|
|
Symbol KEY5 ("Key5");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-23 04:23:03 +01:00
|
|
|
|
typedef std::unordered_map< Symbol, string, hash<Symbol>> HTable;
|
2009-09-25 01:10:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
|
/*********************************************************//**
|
2009-09-25 01:10:20 +02:00
|
|
|
|
* @test build a hashtable using Symbol objects as Keys.
|
|
|
|
|
|
* Especially this verifies picking up a customised
|
|
|
|
|
|
* hash implementation via ADL
|
|
|
|
|
|
* @see symbol-impl.cpp
|
|
|
|
|
|
*/
|
|
|
|
|
|
class SymbolHashtable_test : public Test
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2010-12-17 22:51:27 +01:00
|
|
|
|
virtual void
|
2009-09-25 01:10:20 +02:00
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
2024-11-13 02:23:23 +01:00
|
|
|
|
seedRand();
|
2009-10-30 05:15:26 +01:00
|
|
|
|
checkHashFunction();
|
|
|
|
|
|
|
2009-09-25 01:10:20 +02:00
|
|
|
|
HTable table;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (isnil(table));
|
2009-09-25 01:10:20 +02:00
|
|
|
|
|
|
|
|
|
|
table[KEY1] = string (KEY1);
|
|
|
|
|
|
table[KEY2] = string (KEY2);
|
|
|
|
|
|
table[KEY3] = string (KEY3);
|
|
|
|
|
|
table[KEY4] = string (KEY4);
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!isnil(table));
|
|
|
|
|
|
CHECK (4 == table.size());
|
|
|
|
|
|
CHECK (contains (table, KEY1));
|
|
|
|
|
|
CHECK (contains (table, KEY2));
|
|
|
|
|
|
CHECK (contains (table, KEY3));
|
|
|
|
|
|
CHECK (contains (table, KEY4));
|
|
|
|
|
|
CHECK (!contains (table, KEY5));
|
2010-12-17 22:51:27 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (string (KEY1) == table[KEY1]);
|
|
|
|
|
|
CHECK (string (KEY2) == table[KEY2]);
|
|
|
|
|
|
CHECK (string (KEY3) == table[KEY3]);
|
|
|
|
|
|
CHECK (string (KEY4) == table[KEY4]);
|
2010-12-17 22:51:27 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
table[KEY3] += "...";
|
|
|
|
|
|
CHECK (string(KEY3) != table[KEY3]);
|
|
|
|
|
|
CHECK (string(KEY3)+"..." == table[KEY3]);
|
2010-12-17 22:51:27 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (isnil (table[KEY5])); // adds a new empty value object as side effect
|
|
|
|
|
|
CHECK (5 == table.size());
|
2009-09-25 01:10:20 +02:00
|
|
|
|
}
|
2009-10-30 05:15:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
checkHashFunction()
|
|
|
|
|
|
{
|
|
|
|
|
|
string random = randStr(STRING_MAX_RELEVANT+1);
|
|
|
|
|
|
|
|
|
|
|
|
string copy1(random);
|
|
|
|
|
|
copy1[5] = '\0'; // truncate the c-String to 5 chars
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
string copy2(random);
|
2024-11-13 02:23:23 +01:00
|
|
|
|
copy2[rani (STRING_MAX_RELEVANT)] = '*'; // modify a random position
|
2009-10-30 05:15:26 +01:00
|
|
|
|
|
|
|
|
|
|
string copy3(copy2);
|
2024-11-13 02:23:23 +01:00
|
|
|
|
copy3[STRING_MAX_RELEVANT] = '*'; // modify behind observation limit
|
2009-10-30 05:15:26 +01:00
|
|
|
|
|
|
|
|
|
|
Symbol l0;
|
|
|
|
|
|
Literal l51 (copy1.c_str());
|
|
|
|
|
|
Literal l52 (random.substr(0,5).c_str());
|
|
|
|
|
|
|
|
|
|
|
|
Literal l_1 (random.c_str());
|
|
|
|
|
|
Literal l_2 (copy2.c_str());
|
|
|
|
|
|
Literal l_3 (copy3.c_str());
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (isnil (l0));
|
|
|
|
|
|
CHECK (l0 != l51);
|
|
|
|
|
|
CHECK (l51 == l52);
|
2009-10-30 05:15:26 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (l51 != l_1);
|
|
|
|
|
|
CHECK (l_1 != l_2);
|
|
|
|
|
|
CHECK (l_2 == l_3); // difference not detected due to observation limit...
|
|
|
|
|
|
CHECK (!std::strncmp (l_2, l_3, STRING_MAX_RELEVANT ));
|
|
|
|
|
|
CHECK ( std::strncmp (l_2, l_3, STRING_MAX_RELEVANT+1));
|
2009-10-30 05:15:26 +01:00
|
|
|
|
|
|
|
|
|
|
size_t h0 = hash_value (l0);
|
|
|
|
|
|
size_t h51 = hash_value (l51);
|
|
|
|
|
|
size_t h52 = hash_value (l52);
|
|
|
|
|
|
size_t h_1 = hash_value (l_1);
|
|
|
|
|
|
size_t h_2 = hash_value (l_2);
|
|
|
|
|
|
size_t h_3 = hash_value (l_3);
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (h0 == 0);
|
|
|
|
|
|
CHECK (h51 != 0);
|
|
|
|
|
|
CHECK (h52 != 0);
|
|
|
|
|
|
CHECK (h_1 != 0);
|
|
|
|
|
|
CHECK (h_2 != 0);
|
|
|
|
|
|
CHECK (h_3 != 0);
|
2010-12-17 22:51:27 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (h51 == h52); // verify the hash function indeed stops at '\0'
|
|
|
|
|
|
CHECK (h51 != h_1); // share a common prefix, but the hash differs
|
|
|
|
|
|
CHECK (h_1 != h_2); // the single random modification is detected
|
|
|
|
|
|
CHECK (h_2 == h_3); // because l_2 and l_3 differ behind the fixed observation limit
|
2010-12-17 22:51:27 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (h_1 == hash_value (l_1)); //reproducible
|
|
|
|
|
|
CHECK (h_2 == hash_value (l_2));
|
|
|
|
|
|
CHECK (h_3 == hash_value (l_3));
|
2009-10-30 05:15:26 +01:00
|
|
|
|
}
|
2009-09-25 01:10:20 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
LAUNCHER (SymbolHashtable_test, "function common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|