PageRenderTime 19ms CodeModel.GetById 1ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  29. #include "../llstring.h"
  30. #include "../lldate.h"
  31. #include "../test/lltut.h"
  32. #define VALID_DATE "2003-04-30T04:00:00Z"
  33. #define VALID_DATE_LEAP "2004-02-29T04:00:00Z"
  34. #define VALID_DATE_HOUR_BOUNDARY "2003-04-30T23:59:59Z"
  35. #define VALID_DATE_FRACTIONAL_SECS "2007-09-26T20:31:33.70Z"
  36. // invalid format
  37. #define INVALID_DATE_MISSING_YEAR "-04-30T22:59:59Z"
  38. #define INVALID_DATE_MISSING_MONTH "1900-0430T22:59:59Z"
  39. #define INVALID_DATE_MISSING_DATE "1900-0430-T22:59:59Z"
  40. #define INVALID_DATE_MISSING_T "1900-04-30-22:59:59Z"
  41. #define INVALID_DATE_MISSING_HOUR "1900-04-30T:59:59Z"
  42. #define INVALID_DATE_MISSING_MIN "1900-04-30T01::59Z"
  43. #define INVALID_DATE_MISSING_SEC "1900-04-30T01:59Z"
  44. #define INVALID_DATE_MISSING_Z "1900-04-30T01:59:23"
  45. #define INVALID_DATE_EMPTY ""
  46. // invalid values
  47. // apr 1.1.1 seems to not care about constraining the date to valid
  48. // dates. Put these back when the parser checks.
  49. #define LL_DATE_PARSER_CHECKS_BOUNDARY 0
  50. //#define INVALID_DATE_24HOUR_BOUNDARY "2003-04-30T24:00:00Z"
  51. //#define INVALID_DATE_LEAP "2003-04-29T04:00:00Z"
  52. //#define INVALID_DATE_HOUR "2003-04-30T24:59:59Z"
  53. //#define INVALID_DATE_MIN "2003-04-30T22:69:59Z"
  54. //#define INVALID_DATE_SEC "2003-04-30T22:59:69Z"
  55. //#define INVALID_DATE_YEAR "0-04-30T22:59:59Z"
  56. //#define INVALID_DATE_MONTH "2003-13-30T22:59:59Z"
  57. //#define INVALID_DATE_DAY "2003-04-35T22:59:59Z"
  58. namespace tut
  59. {
  60. struct date_test
  61. {
  62. };
  63. typedef test_group<date_test> date_test_t;
  64. typedef date_test_t::object date_test_object_t;
  65. tut::date_test_t tut_date_test("LLDate");
  66. /* format validation */
  67. template<> template<>
  68. void date_test_object_t::test<1>()
  69. {
  70. LLDate date(VALID_DATE);
  71. std::string expected_string;
  72. bool result;
  73. expected_string = VALID_DATE;
  74. ensure_equals("Valid Date failed" , expected_string, date.asString());
  75. result = date.fromString(VALID_DATE_LEAP);
  76. expected_string = VALID_DATE_LEAP;
  77. ensure_equals("VALID_DATE_LEAP failed" , expected_string, date.asString());
  78. result = date.fromString(VALID_DATE_HOUR_BOUNDARY);
  79. expected_string = VALID_DATE_HOUR_BOUNDARY;
  80. ensure_equals("VALID_DATE_HOUR_BOUNDARY failed" , expected_string, date.asString());
  81. result = date.fromString(VALID_DATE_FRACTIONAL_SECS);
  82. expected_string = VALID_DATE_FRACTIONAL_SECS;
  83. ensure_equals("VALID_DATE_FRACTIONAL_SECS failed" , expected_string, date.asString());
  84. result = date.fromString(INVALID_DATE_MISSING_YEAR);
  85. ensure_equals("INVALID_DATE_MISSING_YEAR should have failed" , result, false);
  86. result = date.fromString(INVALID_DATE_MISSING_MONTH);
  87. ensure_equals("INVALID_DATE_MISSING_MONTH should have failed" , result, false);
  88. result = date.fromString(INVALID_DATE_MISSING_DATE);
  89. ensure_equals("INVALID_DATE_MISSING_DATE should have failed" , result, false);
  90. result = date.fromString(INVALID_DATE_MISSING_T);
  91. ensure_equals("INVALID_DATE_MISSING_T should have failed" , result, false);
  92. result = date.fromString(INVALID_DATE_MISSING_HOUR);
  93. ensure_equals("INVALID_DATE_MISSING_HOUR should have failed" , result, false);
  94. result = date.fromString(INVALID_DATE_MISSING_MIN);
  95. ensure_equals("INVALID_DATE_MISSING_MIN should have failed" , result, false);
  96. result = date.fromString(INVALID_DATE_MISSING_SEC);
  97. ensure_equals("INVALID_DATE_MISSING_SEC should have failed" , result, false);
  98. result = date.fromString(INVALID_DATE_MISSING_Z);
  99. ensure_equals("INVALID_DATE_MISSING_Z should have failed" , result, false);
  100. result = date.fromString(INVALID_DATE_EMPTY);
  101. ensure_equals("INVALID_DATE_EMPTY should have failed" , result, false);
  102. }
  103. /* Invalid Value Handling */
  104. template<> template<>
  105. void date_test_object_t::test<2>()
  106. {
  107. #if LL_DATE_PARSER_CHECKS_BOUNDARY
  108. LLDate date;
  109. std::string expected_string;
  110. bool result;
  111. result = date.fromString(INVALID_DATE_24HOUR_BOUNDARY);
  112. ensure_equals("INVALID_DATE_24HOUR_BOUNDARY should have failed" , result, false);
  113. ensure_equals("INVALID_DATE_24HOUR_BOUNDARY date still set to old value on failure!" , date.secondsSinceEpoch(), 0);
  114. result = date.fromString(INVALID_DATE_LEAP);
  115. ensure_equals("INVALID_DATE_LEAP should have failed" , result, false);
  116. result = date.fromString(INVALID_DATE_HOUR);
  117. ensure_equals("INVALID_DATE_HOUR should have failed" , result, false);
  118. result = date.fromString(INVALID_DATE_MIN);
  119. ensure_equals("INVALID_DATE_MIN should have failed" , result, false);
  120. result = date.fromString(INVALID_DATE_SEC);
  121. ensure_equals("INVALID_DATE_SEC should have failed" , result, false);
  122. result = date.fromString(INVALID_DATE_YEAR);
  123. ensure_equals("INVALID_DATE_YEAR should have failed" , result, false);
  124. result = date.fromString(INVALID_DATE_MONTH);
  125. ensure_equals("INVALID_DATE_MONTH should have failed" , result, false);
  126. result = date.fromString(INVALID_DATE_DAY);
  127. ensure_equals("INVALID_DATE_DAY should have failed" , result, false);
  128. #endif
  129. }
  130. /* API checks */
  131. template<> template<>
  132. void date_test_object_t::test<3>()
  133. {
  134. LLDate date;
  135. std::istringstream stream(VALID_DATE);
  136. std::string expected_string = VALID_DATE;
  137. date.fromStream(stream);
  138. ensure_equals("fromStream failed", date.asString(), expected_string);
  139. }
  140. template<> template<>
  141. void date_test_object_t::test<4>()
  142. {
  143. LLDate date1(VALID_DATE);
  144. LLDate date2(date1);
  145. ensure_equals("LLDate(const LLDate& date) constructor failed", date1.asString(), date2.asString());
  146. }
  147. template<> template<>
  148. void date_test_object_t::test<5>()
  149. {
  150. LLDate date1(VALID_DATE);
  151. LLDate date2(date1.secondsSinceEpoch());
  152. ensure_equals("secondsSinceEpoch not equal",date1.secondsSinceEpoch(), date2.secondsSinceEpoch());
  153. ensure_equals("LLDate created using secondsSinceEpoch not equal", date1.asString(), date2.asString());
  154. }
  155. template<> template<>
  156. void date_test_object_t::test<6>()
  157. {
  158. LLDate date(VALID_DATE);
  159. std::ostringstream stream;
  160. stream << date;
  161. std::string expected_str = VALID_DATE;
  162. ensure_equals("ostringstream failed", expected_str, stream.str());
  163. }
  164. template<> template<>
  165. void date_test_object_t::test<7>()
  166. {
  167. LLDate date;
  168. std::istringstream stream(VALID_DATE);
  169. stream >> date;
  170. std::string expected_str = VALID_DATE;
  171. std::ostringstream out_stream;
  172. out_stream << date;
  173. ensure_equals("<< failed", date.asString(),expected_str);
  174. ensure_equals("<< to >> failed", stream.str(),out_stream.str());
  175. }
  176. }