• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright 2006-2009 Daniel James.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include "./containers.hpp"
7 
8 #include "../helpers/invariants.hpp"
9 #include "../helpers/random_values.hpp"
10 #include "../helpers/tracker.hpp"
11 
12 #if defined(BOOST_MSVC)
13 #pragma warning(disable : 4512) // assignment operator could not be generated
14 #endif
15 
16 test::seed_t initialize_seed(12847);
17 
18 template <class T> struct self_assign_base : public test::exception_base
19 {
20   test::random_values<T> values;
self_assign_baseself_assign_base21   self_assign_base(std::size_t count = 0) : values(count, test::limited_range)
22   {
23   }
24 
25   typedef T data_type;
initself_assign_base26   T init() const { return T(values.begin(), values.end()); }
27 
runself_assign_base28   void run(T& x) const
29   {
30     x = x;
31 
32     DISABLE_EXCEPTIONS;
33     test::check_container(x, values);
34     test::check_equivalent_keys(x);
35   }
36 
BOOST_PREVENT_MACRO_SUBSTITUTIONself_assign_base37   void check BOOST_PREVENT_MACRO_SUBSTITUTION(T const& x) const
38   {
39     test::check_equivalent_keys(x);
40   }
41 };
42 
43 template <class T> struct self_assign_test1 : self_assign_base<T>
44 {
45 };
46 
47 template <class T> struct self_assign_test2 : self_assign_base<T>
48 {
self_assign_test2self_assign_test249   self_assign_test2() : self_assign_base<T>(100) {}
50 };
51 
52 template <class T> struct assign_base : public test::exception_base
53 {
54   test::random_values<T> x_values, y_values;
55   T x, y;
56 
57   typedef typename T::hasher hasher;
58   typedef typename T::key_equal key_equal;
59   typedef typename T::allocator_type allocator_type;
60 
assign_baseassign_base61   assign_base(int tag1, int tag2, float mlf1 = 1.0, float mlf2 = 1.0)
62       : x_values(), y_values(),
63         x(0, hasher(tag1), key_equal(tag1), allocator_type(tag1)),
64         y(0, hasher(tag2), key_equal(tag2), allocator_type(tag2))
65   {
66     x.max_load_factor(mlf1);
67     y.max_load_factor(mlf2);
68   }
69 
70   typedef T data_type;
initassign_base71   T init() const { return T(x); }
72 
runassign_base73   void run(T& x1) const
74   {
75     x1 = y;
76 
77     DISABLE_EXCEPTIONS;
78     test::check_container(x1, y_values);
79     test::check_equivalent_keys(x1);
80   }
81 
BOOST_PREVENT_MACRO_SUBSTITUTIONassign_base82   void check BOOST_PREVENT_MACRO_SUBSTITUTION(T const& x1) const
83   {
84     test::check_equivalent_keys(x1);
85 
86     // If the container is empty at the point of the exception, the
87     // internal structure is hidden, this exposes it, at the cost of
88     // messing up the data.
89     if (x_values.size()) {
90       T& x2 = const_cast<T&>(x1);
91       x2.emplace(*x_values.begin());
92       test::check_equivalent_keys(x2);
93     }
94   }
95 };
96 
97 template <class T> struct assign_values : assign_base<T>
98 {
assign_valuesassign_values99   assign_values(unsigned int count1, unsigned int count2, int tag1, int tag2,
100     test::random_generator gen = test::default_generator, float mlf1 = 1.0,
101     float mlf2 = 1.0)
102       : assign_base<T>(tag1, tag2, mlf1, mlf2)
103   {
104     this->x_values.fill(count1, gen);
105     this->y_values.fill(count2, gen);
106     this->x.insert(this->x_values.begin(), this->x_values.end());
107     this->y.insert(this->y_values.begin(), this->y_values.end());
108   }
109 };
110 
111 template <class T> struct assign_test1 : assign_values<T>
112 {
assign_test1assign_test1113   assign_test1() : assign_values<T>(0, 0, 0, 0) {}
114 };
115 
116 template <class T> struct assign_test2 : assign_values<T>
117 {
assign_test2assign_test2118   assign_test2() : assign_values<T>(60, 0, 0, 0) {}
119 };
120 
121 template <class T> struct assign_test2a : assign_values<T>
122 {
assign_test2aassign_test2a123   assign_test2a() : assign_values<T>(60, 0, 0, 0, test::limited_range) {}
124 };
125 
126 template <class T> struct assign_test3 : assign_values<T>
127 {
assign_test3assign_test3128   assign_test3() : assign_values<T>(0, 60, 0, 0) {}
129 };
130 
131 template <class T> struct assign_test3a : assign_values<T>
132 {
assign_test3aassign_test3a133   assign_test3a() : assign_values<T>(0, 60, 0, 0, test::limited_range) {}
134 };
135 
136 template <class T> struct assign_test4 : assign_values<T>
137 {
assign_test4assign_test4138   assign_test4() : assign_values<T>(10, 10, 1, 2) {}
139 };
140 
141 template <class T> struct assign_test4a : assign_values<T>
142 {
assign_test4aassign_test4a143   assign_test4a() : assign_values<T>(10, 100, 1, 2) {}
144 };
145 
146 template <class T> struct assign_test4b : assign_values<T>
147 {
assign_test4bassign_test4b148   assign_test4b() : assign_values<T>(10, 100, 1, 2, test::limited_range) {}
149 };
150 
151 template <class T> struct assign_test5 : assign_values<T>
152 {
assign_test5assign_test5153   assign_test5()
154       : assign_values<T>(5, 60, 0, 0, test::default_generator, 1.0f, 0.1f)
155   {
156   }
157 };
158 
159 template <class T> struct equivalent_test1 : assign_base<T>
160 {
equivalent_test1equivalent_test1161   equivalent_test1() : assign_base<T>(0, 0)
162   {
163     test::random_values<T> x_values2(10);
164     this->x_values.insert(x_values2.begin(), x_values2.end());
165     this->x_values.insert(x_values2.begin(), x_values2.end());
166     test::random_values<T> y_values2(10);
167     this->y_values.insert(y_values2.begin(), y_values2.end());
168     this->y_values.insert(y_values2.begin(), y_values2.end());
169     this->x.insert(this->x_values.begin(), this->x_values.end());
170     this->y.insert(this->y_values.begin(), this->y_values.end());
171   }
172 };
173 
174 // clang-format off
175 EXCEPTION_TESTS_REPEAT(5,
176     (self_assign_test1)(self_assign_test2)
177     (assign_test1)(assign_test2)(assign_test2a)
178     (assign_test3)(assign_test3a)
179     (assign_test4)(assign_test4a)(assign_test4b)
180     (assign_test5)
181     (equivalent_test1),
182     CONTAINER_SEQ)
183 // clang-format on
184 
185 RUN_TESTS()
186