Home | History | Annotate | Download | only in unord.map.modifiers
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // UNSUPPORTED: c++98, c++03, c++11, c++14
     11 
     12 // <unordered_map>
     13 
     14 // class unordered_map
     15 
     16 // insert_return_type insert(node_type&&);
     17 
     18 #include <unordered_map>
     19 #include "min_allocator.h"
     20 
     21 template <class Container>
     22 typename Container::node_type
     23 node_factory(typename Container::key_type const& key,
     24              typename Container::mapped_type const& mapped)
     25 {
     26     static Container c;
     27     auto p = c.insert({key, mapped});
     28     assert(p.second);
     29     return c.extract(p.first);
     30 }
     31 
     32 template <class Container>
     33 void test(Container& c)
     34 {
     35     auto* nf = &node_factory<Container>;
     36 
     37     for (int i = 0; i != 10; ++i)
     38     {
     39         typename Container::node_type node = nf(i, i + 1);
     40         assert(!node.empty());
     41         typename Container::insert_return_type irt = c.insert(std::move(node));
     42         assert(node.empty());
     43         assert(irt.inserted);
     44         assert(irt.node.empty());
     45         assert(irt.position->first == i && irt.position->second == i + 1);
     46     }
     47 
     48     assert(c.size() == 10);
     49 
     50     { // Insert empty node.
     51         typename Container::node_type def;
     52         auto irt = c.insert(std::move(def));
     53         assert(def.empty());
     54         assert(!irt.inserted);
     55         assert(irt.node.empty());
     56         assert(irt.position == c.end());
     57     }
     58 
     59     { // Insert duplicate node.
     60         typename Container::node_type dupl = nf(0, 42);
     61         auto irt = c.insert(std::move(dupl));
     62         assert(dupl.empty());
     63         assert(!irt.inserted);
     64         assert(!irt.node.empty());
     65         assert(irt.position == c.find(0));
     66         assert(irt.node.key() == 0 && irt.node.mapped() == 42);
     67     }
     68 
     69     assert(c.size() == 10);
     70 
     71     for (int i = 0; i != 10; ++i)
     72     {
     73         assert(c.count(i) == 1);
     74         assert(c[i] == i + 1);
     75     }
     76 }
     77 
     78 int main()
     79 {
     80     std::unordered_map<int, int> m;
     81     test(m);
     82     std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, min_allocator<std::pair<const int, int>>> m2;
     83     test(m2);
     84 }
     85