General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.1 KiB

пре 3 година
пре 4 година
пре 3 година
  1. #include "gp/utils/pair.hpp"
  2. #include "gp/algorithms/min_max.hpp"
  3. #include "test_scaffold.h"
  4. #include <random>
  5. #include <string>
  6. typedef std::mt19937_64 cheap_rand;
  7. struct pair_test : public test_scaffold {
  8. uint32_t seed;
  9. pair_test() {
  10. seed = std::random_device{}();
  11. name = __FILE__ ":1_seed";
  12. name += std::to_string(seed);
  13. }
  14. virtual int run() {
  15. cheap_rand setter(seed);
  16. gp::pair<double, std::string> v{0, "zero"};
  17. bool result = true;
  18. for(int i = 0 ; i < 100; i++)
  19. {
  20. auto a = setter();
  21. v = gp::pair(a, std::to_string(a));
  22. result = gp::pair<double, std::string>(a, std::to_string(a)) == v ? result : false;
  23. }
  24. return !result;
  25. }
  26. };
  27. append_test dummy_rsly21r43(new pair_test{});
  28. template<typename T>
  29. struct sort_pair_test : public test_scaffold {
  30. uint32_t seed;
  31. sort_pair_test() {
  32. seed = std::random_device{}();
  33. name = __FILE__ ":2_sort_pair";
  34. name += std::to_string(seed);
  35. }
  36. virtual int run() {
  37. // TODO: Verify things, like, for real
  38. cheap_rand setter(seed);
  39. bool result = true;
  40. std::uniform_real_distribution<T> dist{-1.0, 1.0};
  41. for(int i = 0 ; i < 10000; i++)
  42. {
  43. gp::pair<T, T> v{dist(setter), dist(setter)};
  44. gp::pair<T, T>
  45. ascending{gp::min(v.first, v.second), gp::max(v.first, v.second)},
  46. descending{gp::max(v.first, v.second), gp::min(v.first, v.second)},
  47. clamped{gp::clamp<float>(0.25,v.first, 0.5), gp::min(0.5, v.first, v.second)},
  48. minmaxed{gp::min<T>(dist(setter), dist(setter), dist(setter)), gp::max<T>(dist(setter), dist(setter), dist(setter))},
  49. rngclamp{gp::clamp<T>(dist(setter),dist(setter), dist(setter)), gp::clamp<T>(dist(setter), dist(setter), dist(setter))};
  50. result = ascending.first == descending.second ? result : false;
  51. }
  52. return !result;
  53. }
  54. };
  55. append_test dummy_5qsd5r43(new sort_pair_test<float>{});
  56. append_test dummy_4365xv43(new sort_pair_test<double>{});
  57. template<typename T>
  58. struct sort_pair_test2 : public test_scaffold {
  59. uint32_t seed;
  60. sort_pair_test2() {
  61. seed = std::random_device{}();
  62. name = __FILE__ ":3_sort_pair2";
  63. name += std::to_string(seed);
  64. }
  65. virtual int run() {
  66. // TODO: Verify things, like, for real
  67. cheap_rand setter(seed);
  68. bool result = true;
  69. std::uniform_int_distribution<T> dist{0, 4096};
  70. for(int i = 0 ; i < 10000; i++)
  71. {
  72. gp::pair<T, T> v{dist(setter), dist(setter)};
  73. gp::pair<T, T>
  74. ascending{gp::min<T>(v.first, v.second), gp::max<T>(v.first, v.second)},
  75. descending{gp::max<T>(v.first, v.second), gp::min<T>(v.first, v.second)},
  76. clamped{gp::clamp<T>(512,v.first, 1024), gp::min(1024, v.first, v.second)},
  77. minmaxed{gp::min<T>(dist(setter),dist(setter), dist(setter)), gp::max<T>(dist(setter), dist(setter), dist(setter))},
  78. rngclamp{gp::clamp<T>(dist(setter),dist(setter), dist(setter)), gp::clamp<T>(dist(setter), dist(setter), dist(setter))};
  79. result = ascending.first == descending.second ? result : false;
  80. }
  81. return !result;
  82. }
  83. };
  84. append_test dummy_5zegfh43(new sort_pair_test2<int>{});
  85. append_test dummy_sdghtfh4(new sort_pair_test2<unsigned long>{});
  86. append_test dummy_judlg8gb(new sort_pair_test2<long>{});
  87. append_test dummy_5egkzbcd(new sort_pair_test2<long long>{});