Skip to content

Commit b63d1ff

Browse files
committed
[uri] Added a test to check that the URI can be used as a key in a hash set.
1 parent 652c9d3 commit b63d1ff

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

boost/network/uri/uri.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# include <boost/range/iterator_range.hpp>
1919
# include <boost/lexical_cast.hpp>
2020
# include <boost/optional.hpp>
21+
# include <boost/functional/hash_fwd.hpp>
2122

2223

2324
namespace boost {
@@ -300,6 +301,16 @@ void swap(uri &lhs, uri &rhs) {
300301
lhs.swap(rhs);
301302
}
302303

304+
inline
305+
std::size_t hash_value(const uri &uri_)
306+
{
307+
std::size_t seed = 0;
308+
for (uri::const_iterator it = begin(uri_); it != end(uri_); ++it) {
309+
hash_combine(seed, *it);
310+
}
311+
return seed;
312+
}
313+
303314
inline
304315
bool operator == (const uri &lhs, const uri &rhs) {
305316
return boost::equal(lhs, rhs);
@@ -318,7 +329,6 @@ bool operator < (const uri &lhs, const uri &rhs) {
318329
} // namespace network
319330
} // namespace boost
320331

321-
322332
# include <boost/network/uri/accessors.hpp>
323333
# include <boost/network/uri/directives.hpp>
324334
# include <boost/network/uri/builder.hpp>

libs/network/test/uri/uri_test.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <boost/range/algorithm/equal.hpp>
1313
#include <boost/scoped_ptr.hpp>
1414
#include <map>
15+
#include <set>
16+
#include <boost/unordered_set.hpp>
1517

1618

1719
using namespace boost::network;
@@ -316,14 +318,13 @@ BOOST_AUTO_TEST_CASE(encoded_uri_test) {
316318
BOOST_AUTO_TEST_CASE(copy_constructor_test) {
317319
uri::uri instance("http://www.example.com/");
318320
uri::uri copy = instance;
319-
BOOST_CHECK(instance == copy);
321+
BOOST_CHECK_EQUAL(instance, copy);
320322
}
321323

322324
BOOST_AUTO_TEST_CASE(assignment_test) {
323325
uri::uri instance("http://www.example.com/");
324326
uri::uri copy;
325327
copy = instance;
326-
BOOST_CHECK_EQUAL(instance.string(), copy.string());
327328
BOOST_CHECK_EQUAL(instance, copy);
328329
}
329330

@@ -463,3 +464,17 @@ BOOST_AUTO_TEST_CASE(issue_104_test) {
463464
instance.reset();
464465
BOOST_CHECK_EQUAL(uri::scheme(copy), "http");
465466
}
467+
468+
BOOST_AUTO_TEST_CASE(uri_set_test) {
469+
std::set<uri::uri> uri_set;
470+
uri_set.insert(uri::uri("http://www.example.com/"));
471+
BOOST_REQUIRE(!uri_set.empty());
472+
BOOST_CHECK_EQUAL((*uri_set.begin()), uri::uri("http://www.example.com/"));
473+
}
474+
475+
BOOST_AUTO_TEST_CASE(uri_unordered_set_test) {
476+
boost::unordered_set<uri::uri> uri_set;
477+
uri_set.insert(uri::uri("http://www.example.com/"));
478+
BOOST_REQUIRE(!uri_set.empty());
479+
BOOST_CHECK_EQUAL((*uri_set.begin()), uri::uri("http://www.example.com/"));
480+
}

0 commit comments

Comments
 (0)