1 // { dg-do run { target c++11 } }
2 
3 // Copyright (C) 2011-2021 Free Software Foundation, Inc.
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 // range insert
21 
22 #include <tuple>
23 #include <vector>
24 #include <unordered_map>
25 #include <testsuite_hooks.h>
26 
27 class PathPoint
28 {
29 public:
PathPoint(char t,const std::vector<double> & c)30   PathPoint(char t, const std::vector<double>& c)
31   : type(t), coords(c) { }
PathPoint(char t,std::vector<double> && c)32   PathPoint(char t, std::vector<double>&& c)
33   : type(t), coords(std::move(c)) { }
getType() const34   char getType() const { return type; }
getCoords() const35   const std::vector<double>& getCoords() const { return coords; }
36 private:
37   char type;
38   std::vector<double> coords;
39 };
40 
test01()41 void test01()
42 {
43   typedef std::unordered_multimap<char, std::vector<double>> MMap;
44   MMap mm;
45 
46   std::vector<double> coord1 = { 0.0, 1.0, 2.0 };
47 
48   auto it = mm.emplace('a', coord1);
49   VERIFY( mm.size() == 1 );
50   VERIFY( it->first == 'a' );
51 
52   coord1[0] = 3.0;
53   it = mm.emplace('a', coord1);
54   VERIFY( mm.size() == 2 );
55   VERIFY( it->first == 'a' );
56   VERIFY( it->second[0] == 3.0 );
57 
58   it = mm.emplace_hint(mm.begin(), 'b', coord1);
59   VERIFY( it != mm.end() );
60   VERIFY( it->first == 'b' );
61   VERIFY( it->second[0] == 3.0 );
62 
63   double *px = &coord1[0];
64   it = mm.emplace('c', std::move(coord1));
65   VERIFY( it->first == 'c' );
66   VERIFY( &(it->second[0]) == px );
67 }
68 
test02()69 void test02()
70 {
71   using namespace std;
72   typedef unordered_multimap<char, PathPoint> Map;
73   Map m;
74 
75   std::vector<double> coord1 = { 0.0, 1.0, 2.0 };
76 
77   auto it = m.emplace(piecewise_construct,
78 		       make_tuple('a'), make_tuple('a', coord1));
79   VERIFY( m.size() == 1 );
80   VERIFY( it->first == 'a' );
81 
82   coord1[0] = 3.0;
83   it = m.emplace(piecewise_construct,
84 		  make_tuple('a'), make_tuple( 'b', coord1));
85   VERIFY( m.size() == 2 );
86   VERIFY( it->first == 'a' );
87   VERIFY( it->second.getCoords()[0] == 3.0 );
88 
89   it = m.emplace_hint(m.begin(), piecewise_construct,
90 		      make_tuple('b'), make_tuple('c', coord1));
91   VERIFY( it != m.end() );
92   VERIFY( it->first == 'b' );
93   VERIFY( it->second.getCoords()[0] == 3.0 );
94 
95   double *px = &coord1[0];
96   it = m.emplace(piecewise_construct,
97 		  make_tuple('c'), make_tuple('d', move(coord1)));
98   VERIFY( it->first == 'c' );
99   VERIFY( &(it->second.getCoords()[0]) == px );
100 }
101 
102 
main()103 int main()
104 {
105   test01();
106   test02();
107   return 0;
108 }
109