From 6106128dc9969c8153c6ffcfb66904d23fb33f3d Mon Sep 17 00:00:00 2001 From: bert hubert Date: Thu, 11 Feb 2016 14:08:57 +0100 Subject: [PATCH] make DNSName friends with unordered maps and sets, plus test this --- pdns/dnsname.hh | 6 ++++++ pdns/test-dnsname_cc.cc | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pdns/dnsname.hh b/pdns/dnsname.hh index 5dbd6fc3e..6e7e744ac 100644 --- a/pdns/dnsname.hh +++ b/pdns/dnsname.hh @@ -251,3 +251,9 @@ struct SuffixMatchNode }; std::ostream & operator<<(std::ostream &os, const DNSName& d); +namespace std { + template <> + struct hash { + size_t operator () (const DNSName& dn) const { return dn.hash(0); } + }; +} diff --git a/pdns/test-dnsname_cc.cc b/pdns/test-dnsname_cc.cc index 295898a10..c2c683b06 100644 --- a/pdns/test-dnsname_cc.cc +++ b/pdns/test-dnsname_cc.cc @@ -8,6 +8,7 @@ #include "misc.hh" #include "dnswriter.hh" #include "dnsrecords.hh" +#include using namespace boost; using std::string; @@ -280,6 +281,25 @@ BOOST_AUTO_TEST_CASE(test_hash) { BOOST_CHECK(stdev < 10); } +BOOST_AUTO_TEST_CASE(test_hashContainer) { + std::unordered_set s; + s.insert(DNSName("www.powerdns.com")); + BOOST_CHECK(s.count(DNSName("WwW.PoWerDNS.CoM"))); + BOOST_CHECK_EQUAL(s.size(), 1); + s.insert(DNSName("www.POWERDNS.com")); + BOOST_CHECK_EQUAL(s.size(), 1); + s.insert(DNSName("www2.POWERDNS.com")); + BOOST_CHECK_EQUAL(s.size(), 2); + + s.clear(); + unsigned int n=0; + for(; n < 100000; ++n) + s.insert(DNSName(std::to_string(n)+".test.nl")); + BOOST_CHECK_EQUAL(s.size(), n); + +} + + BOOST_AUTO_TEST_CASE(test_QuestionHash) { vector packet; reportBasicTypes(); @@ -618,4 +638,5 @@ BOOST_AUTO_TEST_CASE(test_wirelength) { // Testing if we get the correct value f BOOST_CHECK_EQUAL(sname.wirelength(), 19); } + BOOST_AUTO_TEST_SUITE_END() -- 2.40.0