llvm-project
89 строк · 2.1 Кб
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-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <map>
10
11// class map
12
13// size_type count(const key_type& k) const;
14
15#include <map>
16#include <cassert>
17
18#include "test_macros.h"
19#include "min_allocator.h"
20#include "private_constructor.h"
21#include "is_transparent.h"
22
23#if TEST_STD_VER >= 11
24template <class T>
25struct FinalCompare final {
26bool operator()(const T& x, const T& y) const { return x < y; }
27};
28#endif
29
30template <class Map, class ArgType = typename Map::key_type>
31void test() {
32typedef typename Map::value_type V;
33typedef typename Map::size_type R;
34
35V ar[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)};
36
37const Map m(ar, ar + sizeof(ar) / sizeof(ar[0]));
38
39for (int i = 0; i < 5; ++i) {
40R r = m.count(ArgType(i));
41assert(r == 0);
42}
43
44for (int i = 5; i < 13; ++i) {
45R r = m.count(ArgType(i));
46assert(r == 1);
47}
48}
49
50int main(int, char**) {
51test<std::map<int, double> >();
52#if TEST_STD_VER >= 11
53typedef std::pair<const int, double> V;
54test<std::map<int, double, std::less<int>, min_allocator<V>>>();
55test<std::map<int, double, FinalCompare<int>>>();
56#endif
57#if TEST_STD_VER >= 14
58typedef std::map<int, double, std::less<>> TM;
59test<TM>();
60test<TM, C2Int>();
61
62{
63typedef PrivateConstructor PC;
64typedef std::map<PC, double, std::less<> > M;
65typedef M::size_type R;
66
67M m;
68m[PC::make(5)] = 5;
69m[PC::make(6)] = 6;
70m[PC::make(7)] = 7;
71m[PC::make(8)] = 8;
72m[PC::make(9)] = 9;
73m[PC::make(10)] = 10;
74m[PC::make(11)] = 11;
75m[PC::make(12)] = 12;
76
77for (int i = 0; i < 5; ++i) {
78R r = m.count(i);
79assert(r == 0);
80}
81
82for (int i = 5; i < 13; ++i) {
83R r = m.count(i);
84assert(r == 1);
85}
86}
87#endif // TEST_STD_VER >= 14
88return 0;
89}
90