1 2 // Copyright 2007-2009 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 // clang-format off 7 #include "../helpers/prefix.hpp" 8 #include <boost/unordered_map.hpp> 9 #include "../helpers/postfix.hpp" 10 // clang-format on 11 12 #include "../helpers/test.hpp" 13 #include <string> 14 15 namespace at_tests { 16 UNORDERED_AUTO_TEST(at_tests)17 UNORDERED_AUTO_TEST (at_tests) { 18 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Create Map" << std::endl; 19 20 boost::unordered_map<std::string, int> x; 21 boost::unordered_map<std::string, int> const& x_const(x); 22 23 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Check empty container" << std::endl; 24 25 try { 26 x.at("one"); 27 BOOST_ERROR("Should have thrown."); 28 } catch (std::out_of_range&) { 29 } 30 31 try { 32 x_const.at("one"); 33 BOOST_ERROR("Should have thrown."); 34 } catch (std::out_of_range&) { 35 } 36 37 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Add elements" << std::endl; 38 39 x["one"] = 1; 40 x["two"] = 2; 41 42 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Check existing elements" << std::endl; 43 44 BOOST_TEST(x.at("one") == 1); 45 BOOST_TEST(x.at("two") == 2); 46 BOOST_TEST(x_const.at("one") == 1); 47 BOOST_TEST(x_const.at("two") == 2); 48 49 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Check missing element" << std::endl; 50 51 try { 52 x.at("three"); 53 BOOST_ERROR("Should have thrown."); 54 } catch (std::out_of_range&) { 55 } 56 57 try { 58 x_const.at("three"); 59 BOOST_ERROR("Should have thrown."); 60 } catch (std::out_of_range&) { 61 } 62 63 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Finished" << std::endl; 64 } 65 } 66 67 RUN_TESTS() 68