exit_handler_intel_x64_unittests_containers.cpp
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
23 
24 #ifdef INCLUDE_LIBCXX_UNITTESTS
25 
26 #include <array>
27 #include <vector>
28 #include <deque>
29 #include <forward_list>
30 #include <list>
31 #include <stack>
32 #include <queue>
33 #include <set>
34 #include <map>
35 
36 void
37 exit_handler_intel_x64::unittest_1001_containers_array() const
38 {
39  std::array<int, 4> myarray = {{0, 1, 2, 3}};
40  std::array<int, 4> myarray2 = {{0, 1, 2, 3}};
41 
42  auto total = 0;
43  for (auto iter = myarray.begin(); iter != myarray.end(); iter++)
44  total += *iter;
45 
46  auto rtotal = 0;
47  for (auto iter = myarray.rbegin(); iter != myarray.rend(); iter++)
48  rtotal += *iter;
49 
50  auto ctotal = 0;
51  for (auto iter = myarray.cbegin(); iter != myarray.cend(); iter++)
52  ctotal += *iter;
53 
54  auto crtotal = 0;
55  for (auto iter = myarray.crbegin(); iter != myarray.crend(); iter++)
56  crtotal += *iter;
57 
58  expect_true(total == 6);
59  expect_true(rtotal == 6);
60  expect_true(ctotal == 6);
61  expect_true(crtotal == 6);
62 
63  expect_true(myarray.size() == 4);
64  expect_true(myarray.max_size() == 4);
65  expect_false(myarray.empty());
66 
67  expect_true(myarray.at(0) == 0);
68  expect_true(myarray.at(3) == 3);
69  expect_true(myarray.front() == 0);
70  expect_true(myarray.back() == 3);
71  expect_true(myarray.data() != nullptr);
72 
73  myarray.fill(0);
74  myarray.swap(myarray2);
75 
76  expect_true(std::get<0>(myarray) == 0);
77  expect_true(std::get<3>(myarray) == 3);
78 
79  expect_false(myarray == myarray2);
80  expect_true(myarray != myarray2);
81  expect_false(myarray < myarray2);
82  expect_true(myarray > myarray2);
83  expect_false(myarray <= myarray2);
84  expect_true(myarray >= myarray2);
85 }
86 
87 void
88 exit_handler_intel_x64::unittest_1002_containers_vector() const
89 {
90  auto myvector = std::vector<int>({0, 1, 2, 3});
91  auto myvector2 = std::vector<int>({0, 1, 2, 3});
92 
93  auto total = 0;
94  for (auto iter = myvector.begin(); iter != myvector.end(); iter++)
95  total += *iter;
96 
97  auto rtotal = 0;
98  for (auto iter = myvector.rbegin(); iter != myvector.rend(); iter++)
99  rtotal += *iter;
100 
101  auto ctotal = 0;
102  for (auto iter = myvector.cbegin(); iter != myvector.cend(); iter++)
103  ctotal += *iter;
104 
105  auto crtotal = 0;
106  for (auto iter = myvector.crbegin(); iter != myvector.crend(); iter++)
107  crtotal += *iter;
108 
109  expect_true(total == 6);
110  expect_true(rtotal == 6);
111  expect_true(ctotal == 6);
112  expect_true(crtotal == 6);
113 
114  expect_true(myvector.size() == 4);
115  expect_true(myvector.max_size() >= 4);
116  myvector.resize(4);
117  expect_true(myvector.capacity() >= 4);
118  expect_false(myvector.empty());
119  myvector.reserve(4);
120  myvector.shrink_to_fit();
121 
122  expect_true(myvector.at(0) == 0);
123  expect_true(myvector.at(3) == 3);
124  expect_true(myvector.front() == 0);
125  expect_true(myvector.back() == 3);
126  expect_true(myvector.data() != nullptr);
127 
128  myvector.assign(4, 0);
129  myvector = myvector2;
130 
131  myvector.push_back(4);
132  myvector.pop_back();
133  myvector = myvector2;
134 
135  myvector.insert(myvector.begin(), 0);
136  myvector.erase(myvector.begin());
137  myvector = myvector2;
138 
139  myvector.swap(myvector2);
140  std::swap(myvector, myvector2);
141 
142  myvector.emplace(myvector.begin());
143  myvector.emplace_back();
144  myvector = myvector2;
145 
146  expect_true(myvector == myvector2);
147  expect_false(myvector != myvector2);
148  expect_false(myvector < myvector2);
149  expect_false(myvector > myvector2);
150  expect_true(myvector <= myvector2);
151  expect_true(myvector >= myvector2);
152  myvector = myvector2;
153 
154  myvector.get_allocator();
155  myvector.clear();
156 }
157 
158 void
159 exit_handler_intel_x64::unittest_1003_containers_deque() const
160 {
161  std::deque<int> mydeque = {{0, 1, 2, 3}};
162  std::deque<int> mydeque2 = {{0, 1, 2, 3}};
163 
164  auto total = 0;
165  for (auto iter = mydeque.begin(); iter != mydeque.end(); iter++)
166  total += *iter;
167 
168  auto rtotal = 0;
169  for (auto iter = mydeque.rbegin(); iter != mydeque.rend(); iter++)
170  rtotal += *iter;
171 
172  auto ctotal = 0;
173  for (auto iter = mydeque.cbegin(); iter != mydeque.cend(); iter++)
174  ctotal += *iter;
175 
176  auto crtotal = 0;
177  for (auto iter = mydeque.crbegin(); iter != mydeque.crend(); iter++)
178  crtotal += *iter;
179 
180  expect_true(total == 6);
181  expect_true(rtotal == 6);
182  expect_true(ctotal == 6);
183  expect_true(crtotal == 6);
184 
185  expect_true(mydeque.size() == 4);
186  expect_true(mydeque.max_size() >= 4);
187  mydeque.resize(4);
188  expect_false(mydeque.empty());
189  mydeque.shrink_to_fit();
190 
191  expect_true(mydeque.at(0) == 0);
192  expect_true(mydeque.at(3) == 3);
193  expect_true(mydeque.front() == 0);
194  expect_true(mydeque.back() == 3);
195 
196  mydeque.assign(4, 0);
197  mydeque = mydeque2;
198 
199  mydeque.push_back(4);
200  mydeque.pop_back();
201  mydeque = mydeque2;
202 
203  mydeque.push_front(4);
204  mydeque.pop_front();
205  mydeque = mydeque2;
206 
207  mydeque.insert(mydeque.begin(), 0);
208  mydeque.erase(mydeque.begin());
209  mydeque = mydeque2;
210 
211  mydeque.swap(mydeque2);
212  std::swap(mydeque, mydeque2);
213 
214  mydeque.emplace(mydeque.begin());
215  mydeque.emplace_back();
216  mydeque.emplace_front();
217  mydeque = mydeque2;
218 
219  expect_true(mydeque == mydeque2);
220  expect_false(mydeque != mydeque2);
221  expect_false(mydeque < mydeque2);
222  expect_false(mydeque > mydeque2);
223  expect_true(mydeque <= mydeque2);
224  expect_true(mydeque >= mydeque2);
225  mydeque = mydeque2;
226 
227  mydeque.get_allocator();
228  mydeque.clear();
229 }
230 
231 void
232 exit_handler_intel_x64::unittest_1004_containers_forward_list() const
233 {
234  std::forward_list<int> mylist = {{0, 1, 2, 3}};
235  std::forward_list<int> mylist2 = {{0, 1, 2, 3}};
236 
237  mylist.insert_after(mylist.before_begin(), 10);
238  mylist.erase_after(mylist.before_begin());
239  mylist.insert_after(mylist.cbefore_begin(), 10);
240  mylist.erase_after(mylist.cbefore_begin());
241  mylist = mylist2;
242 
243  auto total = 0;
244  for (auto iter = mylist.begin(); iter != mylist.end(); iter++)
245  total += *iter;
246 
247  auto ctotal = 0;
248  for (auto iter = mylist.cbegin(); iter != mylist.cend(); iter++)
249  ctotal += *iter;
250 
251  expect_true(total == 6);
252  expect_true(ctotal == 6);
253 
254  expect_true(mylist.max_size() >= 4);
255  expect_false(mylist.empty());
256  mylist.resize(4);
257 
258  expect_true(mylist.front() == 0);
259 
260  mylist.assign(4, 0);
261  mylist = mylist2;
262 
263  mylist.push_front(4);
264  mylist.pop_front();
265  mylist = mylist2;
266 
267  mylist.swap(mylist2);
268  std::swap(mylist, mylist2);
269 
270  mylist.emplace_front();
271  mylist.emplace_after(mylist.begin());
272  mylist = mylist2;
273 
274  expect_true(mylist == mylist2);
275  expect_false(mylist != mylist2);
276  expect_false(mylist < mylist2);
277  expect_false(mylist > mylist2);
278  expect_true(mylist <= mylist2);
279  expect_true(mylist >= mylist2);
280  mylist = mylist2;
281 
282  mylist.splice_after(mylist.before_begin(), mylist2);
283  mylist.remove(0);
284  mylist.unique();
285  mylist.merge(mylist2, std::greater<int>());
286  mylist.sort(std::greater<int>());
287  mylist.reverse();
288  mylist = mylist2;
289 
290  mylist.get_allocator();
291  mylist.clear();
292 }
293 
294 void
295 exit_handler_intel_x64::unittest_1005_containers_list() const
296 {
297  std::list<int> mylist = {{0, 1, 2, 3}};
298  std::list<int> mylist2 = {{0, 1, 2, 3}};
299 
300  auto total = 0;
301  for (auto iter = mylist.begin(); iter != mylist.end(); iter++)
302  total += *iter;
303 
304  auto rtotal = 0;
305  for (auto iter = mylist.rbegin(); iter != mylist.rend(); iter++)
306  rtotal += *iter;
307 
308  auto ctotal = 0;
309  for (auto iter = mylist.cbegin(); iter != mylist.cend(); iter++)
310  ctotal += *iter;
311 
312  auto crtotal = 0;
313  for (auto iter = mylist.crbegin(); iter != mylist.crend(); iter++)
314  crtotal += *iter;
315 
316  expect_true(total == 6);
317  expect_true(rtotal == 6);
318  expect_true(ctotal == 6);
319  expect_true(crtotal == 6);
320 
321  expect_true(mylist.size() == 4);
322  expect_true(mylist.max_size() >= 4);
323  expect_false(mylist.empty());
324  mylist.resize(4);
325 
326  expect_true(mylist.front() == 0);
327  expect_true(mylist.back() == 3);
328 
329  mylist.assign(4, 0);
330  mylist = mylist2;
331 
332  mylist.push_back(4);
333  mylist.pop_back();
334  mylist = mylist2;
335 
336  mylist.push_front(4);
337  mylist.pop_front();
338  mylist = mylist2;
339 
340  mylist.insert(mylist.begin(), 0);
341  mylist.erase(mylist.begin());
342  mylist = mylist2;
343 
344  mylist.swap(mylist2);
345  std::swap(mylist, mylist2);
346 
347  mylist.emplace(mylist.begin());
348  mylist.emplace_back();
349  mylist.emplace_front();
350  mylist = mylist2;
351 
352  expect_true(mylist == mylist2);
353  expect_false(mylist != mylist2);
354  expect_false(mylist < mylist2);
355  expect_false(mylist > mylist2);
356  expect_true(mylist <= mylist2);
357  expect_true(mylist >= mylist2);
358  mylist = mylist2;
359 
360  mylist.splice(mylist.begin(), mylist2);
361  mylist.remove(0);
362  mylist.unique();
363  mylist.merge(mylist2, std::greater<int>());
364  mylist.sort(std::greater<int>());
365  mylist.reverse();
366  mylist = mylist2;
367 
368  mylist.get_allocator();
369  mylist.clear();
370 }
371 
372 void
373 exit_handler_intel_x64::unittest_1006_containers_stack() const
374 {
375  std::stack<int> mystack{{0, 1, 2, 3}};
376  std::stack<int> mystack2{{0, 1, 2, 3}};
377 
378  expect_true(mystack.size() == 4);
379  expect_false(mystack.empty());
380 
381  expect_true(mystack.top() == 3);
382 
383  mystack.push(4);
384  mystack.pop();
385 
386  mystack.emplace();
387  mystack.pop();
388 
389  mystack.swap(mystack2);
390  std::swap(mystack, mystack2);
391 
392  expect_true(mystack == mystack2);
393  expect_false(mystack != mystack2);
394  expect_false(mystack < mystack2);
395  expect_false(mystack > mystack2);
396  expect_true(mystack <= mystack2);
397  expect_true(mystack >= mystack2);
398 }
399 
400 void
401 exit_handler_intel_x64::unittest_1007_containers_queue() const
402 {
403  std::queue<int> myqueue{{0, 1, 2, 3}};
404  std::queue<int> myqueue2{{0, 1, 2, 3}};
405 
406  expect_true(myqueue.size() == 4);
407  expect_false(myqueue.empty());
408 
409  expect_true(myqueue.front() == 0);
410  expect_true(myqueue.back() == 3);
411 
412  myqueue.emplace();
413  myqueue.push(1);
414  myqueue.push(2);
415  myqueue.push(3);
416 
417  myqueue.pop();
418  myqueue.pop();
419  myqueue.pop();
420  myqueue.pop();
421 
422  myqueue.swap(myqueue2);
423  std::swap(myqueue, myqueue2);
424 
425  expect_true(myqueue == myqueue2);
426  expect_false(myqueue != myqueue2);
427  expect_false(myqueue < myqueue2);
428  expect_false(myqueue > myqueue2);
429  expect_true(myqueue <= myqueue2);
430  expect_true(myqueue >= myqueue2);
431 }
432 
433 void
434 exit_handler_intel_x64::unittest_1008_containers_priority_queue() const
435 {
436  int myints[] = {0, 1, 2, 3};
437 
438  auto myqueue = std::priority_queue<int>(myints, myints + 4);
439  auto myqueue2 = std::priority_queue<int>(myints, myints + 4);
440 
441  expect_true(myqueue.size() == 4);
442  expect_false(myqueue.empty());
443 
444  expect_true(myqueue.top() == 3);
445 
446  myqueue.emplace();
447  myqueue.push(1);
448  myqueue.push(2);
449  myqueue.push(3);
450 
451  myqueue.pop();
452  myqueue.pop();
453  myqueue.pop();
454  myqueue.pop();
455 
456  myqueue.swap(myqueue2);
457  std::swap(myqueue, myqueue2);
458 }
459 
460 void
461 exit_handler_intel_x64::unittest_1009_containers_set() const
462 {
463  auto myset = std::set<int>({0, 1, 2, 3});
464  auto myset2 = std::set<int>({0, 1, 2, 3});
465 
466  auto total = 0;
467  for (auto iter = myset.begin(); iter != myset.end(); iter++)
468  total += *iter;
469 
470  auto rtotal = 0;
471  for (auto iter = myset.rbegin(); iter != myset.rend(); iter++)
472  rtotal += *iter;
473 
474  auto ctotal = 0;
475  for (auto iter = myset.cbegin(); iter != myset.cend(); iter++)
476  ctotal += *iter;
477 
478  auto crtotal = 0;
479  for (auto iter = myset.crbegin(); iter != myset.crend(); iter++)
480  crtotal += *iter;
481 
482  expect_true(total == 6);
483  expect_true(rtotal == 6);
484  expect_true(ctotal == 6);
485  expect_true(crtotal == 6);
486 
487  expect_true(myset.size() == 4);
488  expect_true(myset.max_size() >= 4);
489  expect_false(myset.empty());
490 
491  myset.insert(myset.begin(), 0);
492  myset.erase(myset.begin());
493  myset = myset2;
494 
495  myset.swap(myset2);
496  myset.swap(myset2);
497 
498  myset.emplace();
499  myset.emplace_hint(myset.begin());
500  myset = myset2;
501 
502  myset.key_comp();
503  myset.value_comp();
504 
505  expect_true(myset.find(0) != myset.end());
506  expect_true(myset.count(0) == 1);
507  expect_true(myset.lower_bound(0) != myset.end());
508  expect_true(myset.upper_bound(0) != myset.end());
509  myset.equal_range(0);
510 
511  myset.get_allocator();
512  myset.clear();
513 }
514 
515 void
516 exit_handler_intel_x64::unittest_100A_containers_map() const
517 {
518  auto mymap = std::map<int, int>();
519  auto mymap2 = std::map<int, int>();
520 
521  mymap2[0] = 0;
522  mymap2[1] = 1;
523  mymap2[2] = 2;
524  mymap2[3] = 3;
525 
526  mymap = mymap2;
527 
528  auto total = 0;
529  for (auto iter = mymap.begin(); iter != mymap.end(); iter++)
530  total += iter->second;
531 
532  auto rtotal = 0;
533  for (auto iter = mymap.rbegin(); iter != mymap.rend(); iter++)
534  rtotal += iter->second;
535 
536  auto ctotal = 0;
537  for (auto iter = mymap.cbegin(); iter != mymap.cend(); iter++)
538  ctotal += iter->second;
539 
540  auto crtotal = 0;
541  for (auto iter = mymap.crbegin(); iter != mymap.crend(); iter++)
542  crtotal += iter->second;
543 
544  expect_true(total == 6);
545  expect_true(rtotal == 6);
546  expect_true(ctotal == 6);
547  expect_true(crtotal == 6);
548 
549  expect_true(mymap.size() == 4);
550  expect_true(mymap.max_size() >= 4);
551  expect_false(mymap.empty());
552 
553  expect_true(mymap.at(0) == 0);
554  expect_true(mymap.at(3) == 3);
555 
556  mymap.insert(std::pair<int, int>(4, 4));
557  mymap.erase(4);
558  mymap = mymap2;
559 
560  mymap.swap(mymap2);
561  mymap.swap(mymap2);
562 
563  mymap.emplace();
564  mymap.emplace_hint(mymap.begin(), std::pair<int, int>(4, 4));
565  mymap = mymap2;
566 
567  mymap.key_comp();
568  mymap.value_comp();
569 
570  expect_true(mymap.find(0) != mymap.end());
571  expect_true(mymap.count(0) == 1);
572  expect_true(mymap.lower_bound(0) != mymap.end());
573  expect_true(mymap.upper_bound(0) != mymap.end());
574  mymap.equal_range(0);
575 
576  mymap.get_allocator();
577  mymap.clear();
578 }
579 
580 #endif
for(auto offset=0UL;offset< size;offset+=x64::page_size)
Definition: map_ptr_x64.cpp:45
#define expect_false(a)
void swap(unique_map_ptr_x64< T > &x, unique_map_ptr_x64< T > &y) noexcept
Definition: map_ptr_x64.h:876
#define expect_true(a)