From ff9957768721cafbdaa1c547cea579629338cefa Mon Sep 17 00:00:00 2001 From: danij Date: Fri, 9 Nov 2012 00:15:46 +0000 Subject: [PATCH] Tests: Added StringPool test Tests *all* functionality of de::StringPool. --- doomsday/libdeng2/src/data/stringpool.cpp | 87 ---------------- doomsday/tests/stringpool/main.cpp | 115 ++++++++++++++++++++++ doomsday/tests/stringpool/stringpool.pro | 8 ++ doomsday/tests/tests.pro | 1 + 4 files changed, 124 insertions(+), 87 deletions(-) create mode 100644 doomsday/tests/stringpool/main.cpp create mode 100644 doomsday/tests/stringpool/stringpool.pro diff --git a/doomsday/libdeng2/src/data/stringpool.cpp b/doomsday/libdeng2/src/data/stringpool.cpp index 5bf6ad8893..184be94d21 100644 --- a/doomsday/libdeng2/src/data/stringpool.cpp +++ b/doomsday/libdeng2/src/data/stringpool.cpp @@ -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 diff --git a/doomsday/tests/stringpool/main.cpp b/doomsday/tests/stringpool/main.cpp new file mode 100644 index 0000000000..8c682aee4e --- /dev/null +++ b/doomsday/tests/stringpool/main.cpp @@ -0,0 +1,115 @@ +/** + * @file main.cpp + * + * StringPool unit tests. @ingroup tests + * + * @author Copyright © 2010-2012 Daniel Swanson + * @author Copyright © 2012 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * 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 + */ + +#include +#include + +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; +} diff --git a/doomsday/tests/stringpool/stringpool.pro b/doomsday/tests/stringpool/stringpool.pro new file mode 100644 index 0000000000..1255227272 --- /dev/null +++ b/doomsday/tests/stringpool/stringpool.pro @@ -0,0 +1,8 @@ +include(../config_test.pri) + +TEMPLATE = app +TARGET = test_stringpool + +SOURCES += main.cpp + +deployTest($$TARGET) diff --git a/doomsday/tests/tests.pro b/doomsday/tests/tests.pro index b943493537..d98762789f 100644 --- a/doomsday/tests/tests.pro +++ b/doomsday/tests/tests.pro @@ -9,5 +9,6 @@ deng_tests: SUBDIRS += \ archive \ record \ script \ + stringpool \ vectors #basiclink