1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 // Tests for compatibility with the host's STL.
18 
19 #ifndef __TBB_test_common_allocator_stl_test_H_
20 #define __TBB_test_common_allocator_stl_test_H_
21 
22 #include "common/test.h"
23 
24 template<typename Container>
TestSequence(const typename Container::allocator_type & a)25 void TestSequence(const typename Container::allocator_type &a) {
26     constexpr auto iter_count = 1000;
27     Container c(a);
28     for(int i = 0; i < iter_count; ++i){
29         c.push_back(i * i);
30     }
31     typename Container::const_iterator p = c.begin();
32     for(int i = 0; i < iter_count; ++i) {
33         REQUIRE(*p == i*i);
34         ++p;
35     }
36     // regression test against compilation error for GCC 4.6.2
37     c.resize(1000);
38 }
39 
40 template<typename Set>
TestSet(const typename Set::allocator_type & a)41 void TestSet(const typename Set::allocator_type &a) {
42     Set s(typename Set::key_compare(), a);
43     using value_type = typename Set::value_type;
44     for(int i = 0; i < 100; ++i)
45         s.insert(value_type(3 * i));
46     for( int i = 0; i < 300; ++i ) {
47         REQUIRE(s.erase(i) == size_t(i % 3 == 0));
48     }
49 }
50 
51 template<typename Map>
TestMap(const typename Map::allocator_type & a)52 void TestMap(const typename Map::allocator_type &a) {
53     Map m(typename Map::key_compare(), a);
54     using value_type = typename Map::value_type;
55     for(int i = 0; i < 100; ++i)
56         m.insert(value_type(i,i*i));
57     for(int i=0; i < 100; ++i)
58         REQUIRE(m.find(i)->second == i * i);
59 }
60 
61 #include <deque>
62 #include <list>
63 #include <map>
64 #include <set>
65 #include <vector>
66 
67 struct MoveOperationTracker {
68     int my_value;
69 
my_valueMoveOperationTracker70     MoveOperationTracker(int value = 0) : my_value(value) {}
MoveOperationTrackerMoveOperationTracker71     MoveOperationTracker(const MoveOperationTracker&) {
72         REQUIRE_MESSAGE(false, "Copy constructor is called");
73     }
MoveOperationTrackerMoveOperationTracker74     MoveOperationTracker(MoveOperationTracker&& m) noexcept : my_value( m.my_value ) {
75     }
76     MoveOperationTracker& operator=(MoveOperationTracker const&) {
77         REQUIRE_MESSAGE(false, "Copy assignment operator is called");
78         return *this;
79     }
80     MoveOperationTracker& operator=(MoveOperationTracker&& m) noexcept {
81         my_value = m.my_value;
82         return *this;
83     }
84 
85     bool operator==(int value) const {
86         return my_value == value;
87     }
88 
89     bool operator==(const MoveOperationTracker& m) const {
90         return my_value == m.my_value;
91     }
92 };
93 
94 template<typename Allocator>
95 void TestAllocatorWithSTL(const Allocator &a = Allocator()) {
96 
97 // Allocator type conversion section
98     using Ai = typename std::allocator_traits<Allocator>::template rebind_alloc<int>;
99     using Acii = typename std::allocator_traits<Allocator>::template rebind_alloc<std::pair<const int, int> >;
100 #if _MSC_VER && _CPPLIB_VER < 650
101     using Aci = typename std::allocator_traits<Allocator>::template rebind_alloc<const int>;
102     using Aii = typename std::allocator_traits<Allocator>::template rebind_alloc<std::pair<int, int> >;
103 #endif // _MSC_VER
104 
105     // Sequenced containers
106     TestSequence<std::deque <int,Ai> >(a);
107     TestSequence<std::list  <int,Ai> >(a);
108     TestSequence<std::vector<int,Ai> >(a);
109 
110     using Amot = typename std::allocator_traits<Allocator>::template rebind_alloc<MoveOperationTracker>;
111     TestSequence<std::deque <MoveOperationTracker, Amot> >(a);
112     TestSequence<std::list  <MoveOperationTracker, Amot> >(a);
113     TestSequence<std::vector<MoveOperationTracker, Amot> >(a);
114 
115     // Associative containers
116     TestSet<std::set     <int, std::less<int>, Ai> >(a);
117     TestSet<std::multiset<int, std::less<int>, Ai> >(a);
118     TestMap<std::map     <int, int, std::less<int>, Acii> >(a);
119     TestMap<std::multimap<int, int, std::less<int>, Acii> >(a);
120 
121 #if _MSC_VER && _CPPLIB_VER < 650
122     // Test compatibility with Microsoft's implementation of std::allocator for some cases that
123     // are undefined according to the ISO standard but permitted by Microsoft.
124     TestSequence<std::deque <const int,Aci> >(a);
125 #if _CPPLIB_VER>=500
126     TestSequence<std::list  <const int,Aci> >(a);
127 #endif
128     TestSequence<std::vector<const int,Aci> >(a);
129     TestSet<std::set<const int, std::less<int>, Aci> >(a);
130     TestMap<std::map<int, int, std::less<int>, Aii> >(a);
131     TestMap<std::map<const int, int, std::less<int>, Acii> >(a);
132     TestMap<std::multimap<int, int, std::less<int>, Aii> >(a);
133     TestMap<std::multimap<const int, int, std::less<int>, Acii> >(a);
134 #endif /* _MSC_VER */
135 }
136 #endif // __TBB_test_common_allocator_stl_test_H_
137