optional.cpp 764 B

123456789101112131415161718192021222324252627282930
  1. #include <iostream>
  2. #include "optional.hpp"
  3. #define FAILED(got, op, expt) {std::cout << __FILE__ << ":" << __LINE__ << ": failed asserting " << got << " " << op << " expected " << expt << std::endl; return false; }
  4. bool simpleTest()
  5. {
  6. Optional<int> test = Optional<int>::of(42);
  7. if (test.absent())
  8. FAILED(test.absent(), "!=", true);
  9. if (test.get() != 42)
  10. FAILED(test.get(), "!=", 42);
  11. if (test.orValue(1) != 42)
  12. FAILED(test.get(), "!=", 42);
  13. test = Optional<int>::empty;
  14. if (!test.absent())
  15. FAILED(test.absent(), "!=", false);
  16. if (test.orValue(0) != 0)
  17. FAILED(test.get(), "!=", 0);
  18. return true;
  19. }
  20. int main()
  21. {
  22. if (!simpleTest())
  23. exit(EXIT_FAILURE);
  24. exit(EXIT_SUCCESS);
  25. }