1
2 // Copyright 2006-2007 Daniel James.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <boost/unordered_map.hpp>
7 #include <boost/core/lightweight_test.hpp>
8 #include <boost/algorithm/string/predicate.hpp>
9 #include "../../examples/fnv1.hpp"
10
11 //[case_insensitive_functions
12 struct iequal_to
13 {
operator ()iequal_to14 bool operator()(std::string const& x,
15 std::string const& y) const
16 {
17 return boost::algorithm::iequals(x, y, std::locale());
18 }
19 };
20
21 struct ihash
22 {
operator ()ihash23 std::size_t operator()(std::string const& x) const
24 {
25 std::size_t seed = 0;
26 std::locale locale;
27
28 for(std::string::const_iterator it = x.begin();
29 it != x.end(); ++it)
30 {
31 boost::hash_combine(seed, std::toupper(*it, locale));
32 }
33
34 return seed;
35 }
36 };
37 //]
38
main()39 int main() {
40 //[case_sensitive_dictionary_fnv
41 boost::unordered_map<std::string, int, hash::fnv_1>
42 dictionary;
43 //]
44
45 BOOST_TEST(dictionary.empty());
46
47 dictionary["one"] = 1;
48 BOOST_TEST(dictionary.size() == 1);
49 BOOST_TEST(dictionary.find("ONE") == dictionary.end());
50
51 dictionary.insert(std::make_pair("ONE", 2));
52 BOOST_TEST(dictionary.size() == 2);
53 BOOST_TEST(dictionary.find("ONE") != dictionary.end() &&
54 dictionary.find("ONE")->first == "ONE" &&
55 dictionary.find("ONE")->second == 2);
56
57 dictionary["One"] = 3;
58 BOOST_TEST(dictionary.size() == 3);
59 BOOST_TEST(dictionary.find("One") != dictionary.end() &&
60 dictionary.find("One")->first == "One" &&
61 dictionary.find("One")->second == 3);
62
63 dictionary["two"] = 4;
64 BOOST_TEST(dictionary.size() == 4);
65 BOOST_TEST(dictionary.find("Two") == dictionary.end() &&
66 dictionary.find("two") != dictionary.end() &&
67 dictionary.find("two")->second == 4);
68
69
70 //[case_insensitive_dictionary
71 boost::unordered_map<std::string, int, ihash, iequal_to>
72 idictionary;
73 //]
74
75 BOOST_TEST(idictionary.empty());
76
77 idictionary["one"] = 1;
78 BOOST_TEST(idictionary.size() == 1);
79 BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
80 idictionary.find("ONE") == idictionary.find("one"));
81
82 idictionary.insert(std::make_pair("ONE", 2));
83 BOOST_TEST(idictionary.size() == 1);
84 BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
85 idictionary.find("ONE")->first == "one" &&
86 idictionary.find("ONE")->second == 1);
87
88 idictionary["One"] = 3;
89 BOOST_TEST(idictionary.size() == 1);
90 BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
91 idictionary.find("ONE")->first == "one" &&
92 idictionary.find("ONE")->second == 3);
93
94 idictionary["two"] = 4;
95 BOOST_TEST(idictionary.size() == 2);
96 BOOST_TEST(idictionary.find("two") != idictionary.end() &&
97 idictionary.find("TWO")->first == "two" &&
98 idictionary.find("Two")->second == 4);
99
100 return boost::report_errors();
101 }
102