| 1 |
// 2006-11-25 Paolo Carlini <[email protected]> |
| 2 |
|
| 3 |
// Copyright (C) 2006-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 |
|
| 21 |
#include <map> |
| 22 |
#include <testsuite_hooks.h> |
| 23 |
|
| 24 |
// A few tests for equal_range, in the occasion of libstdc++/29385. |
| 25 |
void test01() |
| 26 |
{ |
| 27 |
using namespace std; |
| 28 |
|
| 29 |
map<int, int> m0; |
| 30 |
typedef map<int, int>::iterator iterator; |
| 31 |
typedef map<int, int>::const_iterator const_iterator; |
| 32 |
typedef pair<iterator, bool> insert_return_type; |
| 33 |
pair<iterator, iterator> pp0; |
| 34 |
typedef map<int, int>::value_type value_type; |
| 35 |
|
| 36 |
pp0 = m0.equal_range(1); |
| 37 |
VERIFY( m0.count(1) == 0 ); |
| 38 |
VERIFY( pp0.first == m0.end() ); |
| 39 |
VERIFY( pp0.second == m0.end() ); |
| 40 |
|
| 41 |
insert_return_type irt0 = m0.insert(value_type(1, 1)); |
| 42 |
insert_return_type irt1 = m0.insert(value_type(2, 2)); |
| 43 |
insert_return_type irt2 = m0.insert(value_type(3, 3)); |
| 44 |
|
| 45 |
pp0 = m0.equal_range(2); |
| 46 |
VERIFY( m0.count(2) == 1 ); |
| 47 |
VERIFY( *pp0.first == value_type(2, 2) ); |
| 48 |
VERIFY( *pp0.second == value_type(3, 3) ); |
| 49 |
VERIFY( pp0.first == irt1.first ); |
| 50 |
VERIFY( --pp0.first == irt0.first ); |
| 51 |
VERIFY( pp0.second == irt2.first ); |
| 52 |
|
| 53 |
m0.insert(value_type(3, 4)); |
| 54 |
insert_return_type irt3 = m0.insert(value_type(3, 5)); |
| 55 |
insert_return_type irt4 = m0.insert(value_type(4, 6)); |
| 56 |
|
| 57 |
pp0 = m0.equal_range(3); |
| 58 |
VERIFY( m0.count(3) == 1 ); |
| 59 |
VERIFY( *pp0.first == value_type(3, 3) ); |
| 60 |
VERIFY( *pp0.second == value_type(4, 6) ); |
| 61 |
VERIFY( pp0.first == irt2.first ); |
| 62 |
VERIFY( --pp0.first == irt1.first ); |
| 63 |
VERIFY( pp0.second == irt4.first ); |
| 64 |
|
| 65 |
insert_return_type irt5 = m0.insert(value_type(0, 7)); |
| 66 |
m0.insert(value_type(1, 8)); |
| 67 |
m0.insert(value_type(1, 9)); |
| 68 |
m0.insert(value_type(1, 10)); |
| 69 |
|
| 70 |
pp0 = m0.equal_range(1); |
| 71 |
VERIFY( m0.count(1) == 1 ); |
| 72 |
VERIFY( *pp0.first == value_type(1, 1) ); |
| 73 |
VERIFY( *pp0.second == value_type(2, 2) ); |
| 74 |
VERIFY( pp0.first == irt0.first ); |
| 75 |
VERIFY( --pp0.first == irt5.first ); |
| 76 |
VERIFY( pp0.second == irt1.first ); |
| 77 |
|
| 78 |
insert_return_type irt6 = m0.insert(value_type(5, 11)); |
| 79 |
m0.insert(value_type(5, 12)); |
| 80 |
m0.insert(value_type(5, 13)); |
| 81 |
|
| 82 |
pp0 = m0.equal_range(5); |
| 83 |
VERIFY( m0.count(5) == 1 ); |
| 84 |
VERIFY( *pp0.first == value_type(5, 11) ); |
| 85 |
VERIFY( pp0.first == irt6.first ); |
| 86 |
VERIFY( --pp0.first == irt4.first ); |
| 87 |
VERIFY( pp0.second == m0.end() ); |
| 88 |
|
| 89 |
m0.insert(value_type(4, 14)); |
| 90 |
m0.insert(value_type(4, 15)); |
| 91 |
m0.insert(value_type(4, 16)); |
| 92 |
|
| 93 |
pp0 = m0.equal_range(4); |
| 94 |
VERIFY( m0.count(4) == 1 ); |
| 95 |
VERIFY( *pp0.first == value_type(4, 6) ); |
| 96 |
VERIFY( *pp0.second == value_type(5, 11) ); |
| 97 |
VERIFY( pp0.first == irt4.first ); |
| 98 |
VERIFY( --pp0.first == irt3.first ); |
| 99 |
VERIFY( pp0.second == irt6.first ); |
| 100 |
|
| 101 |
m0.insert(value_type(0, 17)); |
| 102 |
insert_return_type irt7 = m0.insert(value_type(0, 18)); |
| 103 |
m0.insert(value_type(1, 19)); |
| 104 |
|
| 105 |
pp0 = m0.equal_range(0); |
| 106 |
VERIFY( m0.count(0) == 1 ); |
| 107 |
VERIFY( *pp0.first == value_type(0, 7) ); |
| 108 |
VERIFY( *pp0.second == value_type(1, 1) ); |
| 109 |
VERIFY( pp0.first == irt5.first ); |
| 110 |
VERIFY( pp0.first == m0.begin() ); |
| 111 |
VERIFY( pp0.second == irt0.first ); |
| 112 |
|
| 113 |
const map<int, int>& m1 = m0; |
| 114 |
pair<const_iterator, const_iterator> pp1 = m1.equal_range(1); |
| 115 |
VERIFY( m1.count(1) == 1 ); |
| 116 |
VERIFY( *pp1.first == value_type(1, 1) ); |
| 117 |
VERIFY( *pp1.second == value_type(2, 2) ); |
| 118 |
VERIFY( pp1.first == irt0.first ); |
| 119 |
VERIFY( --pp1.first == irt7.first); |
| 120 |
VERIFY( pp1.second == irt1.first ); |
| 121 |
} |
| 122 |
|
| 123 |
int |
| 124 |
main() |
| 125 |
{ |
| 126 |
test01(); |
| 127 |
return 0; |
| 128 |
} |