xref: /llvm-project/libcxx/test/std/containers/associative/map/map.ops/count.pass.cpp (revision 2e106d55203a2467bccfe7cf39360830c27c67e4)
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
24 template <class T>
25 struct FinalCompare final {
operator ()FinalCompare26   bool operator()(const T& x, const T& y) const { return x < y; }
27 };
28 #endif
29 
30 template <class Map, class ArgType = typename Map::key_type>
test()31 void test() {
32   typedef typename Map::value_type V;
33   typedef typename Map::size_type R;
34 
35   V 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 
37   const Map m(ar, ar + sizeof(ar) / sizeof(ar[0]));
38 
39   for (int i = 0; i < 5; ++i) {
40     R r = m.count(ArgType(i));
41     assert(r == 0);
42   }
43 
44   for (int i = 5; i < 13; ++i) {
45     R r = m.count(ArgType(i));
46     assert(r == 1);
47   }
48 }
49 
main(int,char **)50 int main(int, char**) {
51   test<std::map<int, double> >();
52 #if TEST_STD_VER >= 11
53   typedef std::pair<const int, double> V;
54   test<std::map<int, double, std::less<int>, min_allocator<V>>>();
55   test<std::map<int, double, FinalCompare<int>>>();
56 #endif
57 #if TEST_STD_VER >= 14
58   typedef std::map<int, double, std::less<>> TM;
59   test<TM>();
60   test<TM, C2Int>();
61 
62   {
63     typedef PrivateConstructor PC;
64     typedef std::map<PC, double, std::less<> > M;
65     typedef M::size_type R;
66 
67     M m;
68     m[PC::make(5)]  = 5;
69     m[PC::make(6)]  = 6;
70     m[PC::make(7)]  = 7;
71     m[PC::make(8)]  = 8;
72     m[PC::make(9)]  = 9;
73     m[PC::make(10)] = 10;
74     m[PC::make(11)] = 11;
75     m[PC::make(12)] = 12;
76 
77     for (int i = 0; i < 5; ++i) {
78       R r = m.count(i);
79       assert(r == 0);
80     }
81 
82     for (int i = 5; i < 13; ++i) {
83       R r = m.count(i);
84       assert(r == 1);
85     }
86   }
87 #endif // TEST_STD_VER >= 14
88   return 0;
89 }
90