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