midpoint.cpp 910 B

12345678910111213141516171819202122232425262728293031323334353637
  1. //#define CXXOMFORT_NOTICES 2
  2. #include <cxxomfort/base.hpp>
  3. #include <cxxomfort/numeric.hpp>
  4. #include <cxxomfort/cstdint.hpp>
  5. #include <limits>
  6. #include <string>
  7. #include <iostream>
  8. #include <cassert>
  9. void on_pointers (int i, int j) {
  10. using namespace std;
  11. char const* text = "0123456789";
  12. char const* p = text + i;
  13. char const* q = text + j;
  14. char const* r = midpoint(p,q);
  15. cout << "std::midpoint('" << *p << "', '" << *q << "'): '"
  16. << *r << "'\n";
  17. };
  18. int main() {
  19. using namespace std;
  20. uint32_t a = numeric_limits<uint32_t>::max();
  21. uint32_t b = numeric_limits<uint32_t>::max() - 2;
  22. cout<< "a: " << a << '\n'
  23. << "b: " << b << '\n'
  24. << "Incorrect (overflow and wrapping): " << (a + b) / 2 << '\n'
  25. << "Correct: " << midpoint(a, b) << "\n\n";
  26. on_pointers(2, 4);
  27. on_pointers(2, 5);
  28. on_pointers(5, 2);
  29. on_pointers(2, 6);
  30. }