]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // (C) Copyright Ion Gaztanaga 2015-2016. | |
4 | // Distributed under the Boost Software License, Version 1.0. | |
5 | // (See accompanying file LICENSE_1_0.txt or copy at | |
6 | // http://www.boost.org/LICENSE_1_0.txt) | |
7 | // | |
8 | // See http://www.boost.org/libs/move for documentation. | |
9 | // | |
10 | ////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include <cstdlib> //std::srand | |
13 | #include <algorithm> //std::next_permutation | |
14 | #include <iostream> //std::cout | |
15 | ||
16 | #include <boost/config.hpp> | |
17 | ||
18 | #include <boost/move/unique_ptr.hpp> | |
19 | #include <boost/container/vector.hpp> | |
20 | #include <boost/timer/timer.hpp> | |
21 | ||
22 | using boost::timer::cpu_timer; | |
23 | using boost::timer::cpu_times; | |
24 | using boost::timer::nanosecond_type; | |
25 | ||
26 | #include "order_type.hpp" | |
27 | ||
28 | #include <boost/move/algo/adaptive_merge.hpp> | |
29 | #include <boost/move/core.hpp> | |
30 | ||
31 | ||
32 | template<class T> | |
33 | bool test_random_shuffled(std::size_t const element_count, std::size_t const num_keys, std::size_t const num_iter) | |
34 | { | |
35 | boost::movelib::unique_ptr<T[]> elements(new T[element_count]); | |
36 | boost::movelib::unique_ptr<std::size_t[]> key_reps(new std::size_t[num_keys ? num_keys : element_count]); | |
37 | std::cout << "- - N: " << element_count << ", Keys: " << num_keys << ", It: " << num_iter << " \n"; | |
38 | ||
39 | //Initialize keys | |
40 | for(std::size_t i=0; i < element_count; ++i){ | |
41 | std::size_t key = num_keys ? (i % num_keys) : i; | |
42 | elements[i].key=key; | |
43 | } | |
44 | ||
45 | std::srand(0); | |
46 | ||
47 | for (std::size_t i = 0; i != num_iter; ++i) | |
48 | { | |
49 | std::random_shuffle(elements.get(), elements.get() + element_count); | |
50 | for(std::size_t i = 0; i < (num_keys ? num_keys : element_count); ++i){ | |
51 | key_reps[i]=0; | |
52 | } | |
53 | for(std::size_t i = 0; i < element_count; ++i){ | |
54 | elements[i].val = key_reps[elements[i].key]++; | |
55 | } | |
56 | ||
57 | boost::container::vector<order_type> tmp(elements.get(), elements.get()+element_count); | |
58 | std::size_t const split = std::size_t(std::rand()) % element_count; | |
59 | std::stable_sort(tmp.data(), tmp.data()+split, order_type_less<order_type>()); | |
60 | std::stable_sort(tmp.data()+split, tmp.data()+element_count, order_type_less<order_type>()); | |
61 | ||
62 | boost::movelib::adaptive_merge(tmp.data(), tmp.data()+split, tmp.data()+element_count, order_type_less<order_type>()); | |
63 | ||
64 | if (!is_order_type_ordered(tmp.data(), element_count)) | |
65 | { | |
66 | std::cout << "\n ERROR\n"; | |
67 | throw int(0); | |
68 | } | |
69 | } | |
70 | return true; | |
71 | } | |
72 | ||
73 | int main() | |
74 | { | |
75 | #ifdef NDEBUG | |
76 | const std::size_t NIter = 100; | |
77 | #else | |
78 | const std::size_t NIter = 10; | |
79 | #endif | |
80 | test_random_shuffled<order_type>(10001, 65, NIter); | |
81 | test_random_shuffled<order_type>(10001, 101, NIter); | |
82 | test_random_shuffled<order_type>(10001, 1023, NIter); | |
83 | test_random_shuffled<order_type>(10001, 4095, NIter); | |
84 | test_random_shuffled<order_type>(10001, 0, NIter); | |
85 | ||
86 | return 0; | |
87 | } |