llvm-project
165 строк · 6.3 Кб
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-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03
10
11// <map>
12
13// class map
14
15// template <class... Args>
16// pair<iterator, bool> emplace(Args&&... args);
17
18#include <map>19#include <cassert>20#include <tuple>21
22#include "test_macros.h"23#include "../../../Emplaceable.h"24#include "DefaultOnly.h"25#include "min_allocator.h"26
27int main(int, char**)28{
29{30typedef std::map<int, DefaultOnly> M;31typedef std::pair<M::iterator, bool> R;32M m;33assert(DefaultOnly::count == 0);34R r = m.emplace();35assert(r.second);36assert(r.first == m.begin());37assert(m.size() == 1);38assert(m.begin()->first == 0);39assert(m.begin()->second == DefaultOnly());40assert(DefaultOnly::count == 1);41r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),42std::forward_as_tuple());43assert(r.second);44assert(r.first == std::next(m.begin()));45assert(m.size() == 2);46assert(std::next(m.begin())->first == 1);47assert(std::next(m.begin())->second == DefaultOnly());48assert(DefaultOnly::count == 2);49r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),50std::forward_as_tuple());51assert(!r.second);52assert(r.first == std::next(m.begin()));53assert(m.size() == 2);54assert(std::next(m.begin())->first == 1);55assert(std::next(m.begin())->second == DefaultOnly());56assert(DefaultOnly::count == 2);57}58assert(DefaultOnly::count == 0);59{60typedef std::map<int, Emplaceable> M;61typedef std::pair<M::iterator, bool> R;62M m;63R r = m.emplace(std::piecewise_construct, std::forward_as_tuple(2),64std::forward_as_tuple());65assert(r.second);66assert(r.first == m.begin());67assert(m.size() == 1);68assert(m.begin()->first == 2);69assert(m.begin()->second == Emplaceable());70r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),71std::forward_as_tuple(2, 3.5));72assert(r.second);73assert(r.first == m.begin());74assert(m.size() == 2);75assert(m.begin()->first == 1);76assert(m.begin()->second == Emplaceable(2, 3.5));77r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),78std::forward_as_tuple(2, 3.5));79assert(!r.second);80assert(r.first == m.begin());81assert(m.size() == 2);82assert(m.begin()->first == 1);83assert(m.begin()->second == Emplaceable(2, 3.5));84}85{86typedef std::map<int, double> M;87typedef std::pair<M::iterator, bool> R;88M m;89R r = m.emplace(M::value_type(2, 3.5));90assert(r.second);91assert(r.first == m.begin());92assert(m.size() == 1);93assert(m.begin()->first == 2);94assert(m.begin()->second == 3.5);95}96{97typedef std::map<int, DefaultOnly, std::less<int>, min_allocator<std::pair<const int, DefaultOnly>>> M;98typedef std::pair<M::iterator, bool> R;99M m;100assert(DefaultOnly::count == 0);101R r = m.emplace();102assert(r.second);103assert(r.first == m.begin());104assert(m.size() == 1);105assert(m.begin()->first == 0);106assert(m.begin()->second == DefaultOnly());107assert(DefaultOnly::count == 1);108r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),109std::forward_as_tuple());110assert(r.second);111assert(r.first == std::next(m.begin()));112assert(m.size() == 2);113assert(std::next(m.begin())->first == 1);114assert(std::next(m.begin())->second == DefaultOnly());115assert(DefaultOnly::count == 2);116r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),117std::forward_as_tuple());118assert(!r.second);119assert(r.first == std::next(m.begin()));120assert(m.size() == 2);121assert(std::next(m.begin())->first == 1);122assert(std::next(m.begin())->second == DefaultOnly());123assert(DefaultOnly::count == 2);124}125assert(DefaultOnly::count == 0);126{127typedef std::map<int, Emplaceable, std::less<int>, min_allocator<std::pair<const int, Emplaceable>>> M;128typedef std::pair<M::iterator, bool> R;129M m;130R r = m.emplace(std::piecewise_construct, std::forward_as_tuple(2),131std::forward_as_tuple());132assert(r.second);133assert(r.first == m.begin());134assert(m.size() == 1);135assert(m.begin()->first == 2);136assert(m.begin()->second == Emplaceable());137r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),138std::forward_as_tuple(2, 3.5));139assert(r.second);140assert(r.first == m.begin());141assert(m.size() == 2);142assert(m.begin()->first == 1);143assert(m.begin()->second == Emplaceable(2, 3.5));144r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1),145std::forward_as_tuple(2, 3.5));146assert(!r.second);147assert(r.first == m.begin());148assert(m.size() == 2);149assert(m.begin()->first == 1);150assert(m.begin()->second == Emplaceable(2, 3.5));151}152{153typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;154typedef std::pair<M::iterator, bool> R;155M m;156R r = m.emplace(M::value_type(2, 3.5));157assert(r.second);158assert(r.first == m.begin());159assert(m.size() == 1);160assert(m.begin()->first == 2);161assert(m.begin()->second == 3.5);162}163
164return 0;165}
166