1
2// Copyright 2007-2009 Daniel James.
3// Copyright 2022-2023 Christian Mazakas.
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7#include "../helpers/unordered.hpp"
8
9#include "../helpers/test.hpp"
10#include <string>
11
12namespace at_tests {
13
14 template <class X> static void at_tests(X*)
15 {
16 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Create Map" << std::endl;
17
18 X x;
19 X const& x_const(x);
20
21 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Check empty container" << std::endl;
22
23 try {
24 x.at("one");
25 BOOST_ERROR("Should have thrown.");
26 } catch (std::out_of_range&) {
27 }
28
29 try {
30 x_const.at("one");
31 BOOST_ERROR("Should have thrown.");
32 } catch (std::out_of_range&) {
33 }
34
35 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Add elements" << std::endl;
36
37 x["one"] = 1;
38 x["two"] = 2;
39
40 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Check existing elements" << std::endl;
41
42 BOOST_TEST(x.at("one") == 1);
43 BOOST_TEST(x.at("two") == 2);
44 BOOST_TEST(x_const.at("one") == 1);
45 BOOST_TEST(x_const.at("two") == 2);
46
47 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Check missing element" << std::endl;
48
49 try {
50 x.at("three");
51 BOOST_ERROR("Should have thrown.");
52 } catch (std::out_of_range&) {
53 }
54
55 try {
56 x_const.at("three");
57 BOOST_ERROR("Should have thrown.");
58 } catch (std::out_of_range&) {
59 }
60
61 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Finished" << std::endl;
62 }
63
64#ifdef BOOST_UNORDERED_FOA_TESTS
65 static boost::unordered_flat_map<std::string, int>* test_map;
66 static boost::unordered_node_map<std::string, int>* test_node_map;
67
68 // clang-format off
69 UNORDERED_TEST(at_tests, ((test_map)(test_node_map)))
70 // clang-format on
71#else
72 static boost::unordered_map<std::string, int>* test_map;
73
74 // clang-format off
75 UNORDERED_TEST(at_tests, ((test_map)))
76 // clang-format on
77#endif
78} // namespace at_tests
79
80RUN_TESTS()
81

source code of boost/libs/unordered/test/unordered/at_tests.cpp