testconstrained_value.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
  2. * Use, modification and distribution is subject to the
  3. * Boost Software License, Version 1.0. (See accompanying
  4. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  5. * Author: Jeff Garland
  6. */
  7. #include "boost/config.hpp"
  8. #include "boost/date_time/constrained_value.hpp"
  9. #include "testfrmwk.hpp"
  10. #include <iostream>
  11. class bad_day {}; //exception type
  12. class day_value_policies
  13. {
  14. public:
  15. typedef unsigned int value_type;
  16. static unsigned int min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; };
  17. static unsigned int max BOOST_PREVENT_MACRO_SUBSTITUTION () { return 31;};
  18. static void on_error(unsigned int&, unsigned int, boost::CV::violation_enum)
  19. {
  20. throw bad_day();
  21. }
  22. };
  23. struct range_error {}; //exception type
  24. typedef boost::CV::simple_exception_policy<int,1,10,range_error> one_to_ten_range_policy;
  25. int main()
  26. {
  27. using namespace boost::CV;
  28. constrained_value<day_value_policies> cv1(0), cv2(31);
  29. check("not equal", cv1 != cv2);
  30. check("equal", cv1 == cv1);
  31. check("greater", cv2 > cv1);
  32. check("greater or equal ", cv2 >= cv1);
  33. //various running of the conversion operator
  34. std::cout << cv1 << std::endl;
  35. unsigned int i = cv1;
  36. check("test conversion", i == cv1);
  37. try {
  38. constrained_value<one_to_ten_range_policy> cv3(11);
  39. std::cout << "Not Reachable: " << cv3 << " ";
  40. check("got range exception max", false);
  41. }
  42. catch(range_error& e) {
  43. e = e; // removes compiler warning
  44. check("got range exception max", true);
  45. }
  46. try {
  47. constrained_value<one_to_ten_range_policy> cv3(0);
  48. std::cout << "Not Reachable: " << cv3 << " ";
  49. check("got range exception min", false);
  50. }
  51. catch(range_error& e) {
  52. e = e; // removes compiler warning
  53. check("got range exception min", true);
  54. }
  55. try {
  56. constrained_value<one_to_ten_range_policy> cv4(1);
  57. cv4 = 12;
  58. check("range exception on assign", false);
  59. }
  60. catch(range_error& e) {
  61. e = e; // removes compiler warning
  62. check("range exception on assign", true);
  63. }
  64. return printTestStats();
  65. }