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