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.

112 lines
2.0 KiB

4 years ago
4 years ago
4 years ago
  1. #include "test_scaffold.h"
  2. #include "gp/array.hpp"
  3. #include "gp/allocator/buddy.hpp"
  4. #include "gp/allocator/dummy.hpp"
  5. #include <thread>
  6. #include <chrono>
  7. #include <set>
  8. #include <numeric>
  9. #include <iomanip>
  10. #include <fstream>
  11. struct arraysum_test : public test_scaffold {
  12. arraysum_test() {
  13. name = __FILE__ ":1";
  14. }
  15. virtual int run() {
  16. gp::array<uint32_t, 16> test;
  17. for(auto& elem : test)
  18. {
  19. elem = 12;
  20. }
  21. return std::accumulate(test.begin(), test.end(), 0) != 12*test.size();
  22. }
  23. };
  24. append_test dummy_sd45uisd3(new arraysum_test{});
  25. struct optional_test : public test_scaffold {
  26. optional_test() {
  27. name = __FILE__ ":1";
  28. }
  29. virtual int run() {
  30. int res = 0;
  31. {
  32. gp::optional<uint32_t> test;
  33. if(test.has_value())
  34. {
  35. res++;
  36. }
  37. test = 12;
  38. if(test.has_value())
  39. {
  40. if(test.value()!=12)
  41. {
  42. res++;
  43. }
  44. }
  45. else
  46. {
  47. res++;
  48. }
  49. }
  50. {
  51. gp::optional<std::ifstream> test;
  52. if(test.has_value())
  53. {
  54. res++;
  55. }
  56. test = std::ifstream("/proc/cpuinfo");
  57. if(!test.has_value())
  58. {
  59. res++;
  60. }
  61. }
  62. return res;
  63. }
  64. };
  65. append_test dummy_mlyusisd3(new optional_test{});
  66. struct buddy_test : public test_scaffold {
  67. buddy_test() {
  68. name = __FILE__ ":3";
  69. }
  70. gp::array<char, 4096> store;
  71. virtual int run() {
  72. int res = 0;
  73. {
  74. gp::buddy<gp::dummy_allocator, gp::math::msb<uint64_t>(4096)> bud{&*store.begin(), store.size()};
  75. std::set<void*> ptr_set;
  76. for(int i = 0; i < 4096 / 8; i++)
  77. {
  78. void* v = bud.allocate(8);
  79. if(v == nullptr) throw gp::runtime_error("allocation failed");
  80. ptr_set.insert(v);
  81. }
  82. std::cout << ptr_set.size() << "//" << ptr_set.count(nullptr);
  83. if(ptr_set.count(nullptr)!=0 || ptr_set.size()!=(4096/8)) throw gp::runtime_error("some allocations failed");
  84. res++;
  85. res += nullptr == bud.allocate(8);
  86. if(nullptr != bud.allocate(8)) throw gp::runtime_error("allocation succeeded, failure was expected");
  87. ++res;
  88. }
  89. return res;
  90. }
  91. };
  92. append_test dummy_654sisd3(new buddy_test{});