107 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++0310 11// <map>12 13// class map14 15// pair<iterator, bool> insert( value_type&& v); // C++17 and later16// template <class P>17// pair<iterator, bool> insert(P&& p);18 19#include <map>20#include <cassert>21 22#include "MoveOnly.h"23#include "min_allocator.h"24#include "test_macros.h"25 26template <class Container, class Pair>27void do_insert_rv_test() {28 typedef Container M;29 typedef Pair P;30 typedef std::pair<typename M::iterator, bool> R;31 M m;32 R r = m.insert(P(2, 2));33 assert(r.second);34 assert(r.first == m.begin());35 assert(m.size() == 1);36 assert(r.first->first == 2);37 assert(r.first->second == 2);38 39 r = m.insert(P(1, 1));40 assert(r.second);41 assert(r.first == m.begin());42 assert(m.size() == 2);43 assert(r.first->first == 1);44 assert(r.first->second == 1);45 46 r = m.insert(P(3, 3));47 assert(r.second);48 assert(r.first == std::prev(m.end()));49 assert(m.size() == 3);50 assert(r.first->first == 3);51 assert(r.first->second == 3);52 53 r = m.insert(P(3, 3));54 assert(!r.second);55 assert(r.first == std::prev(m.end()));56 assert(m.size() == 3);57 assert(r.first->first == 3);58 assert(r.first->second == 3);59}60 61int main(int, char**) {62 do_insert_rv_test<std::map<int, MoveOnly>, std::pair<int, MoveOnly>>();63 do_insert_rv_test<std::map<int, MoveOnly>, std::pair<const int, MoveOnly>>();64 65 {66 typedef std::map<int, MoveOnly, std::less<int>, min_allocator<std::pair<const int, MoveOnly>>> M;67 typedef std::pair<int, MoveOnly> P;68 typedef std::pair<const int, MoveOnly> CP;69 do_insert_rv_test<M, P>();70 do_insert_rv_test<M, CP>();71 }72 {73 typedef std::map<int, MoveOnly> M;74 typedef std::pair<M::iterator, bool> R;75 M m;76 R r = m.insert({2, MoveOnly(2)});77 assert(r.second);78 assert(r.first == m.begin());79 assert(m.size() == 1);80 assert(r.first->first == 2);81 assert(r.first->second == 2);82 83 r = m.insert({1, MoveOnly(1)});84 assert(r.second);85 assert(r.first == m.begin());86 assert(m.size() == 2);87 assert(r.first->first == 1);88 assert(r.first->second == 1);89 90 r = m.insert({3, MoveOnly(3)});91 assert(r.second);92 assert(r.first == std::prev(m.end()));93 assert(m.size() == 3);94 assert(r.first->first == 3);95 assert(r.first->second == 3);96 97 r = m.insert({3, MoveOnly(3)});98 assert(!r.second);99 assert(r.first == std::prev(m.end()));100 assert(m.size() == 3);101 assert(r.first->first == 3);102 assert(r.first->second == 3);103 }104 105 return 0;106}107