blob: c1755331e863a106d65ebc03d0c131b5d08e104d [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// Copyright (C) 2002-2003 Chad C. Yates cyates@uidaho.edu
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation; either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16//
17// As a special exception to the GNU General Public License, permission is
18// granted for additional uses of the text contained in its release
19// of Common C++.
20//
21// The exception is that, if you link the Common C++ library with other
22// files to produce an executable, this does not by itself cause the
23// resulting executable to be covered by the GNU General Public License.
24// Your use of that executable is in no way restricted on account of
25// linking the Common C++ library code into it.
26//
27// This exception does not however invalidate any other reasons why
28// the executable file might be covered by the GNU General Public License.
29//
30// This exception applies only to the code released under the
31// name Common C++. If you copy code from other releases into a copy of
32// Common C++, as the General Public License permits, the exception does
33// not apply to the code that you add in this way. To avoid misleading
34// anyone as to the status of such modified files, you must delete
35// this exception notice from them.
36//
37// If you write modifications of your own for Common C++, it is your choice
38// whether to permit this exception to apply to your modifications.
39// If you do not wish that, delete this exception notice.
40
41#include <cppunit/extensions/HelperMacros.h>
42#include <cc++/thread.h>
43#include <cc++/numbers.h> // includes Date
44#include <iostream>
45#include <iomanip>
46
47using namespace ost;
48using std::string;
49
50/**
51 * Test Fixture to excercise the Common C++ urlstring functions
52 *
53 * @author Chad C. Yates
54 */
55class DateTest : public CppUnit::TestFixture
56{
57 CPPUNIT_TEST_SUITE(DateTest);
58 CPPUNIT_TEST(testSimpleGets);
59 CPPUNIT_TEST(testSimpleSets);
60 CPPUNIT_TEST(testIsValid);
61 CPPUNIT_TEST(testGetDate_String);
62 CPPUNIT_TEST(testGetDate_time_t);
63 CPPUNIT_TEST(testGeteDate_struct_tm);
64 CPPUNIT_TEST(testOperations);
65 //CPPUNIT_TEST_SUB_SUITE(CppUnit::TestCaller<Orthodox<Date> >, DateTest);
66 CPPUNIT_TEST_SUITE_END();
67
68protected:
69 Date date;
70 int exp_year;
71 long exp_value;
72 unsigned exp_month;
73 unsigned exp_day;
74 unsigned exp_dayofweek;
75 string exp_stringdate;
76 tm exp_dt;
77 time_t exp_ctime;
78
79public:
80 void setUp() {
81 Thread::setException(Thread::throwNothing);
82
83 date = Date(2003, 1, 6);
84 exp_year = 2003;
85 exp_month = 1;
86 exp_day = 6;
87 exp_dayofweek = 1;
88
89 std::stringstream tmp;
90 tmp << exp_year << "-" << std::setfill('0') << std::setw(2) << exp_month << "-" << std::setw(2) << exp_day;
91 exp_stringdate = tmp.str();
92
93 std::stringstream tmp2;
94 tmp2 << exp_year << std::setfill('0') << std::setw(2) << exp_month << std::setw(2) << exp_day;
95 exp_value = atoi(tmp2.str().c_str());
96
97 // make a ctime style datetime stamp
98 memset(&exp_dt, 0, sizeof(exp_dt));
99 exp_dt.tm_year = exp_year - 1900; // years since 1900
100 exp_dt.tm_mon = exp_month - 1; // months since january (0-11)
101 exp_dt.tm_mday = exp_day;
102 exp_ctime = mktime(&exp_dt);
103 }
104
105 void testSimpleGets() {
106 CPPUNIT_ASSERT_EQUAL(exp_year, date.getYear());
107 CPPUNIT_ASSERT_EQUAL(exp_month, date.getMonth());
108 CPPUNIT_ASSERT_EQUAL(exp_day, date.getDay());
109 CPPUNIT_ASSERT_EQUAL(exp_dayofweek, date.getDayOfWeek()); // 0 = sunday (what about locales?)
110 CPPUNIT_ASSERT_EQUAL(exp_value, date.getValue());
111 }
112
113 void testSimpleSets() {
114 //Date date;
115 //const char aDate[] = "20030106";
116 //date.setDate(aDate, sizeof(aDate));
117 //CPPUNIT_ASSERT_EQUAL(long(atoi(aDate)), date.getValue()); // also checks that aDate was not changed (constness)
118 }
119
120 void testIsValid() {
121 char aDate[9];
122 strcpy(aDate, "20031306"); // 13th month
123
124 date.setDate(aDate, sizeof(aDate));
125 CPPUNIT_ASSERT_EQUAL(bool(false), date.isValid());
126 }
127
128 void testGetDate_String() {
129 char dateBuffer[1000];
130 CPPUNIT_ASSERT_EQUAL(exp_stringdate, string(date.getDate(dateBuffer)));
131 }
132
133 void testGetDate_time_t() {
134 CPPUNIT_ASSERT_EQUAL_MESSAGE("time_t Date::getDate()", exp_ctime, date.getDate());
135 }
136
137 void testGeteDate_struct_tm() {
138 //tm dtBuffer;
139 //CPPUNIT_ASSERT_EQUAL_MESSAGE("time_t does not match", exp_ctime, date.getDate(&dtBuffer));
140 //CPPUNIT_ASSERT_MESSAGE("tm structs do not match", memcmp(&exp_dt, &dtBuffer, sizeof(exp_dt)) == 0);
141 }
142
143 void testOperations() {
144 Date aDate = date;
145 Date theNextDay(2003, 1, 7);
146 //Date badDate(2003, 2, 30);
147
148 CPPUNIT_ASSERT_MESSAGE("Operator ==", aDate == date);
149
150 ++aDate;
151 CPPUNIT_ASSERT_MESSAGE("Operator ++ (prefix)", aDate == theNextDay);
152
153 CPPUNIT_ASSERT_MESSAGE("Operator !=", aDate != date);
154 CPPUNIT_ASSERT_MESSAGE("Operator <", date < aDate);
155 CPPUNIT_ASSERT_MESSAGE("Operator <=", date <= aDate);
156 CPPUNIT_ASSERT_MESSAGE("Operator <=", date <= date);
157 CPPUNIT_ASSERT_MESSAGE("Operator >", aDate > date);
158 CPPUNIT_ASSERT_MESSAGE("Operator >=", aDate >= date);
159 CPPUNIT_ASSERT_MESSAGE("Operator >=", date >= date);
160
161 --aDate;
162 CPPUNIT_ASSERT_MESSAGE("Operator -- (prefix)", aDate == date);
163
164 aDate += 1;
165 CPPUNIT_ASSERT_MESSAGE("Operator +=", aDate == theNextDay);
166
167 aDate -= 1;
168 CPPUNIT_ASSERT_MESSAGE("Operator -= ", aDate == date);
169
170 CPPUNIT_ASSERT_MESSAGE("Operator !", !aDate == false);
171
172 // FIXME can't get these to work???
173 //std::cout << theNextDay - long(1) << std::endl;
174 //CPPUNIT_ASSERT_MESSAGE("", theNextDay - date == 1);
175 //CPPUNIT_ASSERT_MESSAGE("", date + 1 == theNextDay);
176 //CPPUNIT_ASSERT_MESSAGE("", theNextDay - 1 == date);
177
178 //FIXME add leap year checks
179 }
180
181};