match.pd: Fix indefinite recursion during exp-log transformations [PR118490]
[gcc.git] / libstdc++-v3 / testsuite / 23_containers / flat_multimap / 1.cc
blob08f4bbc38fd2a54a18d4fffb9a3137419b7c7571
1 // { dg-do run { target c++23 } }
3 #include <flat_map>
4 #include <deque>
5 #include <vector>
6 #include <testsuite_allocator.h>
7 #include <testsuite_hooks.h>
9 template<template<typename> class KeyContainer, template<typename> class MappedContainer>
10 void
11 test01()
13 std::flat_multimap<int, int, std::less<int>, KeyContainer<int>, MappedContainer<int>> m;
14 static_assert( std::ranges::random_access_range<decltype(m)> );
16 m.insert({1,-1});
17 m.insert({2,-2});
18 m.insert({3,-3});
19 m.insert({1,-4});
20 m.insert({2,-5});
21 m.insert({3,-6});
22 m.insert({0, 0});
23 VERIFY( m.size() == 7 );
24 VERIFY( std::ranges::equal(m.keys(), (int[]){0, 1, 1, 2, 2, 3, 3}) );
25 VERIFY( std::ranges::equal(m.values(), (int[]){0, -1, -4, -2, -5, -3, -6}) );
27 m.clear();
28 m.insert(m.begin(), {0, 0});
29 m.insert(m.begin(), {1,-1});
30 m.insert(m.begin(), {2,-2});
31 m.insert(m.begin(), {3,-3});
32 m.insert(m.begin(), {1,-4});
33 m.insert(m.begin(), {2,-5});
34 m.insert(m.begin(), {3,-6});
35 m.insert(m.begin(), {0,-7});
36 VERIFY( std::ranges::equal(m.keys(), (int[]){0, 0, 1, 1, 2, 2, 3, 3}) );
37 VERIFY( std::ranges::equal(m.values(), (int[]){-7, 0, -4, -1, -5, -2, -6, -3}) );
39 m.clear();
40 m = {{10,0},{10,1}};
41 VERIFY( m.size() == 2 );
42 m.insert({{11,2},{12,3},{11,4}});
43 VERIFY( m.size() == 5 );
44 VERIFY( m.end()[-1] == std::pair(12,3) );
47 void
48 test02()
50 std::flat_multimap<int, int, std::greater<int>> m;
51 static_assert( std::ranges::random_access_range<decltype(m)> );
53 auto r = m.insert({1,-1});
54 VERIFY( r->first == 1 && r->second == -1 );
55 r = m.insert({2,-2});
56 VERIFY( r->first == 2 && r->second == -2 );
57 r = m.insert({3,-3});
58 VERIFY( r->first == 3 && r->second == -3 );
59 r = m.insert({1,-4});
60 VERIFY( r->first == 1 && r->second == -4 );
61 r = m.insert({2,-5});
62 VERIFY( r->first == 2 && r->second == -5 );
63 r = m.insert({3,-6});
64 VERIFY( r->first == 3 && r->second == -6 );
65 VERIFY( m.size() == 6 );
66 VERIFY( std::ranges::equal(m.keys(), (int[]){3, 3, 2, 2, 1, 1}) );
67 VERIFY( std::ranges::equal(m.values(), (int[]){-3, -6, -2, -5, -1, -4}) );
69 VERIFY( m.contains(3) && !m.contains(7) );
70 VERIFY( m.count(3) == 2 );
73 void
74 test03()
76 std::flat_multimap<int, int> m;
77 m = {std::pair(1, 2), {3, 4}, {5, 6}};
78 m.insert({std::pair(7, 8), {9, 10}});
80 auto it = m.find(0);
81 VERIFY( it == m.end() );
82 it = m.find(9);
83 VERIFY( it->second == 10 );
85 const auto n = m;
86 VERIFY( m == m );
87 VERIFY( m == n );
89 m.erase(m.begin());
90 m.erase(5);
91 m.erase(m.end()-2, m.end());
92 VERIFY( std::ranges::equal(m, (std::pair<int, int>[]){{3, 4}}) );
93 VERIFY( m != n );
94 VERIFY( n < m );
96 m = n;
97 erase_if(m, [](const auto& x) { auto [k, v] = x; return k < 5 || k > 5; });
98 VERIFY( std::ranges::equal(m, (std::pair<int, int>[]){{5, 6}}) );
101 void
102 test04()
104 using vector = std::vector<int, __gnu_test::uneq_allocator<int>>;
105 vector v1 = {1, 2, 3};
106 vector v2 = {4, 5, 6};
107 __gnu_test::uneq_allocator<int> alloc(42);
109 using flat_multimap = std::flat_multimap<int, int, std::less<int>, vector, vector>;
110 flat_multimap m1(alloc);
111 VERIFY( m1.keys().get_allocator().get_personality() == 42 );
112 VERIFY( m1.values().get_allocator().get_personality() == 42 );
114 flat_multimap m2(v1, v2, alloc);
115 VERIFY( m2.keys().get_allocator().get_personality() == 42 );
116 VERIFY( m2.values().get_allocator().get_personality() == 42 );
118 flat_multimap m3(std::sorted_equivalent_t{}, v1, v2, alloc);
119 VERIFY( m2.keys().get_allocator().get_personality() == 42 );
120 VERIFY( m2.values().get_allocator().get_personality() == 42 );
122 alloc = __gnu_test::uneq_allocator<int>(43);
123 flat_multimap m4(m2, alloc);
124 VERIFY( m4.keys().get_allocator().get_personality() == 43 );
125 VERIFY( m4.values().get_allocator().get_personality() == 43 );
127 alloc = __gnu_test::uneq_allocator<int>(44);
128 flat_multimap m5(std::move(m4), alloc);
129 VERIFY( m5.keys().get_allocator().get_personality() == 44 );
130 VERIFY( m5.values().get_allocator().get_personality() == 44 );
133 void
134 test05()
136 std::vector<std::pair<int, int>> v = {{2, -2}, {3,-3}, {1,-1}, {5,-5}, {4,-4}, {3,3}};
137 std::flat_multimap<int, int> m = {std::from_range, v};
138 VERIFY( std::ranges::equal(m | std::views::keys, (int[]){1, 2, 3, 3, 4, 5}) );
139 VERIFY( std::ranges::equal(m | std::views::values, (int[]){-1, -2, -3, 3, -4, -5}) );
143 main()
145 test01<std::vector, std::vector>();
146 test01<std::deque, std::deque>();
147 test01<std::vector, std::deque>();
148 test01<std::deque, std::vector>();
149 test02();
150 test03();
151 test04();
152 test05();