byte_iterator.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "simple/support/iterator/byte.hpp"
  2. #include <cassert>
  3. #include <array>
  4. #include <algorithm>
  5. #include <numeric>
  6. using namespace simple::support;
  7. void ProxySemantics()
  8. {
  9. // using ici = std::tuple<int, char, int>; // not trivially copyable -_-
  10. struct ici {
  11. int i;
  12. char c;
  13. int i2;
  14. bool operator==(ici other) { return i == other.i && c == other.c && i2 == other.i2; }
  15. bool operator!=(ici other) { return not ((*this) == other); }
  16. void swap(ici& other) { std::swap(i, other.i); std::swap(c, other.c); std::swap(i2, other.i2); }
  17. };
  18. ici x{};
  19. byte_iterator<ici> b{reinterpret_cast<std::byte*>(&x)};
  20. *b = ici{1,2,3};
  21. assert((x == ici{1,2,3}));
  22. ici y{2,3,4};
  23. b->swap(y);
  24. assert((x == ici{2,3,4}));
  25. assert((y == ici{1,2,3}));
  26. byte_iterator<ici> bb{reinterpret_cast<std::byte*>(&y)};
  27. *b = *bb;
  28. assert((x == ici{1,2,3}));
  29. assert((y == x));
  30. }
  31. void BasicAlgos()
  32. {
  33. std::array<std::byte, sizeof(int) * 13> arr{};
  34. byte_iterator<int> begin{arr.data()};
  35. byte_iterator<int> end{arr.data() + arr.size()};
  36. std::iota(begin, end, 1);
  37. std::array iota {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
  38. assert(( std::equal(begin, end, iota.begin(), iota.end()) ));
  39. std::transform(begin, end, begin, [](int x) { return x * 5; });
  40. std::array fivota {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65};
  41. assert(( std::equal(begin, end, fivota.begin(), fivota.end()) ));
  42. }
  43. int main()
  44. {
  45. ProxySemantics();
  46. BasicAlgos();
  47. return 0;
  48. }