]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/23_containers/map/modifiers/emplace/1.cc
Update copyright years in libstdc++-v3/
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / map / modifiers / emplace / 1.cc
CommitLineData
55826ab6
FD
1// { dg-options "-std=c++11" }
2
aa118a03 3// Copyright (C) 2012-2014 Free Software Foundation, Inc.
55826ab6
FD
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
19
20#include <utility>
21#include <tuple>
22#include <vector>
23#include <map>
24#include <testsuite_hooks.h>
25
26class PathPoint
27{
28public:
29 PathPoint(char t, const std::vector<double>& c)
30 : type(t), coords(c) { }
31 PathPoint(char t, std::vector<double>&& c)
32 : type(t), coords(std::move(c)) { }
33 char getType() const { return type; }
34 const std::vector<double>& getCoords() const { return coords; }
35private:
36 char type;
37 std::vector<double> coords;
38};
39
40bool test __attribute__((unused)) = true;
41
42void test01()
43{
44 typedef std::map<char, std::vector<double>> Map;
45 Map m;
46
47 std::vector<double> coord1 = { 0.0, 1.0, 2.0 };
48
49 auto ret = m.emplace('a', coord1);
50 VERIFY( ret.second );
51 VERIFY( m.size() == 1 );
52 VERIFY( ret.first->first == 'a' );
53
54 coord1[0] = 3.0;
55 ret = m.emplace('a', coord1);
56 VERIFY( !ret.second );
57 VERIFY( m.size() == 1 );
58 VERIFY( ret.first->first == 'a' );
59 VERIFY( ret.first->second[0] == 0.0 );
60
61 auto it = m.emplace_hint(m.begin(), 'b', coord1);
62 VERIFY( it != m.end() );
63 VERIFY( it->first == 'b' );
64 VERIFY( it->second[0] == 3.0 );
65
66 double *px = &coord1[0];
67 ret = m.emplace('c', std::move(coord1));
68 VERIFY( ret.second );
69 VERIFY( ret.first->first == 'c' );
70 VERIFY( &(ret.first->second[0]) == px );
71}
72
73void test02()
74{
75 using namespace std;
76 typedef map<char, PathPoint> Map;
77 Map m;
78
79 vector<double> coord1 = { 0.0, 1.0, 2.0 };
80
81 auto ret = m.emplace(piecewise_construct,
82 make_tuple('a'), make_tuple('a', coord1));
83 VERIFY( ret.second );
84 VERIFY( m.size() == 1 );
85 VERIFY( ret.first->first == 'a' );
86
87 coord1[0] = 3.0;
88 ret = m.emplace(piecewise_construct,
89 make_tuple('a'), make_tuple( 'b', coord1));
90 VERIFY( !ret.second );
91 VERIFY( m.size() == 1 );
92 VERIFY( ret.first->first == 'a' );
93 VERIFY( ret.first->second.getCoords()[0] == 0.0 );
94
95 auto it = m.emplace_hint(m.begin(), piecewise_construct,
96 make_tuple('b'), make_tuple('c', coord1));
97 VERIFY( it != m.end() );
98 VERIFY( it->first == 'b' );
99 VERIFY( it->second.getCoords()[0] == 3.0 );
100
101 double *px = &coord1[0];
102 ret = m.emplace(piecewise_construct,
103 make_tuple('c'), make_tuple('d', move(coord1)));
104 VERIFY( ret.second );
105 VERIFY( ret.first->first == 'c' );
106 VERIFY( &(ret.first->second.getCoords()[0]) == px );
107}
108
109int main()
110{
111 test01();
112 test02();
113 return 0;
114}