]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Implemented CharacterSet's operator + and addRange, fixed bug in interface of operato...
authorFrancesco Chemolli <kinkie@squid-cache.org>
Sun, 29 Dec 2013 22:59:39 +0000 (23:59 +0100)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Sun, 29 Dec 2013 22:59:39 +0000 (23:59 +0100)
src/base/CharacterSet.cc
src/base/CharacterSet.h
src/base/Makefile.am
src/base/testCharacterSet.cc [new file with mode: 0644]
src/base/testCharacterSet.h [new file with mode: 0644]

index cabe47b6f5223fc3dde3b3530ec7813915d9f689..1c6df2f53811bb882a077afd87cc5a8384e9ee3c 100644 (file)
@@ -1,7 +1,7 @@
 #include "squid.h"
 #include "CharacterSet.h"
 
-const CharacterSet &
+CharacterSet &
 CharacterSet::operator +=(const CharacterSet &src)
 {
     Storage::const_iterator s = src.chars_.begin();
@@ -16,6 +16,14 @@ CharacterSet::operator +=(const CharacterSet &src)
     return *this;
 }
 
+CharacterSet
+CharacterSet::operator +(const CharacterSet &src) const
+{
+    CharacterSet rv(*this);
+    rv += src;
+    return rv;
+}
+
 CharacterSet &
 CharacterSet::add(const unsigned char c)
 {
@@ -23,6 +31,18 @@ CharacterSet::add(const unsigned char c)
     return *this;
 }
 
+CharacterSet &
+CharacterSet::addRange(const unsigned char low, const unsigned char high)
+{
+    assert(low <= high);
+    unsigned char c = low;
+    while (c <= high) {
+        chars_[static_cast<uint8_t>(c)] = 1;
+        ++c;
+    }
+    return *this;
+}
+
 CharacterSet::CharacterSet(const char *label, const char * const c)
 : name(label == NULL ? "anonymous" : label), chars_(Storage(256,0))
 {
@@ -30,3 +50,12 @@ CharacterSet::CharacterSet(const char *label, const char * const c)
     for (size_t i = 0; i < clen; ++i)
         add(c[i]);
 }
+
+const CharacterSet
+CharacterSet::ALPHA("ALPHA","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"),
+CharacterSet::BIT("BIT","01"),
+CharacterSet::CRLF("CRLF","\r\n"),
+CharacterSet::DIGIT("DIGIT","0123456789"),
+CharacterSet::HEXDIG("HEXDIG","0123456789aAbBcCdDeEfF"),
+CharacterSet::WSP("WSP"," \t")
+;
index 48a613e9c36b769d081c6b0bee1ca18bbb2ade41..cbe0bbe326ae038d33a123ba4a25164a85f1f97c 100644 (file)
@@ -19,12 +19,32 @@ public:
     /// add a given character to the character set
     CharacterSet & add(const unsigned char c);
 
+    /// add a character range to the set from low to high included
+    CharacterSet & addRange(const unsigned char low, const unsigned char high);
+
     /// add all characters from the given CharacterSet to this one
-    const CharacterSet &operator +=(const CharacterSet &src);
+    CharacterSet &operator +=(const CharacterSet &src);
+
+    /// return a new CharacterSet containing the union of two sets
+    CharacterSet operator +(const CharacterSet &src) const;
 
     /// optional set label for debugging (default: "anonymous")
     const char * name;
 
+    // A-Za-z
+    static const CharacterSet ALPHA;
+    // 0-1
+    static const CharacterSet BIT;
+    // CRLF
+    static const CharacterSet CRLF;
+    // 0-9
+    static const CharacterSet DIGIT;
+    // 0-9aAbBcCdDeEfF
+    static const CharacterSet HEXDIG;
+    // <space><tab>
+    static const CharacterSet WSP;
+
+
 private:
     /** index of characters in this set
      *
index 95c9f0d6f1e61b85839c69dade7c387de73b5341..bff2659301b6be557a7d419ad19606da820dcd9c 100644 (file)
@@ -26,3 +26,24 @@ libbase_la_SOURCES = \
        TextException.h \
        Vector.cc \
        Vector.h
+
+EXTRA_PROGRAMS = \
+       testCharacterSet
+
+check_PROGRAMS += testCharacterSet
+TESTS += testCharacterSet
+
+testCharacterSet_SOURCES = \
+       CharacterSet.h \
+       testCharacterSet.h \
+       testCharacterSet.cc
+nodist_testCharacterSet_SOURCES = \
+       $(top_srcdir)/src/tests/testMain.cc
+testCharacterSet_LDFLAGS = $(LIBADD_DL)
+testCharacterSet_LDADD=\
+       $(SQUID_CPPUNIT_LIBS) \
+       $(SQUID_CPPUNIT_LA) \
+       libbase.la \
+       $(COMPAT_LIB) \
+       $(XTRA_LIBS)
+testCharacterSet_DEPENDENCIES= $(SQUID_CPPUNIT_LA)
diff --git a/src/base/testCharacterSet.cc b/src/base/testCharacterSet.cc
new file mode 100644 (file)
index 0000000..6c4cf53
--- /dev/null
@@ -0,0 +1,54 @@
+#define SQUID_UNIT_TEST 1
+
+#include "squid.h"
+#include "base/CharacterSet.h"
+#include "testCharacterSet.h"
+
+#include <string>
+
+CPPUNIT_TEST_SUITE_REGISTRATION( testCharacterSet );
+
+void
+testCharacterSet::CharacterSetConstruction()
+{
+    {
+        CharacterSet t(NULL,"");
+        CPPUNIT_ASSERT_EQUAL(std::string("anonymous"),std::string(t.name));
+    }
+    {
+        CharacterSet t("test","");
+        CPPUNIT_ASSERT_EQUAL(std::string("test"),std::string(t.name));
+    }
+    {
+        CharacterSet t("test","");
+        for (int j = 0; j < 255; ++j)
+            CPPUNIT_ASSERT_EQUAL(false,t[j]);
+    }
+    {
+        CharacterSet t("test","0");
+        CPPUNIT_ASSERT_EQUAL(true,t['0']);
+        for (int j = 0; j < 255; ++j)
+            if (j != '0')
+                CPPUNIT_ASSERT_EQUAL(false,t[j]);
+    }
+}
+
+void
+testCharacterSet::CharacterSetAdd()
+{
+    CharacterSet t("test","0");
+    t.add(0);
+    CPPUNIT_ASSERT_EQUAL(true,t['\0']);
+    CPPUNIT_ASSERT_EQUAL(true,t['0']);
+}
+
+void
+testCharacterSet::CharacterSetAddRange()
+{
+    CharacterSet t("test","");
+    t.addRange('0','9');
+    CPPUNIT_ASSERT_EQUAL(true,t['0']);
+    CPPUNIT_ASSERT_EQUAL(true,t['5']);
+    CPPUNIT_ASSERT_EQUAL(true,t['9']);
+    CPPUNIT_ASSERT_EQUAL(false,t['a']);
+}
diff --git a/src/base/testCharacterSet.h b/src/base/testCharacterSet.h
new file mode 100644 (file)
index 0000000..f2c2c3b
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef SQUID_BASE_TESTCHARACTERSET_H
+#define SQUID_BASE_TESTCHARACTERSET_H
+
+#define SQUID_UNIT_TEST 1
+
+#include <cppunit/extensions/HelperMacros.h>
+
+class testCharacterSet : public CPPUNIT_NS::TestFixture
+{
+    CPPUNIT_TEST_SUITE( testCharacterSet );
+    CPPUNIT_TEST( CharacterSetConstruction );
+    CPPUNIT_TEST( CharacterSetAdd );
+    CPPUNIT_TEST( CharacterSetAddRange );
+    CPPUNIT_TEST_SUITE_END();
+
+protected:
+    void CharacterSetConstruction();
+    void CharacterSetAdd();
+    void CharacterSetAddRange();
+};
+
+#endif /* SQUID_BASE_TESTCHARACTERSET_H */