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/detail/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         : std::binary_function<std::string, std::string, bool>
14     {
operator ()iequal_to15         bool operator()(std::string const& x,
16             std::string const& y) const
17         {
18             return boost::algorithm::iequals(x, y, std::locale());
19         }
20     };
21 
22     struct ihash
23         : std::unary_function<std::string, std::size_t>
24     {
operator ()ihash25         std::size_t operator()(std::string const& x) const
26         {
27             std::size_t seed = 0;
28             std::locale locale;
29 
30             for(std::string::const_iterator it = x.begin();
31                 it != x.end(); ++it)
32             {
33                 boost::hash_combine(seed, std::toupper(*it, locale));
34             }
35 
36             return seed;
37         }
38     };
39 //]
40 
main()41 int main() {
42 //[case_sensitive_dictionary_fnv
43     boost::unordered_map<std::string, int, hash::fnv_1>
44         dictionary;
45 //]
46 
47     BOOST_TEST(dictionary.empty());
48 
49     dictionary["one"] = 1;
50     BOOST_TEST(dictionary.size() == 1);
51     BOOST_TEST(dictionary.find("ONE") == dictionary.end());
52 
53     dictionary.insert(std::make_pair("ONE", 2));
54     BOOST_TEST(dictionary.size() == 2);
55     BOOST_TEST(dictionary.find("ONE") != dictionary.end() &&
56             dictionary.find("ONE")->first == "ONE" &&
57             dictionary.find("ONE")->second == 2);
58 
59     dictionary["One"] = 3;
60     BOOST_TEST(dictionary.size() == 3);
61     BOOST_TEST(dictionary.find("One") != dictionary.end() &&
62             dictionary.find("One")->first == "One" &&
63             dictionary.find("One")->second == 3);
64 
65     dictionary["two"] = 4;
66     BOOST_TEST(dictionary.size() == 4);
67     BOOST_TEST(dictionary.find("Two") == dictionary.end() &&
68             dictionary.find("two") != dictionary.end() &&
69             dictionary.find("two")->second == 4);
70 
71 
72 //[case_insensitive_dictionary
73     boost::unordered_map<std::string, int, ihash, iequal_to>
74         idictionary;
75 //]
76 
77     BOOST_TEST(idictionary.empty());
78 
79     idictionary["one"] = 1;
80     BOOST_TEST(idictionary.size() == 1);
81     BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
82         idictionary.find("ONE") == idictionary.find("one"));
83 
84     idictionary.insert(std::make_pair("ONE", 2));
85     BOOST_TEST(idictionary.size() == 1);
86     BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
87             idictionary.find("ONE")->first == "one" &&
88             idictionary.find("ONE")->second == 1);
89 
90     idictionary["One"] = 3;
91     BOOST_TEST(idictionary.size() == 1);
92     BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
93             idictionary.find("ONE")->first == "one" &&
94             idictionary.find("ONE")->second == 3);
95 
96     idictionary["two"] = 4;
97     BOOST_TEST(idictionary.size() == 2);
98     BOOST_TEST(idictionary.find("two") != idictionary.end() &&
99             idictionary.find("TWO")->first == "two" &&
100             idictionary.find("Two")->second == 4);
101 
102     return boost::report_errors();
103 }
104