PIVX Core  5.6.99
P2P Digital Currency
prevector_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <vector>
6 #include "prevector.h"
7 #include "random.h"
8 
9 #include "serialize.h"
10 #include "streams.h"
11 
12 #include "test/test_pivx.h"
13 
14 #include <boost/test/unit_test.hpp>
15 
17 
18 template<unsigned int N, typename T>
20  typedef std::vector<T> realtype;
23 
27 
28  typedef typename pretype::size_type Size;
29  bool passed = true;
32 
33  template <typename A, typename B>
34  void local_check_equal(A a, B b)
35  {
36  local_check(a == b);
37  }
38  void local_check(bool b)
39  {
40  passed &= b;
41  }
42  void test() {
43  const pretype& const_pre_vector = pre_vector;
46  for (Size s = 0; s < real_vector.size(); s++) {
48  local_check(&(pre_vector[s]) == &(pre_vector.begin()[s]));
49  local_check(&(pre_vector[s]) == &*(pre_vector.begin() + s));
50  local_check(&(pre_vector[s]) == &*((pre_vector.end() + s) - real_vector.size()));
51  }
52  // local_check(realtype(pre_vector) == real_vector);
55  size_t pos = 0;
56 
57  for (const T& v : pre_vector) {
58  local_check(v == real_vector[pos++]);
59  }
60  pos = 0;
61  for (const T& v : const_pre_vector) {
62  local_check(v == real_vector[pos++]);
63  }
64 
67  ss1 << real_vector;
68  ss2 << pre_vector;
69  local_check_equal(ss1.size(), ss2.size());
70  for (Size s = 0; s < ss1.size(); s++) {
71  local_check_equal(ss1[s], ss2[s]);
72  }
73  }
74 
75 public:
76  void resize(Size s) {
77  real_vector.resize(s);
78  local_check_equal(real_vector.size(), s);
79  pre_vector.resize(s);
81  test();
82  }
83 
84  void reserve(Size s) {
85  real_vector.reserve(s);
86  local_check(real_vector.capacity() >= s);
89  test();
90  }
91 
92  void insert(Size position, const T& value) {
93  real_vector.insert(real_vector.begin() + position, value);
94  pre_vector.insert(pre_vector.begin() + position, value);
95  test();
96  }
97 
98  void insert(Size position, Size count, const T& value) {
99  real_vector.insert(real_vector.begin() + position, count, value);
100  pre_vector.insert(pre_vector.begin() + position, count, value);
101  test();
102  }
103 
104  template<typename I>
105  void insert_range(Size position, I first, I last) {
106  real_vector.insert(real_vector.begin() + position, first, last);
107  pre_vector.insert(pre_vector.begin() + position, first, last);
108  test();
109  }
110 
111  void erase(Size position) {
112  real_vector.erase(real_vector.begin() + position);
113  pre_vector.erase(pre_vector.begin() + position);
114  test();
115  }
116 
117  void erase(Size first, Size last) {
118  real_vector.erase(real_vector.begin() + first, real_vector.begin() + last);
119  pre_vector.erase(pre_vector.begin() + first, pre_vector.begin() + last);
120  test();
121  }
122 
123  void update(Size pos, const T& value) {
124  real_vector[pos] = value;
125  pre_vector[pos] = value;
126  test();
127  }
128 
129  void push_back(const T& value) {
130  real_vector.push_back(value);
131  pre_vector.push_back(value);
132  test();
133  }
134 
135  void pop_back() {
136  real_vector.pop_back();
138  test();
139  }
140 
141  void clear() {
142  real_vector.clear();
143  pre_vector.clear();
144  }
145 
146  void assign(Size n, const T& value) {
147  real_vector.assign(n, value);
148  pre_vector.assign(n, value);
149  }
150 
151  Size size() {
152  return real_vector.size();
153  }
154 
156  return pre_vector.capacity();
157  }
158 
159  void shrink_to_fit() {
161  test();
162  }
163 
164  void swap() {
167  test();
168  }
169 
170  void move() {
171  real_vector = std::move(real_vector_alt);
172  real_vector_alt.clear();
173  pre_vector = std::move(pre_vector_alt);
175  }
176 
177  void copy() {
180  }
181 
183  size_t r = values.size();
184  size_t s = real_vector.size() / 2;
185  if (real_vector.capacity() < s + r) {
186  real_vector.reserve(s + r);
187  }
188  real_vector.resize(s);
190  for (auto v : values) {
191  real_vector.push_back(v);
192  }
193  auto p = pre_vector.size();
195  for (auto v : values) {
196  pre_vector[p] = v;
197  ++p;
198  }
199  test();
200  }
201 
203  BOOST_CHECK_MESSAGE(passed, "insecure_rand: " + rand_seed.ToString());
204  }
206  SeedInsecureRand();
207  rand_seed = InsecureRand256();
209  }
210 };
211 
212 BOOST_AUTO_TEST_CASE(PrevectorTestInt)
213 {
214  for (int j = 0; j < 64; j++) {
216  for (int i = 0; i < 2048; i++) {
217  int r = InsecureRand32();
218  if ((r % 4) == 0) {
219  test.insert(InsecureRand32() % (test.size() + 1), InsecureRand32());
220  }
221  if (test.size() > 0 && ((r >> 2) % 4) == 1) {
222  test.erase(InsecureRand32() % test.size());
223  }
224  if (((r >> 4) % 8) == 2) {
225  int new_size = std::max<int>(0, std::min<int>(30, test.size() + (InsecureRand32() % 5) - 2));
226  test.resize(new_size);
227  }
228  if (((r >> 7) % 8) == 3) {
229  test.insert(InsecureRand32() % (test.size() + 1), 1 + (InsecureRand32() % 2), InsecureRand32());
230  }
231  if (((r >> 10) % 8) == 4) {
232  int del = std::min<int>(test.size(), 1 + (InsecureRand32() % 2));
233  int beg = InsecureRand32() % (test.size() + 1 - del);
234  test.erase(beg, beg + del);
235  }
236  if (((r >> 13) % 16) == 5) {
237  test.push_back(InsecureRand32());
238  }
239  if (test.size() > 0 && ((r >> 17) % 16) == 6) {
240  test.pop_back();
241  }
242  if (((r >> 21) % 32) == 7) {
243  int values[4];
244  int num = 1 + (InsecureRand32() % 4);
245  for (int i = 0; i < num; i++) {
246  values[i] = InsecureRand32();
247  }
248  test.insert_range(InsecureRand32() % (test.size() + 1), values, values + num);
249  }
250  if (((r >> 26) % 32) == 8) {
251  int del = std::min<int>(test.size(), 1 + (InsecureRand32() % 4));
252  int beg = InsecureRand32() % (test.size() + 1 - del);
253  test.erase(beg, beg + del);
254  }
255  r = InsecureRand32();
256  if (r % 32 == 9) {
257  test.reserve(InsecureRand32() % 32);
258  }
259  if ((r >> 5) % 64 == 10) {
260  test.shrink_to_fit();
261  }
262  if (test.size() > 0) {
263  test.update(InsecureRand32() % test.size(), InsecureRand32());
264  }
265  if (((r >> 11) % 1024) == 11) {
266  test.clear();
267  }
268  if (((r >> 21) % 512) == 12) {
269  test.assign(InsecureRand32() % 32, InsecureRand32());
270  }
271  if (((r >> 15) % 8) == 3) {
272  test.swap();
273  }
274  if (((r >> 15) % 16) == 8) {
275  test.copy();
276  }
277  if (((r >> 15) % 32) == 18) {
278  test.move();
279  }
280  if (InsecureRandBits(5) == 19) {
281  unsigned int num = 1 + (InsecureRandBits(4));
282  std::vector<int> values(num);
283  for (auto &v : values) {
284  v = InsecureRand32();
285  }
286  test.resize_uninitialized(values);
287  }
288  }
289  }
290 }
291 
#define ss1(x)
Definition: bmw.c:140
#define ss2(x)
Definition: bmw.c:142
Fast randomness source.
Definition: random.h:107
std::string ToString() const
Definition: uint256.cpp:65
void erase(Size position)
void update(Size pos, const T &value)
void local_check_equal(A a, B b)
void insert(Size position, Size count, const T &value)
void reserve(Size s)
prevector< N, T > pretype
void local_check(bool b)
void erase(Size first, Size last)
void insert(Size position, const T &value)
std::vector< T > realtype
void resize(Size s)
void assign(Size n, const T &value)
FastRandomContext rand_cache
void insert_range(Size position, I first, I last)
pretype::size_type Size
void resize_uninitialized(realtype values)
void push_back(const T &value)
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
bool empty() const
Definition: prevector.h:281
void pop_back()
Definition: prevector.h:439
void swap(prevector< N, T, Size, Diff > &other)
Definition: prevector.h:459
void shrink_to_fit()
Definition: prevector.h:334
void clear()
Definition: prevector.h:338
size_type size() const
Definition: prevector.h:277
iterator erase(iterator pos)
Definition: prevector.h:399
Size size_type
Definition: prevector.h:40
size_t capacity() const
Definition: prevector.h:295
iterator begin()
Definition: prevector.h:285
iterator end()
Definition: prevector.h:287
void reserve(size_type new_capacity)
Definition: prevector.h:328
void resize_uninitialized(size_type new_size)
Definition: prevector.h:384
void resize(size_type new_size)
Definition: prevector.h:311
iterator insert(iterator pos, const T &value)
Definition: prevector.h:342
void assign(size_type n, const T &val)
Definition: prevector.h:213
void push_back(const T &value)
Definition: prevector.h:435
256-bit opaque blob.
Definition: uint256.h:138
BOOST_AUTO_TEST_SUITE_END()
#define T(expected, seed, data)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_CASE(PrevectorTestInt)
@ SER_DISK
Definition: serialize.h:175
#define B
Definition: util_tests.cpp:688