testwrapping_int.cpp 5.3 KB

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