1 | /* Copyright (c) 2002,2003,2005 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, Bart Garst |
6 | */ |
7 | |
8 | #include "boost/date_time/wrapping_int.hpp" |
9 | //#define BOOST_INCLUDE_MAIN |
10 | //#include <boost/test/test_tools.hpp> |
11 | #include "testfrmwk.hpp" |
12 | #include "boost/cstdint.hpp" |
13 | #include <iostream> |
14 | |
15 | |
16 | int |
17 | main() |
18 | // int |
19 | // test_main(int, char*[]) |
20 | { |
21 | using namespace boost::date_time; |
22 | |
23 | wrapping_int<int, 3600> wi(3599); |
24 | check(testname: "construction/conversion" , testcond: wi == 3599); |
25 | check(testname: "add with wrap" , testcond: wi.add(v: 1) == 1); |
26 | check(testname: "added value ok" , testcond: wi == 0); |
27 | check(testname: "add with 2 wraps" , testcond: wi.add(v: 7201) == 2); |
28 | check(testname: "added value ok" , testcond: wi == 1); |
29 | check(testname: "add with 3 wraps" , testcond: wi.add(v: 10800) == 3); |
30 | check(testname: "added value ok" , testcond: wi == 1); |
31 | check(testname: "subtract no wrap" , testcond: wi.subtract(v: 1) == 0); |
32 | check(testname: "subtract val ok" , testcond: wi == 0); |
33 | check(testname: "subtract no wrap" , testcond: wi.subtract(v: 3601) == 2); |
34 | check(testname: "subtract val ok" , testcond: wi == 3599); |
35 | check(testname: "add again" , testcond: (wi.add(v: 2) == 1) && (wi == 1)); |
36 | check(testname: "subtract again" , testcond: (wi.subtract(v: 2) == 1) && (wi == 3599)); |
37 | check(testname: "add again" , testcond: (wi.add(v: 2) == 1) && (wi == 1)); |
38 | check(testname: "subtract again" , testcond: (wi.subtract(v: 3600) == 1) && (wi == 1)); |
39 | check(testname: "subtract again" , testcond: (wi.subtract(v: 3599) == 1) && (wi == 2)); |
40 | check(testname: "subtract again" , testcond: (wi.subtract(v: 1) == 0) && (wi == 1)); |
41 | std::cout << wi << std::endl; |
42 | |
43 | wrapping_int<short, 60> wi2(0); |
44 | check(testname: "add with wrap - return" , testcond: wi2.add(v: 121) == 2); |
45 | check(testname: "add with wrap - value" , testcond: wi2 == 1); |
46 | |
47 | wrapping_int<short, 60> wi3(-5); |
48 | check(testname: "signed int - add return" , testcond: wi3.add(v: 5) == 0); |
49 | check(testname: "signed int - value" , testcond: wi3 == 0); |
50 | |
51 | { // subtracting negative values |
52 | wrapping_int<short, 10> wi4(5); |
53 | check(testname: "subtract negative value to cause wrap" , |
54 | testcond: (wi4.subtract(v: -8) == -1 && wi4 == 3)); |
55 | check(testname: "reset" , testcond: wi4.add(v: 2) == 0 && wi4 ==5); |
56 | check(testname: "add negative value to cause wrap" , |
57 | testcond: (wi4.add(v: -8) == -1 && wi4 == 7)); |
58 | } |
59 | |
60 | wrapping_int2<short, 1, 5> wi4(1); |
61 | check(testname: "construct" , testcond: wi4 == 1); |
62 | check(testname: "add up to wrap value" , testcond: (wi4.add(v: 4) == 0 && wi4 == 5)); |
63 | check(testname: "add over the wrap value" , testcond: (wi4.add(v: 1) == 1 && wi4 == 1)); |
64 | check(testname: "add over the wrap value X 2" , testcond: (wi4.add(v: 10) == 2 && wi4 == 1)); |
65 | check(testname: "add over the wrap value X 3" , testcond: (wi4.add(v: 15) == 3 && wi4 == 1)); |
66 | |
67 | wrapping_int2<short, 1, 12> wi5(12); |
68 | check(testname: "construct" , testcond: wi5 == 12); |
69 | check(testname: "add over the wrap value" , testcond: (wi5.add(v: 1) == 1 && wi5 == 1)); |
70 | |
71 | check(testname: "subtract of the wrap value" , testcond: (wi5.subtract(v: 1) == -1 && wi5 == 12)); |
72 | check(testname: "subtract of the wrap value" , testcond: (wi5.subtract(v: 13) == -1 && wi5 == 11)); |
73 | |
74 | // min_values other than 1 |
75 | wrapping_int2<short, 2, 6> wi6(2); |
76 | check(testname: "construct" , testcond: wi6 == 2); |
77 | check(testname: "add up to wrap value" , testcond: (wi6.add(v: 4) == 0 && wi6 == 6)); |
78 | check(testname: "add over the wrap value" , testcond: (wi6.add(v: 1) == 1 && wi6 == 2)); |
79 | check(testname: "add over the wrap value X 2" , testcond: wi6.add(v: 11) == 2); |
80 | check(testname: "add over the wrap value X 2" , testcond: wi6 == 3); |
81 | check(testname: "sub down to wrap value" , testcond: wi6.subtract(v: 1) == 0 && wi6 == 2); |
82 | check(testname: "sub under the wrap value" , testcond: wi6.subtract(v: 1) == -1 && wi6 == 6); |
83 | check(testname: "sub under the wrap value X 2" , testcond: wi6.subtract(v: 11) == -2 && wi6 == 5); |
84 | //std::cout << wi6 << std::endl; |
85 | |
86 | // adding & subtracting negative values |
87 | wrapping_int2<short, 1, 12> wi7(6); |
88 | wrapping_int2<short, -5, 5> wi8(0); |
89 | check(testname: "add negative value" , testcond: (wi7.add(v: -2) == 0 && wi7 == 4)); |
90 | check(testname: "add negative value" , testcond: (wi8.add(v: -2) == 0 && wi8 == -2)); |
91 | check(testname: "add negative value to cause single wrap" , |
92 | testcond: (wi7.add(v: -6) == -1 && wi7 == 10)); |
93 | check(testname: "add negative value to cause single wrap" , |
94 | testcond: (wi8.add(v: -5) == -1 && wi8 == 4)); |
95 | check(testname: "add negative value to cause multiple wrap" , |
96 | testcond: (wi7.add(v: -22) == -2 && wi7 == 12)); |
97 | check(testname: "add negative value to cause multiple wrap" , |
98 | testcond: (wi8.add(v: -22) == -2 && wi8 == 4)); |
99 | // reset values to mid range |
100 | wi7.subtract(v: 6); |
101 | check(testname: "reset" , testcond: wi7 == 6); |
102 | wi8.subtract(v: 4); |
103 | check(testname: "reset" , testcond: wi8 == 0); |
104 | check(testname: "subtract negative value" , testcond: (wi7.subtract(v: -2) == 0 && wi7 == 8)); |
105 | check(testname: "subtract negative value" , testcond: (wi8.subtract(v: -2) == 0 && wi8 == 2)); |
106 | check(testname: "subtract negative value to cause single wrap" , |
107 | testcond: (wi7.subtract(v: -6) == 1 && wi7 == 2)); |
108 | check(testname: "subtract negative value to cause single wrap" , |
109 | testcond: (wi8.subtract(v: -5) == 1 && wi8 == -4)); |
110 | check(testname: "subtract negative value to cause multiple wrap" , |
111 | testcond: (wi7.subtract(v: -23) == 2 && wi7 == 1)); |
112 | check(testname: "subtract negative value to cause multiple wrap" , |
113 | testcond: (wi8.subtract(v: -22) == 2 && wi8 == -4)); |
114 | |
115 | // #ifdef BOOST_HAS_LONG_LONG |
116 | // wrapping_int<boost::int64_t, 86400*100000LL> wi4(0); |
117 | // check("construction/conversion", wi4 == 0); |
118 | // boost::int64_t off2 = 372300000; |
119 | // boost::int64_t wrap = 86400LL*100000LL; |
120 | // boost::int64_t wrap2 = 86400000000; |
121 | // wrapping_int<boost::int64_t,86400000000LL> wi5((3600*1 + 60*2 + 3)*100000); |
122 | // std::cout << wi5 << std::endl; |
123 | // boost::int64_t over = wi4.add(off2); |
124 | // std::cout << over << std::endl; |
125 | // std::cout << wrap << std::endl; |
126 | // std::cout << wrap2 << std::endl; |
127 | // // check("construction/conversion", wi4 == 0); |
128 | // #endif |
129 | |
130 | // wrapping_int<int, 60> wi(121); |
131 | // check("construction/conversion", wi == 121); |
132 | // check("add with wrap", wi.add(1) == 1); |
133 | |
134 | return printTestStats(); |
135 | |
136 | } |
137 | |
138 | |
139 | |