General Purpose library for Freestanding C++ and POSIX systems
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

85 рядки
2.1 KiB

  1. #include "gp/containers/array.hpp"
  2. #include "gp/containers/vector.hpp"
  3. #include "gp/algorithms/partition.hpp"
  4. #include "test_scaffold.h"
  5. #include "allocator.hpp"
  6. #include <memory>
  7. #include <random>
  8. #include <iostream>
  9. #include <string>
  10. #include <algorithm>
  11. typedef std::mt19937_64 cheap_rand;
  12. struct partition_array_test : public test_scaffold {
  13. uint32_t seed;
  14. partition_array_test() {
  15. name = __FILE__ ":1_seed";
  16. seed = std::random_device{}();
  17. name += std::to_string(seed);
  18. }
  19. virtual int run() {
  20. cheap_rand setter(seed);
  21. for(int i = 0; i < 250; ++i) {
  22. gp::array<uint32_t, 200> ary;
  23. for(auto& elem : ary) {
  24. elem = setter();
  25. }
  26. auto pivot = gp::hoare_partition(ary.begin(), ary.end(), [](const auto& v){return v % 2;});
  27. for(auto it = ary.begin(); it != pivot;++it) {
  28. gp_config::assertion(!!(*it % 2), "partition failed");
  29. }
  30. for(auto it = pivot; it != ary.end();++it) {
  31. gp_config::assertion(!(*it % 2), "partition failed");
  32. }
  33. }
  34. return 0;
  35. }
  36. };
  37. append_test dummy_s5f468e43(new partition_array_test{});
  38. struct partition_array_lomuto_test : public test_scaffold {
  39. uint32_t seed;
  40. partition_array_lomuto_test() {
  41. name = __FILE__ ":2_seed";
  42. seed = std::random_device{}();
  43. name += std::to_string(seed);
  44. }
  45. virtual int run() {
  46. cheap_rand setter(seed);
  47. for(int i = 0; i < 250; ++i) {
  48. gp::array<uint32_t, 200> ary;
  49. for(auto& elem : ary) {
  50. elem = setter();
  51. }
  52. auto pivot = gp::lomuto_partition(ary.begin(), ary.end(), [](const auto& v){return v % 2;});
  53. for(auto it = ary.begin(); it != pivot;++it) {
  54. gp_config::assertion(!!(*it % 2), "partition failed");
  55. }
  56. for(auto it = pivot; it != ary.end();++it) {
  57. gp_config::assertion(!(*it % 2), "partition failed");
  58. }
  59. }
  60. return 0;
  61. }
  62. };
  63. append_test dummy_lcvudg8e43(new partition_array_lomuto_test{});