testint_adapter.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, Bart Garst
  6. */
  7. #include "boost/date_time/int_adapter.hpp"
  8. #include "testfrmwk.hpp"
  9. #include "boost/cstdint.hpp"
  10. #include <iostream>
  11. #include <sstream>
  12. template<typename int_type>
  13. void print()
  14. {
  15. //MSVC 6 has problems with this, but it's not really important
  16. //so we will just skip them....
  17. #if (defined(BOOST_DATE_TIME_NO_LOCALE)) || (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  18. #else
  19. std::cout << "min: " << (int_type::min)().as_number() << std::endl;
  20. std::cout << "max: " << (int_type::max)().as_number() << std::endl;
  21. std::cout << "neg_infin: " <<
  22. int_type::neg_infinity().as_number() << std::endl;
  23. std::cout << "pos_infin: " <<
  24. int_type::pos_infinity().as_number() << std::endl;
  25. std::cout << "not a number: " <<
  26. int_type::not_a_number().as_number() << std::endl;
  27. std::stringstream ss("");
  28. std::string s("");
  29. int_type i = int_type::neg_infinity();
  30. ss << i;
  31. s = "-infinity";
  32. check("streaming -infinity", ss.str() == s);
  33. i = int_type::pos_infinity();
  34. ss.str("");
  35. ss << i;
  36. s = "+infinity";
  37. check("streaming +infinity", ss.str() == s);
  38. i = int_type::not_a_number();
  39. ss.str("");
  40. ss << i;
  41. s = "not-a-number";
  42. check("streaming nan", ss.str() == s);
  43. i = 12;
  44. ss.str("");
  45. ss << i;
  46. s = "12";
  47. check("streaming digits", ss.str() == s);
  48. #endif
  49. }
  50. template<typename int_type>
  51. void test_int()
  52. {
  53. int_type i = int_type::neg_infinity();
  54. check("is infinity", i.is_infinity());
  55. check("is special_value (neg_inf)", i.is_special());
  56. check("as_special convert", boost::date_time::neg_infin == i.as_special() );
  57. check("as_special convert", boost::date_time::neg_infin == int_type::to_special(i.as_number()) );
  58. int_type h = int_type::neg_infinity();
  59. i = int_type::pos_infinity();
  60. check("is infinity", i.is_infinity());
  61. check("as_special convert", boost::date_time::pos_infin == i.as_special() );
  62. check("infinity less", h < i);
  63. check("infinity less", h < 0);
  64. check("infinity greater", i > h);
  65. check("infinity greater", i > 0);
  66. h = int_type::not_a_number();
  67. check("nan less", !(h < 0));
  68. check("nan greater", !(h > 0));
  69. check("nan equal", h == int_type::not_a_number());
  70. i = 1;
  71. check("is infinity", !i.is_infinity());
  72. int_type j = int_type::neg_infinity();
  73. check("infinity less", j < i);
  74. check("infinity less", !(j < j));
  75. check("infinity greater", (i > j));
  76. check("infinity equal", !(j == i));
  77. check("infinity equal", j == j);
  78. check("infinity equal", !(j == 0));
  79. check("infinity not equal", j != 0);
  80. int_type k = 1;
  81. check("as_special convert", boost::date_time::not_special == k.as_special() );
  82. check("equal", i == k);
  83. check("infinity not equal", i != int_type::neg_infinity());
  84. check("infinity not equal", i != int_type::pos_infinity());
  85. int_type l = i + int_type::pos_infinity();
  86. check("is special_value (pos_inf)", l.is_special());
  87. check("add infinity" , l == int_type::pos_infinity());
  88. { // limiting the scope for these tests was easier than recalculating l
  89. int_type m = i - int_type::pos_infinity();
  90. check("value - +infinity", m == int_type::neg_infinity());
  91. m = i + int_type::neg_infinity();
  92. check("value + -infinity", m == int_type::neg_infinity());
  93. }
  94. check("inf - inf = nan", (l - l) == int_type::not_a_number());
  95. check("-inf + inf = nan", (j + l) == int_type::not_a_number());
  96. check("add 2", (i + 2) == 3);
  97. i = int_type::not_a_number();
  98. check("+inf * integer", (l * 2) == l);
  99. check("+inf / integer", (l / 2) == l);
  100. check("+inf % -integer", (l % -2) == j);
  101. check("+inf % integer", (l % 2) == l);
  102. check("+inf / -integer", (l / -2) == j);
  103. check("+inf * -integer", (l * -2) == j);
  104. check("+inf * -inf", (l * j) == j);
  105. check("+inf / +inf", (l / l) == i);
  106. check("+inf % +inf", (l % l) == i);
  107. check("+inf * zero", (l * 0) == i);
  108. check("is special_value (nan)", i.is_special());
  109. check("as_special convert", boost::date_time::not_a_date_time == i.as_special() );
  110. check("add not a number", (i + 2) == int_type::not_a_number());
  111. check("sub not a number", (i - 2) == int_type::not_a_number());
  112. check("sub from infin", (l - 2) == int_type::pos_infinity());
  113. i = 5;
  114. h = 3;
  115. check("add zero ", (i + 0) == 5);
  116. check("sub from 5-2 ", (i - 2) == 3);
  117. check("remainder from 5/2 ", (i % 2) == 1);
  118. check("remainder from 5/3 ", (i % h) == 2);
  119. // std::cout << i.as_number() << std::endl;
  120. check("from special ",
  121. int_type::from_special(boost::date_time::pos_infin) == int_type::pos_infinity());
  122. check("from special ",
  123. int_type::from_special(boost::date_time::neg_infin) == int_type::neg_infinity());
  124. check("from special ",
  125. int_type::from_special(boost::date_time::not_a_date_time) == int_type::not_a_number());
  126. check("from special ",
  127. int_type::from_special(boost::date_time::min_date_time) == (int_type::min)());
  128. check("from special ",
  129. int_type::from_special(boost::date_time::max_date_time) == (int_type::max)());
  130. }
  131. int
  132. main()
  133. {
  134. using namespace boost::date_time;
  135. print< int_adapter<unsigned long> >();
  136. test_int< int_adapter<unsigned long> >();
  137. print< int_adapter<long> >();
  138. test_int< int_adapter<long> >();
  139. print< int_adapter<boost::int64_t> >();
  140. test_int< int_adapter<boost::int64_t> >();
  141. return printTestStats();
  142. }