From 70b44f6485bcaa2c51097449d0fa581c7e1453ce Mon Sep 17 00:00:00 2001 From: janwas Date: Sun, 14 Aug 2005 23:36:43 +0000 Subject: [PATCH] moved self-tests into their respective files (this makes checking if a file has one easier) and automatically calls them if #if PERFORM_SELF_TEST == 1 (allows global enable/disable of all tests and also per-file override). This was SVN commit r2623. --- source/maths/Matrix3D.cpp | 40 +++++++++ source/ps/Parser.cpp | 119 ++++++++++++++++++++++++- source/ps/XMLWriter.cpp | 73 ++++++++++++++++ source/tests/Tests.cpp | 178 -------------------------------------- 4 files changed, 230 insertions(+), 180 deletions(-) delete mode 100755 source/tests/Tests.cpp diff --git a/source/maths/Matrix3D.cpp b/source/maths/Matrix3D.cpp index 8f7460e4d5..e68e4a25ce 100755 --- a/source/maths/Matrix3D.cpp +++ b/source/maths/Matrix3D.cpp @@ -14,6 +14,11 @@ #include "Matrix3D.h" #include "Quaternion.h" +#ifndef PERFORM_SELF_TEST +#define PERFORM_SELF_TEST 0 +#endif + + CMatrix3D::CMatrix3D () { } @@ -477,3 +482,38 @@ void CMatrix3D::SetRotation(const CQuaternion& quat) } +//---------------------------------------------------------------------------- +// built-in self test +//---------------------------------------------------------------------------- + +#if PERFORM_SELF_TEST +namespace test { + +static void test1() +{ + CMatrix3D m; + srand(0); + for (int i = 0; i < 4; ++i) + { + for (int j = 0; j < 16; ++j) + m._data[j] = -1.0f + 2.0f*(rand()/(float)RAND_MAX); + CMatrix3D n; + m.GetInverse(n); + m *= n; + for (int x = 0; x < 4; ++x) + for (int y = 0; y < 4; ++y) + debug_assert(abs(m(x,y) - (x==y ? 1.0f : 0.0f)) < 0.0001f); + } +} + + +static int run_tests() +{ + test1(); + return 0; +} + +static int dummy = run_tests(); + +} // namespace test +#endif // #if PERFORM_SELF_TEST diff --git a/source/ps/Parser.cpp b/source/ps/Parser.cpp index 4abef2cbe5..00c4530d0c 100755 --- a/source/ps/Parser.cpp +++ b/source/ps/Parser.cpp @@ -1,5 +1,3 @@ -// last modified Thursday, May 08, 2003 - #include "precompiled.h" #include "Parser.h" @@ -8,6 +6,10 @@ #pragma warning(disable:4786) using namespace std; +#ifndef PERFORM_SELF_TEST +#define PERFORM_SELF_TEST 0 +#endif + //------------------------------------------------- // Macros //------------------------------------------------- @@ -1087,3 +1089,116 @@ CParser& CParserCache::Get(const char* str) return *parser; } } + + + + + +//---------------------------------------------------------------------------- +// built-in self test +//---------------------------------------------------------------------------- + +#if PERFORM_SELF_TEST +namespace test { + +static void test1() +{ + CParser Parser; + Parser.InputTaskType("test", "_$ident_=_$value_"); + + std::string str; + int i; + + CParserLine Line; + + debug_assert(Line.ParseString(Parser, "value=23")); + + debug_assert(Line.GetArgString(0, str) && str == "value"); + debug_assert(Line.GetArgInt(1, i) && i == 23); +} + + +static void test2() +{ + CParser Parser; + Parser.InputTaskType("test", "_$value_[$value]_"); + + std::string str; + + CParserLine Line; + + debug_assert(Line.ParseString(Parser, "12 34")); + debug_assert(Line.GetArgCount() == 2); + debug_assert(Line.GetArgString(0, str) && str == "12"); + debug_assert(Line.GetArgString(1, str) && str == "34"); + + debug_assert(Line.ParseString(Parser, "56")); + debug_assert(Line.GetArgCount() == 1); + debug_assert(Line.GetArgString(0, str) && str == "56"); + + debug_assert(! Line.ParseString(Parser, " ")); +} + + +static void test3() +{ + CParser Parser; + Parser.InputTaskType("test", "_[$value]_[$value]_[$value]_"); + + std::string str; + + CParserLine Line; + + debug_assert(Line.ParseString(Parser, "12 34 56")); + debug_assert(Line.GetArgCount() == 3); + debug_assert(Line.GetArgString(0, str) && str == "12"); + debug_assert(Line.GetArgString(1, str) && str == "34"); + debug_assert(Line.GetArgString(2, str) && str == "56"); + + debug_assert(Line.ParseString(Parser, "78 90")); + debug_assert(Line.GetArgCount() == 2); + debug_assert(Line.GetArgString(0, str) && str == "78"); + debug_assert(Line.GetArgString(1, str) && str == "90"); + + debug_assert(Line.ParseString(Parser, "ab")); + debug_assert(Line.GetArgCount() == 1); + debug_assert(Line.GetArgString(0, str) && str == "ab"); + + debug_assert(Line.ParseString(Parser, " ")); + debug_assert(Line.GetArgCount() == 0); +} + + +static void test4() +{ + CParser Parser; + Parser.InputTaskType("test", "<[_a_][_b_]_x_>"); + + std::string str; + + CParserLine Line; + debug_assert(Line.ParseString(Parser, "a b x a b x")); + debug_assert(Line.ParseString(Parser, "a x b x")); + debug_assert(Line.ParseString(Parser, "a x")); + debug_assert(Line.ParseString(Parser, "b x")); + debug_assert(Line.ParseString(Parser, "x")); + debug_assert(! Line.ParseString(Parser, "a x c x")); + debug_assert(! Line.ParseString(Parser, "a b a x")); + debug_assert(! Line.ParseString(Parser, "a")); + debug_assert(! Line.ParseString(Parser, "a a x")); + debug_assert(Line.ParseString(Parser, "a x a b x a x b x b x b x b x a x a x a b x a b x b x a x")); +} + +static int run_tests() +{ + test1(); + test2(); + test3(); + test4(); + return 0; +} + +static int dummy = run_tests(); + +} // namespace test +#endif // #if PERFORM_SELF_TEST diff --git a/source/ps/XMLWriter.cpp b/source/ps/XMLWriter.cpp index ea456c3152..7f8a0707d7 100644 --- a/source/ps/XMLWriter.cpp +++ b/source/ps/XMLWriter.cpp @@ -5,6 +5,11 @@ #include "ps/CLogger.h" #include "lib/res/file/vfs.h" +#ifndef PERFORM_SELF_TEST +#define PERFORM_SELF_TEST 0 +#endif + + // TODO (maybe): Write to the VFS handle frequently, instead of buffering // the entire file, so that large files get written faster. @@ -171,3 +176,71 @@ template <> void XMLWriter_File::ElementAttribute(const char* name, const { ElementAttribute(name, value.ToUTF8(), newelement); } + + +//---------------------------------------------------------------------------- +// built-in self test +//---------------------------------------------------------------------------- + +#if PERFORM_SELF_TEST +namespace test { + +static void test1() +{ + XML_Start("utf-8"); + XML_Doctype("Scenario", "/maps/scenario.dtd"); + + { + XML_Element("Scenario"); + { + XML_Comment("Comment test."); + XML_Comment("Comment test again."); + { + XML_Element("a"); + XML_Attribute("one", 1); + XML_Attribute("two", "TWO"); + XML_Text("b"); + XML_Text(" (etc)"); + } + { + XML_Element("c"); + XML_Text("d"); + } + XML_Setting("c2", "d2"); + { + XML_Element("e"); + { + { + XML_Element("f"); + XML_Text("g"); + } + { + XML_Element("h"); + } + { + XML_Element("i"); + XML_Attribute("j", 1.23); + { + XML_Element("k"); + XML_Attribute("l", 2.34); + XML_Text("m"); + } + } + } + } + } + } + + // For this test to be useful, it should actually test something. +} + +static int run_tests() +{ + test1(); + return 0; +} + +static int dummy = run_tests(); + +} // namespace test +#endif // #if PERFORM_SELF_TEST diff --git a/source/tests/Tests.cpp b/source/tests/Tests.cpp deleted file mode 100755 index 46255aad51..0000000000 --- a/source/tests/Tests.cpp +++ /dev/null @@ -1,178 +0,0 @@ -#include "precompiled.h" - -#include "ps/CLogger.h" -#include "ps/Parser.h" -#include "ps/XMLWriter.h" -#include "maths/Matrix3D.h" - -#define ensure(x) if (!(x)) { ++err_count; LOG(ERROR, "", "%s:%d - test failed! (%s)", __FILE__, __LINE__, #x); } - -void PerformTests() -{ - int err_count = 0; - - { - CParser Parser; - Parser.InputTaskType("test", "_$ident_=_$value_"); - - std::string str; - int i; - - CParserLine Line; - - ensure(Line.ParseString(Parser, "value=23")); - - ensure(Line.GetArgString(0, str) && str == "value"); - ensure(Line.GetArgInt(1, i) && i == 23); - } - - - { - CParser Parser; - Parser.InputTaskType("test", "_$value_[$value]_"); - - std::string str; - - CParserLine Line; - - ensure(Line.ParseString(Parser, "12 34")); - ensure(Line.GetArgCount() == 2); - ensure(Line.GetArgString(0, str) && str == "12"); - ensure(Line.GetArgString(1, str) && str == "34"); - - ensure(Line.ParseString(Parser, "56")); - ensure(Line.GetArgCount() == 1); - ensure(Line.GetArgString(0, str) && str == "56"); - - ensure(! Line.ParseString(Parser, " ")); - } - - { - CParser Parser; - Parser.InputTaskType("test", "_[$value]_[$value]_[$value]_"); - - std::string str; - - CParserLine Line; - - ensure(Line.ParseString(Parser, "12 34 56")); - ensure(Line.GetArgCount() == 3); - ensure(Line.GetArgString(0, str) && str == "12"); - ensure(Line.GetArgString(1, str) && str == "34"); - ensure(Line.GetArgString(2, str) && str == "56"); - - ensure(Line.ParseString(Parser, "78 90")); - ensure(Line.GetArgCount() == 2); - ensure(Line.GetArgString(0, str) && str == "78"); - ensure(Line.GetArgString(1, str) && str == "90"); - - ensure(Line.ParseString(Parser, "ab")); - ensure(Line.GetArgCount() == 1); - ensure(Line.GetArgString(0, str) && str == "ab"); - - ensure(Line.ParseString(Parser, " ")); - ensure(Line.GetArgCount() == 0); - } - - { - CParser Parser; - Parser.InputTaskType("test", "<[_a_][_b_]_x_>"); - - std::string str; - - CParserLine Line; - ensure(Line.ParseString(Parser, "a b x a b x")); - ensure(Line.ParseString(Parser, "a x b x")); - ensure(Line.ParseString(Parser, "a x")); - ensure(Line.ParseString(Parser, "b x")); - ensure(Line.ParseString(Parser, "x")); - ensure(! Line.ParseString(Parser, "a x c x")); - ensure(! Line.ParseString(Parser, "a b a x")); - ensure(! Line.ParseString(Parser, "a")); - ensure(! Line.ParseString(Parser, "a a x")); - ensure(Line.ParseString(Parser, "a x a b x a x b x b x b x b x a x a x a b x a b x b x a x")); - } - - if (err_count) - { - LOG(ERROR, "", "%d test failures!", err_count); - debug_warn("Test failures!"); - } - - - - { - XML_Start("utf-8"); - XML_Doctype("Scenario", "/maps/scenario.dtd"); - - { - XML_Element("Scenario"); - { - XML_Comment("Comment test."); - XML_Comment("Comment test again."); - { - XML_Element("a"); - XML_Attribute("one", 1); - XML_Attribute("two", "TWO"); - XML_Text("b"); - XML_Text(" (etc)"); - } - { - XML_Element("c"); - XML_Text("d"); - } - XML_Setting("c2", "d2"); - { - XML_Element("e"); - { - { - XML_Element("f"); - XML_Text("g"); - } - { - XML_Element("h"); - } - { - XML_Element("i"); - XML_Attribute("j", 1.23); - { - XML_Element("k"); - XML_Attribute("l", 2.34); - XML_Text("m"); - } - } - } - } - } - } - - // For this test to be useful, it should actually test something. - } - - { - const wchar_t chr_utf16[] = { 0x12, 0xff, 0x1234, 0x3456, 0x5678, 0x7890, 0x9abc, 0xbcde, 0xfffe }; - const unsigned char chr_utf8[] = { 0x12, 0xc3, 0xbf, 0xe1, 0x88, 0xb4, 0xe3, 0x91, 0x96, 0xe5, 0x99, 0xb8, 0xe7, 0xa2, 0x90, 0xe9, 0xaa, 0xbc, 0xeb, 0xb3, 0x9e, 0xef, 0xbf, 0xbe }; - CStrW str_utf16 (chr_utf16, sizeof(chr_utf16)/sizeof(wchar_t)); - CStr8 str_utf8 = str_utf16.ToUTF8(); - ensure(str_utf8.length() == sizeof(chr_utf8)); - ensure(memcmp(str_utf8.data(), chr_utf8, sizeof(chr_utf8)) == 0); - ensure(str_utf8.FromUTF8() == str_utf16); - } - - { - CMatrix3D m; - srand(0); - for (int i = 0; i < 4; ++i) - { - for (int j = 0; j < 16; ++j) - m._data[j] = -1.0f + 2.0f*(rand()/(float)RAND_MAX); - CMatrix3D n; - m.GetInverse(n); - m *= n; - for (int x = 0; x < 4; ++x) - for (int y = 0; y < 4; ++y) - ensure(abs(m(x,y) - (x==y ? 1.0f : 0.0f)) < 0.0001f); - } - - } -}