adaptive_sort_test.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include <cstdlib> //std::srand
  12. #include <iostream> //std::cout
  13. #include <boost/config.hpp>
  14. #include <boost/move/unique_ptr.hpp>
  15. #include <boost/container/vector.hpp>
  16. #include "order_type.hpp"
  17. #include "random_shuffle.hpp"
  18. #include <boost/move/algo/adaptive_sort.hpp>
  19. #include <boost/move/core.hpp>
  20. template<class T>
  21. bool test_random_shuffled(std::size_t const element_count, std::size_t const num_keys, std::size_t const num_iter)
  22. {
  23. boost::movelib::unique_ptr<T[]> elements(new T[element_count]);
  24. boost::movelib::unique_ptr<std::size_t[]> key_reps(new std::size_t[num_keys ? num_keys : element_count]);
  25. std::cout << "- - N: " << element_count << ", Keys: " << num_keys << ", It: " << num_iter << " \n";
  26. //Initialize keys
  27. for(std::size_t i=0; i < element_count; ++i){
  28. std::size_t key = num_keys ? (i % num_keys) : i;
  29. elements[i].key=key;
  30. }
  31. std::srand(0);
  32. for (std::size_t it = 0; it != num_iter; ++it)
  33. {
  34. ::random_shuffle(elements.get(), elements.get() + element_count);
  35. for(std::size_t i = 0; i < (num_keys ? num_keys : element_count); ++i){
  36. key_reps[i]=0;
  37. }
  38. for(std::size_t i = 0; i < element_count; ++i){
  39. elements[i].val = key_reps[elements[i].key]++;
  40. }
  41. boost::movelib::adaptive_sort(elements.get(), elements.get()+element_count, order_type_less());
  42. if (!is_order_type_ordered(elements.get(), element_count))
  43. {
  44. std::cout << "\n ERROR\n";
  45. throw int(0);
  46. }
  47. }
  48. return true;
  49. }
  50. void instantiate_smalldiff_iterators()
  51. {
  52. typedef randit<int, short> short_rand_it_t;
  53. boost::movelib::adaptive_sort(short_rand_it_t(), short_rand_it_t(), less_int());
  54. typedef randit<int, signed char> schar_rand_it_t;
  55. boost::movelib::adaptive_sort(schar_rand_it_t(), schar_rand_it_t(), less_int());
  56. }
  57. int main()
  58. {
  59. instantiate_smalldiff_iterators();
  60. const std::size_t NIter = 100;
  61. test_random_shuffled<order_move_type>(10001, 3, NIter);
  62. test_random_shuffled<order_move_type>(10001, 65, NIter);
  63. test_random_shuffled<order_move_type>(10001, 101, NIter);
  64. test_random_shuffled<order_move_type>(10001, 1023, NIter);
  65. test_random_shuffled<order_move_type>(10001, 4095, NIter);
  66. test_random_shuffled<order_move_type>(10001, 0, NIter);
  67. return 0;
  68. }