PageRenderTime 32ms CodeModel.GetById 1ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/tests/lldate_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 213 lines | 134 code | 37 blank | 42 comment | 0 complexity | a9a654f1b382e8a3b7aaf0d453847894 MD5 | raw file
  1/**
  2 * @file lldate_test.cpp
  3 * @author Adroit
  4 * @date 2007-02
  5 * @brief LLDate test cases.
  6 *
  7 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28
 29#include "linden_common.h"
 30
 31#include "../llstring.h"
 32#include "../lldate.h"
 33
 34#include "../test/lltut.h"
 35
 36
 37#define VALID_DATE									"2003-04-30T04:00:00Z"
 38#define VALID_DATE_LEAP								"2004-02-29T04:00:00Z"
 39#define VALID_DATE_HOUR_BOUNDARY					"2003-04-30T23:59:59Z"
 40#define VALID_DATE_FRACTIONAL_SECS					"2007-09-26T20:31:33.70Z"
 41
 42// invalid format
 43#define INVALID_DATE_MISSING_YEAR					"-04-30T22:59:59Z"
 44#define INVALID_DATE_MISSING_MONTH					"1900-0430T22:59:59Z"
 45#define INVALID_DATE_MISSING_DATE					"1900-0430-T22:59:59Z"
 46#define INVALID_DATE_MISSING_T						"1900-04-30-22:59:59Z"
 47#define INVALID_DATE_MISSING_HOUR					"1900-04-30T:59:59Z"
 48#define INVALID_DATE_MISSING_MIN					"1900-04-30T01::59Z"
 49#define INVALID_DATE_MISSING_SEC					"1900-04-30T01:59Z"
 50#define INVALID_DATE_MISSING_Z						"1900-04-30T01:59:23"
 51#define INVALID_DATE_EMPTY							""
 52
 53// invalid values
 54// apr 1.1.1 seems to not care about constraining the date to valid
 55// dates. Put these back when the parser checks.
 56#define LL_DATE_PARSER_CHECKS_BOUNDARY 0
 57//#define INVALID_DATE_24HOUR_BOUNDARY				"2003-04-30T24:00:00Z"
 58//#define INVALID_DATE_LEAP							"2003-04-29T04:00:00Z"
 59//#define INVALID_DATE_HOUR							"2003-04-30T24:59:59Z"
 60//#define INVALID_DATE_MIN							"2003-04-30T22:69:59Z"
 61//#define INVALID_DATE_SEC							"2003-04-30T22:59:69Z"
 62//#define INVALID_DATE_YEAR							"0-04-30T22:59:59Z"
 63//#define INVALID_DATE_MONTH							"2003-13-30T22:59:59Z"
 64//#define INVALID_DATE_DAY							"2003-04-35T22:59:59Z"
 65
 66namespace tut
 67{
 68	struct date_test
 69	{
 70
 71	};
 72	typedef test_group<date_test> date_test_t;
 73	typedef date_test_t::object date_test_object_t;
 74	tut::date_test_t tut_date_test("LLDate");
 75
 76	/* format validation */
 77	template<> template<>
 78	void date_test_object_t::test<1>()
 79	{
 80		LLDate date(VALID_DATE);
 81		std::string  expected_string;
 82		bool result;
 83		expected_string = VALID_DATE;
 84		ensure_equals("Valid Date failed" , expected_string, date.asString());
 85
 86		result = date.fromString(VALID_DATE_LEAP);
 87		expected_string = VALID_DATE_LEAP; 	
 88		ensure_equals("VALID_DATE_LEAP failed" , expected_string, date.asString());
 89
 90		result = date.fromString(VALID_DATE_HOUR_BOUNDARY);
 91		expected_string = VALID_DATE_HOUR_BOUNDARY; 	
 92		ensure_equals("VALID_DATE_HOUR_BOUNDARY failed" , expected_string, date.asString());
 93
 94		result = date.fromString(VALID_DATE_FRACTIONAL_SECS);
 95		expected_string = VALID_DATE_FRACTIONAL_SECS;
 96		ensure_equals("VALID_DATE_FRACTIONAL_SECS failed" , expected_string, date.asString());
 97
 98		result = date.fromString(INVALID_DATE_MISSING_YEAR);
 99		ensure_equals("INVALID_DATE_MISSING_YEAR should have failed" , result, false);
100
101		result = date.fromString(INVALID_DATE_MISSING_MONTH);
102		ensure_equals("INVALID_DATE_MISSING_MONTH should have failed" , result, false);
103
104		result = date.fromString(INVALID_DATE_MISSING_DATE);
105		ensure_equals("INVALID_DATE_MISSING_DATE should have failed" , result, false);
106
107		result = date.fromString(INVALID_DATE_MISSING_T);
108		ensure_equals("INVALID_DATE_MISSING_T should have failed" , result, false);
109
110		result = date.fromString(INVALID_DATE_MISSING_HOUR);
111		ensure_equals("INVALID_DATE_MISSING_HOUR should have failed" , result, false);
112
113		result = date.fromString(INVALID_DATE_MISSING_MIN);
114		ensure_equals("INVALID_DATE_MISSING_MIN should have failed" , result, false);
115
116		result = date.fromString(INVALID_DATE_MISSING_SEC);
117		ensure_equals("INVALID_DATE_MISSING_SEC should have failed" , result, false);
118
119		result = date.fromString(INVALID_DATE_MISSING_Z);
120		ensure_equals("INVALID_DATE_MISSING_Z should have failed" , result, false);
121
122		result = date.fromString(INVALID_DATE_EMPTY);
123		ensure_equals("INVALID_DATE_EMPTY should have failed" , result, false);
124	}
125
126	/* Invalid Value Handling */
127	template<> template<>
128	void date_test_object_t::test<2>()
129	{
130#if LL_DATE_PARSER_CHECKS_BOUNDARY
131		LLDate date;
132		std::string  expected_string;
133		bool result;
134
135		result = date.fromString(INVALID_DATE_24HOUR_BOUNDARY);
136		ensure_equals("INVALID_DATE_24HOUR_BOUNDARY should have failed" , result, false);
137		ensure_equals("INVALID_DATE_24HOUR_BOUNDARY date still set to old value on failure!" , date.secondsSinceEpoch(), 0);
138
139		result = date.fromString(INVALID_DATE_LEAP);
140		ensure_equals("INVALID_DATE_LEAP should have failed" , result, false);
141
142		result = date.fromString(INVALID_DATE_HOUR);
143		ensure_equals("INVALID_DATE_HOUR should have failed" , result, false);
144
145		result = date.fromString(INVALID_DATE_MIN);
146		ensure_equals("INVALID_DATE_MIN should have failed" , result, false);
147
148		result = date.fromString(INVALID_DATE_SEC);
149		ensure_equals("INVALID_DATE_SEC should have failed" , result, false);
150
151		result = date.fromString(INVALID_DATE_YEAR);
152		ensure_equals("INVALID_DATE_YEAR should have failed" , result, false);
153
154		result = date.fromString(INVALID_DATE_MONTH);
155		ensure_equals("INVALID_DATE_MONTH should have failed" , result, false);
156
157		result = date.fromString(INVALID_DATE_DAY);
158		ensure_equals("INVALID_DATE_DAY should have failed" , result, false);
159#endif
160	}
161
162	/* API checks */
163	template<> template<>
164	void date_test_object_t::test<3>()
165	{
166		LLDate date;
167		std::istringstream stream(VALID_DATE);
168		std::string  expected_string = VALID_DATE;
169		date.fromStream(stream);
170		ensure_equals("fromStream failed", date.asString(), expected_string);
171	}
172
173	template<> template<>
174	void date_test_object_t::test<4>()
175	{
176		LLDate date1(VALID_DATE);
177		LLDate date2(date1);
178		ensure_equals("LLDate(const LLDate& date) constructor failed", date1.asString(), date2.asString());
179	}
180
181	template<> template<>
182	void date_test_object_t::test<5>()
183	{
184		LLDate date1(VALID_DATE);
185		LLDate date2(date1.secondsSinceEpoch());
186		ensure_equals("secondsSinceEpoch not equal",date1.secondsSinceEpoch(), date2.secondsSinceEpoch());
187		ensure_equals("LLDate created using secondsSinceEpoch not equal", date1.asString(), date2.asString());
188	}
189
190	template<> template<>
191	void date_test_object_t::test<6>()
192	{
193		LLDate date(VALID_DATE);
194		std::ostringstream stream;
195		stream << date;
196		std::string expected_str = VALID_DATE;
197		ensure_equals("ostringstream failed", expected_str, stream.str());
198	}
199
200	template<> template<>
201	void date_test_object_t::test<7>()
202	{
203		LLDate date;
204		std::istringstream stream(VALID_DATE);
205		stream >> date;
206		std::string expected_str = VALID_DATE;
207		std::ostringstream out_stream;
208        out_stream << date;
209
210		ensure_equals("<< failed", date.asString(),expected_str);
211		ensure_equals("<< to >> failed", stream.str(),out_stream.str());		
212	}
213}