--------------------- PatchSet 4458 Date: 2007/05/06 03:18:15 Author: amosjeffries Branch: ayjwork Tag: (none) Log: Add unit tests for String. Fix bugs identified by new tests. Members: src/Makefile.am:1.116.2.4->1.116.2.5 src/SqString.cc:1.1.2.1->1.1.2.2 src/SqString.h:1.1.2.1->1.1.2.2 src/SquidString.h:1.8.8.3->1.8.8.4 src/tests/testString.cc:1.2.12.1->1.2.12.2 src/tests/testString.h:1.1->1.1.18.1 Index: squid3/src/Makefile.am =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/Makefile.am,v retrieving revision 1.116.2.4 retrieving revision 1.116.2.5 diff -u -r1.116.2.4 -r1.116.2.5 --- squid3/src/Makefile.am 3 May 2007 10:05:11 -0000 1.116.2.4 +++ squid3/src/Makefile.am 6 May 2007 03:18:15 -0000 1.116.2.5 @@ -1,7 +1,7 @@ # # Makefile for the Squid Object Cache server # -# $Id: Makefile.am,v 1.116.2.4 2007/05/03 10:05:11 amosjeffries Exp $ +# $Id: Makefile.am,v 1.116.2.5 2007/05/06 03:18:15 amosjeffries Exp $ # # Uncomment and customize the following to suit your needs: # @@ -1090,6 +1090,7 @@ globals.cc check_PROGRAMS+= \ + tests/testString \ tests/testAuth \ tests/testACLMaxUserIP \ tests/testBoilerplate \ @@ -1101,7 +1102,6 @@ tests/test_http_range \ tests/testHttpRequest \ tests/testStore \ - tests/testString \ tests/testURL \ @STORE_TESTS@ Index: squid3/src/SqString.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/Attic/SqString.cc,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid3/src/SqString.cc 5 May 2007 16:09:00 -0000 1.1.2.1 +++ squid3/src/SqString.cc 6 May 2007 03:18:15 -0000 1.1.2.2 @@ -1,6 +1,6 @@ /* - * $Id: SqString.cc,v 1.1.2.1 2007/05/05 16:09:00 amosjeffries Exp $ + * $Id: SqString.cc,v 1.1.2.2 2007/05/06 03:18:15 amosjeffries Exp $ * * DEBUG: section 67 String * AUTHOR: Duane Wessels @@ -63,7 +63,6 @@ SqString::init(char const *str) { assert(this); -printf("\nSqString Initialize: '%s'",str); PROF_start(StringInit); @@ -131,17 +130,37 @@ bool SqString::operator == (SqString const &that) const { - int r = this->compare(that); -printf("\nSqString::compare: '%s' == '%s' -> %d",buf_, that.c_str(), r); - return (0 == r); - - return (0 == this->compare(that)); + return (this->compare(that) == 0); } bool SqString::operator != (SqString const &that) const { - return (0 == this->compare(that)); + return (this->compare(that) != 0); +} + +bool +SqString::operator >= (SqString const &that) const +{ + return (this->compare(that) >= 0); +} + +bool +SqString::operator <= (SqString const &that) const +{ + return (this->compare(that) <= 0); +} + +bool +SqString::operator > (SqString const &that) const +{ + return (this->compare(that) > 0); +} + +bool +SqString::operator < (SqString const &that) const +{ + return (this->compare(that) < 0); } SqString::SqString (SqString const &old) @@ -159,11 +178,13 @@ SqString::append(const char *str, int len) { assert(this); - assert(str && len >= 0); PROF_start(StringAppend); + + if(len < 1 || str == NULL) + return; + if (len_ + len < size_) { -assert(buf_); // AYJ DEBUG. strncat(buf_, str, len); len_ += len; } else { Index: squid3/src/SqString.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/Attic/SqString.h,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid3/src/SqString.h 5 May 2007 16:09:00 -0000 1.1.2.1 +++ squid3/src/SqString.h 6 May 2007 03:18:15 -0000 1.1.2.2 @@ -1,6 +1,6 @@ /* - * $Id: SqString.h,v 1.1.2.1 2007/05/05 16:09:00 amosjeffries Exp $ + * $Id: SqString.h,v 1.1.2.2 2007/05/06 03:18:15 amosjeffries Exp $ * * DEBUG: section 67 String * AUTHOR: Duane Wessels @@ -91,6 +91,10 @@ SqString &operator =(SqString const &); bool operator ==(SqString const &) const; bool operator !=(SqString const &) const; + bool operator >=(SqString const &) const; + bool operator <=(SqString const &) const; + bool operator >(SqString const &) const; + bool operator <(SqString const &) const; _SQUID_INLINE_ int size() const; _SQUID_INLINE_ char const * c_str() const; Index: squid3/src/SquidString.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/SquidString.h,v retrieving revision 1.8.8.3 retrieving revision 1.8.8.4 diff -u -r1.8.8.3 -r1.8.8.4 --- squid3/src/SquidString.h 5 May 2007 14:33:54 -0000 1.8.8.3 +++ squid3/src/SquidString.h 6 May 2007 03:18:15 -0000 1.8.8.4 @@ -1,6 +1,6 @@ /* - * $Id: SquidString.h,v 1.8.8.3 2007/05/05 14:33:54 amosjeffries Exp $ + * $Id: SquidString.h,v 1.8.8.4 2007/05/06 03:18:15 amosjeffries Exp $ * * DEBUG: section 67 String * AUTHOR: Duane Wessels @@ -85,20 +85,20 @@ /* Overload standard C functions using the basic string API */ -inline int strncasecmp(const string &lhs, const char *rhs, size_t len) { return strncasecmp(lhs.c_str(), rhs, len); } +//inline int strncasecmp(const string &lhs, const char *rhs, size_t len) { return strncasecmp(lhs.c_str(), rhs, len); } inline int strncasecmp(const string &lhs, const string &rhs, size_t len) { return strncasecmp(lhs.c_str(), rhs.c_str(), len); } -inline int strcasecmp(const string &lhs, const char *rhs) { return strcasecmp(lhs.c_str(), rhs); } +//inline int strcasecmp(const string &lhs, const char *rhs) { return strcasecmp(lhs.c_str(), rhs); } inline int strcasecmp(const string &lhs, const string &rhs) { return strcasecmp(lhs.c_str(), rhs.c_str()); } -inline int strncmp(const string &lhs, const char *rhs, size_t len) { return strncmp(lhs.c_str(), rhs, len); } +//inline int strncmp(const string &lhs, const char *rhs, size_t len) { return strncmp(lhs.c_str(), rhs, len); } inline int strncmp(const string &lhs, const string &rhs, size_t len) { return strncmp(lhs.c_str(), rhs.c_str(), len); } -inline int strcmp(const string &lhs, const char *rhs) { return strcmp(lhs.c_str(), rhs); } +//inline int strcmp(const string &lhs, const char *rhs) { return strcmp(lhs.c_str(), rhs); } inline int strcmp(const string &lhs, const string &rhs) { return strcmp(lhs.c_str(), rhs.c_str()); } -inline const char * strpbrk(const string &lhs, const char *rhs) { return strpbrk(lhs.c_str(), rhs); } -inline const char * strpbrk(const string &lhs, const string &rhs) { return strpbrk(lhs.c_str(), rhs); } +//inline const char * strpbrk(const string &lhs, const char *rhs) { return strpbrk(lhs.c_str(), rhs); } +inline const char * strpbrk(const string &lhs, const string &rhs) { return strpbrk(lhs.c_str(), rhs.c_str()); } inline std::ostream& operator <<(std::ostream &os, const string &s) { os << s.c_str(); return os; } Index: squid3/src/tests/testString.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/tests/testString.cc,v retrieving revision 1.2.12.1 retrieving revision 1.2.12.2 diff -u -r1.2.12.1 -r1.2.12.2 --- squid3/src/tests/testString.cc 3 May 2007 13:37:09 -0000 1.2.12.1 +++ squid3/src/tests/testString.cc 6 May 2007 03:18:15 -0000 1.2.12.2 @@ -21,6 +21,133 @@ static Initer ensure_mempools; void +testString::testDefaults() +{ + string aStr; + + /* check this reports as empty */ + CPPUNIT_ASSERT( aStr.empty() ); + CPPUNIT_ASSERT_EQUAL( (const char*)NULL, aStr.c_str() ); + CPPUNIT_ASSERT_EQUAL( 0, aStr.size() ); + + string bStr("foo bar"); + + /* check copy constructor */ + CPPUNIT_ASSERT( !bStr.empty() ); + CPPUNIT_ASSERT_EQUAL( 7, bStr.size() ); + CPPUNIT_ASSERT( NULL != bStr.c_str() ); + CPPUNIT_ASSERT( memcmp(bStr.c_str(), "foo bar", 8) == 0 ); +} + +void +testString::testBooleans() +{ + const string smStr("bar"); + const string bgStr("foo"); + const string eqStr("foo"); + const string nqStr("food"); + + /* mathematical boolean operators */ + CPPUNIT_ASSERT(!(bgStr == smStr )); + CPPUNIT_ASSERT( bgStr != smStr ); + CPPUNIT_ASSERT( bgStr > smStr ); + CPPUNIT_ASSERT(!(bgStr < smStr )); + CPPUNIT_ASSERT( bgStr >= smStr ); + CPPUNIT_ASSERT(!(bgStr <= smStr )); + + /* reverse order to catch corners */ + CPPUNIT_ASSERT(!(smStr == bgStr )); + CPPUNIT_ASSERT( smStr != bgStr ); + CPPUNIT_ASSERT(!(smStr > bgStr )); + CPPUNIT_ASSERT( smStr < bgStr ); + CPPUNIT_ASSERT(!(smStr >= bgStr )); + CPPUNIT_ASSERT( smStr <= bgStr ); + + /* check identical to catch corners */ + CPPUNIT_ASSERT( bgStr == eqStr ); + CPPUNIT_ASSERT(!(bgStr != eqStr )); + CPPUNIT_ASSERT(!(bgStr > eqStr )); + CPPUNIT_ASSERT(!(bgStr < eqStr )); + CPPUNIT_ASSERT( bgStr >= eqStr ); + CPPUNIT_ASSERT( bgStr <= eqStr ); + + /* check _almost_ identical to catch corners */ + CPPUNIT_ASSERT(!(bgStr == nqStr )); + CPPUNIT_ASSERT( bgStr != nqStr ); + CPPUNIT_ASSERT(!(bgStr > nqStr )); + CPPUNIT_ASSERT( bgStr < nqStr ); + CPPUNIT_ASSERT(!(bgStr >= nqStr )); + CPPUNIT_ASSERT( bgStr <= nqStr ); +} + +void +testString::testAppend() +{ + // FIXME: make tests for this. + string aStr("hello"); + + aStr.append(" world"); + CPPUNIT_ASSERT_EQUAL( (string)"hello world", aStr ); + aStr.append(" howsit", 7); + CPPUNIT_ASSERT_EQUAL( (string)"hello world howsit", aStr ); + + string bStr; + string cStr("hello"); + + /* corner cases */ + bStr.append(NULL, 2); + CPPUNIT_ASSERT( bStr.empty() ); + CPPUNIT_ASSERT_EQUAL( 0, bStr.size() ); + CPPUNIT_ASSERT_EQUAL( (string)"", bStr ); + + bStr.append("hello", 5); + CPPUNIT_ASSERT( !bStr.empty() ); + CPPUNIT_ASSERT_EQUAL( 5, bStr.size() ); + CPPUNIT_ASSERT_EQUAL( (string)"hello", bStr ); + + bStr.append(NULL, 2); + CPPUNIT_ASSERT( !bStr.empty() ); + CPPUNIT_ASSERT_EQUAL( 5, bStr.size() ); + CPPUNIT_ASSERT_EQUAL( (string)"hello", bStr ); + + bStr.append(" world untroubled by things such as null termination", 6); + CPPUNIT_ASSERT( !bStr.empty() ); + CPPUNIT_ASSERT_EQUAL( 11, bStr.size() ); + CPPUNIT_ASSERT_EQUAL( (string)"hello world", bStr ); + + cStr.append(" wo"); + CPPUNIT_ASSERT( !cStr.empty() ); + CPPUNIT_ASSERT_EQUAL( 8, cStr.size() ); + CPPUNIT_ASSERT_EQUAL( (string)"hello wo", cStr ); + + cStr.append("rld\0 untroubled by things such as null termination", 10); + CPPUNIT_ASSERT( !cStr.empty() ); + CPPUNIT_ASSERT_EQUAL( 18, cStr.size() ); + CPPUNIT_ASSERT_EQUAL( (string)"hello world\0 untr", cStr ); +} + +void +testString::testAssignments() +{ + // FIXME: make tests for this. +} + +void +testString::testCstrMethods() +{ + // FIXME: make tests for this. + // strcmp, strncmp, etc.... +} + +void +testString::testSearch() +{ + // FIXME: make tests for this. + +// pos, rpos, find, rfind, etc... +} + +void testString::testCmpDefault() { string left, right; Index: squid3/src/tests/testString.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/tests/testString.h,v retrieving revision 1.1 retrieving revision 1.1.18.1 diff -u -r1.1 -r1.1.18.1 --- squid3/src/tests/testString.h 3 May 2006 14:50:43 -0000 1.1 +++ squid3/src/tests/testString.h 6 May 2007 03:18:16 -0000 1.1.18.1 @@ -11,18 +11,33 @@ class testString : public CPPUNIT_NS::TestFixture { CPPUNIT_TEST_SUITE( testString ); + CPPUNIT_TEST( testDefaults ); + /* boolean helper tests */ CPPUNIT_TEST( testCmpDefault ); CPPUNIT_TEST( testCmpEmptyString ); CPPUNIT_TEST( testCmpNotEmptyDefault ); + + CPPUNIT_TEST( testBooleans ); + CPPUNIT_TEST( testAppend ); + CPPUNIT_TEST( testAssignments ); + CPPUNIT_TEST( testCstrMethods ); + CPPUNIT_TEST( testSearch ); CPPUNIT_TEST_SUITE_END(); public: protected: + + /* std::string API */ + void testDefaults(); void testCmpDefault(); void testCmpEmptyString(); void testCmpNotEmptyDefault(); + void testBooleans(); + void testAppend(); + void testAssignments(); + void testCstrMethods(); + void testSearch(); }; #endif -