207 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// <set>10 11// class multiset12 13// iterator find(const key_type& k);14// const_iterator find(const key_type& k) const;15 16#include <set>17#include <cassert>18 19#include "test_macros.h"20#include "min_allocator.h"21#include "private_constructor.h"22 23int main(int, char**) {24 {25 typedef int V;26 typedef std::multiset<int> M;27 {28 typedef M::iterator R;29 V ar[] = {5, 6, 7, 8, 9, 10, 11, 12};30 M m(ar, ar + sizeof(ar) / sizeof(ar[0]));31 R r = m.find(5);32 assert(r == m.begin());33 r = m.find(6);34 assert(r == std::next(m.begin()));35 r = m.find(7);36 assert(r == std::next(m.begin(), 2));37 r = m.find(8);38 assert(r == std::next(m.begin(), 3));39 r = m.find(9);40 assert(r == std::next(m.begin(), 4));41 r = m.find(10);42 assert(r == std::next(m.begin(), 5));43 r = m.find(11);44 assert(r == std::next(m.begin(), 6));45 r = m.find(12);46 assert(r == std::next(m.begin(), 7));47 r = m.find(4);48 assert(r == std::next(m.begin(), 8));49 }50 {51 typedef M::const_iterator R;52 V ar[] = {5, 6, 7, 8, 9, 10, 11, 12};53 const M m(ar, ar + sizeof(ar) / sizeof(ar[0]));54 R r = m.find(5);55 assert(r == m.begin());56 r = m.find(6);57 assert(r == std::next(m.begin()));58 r = m.find(7);59 assert(r == std::next(m.begin(), 2));60 r = m.find(8);61 assert(r == std::next(m.begin(), 3));62 r = m.find(9);63 assert(r == std::next(m.begin(), 4));64 r = m.find(10);65 assert(r == std::next(m.begin(), 5));66 r = m.find(11);67 assert(r == std::next(m.begin(), 6));68 r = m.find(12);69 assert(r == std::next(m.begin(), 7));70 r = m.find(4);71 assert(r == std::next(m.begin(), 8));72 }73 }74 { // Check with std::greater to ensure we're actually using the correct comparator75 using Set = std::multiset<int, std::greater<int> >;76 int ar[] = {5, 6, 7, 8, 9, 10, 11, 12};77 Set m(ar, ar + sizeof(ar) / sizeof(ar[0]));78 assert(m.find(12) == std::next(m.begin(), 0));79 assert(m.find(11) == std::next(m.begin(), 1));80 assert(m.find(10) == std::next(m.begin(), 2));81 assert(m.find(9) == std::next(m.begin(), 3));82 assert(m.find(8) == std::next(m.begin(), 4));83 assert(m.find(7) == std::next(m.begin(), 5));84 assert(m.find(6) == std::next(m.begin(), 6));85 assert(m.find(5) == std::next(m.begin(), 7));86 assert(m.find(4) == std::next(m.begin(), 8));87 assert(std::next(m.begin(), 8) == m.end());88 }89#if TEST_STD_VER >= 1190 {91 typedef int V;92 typedef std::multiset<int, std::less<int>, min_allocator<int>> M;93 {94 typedef M::iterator R;95 V ar[] = {5, 6, 7, 8, 9, 10, 11, 12};96 M m(ar, ar + sizeof(ar) / sizeof(ar[0]));97 R r = m.find(5);98 assert(r == m.begin());99 r = m.find(6);100 assert(r == std::next(m.begin()));101 r = m.find(7);102 assert(r == std::next(m.begin(), 2));103 r = m.find(8);104 assert(r == std::next(m.begin(), 3));105 r = m.find(9);106 assert(r == std::next(m.begin(), 4));107 r = m.find(10);108 assert(r == std::next(m.begin(), 5));109 r = m.find(11);110 assert(r == std::next(m.begin(), 6));111 r = m.find(12);112 assert(r == std::next(m.begin(), 7));113 r = m.find(4);114 assert(r == std::next(m.begin(), 8));115 }116 {117 typedef M::const_iterator R;118 V ar[] = {5, 6, 7, 8, 9, 10, 11, 12};119 const M m(ar, ar + sizeof(ar) / sizeof(ar[0]));120 R r = m.find(5);121 assert(r == m.begin());122 r = m.find(6);123 assert(r == std::next(m.begin()));124 r = m.find(7);125 assert(r == std::next(m.begin(), 2));126 r = m.find(8);127 assert(r == std::next(m.begin(), 3));128 r = m.find(9);129 assert(r == std::next(m.begin(), 4));130 r = m.find(10);131 assert(r == std::next(m.begin(), 5));132 r = m.find(11);133 assert(r == std::next(m.begin(), 6));134 r = m.find(12);135 assert(r == std::next(m.begin(), 7));136 r = m.find(4);137 assert(r == std::next(m.begin(), 8));138 }139 }140#endif141#if TEST_STD_VER > 11142 {143 typedef int V;144 typedef std::multiset<V, std::less<>> M;145 typedef M::iterator R;146 147 V ar[] = {5, 6, 7, 8, 9, 10, 11, 12};148 M m(ar, ar + sizeof(ar) / sizeof(ar[0]));149 R r = m.find(5);150 assert(r == m.begin());151 r = m.find(6);152 assert(r == std::next(m.begin()));153 r = m.find(7);154 assert(r == std::next(m.begin(), 2));155 r = m.find(8);156 assert(r == std::next(m.begin(), 3));157 r = m.find(9);158 assert(r == std::next(m.begin(), 4));159 r = m.find(10);160 assert(r == std::next(m.begin(), 5));161 r = m.find(11);162 assert(r == std::next(m.begin(), 6));163 r = m.find(12);164 assert(r == std::next(m.begin(), 7));165 r = m.find(4);166 assert(r == std::next(m.begin(), 8));167 }168 169 {170 typedef PrivateConstructor V;171 typedef std::multiset<V, std::less<>> M;172 typedef M::iterator R;173 174 M m;175 m.insert(V::make(5));176 m.insert(V::make(6));177 m.insert(V::make(7));178 m.insert(V::make(8));179 m.insert(V::make(9));180 m.insert(V::make(10));181 m.insert(V::make(11));182 m.insert(V::make(12));183 184 R r = m.find(5);185 assert(r == m.begin());186 r = m.find(6);187 assert(r == std::next(m.begin()));188 r = m.find(7);189 assert(r == std::next(m.begin(), 2));190 r = m.find(8);191 assert(r == std::next(m.begin(), 3));192 r = m.find(9);193 assert(r == std::next(m.begin(), 4));194 r = m.find(10);195 assert(r == std::next(m.begin(), 5));196 r = m.find(11);197 assert(r == std::next(m.begin(), 6));198 r = m.find(12);199 assert(r == std::next(m.begin(), 7));200 r = m.find(4);201 assert(r == std::next(m.begin(), 8));202 }203#endif204 205 return 0;206}207