File: datetime.cpp

package info (click to toggle)
mysql%2B%2B 3.2.5-2.3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,356 kB
  • sloc: cpp: 35,788; sh: 3,693; perl: 789; makefile: 728
file content (218 lines) | stat: -rw-r--r-- 6,110 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/***********************************************************************
 test/datetime.cpp - Tests the Date, DateTime, and Time classes.

 Copyright (c) 2007-2008 by Educational Technology Resources, Inc.
 Others may also hold copyrights on code in this file.  See the
 CREDITS.txt file in the top directory of the distribution for details.

 This file is part of MySQL++.

 MySQL++ is free software; you can redistribute it and/or modify it
 under the terms of the GNU Lesser General Public License as published
 by the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.

 MySQL++ is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with MySQL++; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 USA
***********************************************************************/

#include <mysql++.h>

#include <iostream>
#include <sstream>
#include <string>

#include <stdio.h>

using namespace mysqlpp;
using namespace std;


// Compare the given string against the object inserted into a Query stream.
template <class T>
static unsigned int
test_query_insert(const T& object, const char* expected,
		const char* what)
{
	Query q = Connection().query();	// don't do this in real code
	q << object;
	if (q.str().compare(expected) == 0) {
		cout << what << " is '" << expected <<
				"' in Query, as expected." << endl;
		return 0;
	}
	else {
		cerr << what << " '" << object << "' should be '" <<
				expected << "' when inserted into Query!" << endl;
		return 1;
	}
}


// Compare the given string against the object inserted into an ostream.
template <class T>
static unsigned int
test_ostream_insert(const T& object, const char* expected,
		const char* what)
{
	ostringstream os;
	os << object;
	if (os.str().compare(expected) == 0) {
		cout << what << " is '" << expected <<
				"' in ostream, as expected." << endl;
		return 0;
	}
	else {
		cerr << what << " '" << object << "' should be '" <<
				expected << "' when inserted into ostream!" << endl;
		return 1;
	}
}


// Compare the given string against the return value of the object's
// str() method.
template <class T>
static unsigned int
test_str_method(const T& object, const char* expected, const char* what)
{
	if (object.str().compare(expected) == 0) {
		cout << what << ".str() returns '" << expected <<
				"', as expected." << endl;
		return 0;
	}
	else {
		cerr << what << " '" << object << "' should return '" <<
				expected << "' from str() method!" << endl;
		return 1;
	}
}


// Compare the given string against the object when cast to std::string
template <class T>
static unsigned int
test_string_operator(const T& object, const char* expected,
		const char* what)
{
	if (string(object).compare(expected) == 0) {
		cout << "string(" << what << ") is '" << expected <<
				"', as expected." << endl;
		return 0;
	}
	else {
		cerr << what << " '" << object << "' should be '" <<
				expected << "' when cast to std::string!" << endl;
		return 1;
	}
}


// Compare the given string against the object when converted in several
// different ways to a string.
template <class T>
static unsigned int
test_stringization(const T& object, const char* expected,
		const char* what)
{
	return	test_query_insert(object, expected, what) +
			test_ostream_insert(object, expected, what) +
			test_string_operator(object, expected, what) +
			test_str_method(object, expected, what);
}


// Given a Date and a set of values we should expect to be in it,
// compare its outputs against values we compute separately.
static unsigned int
test_date(const Date& d, int year, int month, int day)
{
	if (	d.year() == year &&
			d.month() == month &&
			d.day() == day) {
		char ac[20];
		snprintf(ac, sizeof(ac), "%04d-%02d-%02d",
				year, month, day);
		return test_stringization(d, ac, "Date");
	}
	else {
		cerr << "Date '" << d << "' values should be '" <<
				year << '-' << month << '-' << day << endl;
		return 1;
	}
}


// Given a Time and a set of values we should expect to be in it,
// compare its outputs against values we compute separately.
static unsigned int
test_time(const Time& t, int hour, int minute, int second)
{
	if (	t.hour() == hour &&
			t.minute() == minute &&
			t.second() == second) {
		char ac[20];
		snprintf(ac, sizeof(ac), "%02d:%02d:%02d",
				hour, minute, second);
		return test_stringization(t, ac, "Time");
	}
	else {
		cerr << "Time '" << t << "' values should be '" <<
				hour << ':' << minute << ':' << second << endl;
		return 1;
	}
}


// Given a DateTime and a set of values we should expect to be in it,
// compare its outputs against values we compute separately.
static unsigned int
test_datetime(const DateTime& dt,
		int year, int month, int day,
		int hour, int minute, int second)
{
	return	test_date(Date(dt), year, month, day) +
			test_time(Time(dt), hour, minute, second);
}


// Run tests above for the various types we support using the date and
// time values given.
static unsigned int
test(int year, int month, int day, int hour, int minute, int second)
{
	unsigned int failures = 0;
	failures += test_date(Date(year, month, day), year, month, day);
	failures += test_datetime(
			DateTime(year, month, day, hour, minute, second),
			year, month, day, hour, minute, second);
	failures += test_time(Time(hour, minute, second), hour, minute,
			second);
	return failures;
}


int
main()
{
	unsigned int failures = 0;
#if !defined(__SUNPRO_CC)
	// Sun CC can't compile these tests for some reason.  So, skip them.
	// See http://lists.mysql.com/plusplus/8164
	failures += test(0, 0, 0, 0, 0, 0);
	failures += test(1, 2, 3, 4, 5, 6);
	failures += test_stringization(DateTime(), "NOW()", "DateTime");
	DateTime dt;
	dt.year(2007);
	failures += test_stringization(dt, "2007-00-00 00:00:00", "DateTime");
#endif
	return failures;
}