Home | History | Annotate | Download | only in unord.multimap.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_multimap
     15 
     16 // iterator 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 it = c.insert({key, mapped});
     28     return c.extract(it);
     29 }
     30 
     31 template <class Container>
     32 void test(Container& c)
     33 {
     34     auto* nf = &node_factory<Container>;
     35 
     36     for (int i = 0; i != 10; ++i)
     37     {
     38         typename Container::node_type node = nf(i, i + 1);
     39         assert(!node.empty());
     40         typename Container::iterator it = c.insert(std::move(node));
     41         assert(node.empty());
     42         assert(it == c.find(i) && it != c.end());
     43         assert(it->first == i && it->second == i + 1);
     44     }
     45 
     46     assert(c.size() == 10);
     47 
     48     { // Insert empty node.
     49         typename Container::node_type def;
     50         auto it = c.insert(std::move(def));
     51         assert(def.empty());
     52         assert(it == c.end());
     53     }
     54 
     55     { // Insert duplicate node.
     56         typename Container::node_type dupl = nf(0, 42);
     57         auto it = c.insert(std::move(dupl));
     58         assert(dupl.empty());
     59         assert(it != c.end() && it->second == 42);
     60     }
     61 
     62     assert(c.size() == 11);
     63     assert(c.count(0) == 2);
     64     for (int i = 1; i != 10; ++i)
     65     {
     66         assert(c.count(i) == 1);
     67         assert(c.find(i)->second == i + 1);
     68     }
     69 }
     70 
     71 int main()
     72 {
     73     std::unordered_multimap<int, int> m;
     74     test(m);
     75     std::unordered_multimap<int, int, std::hash<int>, std::equal_to<int>, min_allocator<std::pair<const int, int>>> m2;
     76     test(m2);
     77 }
     78