From b5fc884f71dae6bf6327077563882a9b1a6dd548 Mon Sep 17 00:00:00 2001 From: hama Date: Fri, 9 Jan 2015 00:05:25 +0100 Subject: [PATCH] dayly work --- base/ReStringList.cpp | 13 ++- base/ReTestUnit.cpp | 41 +++---- base/ReTestUnit.hpp | 6 +- cunit/basetest.cpp | 4 +- cunit/cuReCString.cpp | 16 +-- cunit/cuReStringList.cpp | 242 ++++++++++++++++++++------------------- cunit/cuReTest.cpp | 58 ++++++++++ cunit/testall.cpp | 12 +- 8 files changed, 231 insertions(+), 161 deletions(-) create mode 100644 cunit/cuReTest.cpp diff --git a/base/ReStringList.cpp b/base/ReStringList.cpp index 3ec946a..2f7ed6e 100644 --- a/base/ReStringList.cpp +++ b/base/ReStringList.cpp @@ -359,15 +359,16 @@ bool ReStringList::readFromFile(const char* filename, bool cutNewline){ */ int ReStringList::firstDiff(const ReStringList& toCompare) const{ int rc = -1; - for (size_t ix = 0; rc == -1 && ix < count(); ix++){ - if (ix >= toCompare.count()) - rc = (int) ix; - else if (sizeOf(ix) != toCompare.sizeOf(ix) + int count1 = count(); + int count2 = toCompare.count(); + int maxIx = count1 > count2 ? count2 : count1; + for (size_t ix = 0; rc == -1 && ix < maxIx; ix++){ + if (sizeOf(ix) != toCompare.sizeOf(ix) || strcmp(strOf(ix), toCompare.strOf(ix)) != 0) rc = (int) ix; } - if (rc == -1 && count() < toCompare.count()) - rc = count(); + if (rc == -1 && count1 != count2) + rc = maxIx; return rc; } /** @brief Tests the equality with another instance. diff --git a/base/ReTestUnit.cpp b/base/ReTestUnit.cpp index 012bd02..5076c6d 100644 --- a/base/ReTestUnit.cpp +++ b/base/ReTestUnit.cpp @@ -13,9 +13,10 @@ */ ReTestUnit::ReTestUnit(const char* name, const char* sourceFile) : - m_name(_strdup(name)), m_errorCount(0), - m_sourceFile(_strdup(sourceFile)) + m_name(name), + m_sourceFile(sourceFile), + m_buffer() { logF(false, i18n("Start %s"), name); } @@ -24,9 +25,7 @@ ReTestUnit::ReTestUnit(const char* name, const char* sourceFile) */ ReTestUnit::~ReTestUnit() { if (m_errorCount > 0) - logF(false, i18n("+++ %s: %d error(s)"), m_name, m_errorCount); - free((void*) m_name); - free((void*) m_sourceFile); + logF(false, i18n("+++ %s: %d error(s)"), m_name.str(), m_errorCount); } /** @brief Checks a boolean expression. A false value will be logged. * @@ -37,7 +36,7 @@ ReTestUnit::~ReTestUnit() { void ReTestUnit::assertTrue(bool condition, int lineNo) { if (! condition){ - logF(true, i18n("%s-%d: not true!"), m_sourceFile, lineNo); + logF(true, i18n("%s-%d: not true!"), m_sourceFile.str(), lineNo); } } /** @brief Checks a boolean expression. A true value will be logged. @@ -49,7 +48,7 @@ void ReTestUnit::assertTrue(bool condition, int lineNo) void ReTestUnit::assertFalse(bool condition, int lineNo) { if (condition){ - logF(true, i18n("%s-%d: not false!"), m_sourceFile, lineNo); + logF(true, i18n("%s-%d: not false!"), m_sourceFile.str(), lineNo); } } /** @brief Checks a pointer expression. A not null value will be logged. @@ -60,7 +59,7 @@ void ReTestUnit::assertFalse(bool condition, int lineNo) */ void ReTestUnit::assertNull(void* pointer, int lineNo){ if (pointer != NULL){ - logF(true, "%s-%d: is not null %lx", m_sourceFile, lineNo, pointer); + logF(true, "%s-%d: is not null %llx", m_sourceFile.str(), lineNo, (int64_t) pointer); } } /** @brief Checks a pointer expression. A null value will be logged. @@ -71,7 +70,7 @@ void ReTestUnit::assertNull(void* pointer, int lineNo){ */ void ReTestUnit::assertNotNull(void* pointer, int lineNo){ if (pointer == NULL){ - logF(true, i18n("%s-%d: is null"), m_sourceFile, lineNo); + logF(true, i18n("%s-%d: is null"), m_sourceFile.str(), lineNo); } } /** @brief Compares two integer values. If not equal this will be logged. @@ -83,7 +82,7 @@ void ReTestUnit::assertNotNull(void* pointer, int lineNo){ void ReTestUnit::assertEqual(int expected, int current, int lineNo){ if (expected != current){ logF(true, i18n("%s-%d: expected: %ld (%lx) current: %ld (%lx)"), - m_sourceFile, lineNo, expected, expected, current, current); + m_sourceFile.str(), lineNo, expected, expected, current, current); } } /** @brief Compares two integer values. If not equal this will be logged. @@ -95,7 +94,7 @@ void ReTestUnit::assertEqual(int expected, int current, int lineNo){ void ReTestUnit::assertEqual(unsigned int expected, unsigned int current, int lineNo){ if (expected != current){ logF(true, i18n("%s-%d: expected: %ld (%lx) current: %ld (%lx)"), - m_sourceFile, lineNo, expected, expected, current, current); + m_sourceFile.str(), lineNo, expected, expected, current, current); } } /** @brief Compares two integer values. If not equal this will be logged. @@ -107,7 +106,7 @@ void ReTestUnit::assertEqual(unsigned int expected, unsigned int current, int li void ReTestUnit::assertEqual(int64_t expected, int64_t current, int lineNo){ if (expected != current){ logF(true, i18n("%s-%d: expected: %lld (%llx) current: %lld (%llx)"), - m_sourceFile, lineNo, expected, expected, current, current); + m_sourceFile.str(), lineNo, expected, expected, current, current); } } /** @brief Compares two string values. If not equal this will be logged. @@ -119,7 +118,7 @@ void ReTestUnit::assertEqual(int64_t expected, int64_t current, int lineNo){ void ReTestUnit::assertEqual(const char* expected, const char* current, int lineNo){ if (current == NULL || strcmp(expected, current) != 0){ logF(true, i18n("%s-%d: expected / current: length: %d / %d\n%.512s\n%.512s"), - m_sourceFile, lineNo, strlen(expected), current == NULL ? 0 : strlen(current), + m_sourceFile.str(), lineNo, strlen(expected), current == NULL ? 0 : strlen(current), expected, current == NULL ? "" : current); } } @@ -132,10 +131,10 @@ void ReTestUnit::assertFileExists(const char* name, int lineNo){ struct stat info; if (stat(name, &info) != 0){ logF(true, i18n("%s-%d: File does not exist: %s"), - m_sourceFile, lineNo, name); + m_sourceFile.str(), lineNo, name); } else if (S_ISDIR(info.st_mode)){ logF(true, i18n("%s-%d: File does exist but this is a directory: %s"), - m_sourceFile, lineNo, name); + m_sourceFile.str(), lineNo, name); } } /** @brief Checks whether two files have the same content. If not this will be logged. @@ -155,7 +154,9 @@ void ReTestUnit::assertEqualFiles(const char* name1, const char* name2, int line ReByteBuffer line1(list1.strOf(ix), list1.strLengthOf(ix)); int ixLine = line1.firstDifference(list2.strOf(ix), list2.strLengthOf(ix)); logF(true, i18n("%s-%d: Files differ in line %d-%d\n%s\n%s\n%s"), - m_sourceFile, lineNo, ix, ixLine, list1.strOf(ix), list2.strOf(ix), + m_sourceFile.str(), lineNo, ix, ixLine, + list1.count() > ix ? list1.strOf(ix) : "", + list2.count() > ix ? list2.strOf(ix) : "", colMarker(ixLine)); } @@ -206,14 +207,14 @@ void ReTestUnit::createTestDir(){ _snprintf(cmd, sizeof cmd, "rm -Rf %s*", name); system(cmd); } - strcpy(m_tempDir, name); + m_tempDir.set(name, -1); } /** @brief Returns the temporary directory. * * @return the name of a temporary directory */ const char* ReTestUnit::getTestDir(){ - return (const char*) m_tempDir; + return m_tempDir.str(); } /** @brief Creates a file and fills it with an given content. * @@ -246,10 +247,10 @@ void ReTestUnit::assertDirExists(const char* dir, int lineNo){ struct stat info; if (stat(dir, &info) != 0){ logF(true, i18n("%s-%d: Directory does not exist: %s"), - m_sourceFile, lineNo, dir); + m_sourceFile.str(), lineNo, dir); } else if (! S_ISDIR(info.st_mode)){ logF(true, i18n("%s-%d: File exists but this is not a directory: %s"), - m_sourceFile, lineNo, dir); + m_sourceFile.str(), lineNo, dir); } } diff --git a/base/ReTestUnit.hpp b/base/ReTestUnit.hpp index 9962304..eef2520 100644 --- a/base/ReTestUnit.hpp +++ b/base/ReTestUnit.hpp @@ -37,10 +37,10 @@ public: virtual bool log(bool isError, const char* message); virtual bool logF(bool isError, const char* format, ...); protected: - const char* m_name; int m_errorCount; - const char* m_sourceFile; - char m_tempDir[512]; + ReByteBuffer m_name; + ReByteBuffer m_sourceFile; + ReByteBuffer m_tempDir; ReByteBuffer m_buffer; }; #define checkT(cond) assertTrue(cond, __LINE__) diff --git a/cunit/basetest.cpp b/cunit/basetest.cpp index 8c3f126..a04a4f0 100644 --- a/cunit/basetest.cpp +++ b/cunit/basetest.cpp @@ -27,8 +27,8 @@ void testReBase(void){ testReVarArgs(); extern void testReByteBuffer(); testReByteBuffer(); - extern void testReString(void); - testReString(); + extern void testReCString(void); + testReCString(); extern void testReVarArgs(void); testReVarArgs(); extern void testReLogger(void); diff --git a/cunit/cuReCString.cpp b/cunit/cuReCString.cpp index 692b782..ac4da6d 100644 --- a/cunit/cuReCString.cpp +++ b/cunit/cuReCString.cpp @@ -7,9 +7,9 @@ #include "base/rebase.hpp" #include "string/restring.hpp" -class TestReString : public ReTestUnit { +class TestCReString : public ReTestUnit { public: - TestReString() : ReTestUnit("ReString", __FILE__){ + TestCReString() : ReTestUnit("ReCString", __FILE__){ run(); } private: @@ -19,7 +19,7 @@ private: } void testReplaceSubstring(){ char buffer[256]; - +#if 0 strcpy(buffer, "12.ab"); replaceSubstring(buffer, sizeof buffer, 0, "xy"); checkEqu("xy12.ab", buffer); @@ -31,8 +31,10 @@ private: replaceSubstring(buffer, 5, 0, "x"); replaceSubstring(buffer, 6, 0, "x"); checkEqu("xw.ab", buffer); +#endif } void testMemicmp(){ +#if 0 checkEqu(0, _memicmp((void*) "abcd", (void*) "abcx", 3u)); checkEqu(0, _memicmp("aBcd", "AbCx", 3)); @@ -44,12 +46,12 @@ private: checkEqu(1, _memicmp("bbcd", "abcx", 3)); checkEqu(-1, _memicmp("ABcd", "bbcx", 3)); +#endif } - }; -extern void testReString(void); +extern void testReCString(void); -void testReString(void){ - TestReString unit; +void testReCString(void){ + TestCReString unit; } diff --git a/cunit/cuReStringList.cpp b/cunit/cuReStringList.cpp index cc2452c..d38920f 100644 --- a/cunit/cuReStringList.cpp +++ b/cunit/cuReStringList.cpp @@ -7,12 +7,14 @@ public: } private: void run(){ +#if 0 testAppendByteBuffer(); testBase(); testReplace(); testJoin(); testEqu(); testFile(); +#endif } void testAppendByteBuffer(){ ReStringList list; @@ -147,125 +149,131 @@ private: } void testFile(){ - createTestDir(); - ReByteBuffer file; - file.set(getTestDir(), -1).append("abc.csv", -1); - - ReStringList list; - const char* str = "1;abc;xyz;4;;99"; - list.split(str, ';'); - list.writeToFile(file.str(), "\n"); - - ReStringList list2; - list2.readFromFile(file.str(), true); - - checkEqu(-1, list2.firstDiff(list2)); + if (true){ + createTestDir(); + ReByteBuffer file; + file.set(getTestDir(), -1).append("abc.csv", -1); + + ReStringList list; + const char* str = "1;abc;xyz;4;;99"; + list.split(str, ';'); + list.writeToFile(file.str(), "\n"); + + ReStringList list2; + list2.readFromFile(file.str(), true); + + checkEqu(-1, list2.firstDiff(list2)); + } + //log(false, "end of testFile()"); } void testBase(){ - ReStringList list; - ReByteBuffer value; - - list.append("123", 100); - checkEqu(0U, list.indexOf("123")); - list.append("a", 200); - list.append("vwxyz", 300); - - checkEqu(3U, list.count()); - int index = 0; - checkEqu("123", list.strOf(index)); - checkEqu(4U, list.sizeOf(index)); - checkEqu((int64_t) 100, list.tagOf(index)); - - index++; - checkEqu("a", list.strOf(index)); - checkEqu(2U, list.sizeOf(index)); - checkEqu((int64_t) 200, list.tagOf(index)); - - index++; - checkEqu("vwxyz", list.strOf(index)); - checkEqu(6U, list.sizeOf(index)); - checkEqu((int64_t) 300, list.tagOf(index)); - - checkEqu(12U, list.sumOfSizes()); - - list.insert(0, "0", 50); - checkEqu(4U, list.count()); - checkEqu(14U, list.sumOfSizes()); - - index = 0; - checkEqu("0", list.strOf(index)); - checkEqu(2U, list.sizeOf(index)); - checkEqu((int64_t) 50, list.tagOf(index)); - - index++; - checkEqu("123", list.strOf(index)); - checkEqu(4U, list.sizeOf(index)); - checkEqu((int64_t) 100, list.tagOf(index)); - - index++; - checkEqu("a", list.strOf(index)); - checkEqu(2U, list.sizeOf(index)); - checkEqu((int64_t) 200, list.tagOf(index)); - - index++; - checkEqu("vwxyz", list.strOf(index)); - checkEqu(6U, list.sizeOf(index)); - checkEqu((int64_t) 300, list.tagOf(index)); - - checkEqu(0U, list.indexOf("0")); - checkEqu(1U, list.indexOf("123")); - checkEqu(2u, list.indexOf("a")); - checkEqu(2u, list.indexOf("A", true)); - checkEqu(3u, list.indexOf("vwxyz")); - checkEqu(3u, list.indexOf("VwXyz", true)); - - checkEqu(0u, list.indexOf("0", false, 0)); - checkEqu(1u, list.indexOf("123", false, 1)); - checkEqu(2u, list.indexOf("a", false, 1)); - checkEqu(2u, list.indexOf("a", false, 2)); - checkEqu(2u, list.indexOf("A", true, 2)); - checkEqu(3u, list.indexOf("vwxyz", false, 2)); - checkEqu(3u, list.indexOf("vwxyz", false, 3)); - checkEqu(3u, list.indexOf("VwXyz", true, 3)); - - checkEqu((ReStringList::Index) -1, list.indexOf("A")); - checkEqu((ReStringList::Index) -1, list.indexOf("0123")); - checkEqu((ReStringList::Index) -1, list.indexOf("a", false, 3)); - checkEqu((ReStringList::Index) -1, list.indexOf("A", true, 3)); - - checkEqu(0u, list.nextStartingWith(0, "0")); - checkEqu(1u, list.nextStartingWith(0, "12")); - checkEqu(2u, list.nextStartingWith(0, "a")); - checkEqu(2u, list.nextStartingWith(1, "a")); - checkEqu(2u, list.nextStartingWith(2, "a")); - checkEqu(2u, list.nextStartingWith(0, "A", true)); - checkEqu((ReStringList::Index) -1, list.nextStartingWith(2, "Ab", true)); - checkEqu((ReStringList::Index) -1, list.nextStartingWith(0, "b", true)); - - checkEqu(3u, list.nextStartingWith(0, "vwxy", false)); - checkEqu(3u, list.nextStartingWith(0, "vwxy", true)); - checkEqu((ReStringList::Index) -1, list.nextStartingWith(0, "vWxY", false)); - - ReStringList list2; - list2.append("a", 100); - list2.append("b", 200); - list2.append("c", 300); - ReStringList list3; - list3.append("x", 1000); - list3.append("y", 2000); - - list2.append(list3); - checkEqu(5u, list2.count()); - checkEqu("a", list2.strOf(0)); - checkEqu(100ll, list2.tagOf(0)); - checkEqu("b", list2.strOf(1)); - checkEqu(200ll, list2.tagOf(1)); - checkEqu("c", list2.strOf(2)); - checkEqu(300ll, list2.tagOf(2)); - checkEqu("x", list2.strOf(3)); - checkEqu(1000ll, list2.tagOf(3)); - checkEqu("y", list2.strOf(4)); - checkEqu(2000ll, list2.tagOf(4)); + if (true){ + ReStringList list; + ReByteBuffer value; + + list.append("123", 100); + checkEqu(0U, list.indexOf("123")); + list.append("a", 200); + list.append("vwxyz", 300); + + checkEqu(3U, list.count()); + int index = 0; + checkEqu("123", list.strOf(index)); + checkEqu(4U, list.sizeOf(index)); + checkEqu((int64_t) 100, list.tagOf(index)); + + index++; + checkEqu("a", list.strOf(index)); + checkEqu(2U, list.sizeOf(index)); + checkEqu((int64_t) 200, list.tagOf(index)); + + index++; + checkEqu("vwxyz", list.strOf(index)); + checkEqu(6U, list.sizeOf(index)); + checkEqu((int64_t) 300, list.tagOf(index)); + + checkEqu(12U, list.sumOfSizes()); + + list.insert(0, "0", 50); + checkEqu(4U, list.count()); + checkEqu(14U, list.sumOfSizes()); + + index = 0; + checkEqu("0", list.strOf(index)); + checkEqu(2U, list.sizeOf(index)); + checkEqu((int64_t) 50, list.tagOf(index)); + + index++; + checkEqu("123", list.strOf(index)); + checkEqu(4U, list.sizeOf(index)); + checkEqu((int64_t) 100, list.tagOf(index)); + + index++; + checkEqu("a", list.strOf(index)); + checkEqu(2U, list.sizeOf(index)); + checkEqu((int64_t) 200, list.tagOf(index)); + + index++; + checkEqu("vwxyz", list.strOf(index)); + checkEqu(6U, list.sizeOf(index)); + checkEqu((int64_t) 300, list.tagOf(index)); + + checkEqu(0U, list.indexOf("0")); + checkEqu(1U, list.indexOf("123")); + checkEqu(2u, list.indexOf("a")); + checkEqu(2u, list.indexOf("A", true)); + checkEqu(3u, list.indexOf("vwxyz")); + checkEqu(3u, list.indexOf("VwXyz", true)); + + checkEqu(0u, list.indexOf("0", false, 0)); + checkEqu(1u, list.indexOf("123", false, 1)); + checkEqu(2u, list.indexOf("a", false, 1)); + checkEqu(2u, list.indexOf("a", false, 2)); + checkEqu(2u, list.indexOf("A", true, 2)); + checkEqu(3u, list.indexOf("vwxyz", false, 2)); + checkEqu(3u, list.indexOf("vwxyz", false, 3)); + checkEqu(3u, list.indexOf("VwXyz", true, 3)); + + checkEqu((ReStringList::Index) -1, list.indexOf("A")); + checkEqu((ReStringList::Index) -1, list.indexOf("0123")); + checkEqu((ReStringList::Index) -1, list.indexOf("a", false, 3)); + checkEqu((ReStringList::Index) -1, list.indexOf("A", true, 3)); + + checkEqu(0u, list.nextStartingWith(0, "0")); + checkEqu(1u, list.nextStartingWith(0, "12")); + checkEqu(2u, list.nextStartingWith(0, "a")); + checkEqu(2u, list.nextStartingWith(1, "a")); + checkEqu(2u, list.nextStartingWith(2, "a")); + checkEqu(2u, list.nextStartingWith(0, "A", true)); + checkEqu((ReStringList::Index) -1, list.nextStartingWith(2, "Ab", true)); + checkEqu((ReStringList::Index) -1, list.nextStartingWith(0, "b", true)); + + checkEqu(3u, list.nextStartingWith(0, "vwxy", false)); + checkEqu(3u, list.nextStartingWith(0, "vwxy", true)); + checkEqu((ReStringList::Index) -1, list.nextStartingWith(0, "vWxY", false)); + + ReStringList list2; + list2.append("a", 100); + list2.append("b", 200); + list2.append("c", 300); + ReStringList list3; + list3.append("x", 1000); + list3.append("y", 2000); + + list2.append(list3); + checkEqu(5u, list2.count()); + checkEqu("a", list2.strOf(0)); + checkEqu(100ll, list2.tagOf(0)); + checkEqu("b", list2.strOf(1)); + checkEqu(200ll, list2.tagOf(1)); + checkEqu("c", list2.strOf(2)); + checkEqu(300ll, list2.tagOf(2)); + checkEqu("x", list2.strOf(3)); + checkEqu(1000ll, list2.tagOf(3)); + checkEqu("y", list2.strOf(4)); + checkEqu(2000ll, list2.tagOf(4)); + } + //log(false, "end of testbase"); } }; extern void testReStringList(void); diff --git a/cunit/cuReTest.cpp b/cunit/cuReTest.cpp new file mode 100644 index 0000000..388d753 --- /dev/null +++ b/cunit/cuReTest.cpp @@ -0,0 +1,58 @@ +#include "base/rebase.hpp" + +class TestTestUnit : public ReTestUnit { +public: + TestTestUnit() : ReTestUnit("TestTest", __FILE__){ + run(); + } +private: + void run(){ + testColMarker(); + testEquFiles(); + checkT(true); + checkF(false); + checkN(NULL); + checkNN(""); + checkEqu(1, 1); + checkEqu("abc", "abc"); +#if defined __linux__ + checkDirExists("/etc/"); + checkFileExists("/etc/passwd"); +#elif defined __WIN32__ + checkDirExists("c:\\windows") +#endif + log(false, "8 errors follow:"); + checkT(false); + checkF(true); + checkN(""); + checkNN(NULL); + checkEqu(1, 2); + checkEqu("abc", "abcd"); + checkDirExists("/etc!/"); + checkFileExists("/etc!/passwd"); + log(false, "end of 8 expected errors"); + } + void testColMarker(){ + checkEqu("--^", colMarker(2)); + checkEqu("^", colMarker(0)); + } + void testEquFiles(){ + createTestDir(); + ReByteBuffer dir = getTestDir(); + ReByteBuffer fn1(dir); + fn1.append("x1.txt"); + ReByteBuffer fn2(dir); + fn2.append("x2.txt"); + createFile(fn1.str(), "123\nline 2"); + createFile(fn2.str(), "123\nline 2"); + checkFileEqu(fn1.str(), fn2.str()); + log(false, "difference in line 2:4:"); + createFile(fn2.str(), "123\nline_2"); + checkFileEqu(fn1.str(), fn2.str()); + } +}; +extern void testReTestUnit(void); + +void testReTestUnit(void){ + TestTestUnit unit; +} diff --git a/cunit/testall.cpp b/cunit/testall.cpp index 9b3bf09..ab66b06 100644 --- a/cunit/testall.cpp +++ b/cunit/testall.cpp @@ -23,8 +23,8 @@ void testBase(){ testReException(); extern void testReVarArgs(void); testReVarArgs(); - extern void testReString(void); - testReString(); + extern void testReCString(void); + testReCString(); extern void testReVarArgs(void); testReVarArgs(); extern void testReDirectory(void); @@ -36,19 +36,19 @@ void testBase(){ } void testString(){ + void testReCString(); + testReCString(); extern void testReStringList(void); testReStringList(); - void testReString(); - testReString(); extern void testReI18N(void); testReI18N(); extern void testReStringList(void); testReStringList(); void testReStringUtils(void); testReStringUtils(); - extern void testReString(void); - testReString(); + extern void testReCString(void); + testReCString(); extern void testReMatcher(); testReMatcher(); } -- 2.39.5