Skip to content

Commit

Permalink
Tests: Added StringPool test
Browse files Browse the repository at this point in the history
Tests *all* functionality of de::StringPool.
  • Loading branch information
danij-deng committed Nov 9, 2012
1 parent c6b6a38 commit ff99577
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 87 deletions.
87 changes: 0 additions & 87 deletions doomsday/libdeng2/src/data/stringpool.cpp
Expand Up @@ -511,91 +511,4 @@ void StringPool::print() const
}
#endif

#if 0
#ifdef _DEBUG
LIBDENG_DEFINE_UNITTEST(StringPool)
{
StringPool p;

String s = String("Hello");
DENG2_ASSERT(!p.isInterned(s));
DENG2_ASSERT(p.empty());

// First string.
p.intern(s);
DENG2_ASSERT(p.isInterned(s) == 1);

// Re-insertion.
DENG2_ASSERT(p.intern(s) == 1);

// Case insensitivity.
s = String("heLLO");
DENG2_ASSERT(p.intern(s) == 1);

// Another string.
s = String("abc");
String const& is = p.internAndRetrieve(s);
DENG2_ASSERT(!is.compare(s));

String s2 = String("ABC");
String const& is2 = p.internAndRetrieve(s2);
DENG2_ASSERT(!is2.compare(s));

DENG2_ASSERT(p.intern(is2) == 2);

DENG2_ASSERT(p.size() == 2);
//p.print();

DENG2_ASSERT(!p.empty());

p.setUserValue(1, 1234);
DENG2_ASSERT(p.userValue(1) == 1234);

DENG2_ASSERT(p.userValue(2) == 0);

s = String("HELLO");
p.remove(s);
DENG2_ASSERT(!p.isInterned(s));
DENG2_ASSERT(p.size() == 1);
DENG2_ASSERT(!p.string(2).compare("abc"));

s = String("Third!");
DENG2_ASSERT(p.intern(s) == 1);
DENG2_ASSERT(p.size() == 2);

s = String("FOUR");
p.intern(s);
p.removeById(1); // "Third!"

#if 0
// Serialize.
Writer* w = Writer_NewWithDynamicBuffer(0);
p.write(w);

// Deserialize.
Reader* r = Reader_NewWithBuffer(Writer_Data(w), Writer_Size(w));
StringPool p2;
p2.read(r);
//p2.print();
DENG2_ASSERT(p2.size() == 2);
DENG2_ASSERT(!p2.string(2).compare("abc"));
DENG2_ASSERT(!p2.string(3).compare("FOUR"));
s = String("hello again");
DENG2_ASSERT(p2.intern(s) == 1);

Reader_Delete(r);
Writer_Delete(w);
#endif

p.clear();
DENG2_ASSERT(p.empty());

//exit(123);
return true;
}
#endif

LIBDENG_RUN_UNITTEST(StringPool)
#endif

} // namespace de
115 changes: 115 additions & 0 deletions doomsday/tests/stringpool/main.cpp
@@ -0,0 +1,115 @@
/**
* @file main.cpp
*
* StringPool unit tests. @ingroup tests
*
* @author Copyright &copy; 2010-2012 Daniel Swanson <danij@dengine.net>
* @author Copyright &copy; 2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/

#include <de/StringPool>
#include <QDebug>

using namespace de;

int main(int, char**)
{
try
{
StringPool p;

String s = String("Hello");
DENG2_ASSERT(!p.isInterned(s));
DENG2_ASSERT(p.empty());

// First string.
p.intern(s);
DENG2_ASSERT(p.isInterned(s) == 1);

// Re-insertion.
DENG2_ASSERT(p.intern(s) == 1);

// Case insensitivity.
s = String("heLLO");
DENG2_ASSERT(p.intern(s) == 1);

// Another string.
s = String("abc");
String const& is = p.internAndRetrieve(s);
DENG2_ASSERT(!is.compare(s));

String s2 = String("ABC");
String const& is2 = p.internAndRetrieve(s2);
DENG2_ASSERT(!is2.compare(s));

DENG2_ASSERT(p.intern(is2) == 2);

DENG2_ASSERT(p.size() == 2);
//p.print();

DENG2_ASSERT(!p.empty());

p.setUserValue(1, 1234);
DENG2_ASSERT(p.userValue(1) == 1234);

DENG2_ASSERT(p.userValue(2) == 0);

s = String("HELLO");
p.remove(s);
DENG2_ASSERT(!p.isInterned(s));
DENG2_ASSERT(p.size() == 1);
DENG2_ASSERT(!p.string(2).compare("abc"));

s = String("Third!");
DENG2_ASSERT(p.intern(s) == 1);
DENG2_ASSERT(p.size() == 2);

s = String("FOUR");
p.intern(s);
p.removeById(1); // "Third!"

#if 0
// Serialize.
Writer* w = Writer_NewWithDynamicBuffer(0);
p.write(w);

// Deserialize.
Reader* r = Reader_NewWithBuffer(Writer_Data(w), Writer_Size(w));
StringPool p2;
p2.read(r);
//p2.print();
DENG2_ASSERT(p2.size() == 2);
DENG2_ASSERT(!p2.string(2).compare("abc"));
DENG2_ASSERT(!p2.string(3).compare("FOUR"));
s = String("hello again");
DENG2_ASSERT(p2.intern(s) == 1);

Reader_Delete(r);
Writer_Delete(w);
#endif

p.clear();
DENG2_ASSERT(p.empty());
}
catch(const Error& err)
{
qWarning() << err.asText() << "\n";
}

qDebug() << "Exiting main()...\n";
return 0;
}
8 changes: 8 additions & 0 deletions doomsday/tests/stringpool/stringpool.pro
@@ -0,0 +1,8 @@
include(../config_test.pri)

TEMPLATE = app
TARGET = test_stringpool

SOURCES += main.cpp

deployTest($$TARGET)
1 change: 1 addition & 0 deletions doomsday/tests/tests.pro
Expand Up @@ -9,5 +9,6 @@ deng_tests: SUBDIRS += \
archive \
record \
script \
stringpool \
vectors
#basiclink

0 comments on commit ff99577

Please sign in to comment.