unit test to verify building a hashtable with Symbol keys
This commit is contained in:
parent
18c357eb4a
commit
109cc4701c
5 changed files with 250 additions and 1 deletions
|
|
@ -55,7 +55,7 @@ namespace lib {
|
|||
bool
|
||||
operator== (Literal sy1, Literal sy2)
|
||||
{
|
||||
return lumiera_strncmp (sy1,sy2, STRING_MAX_RELEVANT);
|
||||
return !lumiera_strncmp (sy1,sy2, STRING_MAX_RELEVANT);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -456,6 +456,20 @@ return: 0
|
|||
END
|
||||
|
||||
|
||||
TEST "Symbol_test" Symbol_test <<END
|
||||
out: one
|
||||
out: sizeof.+lib.Literal.+ = (4|8)
|
||||
out: Ss
|
||||
out: one\.\.\.\.one
|
||||
return: 0
|
||||
END
|
||||
|
||||
|
||||
TEST "Hashtable with Symbol keys" SymbolHashtable_test <<END
|
||||
return: 0
|
||||
END
|
||||
|
||||
|
||||
TEST "Test support functions" TestHelper_test <<END
|
||||
out: Displaying types and sizes....
|
||||
out: sizeof..theUniverse.+ = 42
|
||||
|
|
|
|||
|
|
@ -76,6 +76,8 @@ test_lib_SOURCES = \
|
|||
$(testlib_srcdir)/sync-classlock-test.cpp \
|
||||
$(testlib_srcdir)/sync-locking-test.cpp \
|
||||
$(testlib_srcdir)/sync-waiting-test.cpp \
|
||||
$(testlib_srcdir)/symbol-test.cpp \
|
||||
$(testlib_srcdir)/symbol-hashtable-test.cpp \
|
||||
$(testlib_srcdir)/time-wrapper-test.cpp \
|
||||
$(testlib_srcdir)/thread-wrapper-test.cpp \
|
||||
$(testlib_srcdir)/thread-wrapper-join-test.cpp \
|
||||
|
|
|
|||
104
tests/lib/symbol-hashtable-test.cpp
Normal file
104
tests/lib/symbol-hashtable-test.cpp
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
SymbolHashtable(Test) - building a hashtable with Symbol (id) keys
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2009, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program 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.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
* *****************************************************/
|
||||
|
||||
|
||||
|
||||
#include "lib/test/run.hpp"
|
||||
#include "lib/util.hpp"
|
||||
|
||||
#include "lib/symbol.hpp"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
#include <tr1/unordered_map>
|
||||
#include <string>
|
||||
|
||||
using util::isnil;
|
||||
using util::contains;
|
||||
|
||||
|
||||
|
||||
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");
|
||||
|
||||
}
|
||||
|
||||
typedef std::tr1::unordered_map< Symbol, string, hash<Symbol> > HTable;
|
||||
|
||||
|
||||
/*************************************************************
|
||||
* @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
|
||||
{
|
||||
|
||||
void
|
||||
run (Arg)
|
||||
{
|
||||
HTable table;
|
||||
ASSERT (isnil(table));
|
||||
|
||||
table[KEY1] = string (KEY1);
|
||||
table[KEY2] = string (KEY2);
|
||||
table[KEY3] = string (KEY3);
|
||||
table[KEY4] = string (KEY4);
|
||||
|
||||
ASSERT (!isnil(table));
|
||||
ASSERT (4 == table.size());
|
||||
ASSERT (contains (table, KEY1));
|
||||
ASSERT (contains (table, KEY2));
|
||||
ASSERT (contains (table, KEY3));
|
||||
ASSERT (contains (table, KEY4));
|
||||
ASSERT (!contains (table, KEY5));
|
||||
|
||||
ASSERT (string (KEY1) == table[KEY1]);
|
||||
ASSERT (string (KEY2) == table[KEY2]);
|
||||
ASSERT (string (KEY3) == table[KEY3]);
|
||||
ASSERT (string (KEY4) == table[KEY4]);
|
||||
|
||||
table[KEY3] += "...";
|
||||
ASSERT (string(KEY3) != table[KEY3]);
|
||||
ASSERT (string(KEY3)+"..." == table[KEY3]);
|
||||
|
||||
ASSERT (isnil (table[KEY5])); // adds a new empty value object as side effect
|
||||
ASSERT (5 == table.size());
|
||||
}
|
||||
};
|
||||
|
||||
LAUNCHER (SymbolHashtable_test, "function common");
|
||||
|
||||
|
||||
}} // namespace lib::test
|
||||
|
||||
129
tests/lib/symbol-test.cpp
Normal file
129
tests/lib/symbol-test.cpp
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
/*
|
||||
Symbol(Test) - verify basic properties of a Symbol datatype
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2009, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program 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.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
* *****************************************************/
|
||||
|
||||
|
||||
#include "lib/test/run.hpp"
|
||||
#include "lib/util.hpp"
|
||||
#include "lib/test/test-helper.hpp"
|
||||
|
||||
#include "lib/symbol.hpp"
|
||||
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using util::isnil;
|
||||
using util::isSameObject;
|
||||
|
||||
|
||||
|
||||
namespace lib {
|
||||
namespace test{
|
||||
|
||||
|
||||
/**********************************************************
|
||||
* @test properties of Symbol data type. Currently this is
|
||||
* just a thin wrapper for a const char *
|
||||
* @todo this test is very much WIP, as the implementation
|
||||
* of a real symbol type and symbol table remains
|
||||
* to be done. See Ticket #157
|
||||
*/
|
||||
class Symbol_test : public Test
|
||||
{
|
||||
|
||||
void
|
||||
run (Arg)
|
||||
{
|
||||
checkLiteral();
|
||||
checkSymbolCreation();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
checkLiteral()
|
||||
{
|
||||
Literal li1 ("one");
|
||||
Literal li2 (li1);
|
||||
Literal li3 ("one ");
|
||||
|
||||
cout << li1 << endl;
|
||||
cout << showSizeof(li1) << endl;
|
||||
ASSERT (sizeof(Literal) == sizeof(char*));
|
||||
|
||||
ASSERT (li1 == li2);
|
||||
ASSERT (!isSameObject (li1,li2));
|
||||
ASSERT (li1 != li3);
|
||||
ASSERT (li2 != li3);
|
||||
ASSERT (li3 != li2);
|
||||
|
||||
cout << showType(li1 + string("ce")) << endl;
|
||||
cout << showType(string("minus " +li1)) << endl;
|
||||
cout << li2+string("..") << string("..")+li2 << endl;
|
||||
|
||||
ASSERT (hash_value(li1) == hash_value(li2));
|
||||
ASSERT (hash_value(li2) != hash_value(li3));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
checkEmptyLiteral()
|
||||
{
|
||||
Literal nn1 (0);
|
||||
Literal nn2 ("");
|
||||
|
||||
ASSERT (isnil (nn1));
|
||||
ASSERT (isnil (nn2));
|
||||
|
||||
Literal nnn (" ");
|
||||
ASSERT (!isnil (nnn));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
checkSymbolCreation()
|
||||
{
|
||||
Literal l1("1");
|
||||
Symbol sy1("1");
|
||||
Symbol sy2(l1);
|
||||
|
||||
ASSERT (sy1 == sy2);
|
||||
ASSERT (!isSameObject (l1,sy1));
|
||||
ASSERT (!isSameObject (sy1,sy2));
|
||||
|
||||
Symbol sy3;
|
||||
ASSERT (isnil(sy3));
|
||||
ASSERT (sy1 != sy3);
|
||||
|
||||
sy3 = l1;
|
||||
ASSERT (!isnil(sy3));
|
||||
ASSERT (sy1 == sy3);
|
||||
|
||||
TODO ("real functionality of Symbol data type");
|
||||
}
|
||||
};
|
||||
|
||||
LAUNCHER (Symbol_test, "unit common");
|
||||
|
||||
|
||||
}} // namespace lib::test
|
||||
|
||||
Loading…
Reference in a new issue