| 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 or http://www.boost.org/LICENSE-1.0) |
|---|
| 5 | * Author: Jeff Garland |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | #include "boost/config.hpp" |
|---|
| 9 | #include "boost/date_time/constrained_value.hpp" |
|---|
| 10 | #include "boost/date_time/testfrmwk.hpp" |
|---|
| 11 | #include <iostream> |
|---|
| 12 | |
|---|
| 13 | class bad_day {}; //exception type |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | class day_value_policies |
|---|
| 17 | { |
|---|
| 18 | public: |
|---|
| 19 | typedef unsigned int value_type; |
|---|
| 20 | static unsigned int min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }; |
|---|
| 21 | static unsigned int max BOOST_PREVENT_MACRO_SUBSTITUTION () { return 31;}; |
|---|
| 22 | static void on_error(unsigned int&, unsigned int, boost::CV::violation_enum) |
|---|
| 23 | { |
|---|
| 24 | throw bad_day(); |
|---|
| 25 | } |
|---|
| 26 | }; |
|---|
| 27 | |
|---|
| 28 | struct range_error {}; //exception type |
|---|
| 29 | typedef boost::CV::simple_exception_policy<int,1,10,range_error> one_to_ten_range_policy; |
|---|
| 30 | |
|---|
| 31 | int main() |
|---|
| 32 | { |
|---|
| 33 | using namespace boost::CV; |
|---|
| 34 | constrained_value<day_value_policies> cv1(0), cv2(31); |
|---|
| 35 | check("not equal", cv1 != cv2); |
|---|
| 36 | check("equal", cv1 == cv1); |
|---|
| 37 | check("greater", cv2 > cv1); |
|---|
| 38 | check("greater or equal ", cv2 >= cv1); |
|---|
| 39 | //various running of the conversion operator |
|---|
| 40 | std::cout << cv1 << std::endl; |
|---|
| 41 | unsigned int i = cv1; |
|---|
| 42 | check("test conversion", i == cv1); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | try { |
|---|
| 46 | constrained_value<one_to_ten_range_policy> cv2(11); |
|---|
| 47 | std::cout << "Not Reachable: " << cv2 << " "; |
|---|
| 48 | check("got range exception max", false); |
|---|
| 49 | } |
|---|
| 50 | catch(range_error& e) { |
|---|
| 51 | e = e; // removes compiler warning |
|---|
| 52 | check("got range exception max", true); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | try { |
|---|
| 56 | constrained_value<one_to_ten_range_policy> cv3(0); |
|---|
| 57 | std::cout << "Not Reachable: " << cv3 << " "; |
|---|
| 58 | check("got range exception min", false); |
|---|
| 59 | } |
|---|
| 60 | catch(range_error& e) { |
|---|
| 61 | e = e; // removes compiler warning |
|---|
| 62 | check("got range exception min", true); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | try { |
|---|
| 66 | constrained_value<one_to_ten_range_policy> cv4(1); |
|---|
| 67 | cv4 = 12; |
|---|
| 68 | check("range exception on assign", false); |
|---|
| 69 | } |
|---|
| 70 | catch(range_error& e) { |
|---|
| 71 | e = e; // removes compiler warning |
|---|
| 72 | check("range exception on assign", true); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | return printTestStats(); |
|---|
| 76 | } |
|---|
| 77 | |
|---|