lldate_test.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:8k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /**
  2.  * @file lldate_test.cpp
  3.  * @author Adroit
  4.  * @date 2007-02
  5.  * @brief LLDate test cases.
  6.  *
  7.  * $LicenseInfo:firstyear=2007&license=viewergpl$
  8.  * 
  9.  * Copyright (c) 2007-2010, Linden Research, Inc.
  10.  * 
  11.  * Second Life Viewer Source Code
  12.  * The source code in this file ("Source Code") is provided by Linden Lab
  13.  * to you under the terms of the GNU General Public License, version 2.0
  14.  * ("GPL"), unless you have obtained a separate licensing agreement
  15.  * ("Other License"), formally executed by you and Linden Lab.  Terms of
  16.  * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17.  * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18.  * 
  19.  * There are special exceptions to the terms and conditions of the GPL as
  20.  * it is applied to this Source Code. View the full text of the exception
  21.  * in the file doc/FLOSS-exception.txt in this software distribution, or
  22.  * online at
  23.  * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24.  * 
  25.  * By copying, modifying or distributing this software, you acknowledge
  26.  * that you have read and understood your obligations described above,
  27.  * and agree to abide by those obligations.
  28.  * 
  29.  * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30.  * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31.  * COMPLETENESS OR PERFORMANCE.
  32.  * $/LicenseInfo$
  33.  */
  34. #include "linden_common.h"
  35. #include "../llstring.h"
  36. #include "../lldate.h"
  37. #include "../test/lltut.h"
  38. #define VALID_DATE "2003-04-30T04:00:00Z"
  39. #define VALID_DATE_LEAP "2004-02-29T04:00:00Z"
  40. #define VALID_DATE_HOUR_BOUNDARY "2003-04-30T23:59:59Z"
  41. #define VALID_DATE_FRACTIONAL_SECS "2007-09-26T20:31:33.70Z"
  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. // invalid values
  53. // apr 1.1.1 seems to not care about constraining the date to valid
  54. // dates. Put these back when the parser checks.
  55. #define LL_DATE_PARSER_CHECKS_BOUNDARY 0
  56. //#define INVALID_DATE_24HOUR_BOUNDARY "2003-04-30T24:00:00Z"
  57. //#define INVALID_DATE_LEAP "2003-04-29T04:00:00Z"
  58. //#define INVALID_DATE_HOUR "2003-04-30T24:59:59Z"
  59. //#define INVALID_DATE_MIN "2003-04-30T22:69:59Z"
  60. //#define INVALID_DATE_SEC "2003-04-30T22:59:69Z"
  61. //#define INVALID_DATE_YEAR "0-04-30T22:59:59Z"
  62. //#define INVALID_DATE_MONTH "2003-13-30T22:59:59Z"
  63. //#define INVALID_DATE_DAY "2003-04-35T22:59:59Z"
  64. namespace tut
  65. {
  66. struct date_test
  67. {
  68. };
  69. typedef test_group<date_test> date_test_t;
  70. typedef date_test_t::object date_test_object_t;
  71. tut::date_test_t tut_date_test("date_test");
  72. /* format validation */
  73. template<> template<>
  74. void date_test_object_t::test<1>()
  75. {
  76. LLDate date(VALID_DATE);
  77. std::string  expected_string;
  78. bool result;
  79. expected_string = VALID_DATE;
  80. ensure_equals("Valid Date failed" , expected_string, date.asString());
  81. result = date.fromString(VALID_DATE_LEAP);
  82. expected_string = VALID_DATE_LEAP; 
  83. ensure_equals("VALID_DATE_LEAP failed" , expected_string, date.asString());
  84. result = date.fromString(VALID_DATE_HOUR_BOUNDARY);
  85. expected_string = VALID_DATE_HOUR_BOUNDARY; 
  86. ensure_equals("VALID_DATE_HOUR_BOUNDARY failed" , expected_string, date.asString());
  87. result = date.fromString(VALID_DATE_FRACTIONAL_SECS);
  88. expected_string = VALID_DATE_FRACTIONAL_SECS;
  89. ensure_equals("VALID_DATE_FRACTIONAL_SECS failed" , expected_string, date.asString());
  90. result = date.fromString(INVALID_DATE_MISSING_YEAR);
  91. ensure_equals("INVALID_DATE_MISSING_YEAR should have failed" , result, false);
  92. result = date.fromString(INVALID_DATE_MISSING_MONTH);
  93. ensure_equals("INVALID_DATE_MISSING_MONTH should have failed" , result, false);
  94. result = date.fromString(INVALID_DATE_MISSING_DATE);
  95. ensure_equals("INVALID_DATE_MISSING_DATE should have failed" , result, false);
  96. result = date.fromString(INVALID_DATE_MISSING_T);
  97. ensure_equals("INVALID_DATE_MISSING_T should have failed" , result, false);
  98. result = date.fromString(INVALID_DATE_MISSING_HOUR);
  99. ensure_equals("INVALID_DATE_MISSING_HOUR should have failed" , result, false);
  100. result = date.fromString(INVALID_DATE_MISSING_MIN);
  101. ensure_equals("INVALID_DATE_MISSING_MIN should have failed" , result, false);
  102. result = date.fromString(INVALID_DATE_MISSING_SEC);
  103. ensure_equals("INVALID_DATE_MISSING_SEC should have failed" , result, false);
  104. result = date.fromString(INVALID_DATE_MISSING_Z);
  105. ensure_equals("INVALID_DATE_MISSING_Z should have failed" , result, false);
  106. result = date.fromString(INVALID_DATE_EMPTY);
  107. ensure_equals("INVALID_DATE_EMPTY should have failed" , result, false);
  108. }
  109. /* Invalid Value Handling */
  110. template<> template<>
  111. void date_test_object_t::test<2>()
  112. {
  113. #if LL_DATE_PARSER_CHECKS_BOUNDARY
  114. LLDate date;
  115. std::string  expected_string;
  116. bool result;
  117. result = date.fromString(INVALID_DATE_24HOUR_BOUNDARY);
  118. ensure_equals("INVALID_DATE_24HOUR_BOUNDARY should have failed" , result, false);
  119. ensure_equals("INVALID_DATE_24HOUR_BOUNDARY date still set to old value on failure!" , date.secondsSinceEpoch(), 0);
  120. result = date.fromString(INVALID_DATE_LEAP);
  121. ensure_equals("INVALID_DATE_LEAP should have failed" , result, false);
  122. result = date.fromString(INVALID_DATE_HOUR);
  123. ensure_equals("INVALID_DATE_HOUR should have failed" , result, false);
  124. result = date.fromString(INVALID_DATE_MIN);
  125. ensure_equals("INVALID_DATE_MIN should have failed" , result, false);
  126. result = date.fromString(INVALID_DATE_SEC);
  127. ensure_equals("INVALID_DATE_SEC should have failed" , result, false);
  128. result = date.fromString(INVALID_DATE_YEAR);
  129. ensure_equals("INVALID_DATE_YEAR should have failed" , result, false);
  130. result = date.fromString(INVALID_DATE_MONTH);
  131. ensure_equals("INVALID_DATE_MONTH should have failed" , result, false);
  132. result = date.fromString(INVALID_DATE_DAY);
  133. ensure_equals("INVALID_DATE_DAY should have failed" , result, false);
  134. #endif
  135. }
  136. /* API checks */
  137. template<> template<>
  138. void date_test_object_t::test<3>()
  139. {
  140. LLDate date;
  141. std::istringstream stream(VALID_DATE);
  142. std::string  expected_string = VALID_DATE;
  143. date.fromStream(stream);
  144. ensure_equals("fromStream failed", date.asString(), expected_string);
  145. }
  146. template<> template<>
  147. void date_test_object_t::test<4>()
  148. {
  149. LLDate date1(VALID_DATE);
  150. LLDate date2(date1);
  151. ensure_equals("LLDate(const LLDate& date) constructor failed", date1.asString(), date2.asString());
  152. }
  153. template<> template<>
  154. void date_test_object_t::test<5>()
  155. {
  156. LLDate date1(VALID_DATE);
  157. LLDate date2(date1.secondsSinceEpoch());
  158. ensure_equals("secondsSinceEpoch not equal",date1.secondsSinceEpoch(), date2.secondsSinceEpoch());
  159. ensure_equals("LLDate created using secondsSinceEpoch not equal", date1.asString(), date2.asString());
  160. }
  161. template<> template<>
  162. void date_test_object_t::test<6>()
  163. {
  164. LLDate date(VALID_DATE);
  165. std::ostringstream stream;
  166. stream << date;
  167. std::string expected_str = VALID_DATE;
  168. ensure_equals("ostringstream failed", expected_str, stream.str());
  169. }
  170. template<> template<>
  171. void date_test_object_t::test<7>()
  172. {
  173. LLDate date;
  174. std::istringstream stream(VALID_DATE);
  175. stream >> date;
  176. std::string expected_str = VALID_DATE;
  177. std::ostringstream out_stream;
  178.         out_stream << date;
  179. ensure_equals("<< failed", date.asString(),expected_str);
  180. ensure_equals("<< to >> failed", stream.str(),out_stream.str());
  181. }
  182. }