• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TEST_TRANSPARENT_UNORDERED_H
10 #define TEST_TRANSPARENT_UNORDERED_H
11 
12 #include "test_macros.h"
13 #include "is_transparent.h"
14 
15 #include <cassert>
16 
17 // testing transparent unordered containers
18 #if TEST_STD_VER > 17
19 
20 template <template <typename...> class UnorderedSet, typename Hash,
21           typename Equal>
22 using unord_set_type = UnorderedSet<StoredType<int>, Hash, Equal>;
23 
24 template <template <typename...> class UnorderedMap, typename Hash,
25           typename Equal>
26 using unord_map_type = UnorderedMap<StoredType<int>, int, Hash, Equal>;
27 
28 template <typename Container, typename... Args>
test_transparent_find(Args &&...args)29 void test_transparent_find(Args&&... args) {
30   Container c{std::forward<Args>(args)...};
31   int conversions = 0;
32   assert(c.find(SearchedType<int>(1, &conversions)) != c.end());
33   assert(c.find(SearchedType<int>(2, &conversions)) != c.end());
34   assert(conversions == 0);
35   assert(c.find(SearchedType<int>(3, &conversions)) == c.end());
36   assert(conversions == 0);
37 }
38 
39 template <typename Container, typename... Args>
test_non_transparent_find(Args &&...args)40 void test_non_transparent_find(Args&&... args) {
41   Container c{std::forward<Args>(args)...};
42   int conversions = 0;
43   assert(c.find(SearchedType<int>(1, &conversions)) != c.end());
44   assert(conversions > 0);
45   conversions = 0;
46   assert(c.find(SearchedType<int>(2, &conversions)) != c.end());
47   assert(conversions > 0);
48   conversions = 0;
49   assert(c.find(SearchedType<int>(3, &conversions)) == c.end());
50   assert(conversions > 0);
51 }
52 
53 template <typename Container, typename... Args>
test_transparent_count(Args &&...args)54 void test_transparent_count(Args&&... args) {
55   Container c{std::forward<Args>(args)...};
56   int conversions = 0;
57   assert(c.count(SearchedType<int>(1, &conversions)) > 0);
58   assert(c.count(SearchedType<int>(2, &conversions)) > 0);
59   assert(conversions == 0);
60   assert(c.count(SearchedType<int>(3, &conversions)) == 0);
61   assert(conversions == 0);
62 }
63 
64 template <typename Container, typename... Args>
test_non_transparent_count(Args &&...args)65 void test_non_transparent_count(Args&&... args) {
66   Container c{std::forward<Args>(args)...};
67   int conversions = 0;
68   assert(c.count(SearchedType<int>(1, &conversions)) > 0);
69   assert(conversions > 0);
70   conversions = 0;
71   assert(c.count(SearchedType<int>(2, &conversions)) > 0);
72   assert(conversions > 0);
73   conversions = 0;
74   assert(c.count(SearchedType<int>(3, &conversions)) == 0);
75   assert(conversions > 0);
76 }
77 
78 template <typename Container, typename... Args>
test_transparent_contains(Args &&...args)79 void test_transparent_contains(Args&&... args) {
80   Container c{std::forward<Args>(args)...};
81   int conversions = 0;
82   assert(c.contains(SearchedType<int>(1, &conversions)));
83   assert(c.contains(SearchedType<int>(2, &conversions)));
84   assert(conversions == 0);
85   assert(!c.contains(SearchedType<int>(3, &conversions)));
86   assert(conversions == 0);
87 }
88 
89 template <typename Container, typename... Args>
test_non_transparent_contains(Args &&...args)90 void test_non_transparent_contains(Args&&... args) {
91   Container c{std::forward<Args>(args)...};
92   int conversions = 0;
93   assert(c.contains(SearchedType<int>(1, &conversions)));
94   assert(conversions > 0);
95   conversions = 0;
96   assert(c.contains(SearchedType<int>(2, &conversions)));
97   assert(conversions > 0);
98   conversions = 0;
99   assert(!c.contains(SearchedType<int>(3, &conversions)));
100   assert(conversions > 0);
101 }
102 
103 template <typename Container, typename... Args>
test_transparent_equal_range(Args &&...args)104 void test_transparent_equal_range(Args&&... args) {
105   Container c{std::forward<Args>(args)...};
106   int conversions = 0;
107   auto iters = c.equal_range(SearchedType<int>(1, &conversions));
108   assert(std::distance(iters.first, iters.second) > 0);
109   iters = c.equal_range(SearchedType<int>(2, &conversions));
110   assert(std::distance(iters.first, iters.second) > 0);
111   assert(conversions == 0);
112   iters = c.equal_range(SearchedType<int>(3, &conversions));
113   assert(std::distance(iters.first, iters.second) == 0);
114   assert(conversions == 0);
115 }
116 
117 template <typename Container, typename... Args>
test_non_transparent_equal_range(Args &&...args)118 void test_non_transparent_equal_range(Args&&... args) {
119   Container c{std::forward<Args>(args)...};
120   int conversions = 0;
121   auto iters = c.equal_range(SearchedType<int>(1, &conversions));
122   assert(std::distance(iters.first, iters.second) > 0);
123   assert(conversions > 0);
124   conversions = 0;
125   iters = c.equal_range(SearchedType<int>(2, &conversions));
126   assert(std::distance(iters.first, iters.second) > 0);
127   assert(conversions > 0);
128   conversions = 0;
129   iters = c.equal_range(SearchedType<int>(3, &conversions));
130   assert(std::distance(iters.first, iters.second) == 0);
131   assert(conversions > 0);
132 }
133 
134 #endif // TEST_STD_VER > 17
135 
136 #endif // TEST_TRANSPARENT_UNORDERED_H
137