#include "util.h"
#include <cstring>
+#include <functional>
#include <iostream>
#include <sstream>
reAlloc(newsize);
}
+std::size_t std::hash<SBuf>::operator() (const SBuf & sbuf) const noexcept
+{
+ //ripped and adapted from hash_string
+ const char *s = sbuf.rawContent();
+ size_t rv = 0;
+ SBuf::size_type len=sbuf.length();
+ while (len != 0) {
+ rv ^= 271 * *s;
+ ++s;
+ --len;
+ }
+ return rv ^ (sbuf.length() * 271);
+}
return buf;
}
+namespace std {
+ /// default hash functor to support std::unordered_map<SBuf,*>
+ template <>
+ struct hash<SBuf>
+ {
+ size_t operator()(const SBuf &) const noexcept;
+ };
+}
+
inline
SBufIterator::SBufIterator(const SBuf &s, size_type pos)
: iter(s.rawContent()+pos)
}
#endif /* SQUID_SBUF_H */
-
#include <iostream>
#include <stdexcept>
+#include <unordered_map>
CPPUNIT_TEST_SUITE_REGISTRATION( testSBuf );
}
}
+void
+testSBuf::testSBufHash()
+{
+ // same SBuf must have same hash
+ auto hasher=std::hash<SBuf>();
+ CPPUNIT_ASSERT_EQUAL(hasher(literal),hasher(literal));
+
+ // same content must have same hash
+ CPPUNIT_ASSERT_EQUAL(hasher(literal),hasher(SBuf(fox)));
+ CPPUNIT_ASSERT_EQUAL(hasher(SBuf(fox)),hasher(SBuf(fox)));
+
+ //differen content should have different hash
+ CPPUNIT_ASSERT(hasher(SBuf(fox)) != hasher(SBuf(fox1)));
+
+ {
+ std::unordered_map<SBuf, int> um;
+ um[SBuf("one")] = 1;
+ um[SBuf("two")] = 2;
+
+ auto i = um.find(SBuf("one"));
+ CPPUNIT_ASSERT(i != um.end());
+ CPPUNIT_ASSERT(i->second == 1);
+
+ i = um.find(SBuf("eleventy"));
+ CPPUNIT_ASSERT(i == um.end());
+
+ }
+}
CPPUNIT_TEST( testAutoFind );
CPPUNIT_TEST( testStdStringOps );
CPPUNIT_TEST( testIterators );
+ CPPUNIT_TEST( testSBufHash );
// CPPUNIT_TEST( testDumpStats ); //fake test, to print alloc stats
CPPUNIT_TEST_SUITE_END();
protected:
void testAutoFind();
void testStdStringOps();
void testIterators();
+ void testSBufHash();
};
#endif