TsDate.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 // Terralib
22 
23 // Unit-Test TerraLib
24 #include "TsDate.h"
25 
26 
27 static boost::gregorian::date d(2010,8,9);
28 static boost::gregorian::date dg(2010,12,31);
29 static te::dt::Date ds(2010,01,01); //date start
30 static te::dt::Date d28(2010,02,28); //feb 28
31 static te::dt::Date d30(2010,04,30); //abr
32 static te::dt::Date d31(2010,05,31); //mai
33 static te::dt::Date de(2010,12,31); //date end
34 static te::dt::Date di(2010,06,10); //date intermediary
35 static te::dt::Date dmar(2010,03,03);
36 static te::dt::Date d29(2008,02,29);
37 
38 static te::dt::Date dx(2010,12,31);
39 
40 static te::dt::TimeDuration dt(20,30,50,11);
41 
43 
45 {
46 }
47 
49 {
50 }
51 
53 {
54 //#ifdef TE_COMPILE_ALL
55  te::dt::Date dempty;
56  te::dt::Date d1(d); //from gregorian date
57  te::dt::Date d2(dx); //from Date
58  std::cout << std::endl;
59  std::cout << " Empty constructor: " << dempty.toString() << std::endl;
60  std::cout << " Date from gregorian date : " << d1.toString() << std::endl;
61  std::cout << " Date from date: " << d2.toString() << std::endl;
62 
63  CPPUNIT_ASSERT(d2.getYear() == 2010 && d2.getMonth() == 12 && d2.getDay() == 31 );
64 
65 
66 //#endif
67 }
68 
70 {
71 //#ifdef TE_COMPILE_ALL
72  te::dt::Date d1(2010,1,1);
73  CPPUNIT_ASSERT(d1.getYear() == 2010 && d1.getMonth() == 1 && d1.getDay() == 1 );
74 
75  CPPUNIT_ASSERT(ds.getYear() == 2010 && ds.getMonth() == 1 && ds.getDay() == 1 );
76  CPPUNIT_ASSERT(de.getYear() == 2010 && de.getMonth() == 12 && de.getDay() == 31 );
77  CPPUNIT_ASSERT(d28.getYear() == 2010 && d28.getMonth() == 02 && d28.getDay() == 28 );
78  CPPUNIT_ASSERT(d29.getYear() == 2008 && d29.getMonth() == 02 && d29.getDay() == 29 );
79  CPPUNIT_ASSERT(d30.getYear() == 2010 && d30.getMonth() == 04 && d30.getDay() == 30 );
80  CPPUNIT_ASSERT(d31.getYear() == 2010 && d31.getMonth() == 05 && d31.getDay() == 31 );
81 
82 // Checking year range [1400..9999]
83  te::dt::Date d2(1400,1,1);
84  CPPUNIT_ASSERT(d2.getYear() == 1400 && d2.getMonth() == 1 && d2.getDay() == 1 );
85 
86  te::dt::Date d3(9999,8,31);
87  CPPUNIT_ASSERT(d3.getYear() == 9999 && d3.getMonth() == 8 && d3.getDay() == 31 );
88  std::cout << std::endl;
89  std::cout << " Testing range of values Year: " << d2.toString() << std::endl;
90  std::cout << " Testing range of values Year : " << d3.toString() << std::endl;
91 
92  //Is it accepting invalid year ?? TODO: how to use try/catch exception da boost
93  try {
94  te::dt::Date d4(0,12,31);
95  std::cout << " Date from date: " << d4.toString() << std::endl;
96  CPPUNIT_ASSERT(d4.getYear() == 0);
97  }
98  catch (te::dt::Exception e)
99  {
100  std::cout << " Year out of range d(0,12,31)" << std::endl;
101  throw e ;
102  }
103 
104 //#endif
105 }
106 
108 {
109 //#ifdef TE_COMPILE_ALL
110 
111  te::dt::Date d1(2010,12,31);
112  te::dt::Date d(d1);
113 
114  CPPUNIT_ASSERT( d1.getYear() == d.getYear() && d1.getMonth() == d.getMonth() && d1.getDay() == d.getDay());
115 
116 //#endif
117 }
118 
120 {
121 //#ifdef TE_COMPILE_ALL
122  te::dt::Date dx(2010,12,31);
123  te::dt::TimeDuration dt(20,30,50,11);
124 
125  te::dt::TimeInstant d1(dx,dt);
128  d2.operator =(d1);
129  d3 = d2;
130 
131  CPPUNIT_ASSERT( d1.getDate().getYear() == d2.getDate().getYear() && d1.getDate().getMonth() == d2.getDate().getMonth() && d1.getDate().getDay() == d2.getDate().getDay());
132  CPPUNIT_ASSERT( d1.getTime().getHours() == d2.getTime().getHours() && d1.getTime().getMinutes() == d2.getTime().getMinutes() && d1.getTime().getSeconds() == d2.getTime().getSeconds());
133  CPPUNIT_ASSERT( d2.getDate().getYear() == d3.getDate().getYear() && d2.getDate().getMonth() == d3.getDate().getMonth() && d2.getDate().getDay() == d3.getDate().getDay());
134  CPPUNIT_ASSERT( d2.getTime().getHours() == d3.getTime().getHours() && d2.getTime().getMinutes() == d3.getTime().getMinutes() && d2.getTime().getSeconds() == d3.getTime().getSeconds());
135  CPPUNIT_ASSERT( d3.getDate().getYear() == d1.getDate().getYear() && d3.getDate().getMonth() == d1.getDate().getMonth() && d3.getDate().getDay() == d1.getDate().getDay());
136  CPPUNIT_ASSERT( d3.getTime().getHours() == d1.getTime().getHours() && d3.getTime().getMinutes() == d1.getTime().getMinutes() && d3.getTime().getSeconds() == d1.getTime().getSeconds());
137 
138 //#endif
139 }
140 
142 {
143 //#ifdef TE_COMPILE_ALL
144 // Test all operators...
145  te::dt::Date d1(ds);
146  te::dt::Date d2(ds);
147  te::dt::Date d3(2010,01,05);
148  te::dt::AbstractData* d4 = ds.clone();
149 
150  CPPUNIT_ASSERT(d2.operator==(d1));
151 
152  CPPUNIT_ASSERT(static_cast<te::dt::Date*>(d4)->operator==(d1));
153  CPPUNIT_ASSERT(d2.operator-(d1) == 0);
154  CPPUNIT_ASSERT(d3.operator-(ds) == 4);
155  CPPUNIT_ASSERT(de.operator-(ds) == 364);
156  CPPUNIT_ASSERT(ds.operator<(de));
157  CPPUNIT_ASSERT(de.operator>(ds));
158  CPPUNIT_ASSERT(ds.operator!=(de));
159  std::string ds_show = ds.toString();
160  std::string d4_show = static_cast<te::dt::Date*>(d4)->toString();
161  CPPUNIT_ASSERT(ds.operator==(*static_cast<te::dt::Date*>(d4)));
162 // Difference involving feb ( 20100101 - 20100303)
163  CPPUNIT_ASSERT(dmar.operator-(ds) == 61);
164 
165 //#endif
166 }
168 {
169 //#ifdef TE_COMPILE_ALL
170 
171  te::dt::Date d(ds);
172  te::dt::Date dclone = *static_cast<te::dt::Date*>(d.clone());
173  CPPUNIT_ASSERT(d.operator==(dclone));
174 
175 //#endif
176 }
177 
179 {
180 //#ifdef TE_COMPILE_ALL
181 
182  te::dt::Date d(ds);
183  te::dt::Date dclone = *static_cast<te::dt::Date*>(ds.clone());
184  CPPUNIT_ASSERT(ds.operator==(dclone));
185  std::string show = dclone.toString();
186  CPPUNIT_ASSERT( dclone.toString() =="20100101");
187 
188 //#endif
189 }
190 
192 {
193 //#ifdef TE_COMPILE_ALL
194 
195  te::dt::DateDuration dempty;
196  std::string empty = dempty.toString();
197  boost::gregorian::date_duration boostduration(5);
198  te::dt::DateDuration dboost(boostduration);
199  std::string sh_dboost = dboost.toString();
200 
202  te::dt::DateDuration dclone = *static_cast<te::dt::DateDuration*>(d.clone());
203  CPPUNIT_ASSERT(d.operator==(dclone));
204  std::string show = d.toString();
205  te::dt::DateDuration d1(4);
206  std::string show1 = d1.toString();
207 
208  te::dt::DateDuration d3 = d1;
209 
210 
211  CPPUNIT_ASSERT(d3 == d1);
212  CPPUNIT_ASSERT(d < d1);
213  CPPUNIT_ASSERT(d1 > d);
214  CPPUNIT_ASSERT(d != d1);
215  CPPUNIT_ASSERT(d1.toString() == "4");
216 
217 //#endif
218 }
219 
221 {
222 //#ifdef TE_COMPILE_ALL
223 
224  te::dt::DatePeriod p1(ds,de);
225  te::dt::DatePeriod pboost(d,dg);
226  te::dt::DatePeriod p2(pboost);
227  te::dt::DatePeriod p3 = p1;
228  te::dt::Date dsplus1(2010,01,02);
229  te::dt::DatePeriod p1plus1(ds,dsplus1);
230  te::dt::DatePeriod pclone = *static_cast<te::dt::DatePeriod*>(pboost.clone());
231  CPPUNIT_ASSERT(pboost.operator==(pclone));
232 
233  std::cout << std::endl;
234  std::cout << " Testing DatePeriod class with:" << std::endl;
235  std::cout << " DatePeriod p1 : " << p1.toString() << std::endl;
236  std::cout << " DatePeriod pboost: " << pboost.toString() << std::endl;
237  std::cout << " DatePeriod p2 : " << p2.toString() << std::endl;
238  std::cout << " DatePeriod p1plus1:" << p1plus1.toString() << std::endl;
239  std::cout << " DatePeriod p3: " << p3.toString() << std::endl;
240  std::cout << " DatePeriod pboost clone: " << pclone.toString() << std::endl;
241 
242  std::cout << std::endl;
243  std::cout << " DatePeriod p1 InitialDate: " << p1.getInitialDate().toString() << std::endl;
244  std::cout << " DatePeriod p1 FinalDate: " << p1.getFinalDate().toString() << std::endl;
245  std::cout << " DatePeriod: p1 > pboost: " << p1.operator>(pboost) << std::endl;
246  std::cout << " DatePeriod: pboost < p1: " << pboost.operator<(p1) << std::endl;
247  std::cout << " DatePeriod: p1 != pboost: " << p1.operator!=(pboost) << std::endl;
248  std::cout << " DatePeriod: p1 == pboost: " << p1.operator==(pboost) << std::endl;
249  std::cout << " DatePeriod: p1 == p3: " << p1.operator==(p3) << std::endl;
250 
251 // Messages...
252  boost::gregorian::date_period bdp = p3.getDatePeriod();
253  CPPUNIT_ASSERT(p1.getInitialDate() == ds);
254  CPPUNIT_ASSERT(p1.getFinalDate() == de);
255  CPPUNIT_ASSERT(p3.getDatePeriod() == bdp);
256 
257  CPPUNIT_ASSERT(p1.operator>(pboost));
258  CPPUNIT_ASSERT(p1 > pboost);
259  CPPUNIT_ASSERT(pboost.operator<(p1));
260  CPPUNIT_ASSERT(pboost < p1);
261  CPPUNIT_ASSERT(p3.operator==(p1));
262  CPPUNIT_ASSERT(p3 == p1);
263 
264 
265 //#endif
266 }
std::string toString() const
It returns the date duration in a textual format.
void tcClone()
Test Case: Clone this object and return AbstractData.
Definition: TsDate.cpp:167
void tcToString()
It returns the date in the ISO textual format (YYYYMMDD).
Definition: TsDate.cpp:178
void tcCheckOperators()
Test Case: Check operators (==, >,<, !=, -).
Definition: TsDate.cpp:141
static te::dt::Date dx(2010, 12, 31)
static te::dt::Date de(2010, 12, 31)
void tcDateTimeDuration()
Test Case: Check TimeInstant.
Definition: TsDate.cpp:119
long getSeconds() const
It returns the seconds of a minute - from 0 to 59.
Definition: TimeDuration.h:105
void tcDate()
Test Case: It creates a date time with local date. The date and time are obtained from the system clo...
Definition: TsDate.cpp:52
Date getInitialDate() const
It gets the initial date.
Definition: DatePeriod.cpp:50
std::string toString() const
It returns the date in the ISO textual format (YYYYMMDD).
Definition: Date.cpp:82
boost::gregorian::greg_year getYear() const
It returns the gregorian year.
Definition: Date.h:111
static te::dt::Date d29(2008, 02, 29)
AbstractData * clone() const
It returns a clone of this object.
Definition: Date.cpp:77
boost::gregorian::greg_day getDay() const
It returns the gregorian day - from 1 to 31.
Definition: Date.h:97
Test suite for the Date class.
static te::dt::Date ds(2010, 01, 01)
static boost::gregorian::date d(2010, 8, 9)
static boost::gregorian::date dg(2010, 12, 31)
void tcDateDuration()
Definition: TsDate.cpp:191
A class to represent time instant.
Definition: TimeInstant.h:55
static te::dt::Date d28(2010, 02, 28)
static te::dt::Date dmar(2010, 03, 03)
CPPUNIT_TEST_SUITE_REGISTRATION(TsDate)
void tearDown()
Definition: TsDate.cpp:48
Date getDate() const
It returns the date associated to time instant.
Definition: TimeInstant.h:106
AbstractData * clone() const
It returns a clone of this object.
Definition: DatePeriod.cpp:94
A base class for date data types.
Definition: Date.h:53
static te::dt::Date di(2010, 06, 10)
void tcDatePeriod()
Definition: TsDate.cpp:220
static te::dt::TimeDuration dt(20, 30, 50, 11)
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
void setUp()
Definition: TsDate.cpp:44
Test suite for the Date, DateDuration and DatePeriod Classes.
Definition: TsDate.h:60
void tcDateConstructor()
Test Case: Copy constructor.
Definition: TsDate.cpp:107
A class to represent date duration.
Definition: DateDuration.h:51
A class to represent time duration with nano-second/micro-second resolution.
Definition: TimeDuration.h:51
TimeDuration getTime() const
It returns the time duration associated to time instant.
Definition: TimeInstant.cpp:49
const boost::gregorian::date_period & getDatePeriod() const
It returns the boost date period type.
Definition: DatePeriod.h:86
static te::dt::Date d30(2010, 04, 30)
A class to represent date period based on the Gregorian Calendar.
Definition: DatePeriod.h:51
long getMinutes() const
It returns the minutes of a hour - from 0 to 59.
Definition: TimeDuration.h:98
void tcDateCheck()
Test Case: It checks date values (YY,MM,DD)
Definition: TsDate.cpp:69
static te::dt::Date d31(2010, 05, 31)
Date getFinalDate() const
It gets the final date.
Definition: DatePeriod.cpp:55
boost::gregorian::greg_month getMonth() const
It returns the gregorian month - from 1 to 12.
Definition: Date.h:104
std::string toString() const
It returns the date period in the ISO textual format (YYYYMMDDTYYYYMMDD).
Definition: DatePeriod.cpp:99
AbstractData * clone() const
It returns a clone of this object.
long getHours() const
It returns the hours of a day - from 0 to 23.
Definition: TimeDuration.h:91
An exception class for the DataType module.