PageRenderTime 31ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/test/lltut.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 205 lines | 149 code | 27 blank | 29 comment | 14 complexity | bea6f26ccc522f7bf50ec6eb2dcbb23b MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lltut.cpp
  3. * @author Mark Lentczner
  4. * @date 5/16/06
  5. * @brief MacTester
  6. *
  7. * $LicenseInfo:firstyear=2006&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 "lltut.h"
  30. #include "lldate.h"
  31. #include "llformat.h"
  32. #include "llsd.h"
  33. #include "lluri.h"
  34. #include "stringize.h"
  35. namespace tut
  36. {
  37. void ensure_equals(const char* msg, const LLDate& actual,
  38. const LLDate& expected)
  39. {
  40. ensure_equals(msg,
  41. actual.secondsSinceEpoch(), expected.secondsSinceEpoch());
  42. }
  43. void ensure_equals(const char* msg, const LLURI& actual,
  44. const LLURI& expected)
  45. {
  46. ensure_equals(msg,
  47. actual.asString(), expected.asString());
  48. }
  49. void ensure_equals(const char* msg,
  50. const std::vector<U8>& actual, const std::vector<U8>& expected)
  51. {
  52. std::string s(msg);
  53. ensure_equals(s + " size", actual.size(), expected.size());
  54. std::vector<U8>::const_iterator i, j;
  55. int k;
  56. for (i = actual.begin(), j = expected.begin(), k = 0;
  57. i != actual.end();
  58. ++i, ++j, ++k)
  59. {
  60. ensure_equals(s + " field", *i, *j);
  61. }
  62. }
  63. void ensure_equals(const char* m, const LLSD& actual,
  64. const LLSD& expected)
  65. {
  66. ensure_equals(std::string(m), actual, expected);
  67. }
  68. void ensure_equals(const std::string& msg, const LLSD& actual,
  69. const LLSD& expected)
  70. {
  71. ensure_equals(msg + " type", actual.type(), expected.type());
  72. switch (actual.type())
  73. {
  74. case LLSD::TypeUndefined:
  75. return;
  76. case LLSD::TypeBoolean:
  77. ensure_equals(msg + " boolean", actual.asBoolean(), expected.asBoolean());
  78. return;
  79. case LLSD::TypeInteger:
  80. ensure_equals(msg + " integer", actual.asInteger(), expected.asInteger());
  81. return;
  82. case LLSD::TypeReal:
  83. ensure_equals(msg + " real", actual.asReal(), expected.asReal());
  84. return;
  85. case LLSD::TypeString:
  86. ensure_equals(msg + " string", actual.asString(), expected.asString());
  87. return;
  88. case LLSD::TypeUUID:
  89. ensure_equals(msg + " uuid", actual.asUUID(), expected.asUUID());
  90. return;
  91. case LLSD::TypeDate:
  92. ensure_equals(msg + " date", actual.asDate(), expected.asDate());
  93. return;
  94. case LLSD::TypeURI:
  95. ensure_equals(msg + " uri", actual.asURI(), expected.asURI());
  96. return;
  97. case LLSD::TypeBinary:
  98. ensure_equals(msg + " binary", actual.asBinary(), expected.asBinary());
  99. return;
  100. case LLSD::TypeMap:
  101. {
  102. ensure_equals(msg + " map size", actual.size(), expected.size());
  103. LLSD::map_const_iterator actual_iter = actual.beginMap();
  104. LLSD::map_const_iterator expected_iter = expected.beginMap();
  105. while(actual_iter != actual.endMap())
  106. {
  107. ensure_equals(msg + " map keys",
  108. actual_iter->first, expected_iter->first);
  109. ensure_equals(msg + "[" + actual_iter->first + "]",
  110. actual_iter->second, expected_iter->second);
  111. ++actual_iter;
  112. ++expected_iter;
  113. }
  114. return;
  115. }
  116. case LLSD::TypeArray:
  117. {
  118. ensure_equals(msg + " array size", actual.size(), expected.size());
  119. for(int i = 0; i < actual.size(); ++i)
  120. {
  121. ensure_equals(msg + llformat("[%d]", i),
  122. actual[i], expected[i]);
  123. }
  124. return;
  125. }
  126. default:
  127. // should never get here, but compiler produces warning if we
  128. // don't cover this case, and at Linden warnings are fatal.
  129. throw failure(STRINGIZE("invalid type field " << actual.type()));
  130. }
  131. }
  132. void ensure_starts_with(const std::string& msg,
  133. const std::string& actual, const std::string& expectedStart)
  134. {
  135. if( actual.find(expectedStart, 0) != 0 )
  136. {
  137. std::stringstream ss;
  138. ss << msg << ": " << "expected to find " << expectedStart
  139. << " at start of actual " << actual;
  140. throw failure(ss.str().c_str());
  141. }
  142. }
  143. void ensure_ends_with(const std::string& msg,
  144. const std::string& actual, const std::string& expectedEnd)
  145. {
  146. if( actual.size() < expectedEnd.size()
  147. || actual.rfind(expectedEnd)
  148. != (actual.size() - expectedEnd.size()) )
  149. {
  150. std::stringstream ss;
  151. ss << msg << ": " << "expected to find " << expectedEnd
  152. << " at end of actual " << actual;
  153. throw failure(ss.str().c_str());
  154. }
  155. }
  156. void ensure_contains(const std::string& msg,
  157. const std::string& actual, const std::string& expectedSubString)
  158. {
  159. if( actual.find(expectedSubString, 0) == std::string::npos )
  160. {
  161. std::stringstream ss;
  162. ss << msg << ": " << "expected to find " << expectedSubString
  163. << " in actual " << actual;
  164. throw failure(ss.str().c_str());
  165. }
  166. }
  167. void ensure_does_not_contain(const std::string& msg,
  168. const std::string& actual, const std::string& expectedSubString)
  169. {
  170. if( actual.find(expectedSubString, 0) != std::string::npos )
  171. {
  172. std::stringstream ss;
  173. ss << msg << ": " << "expected not to find " << expectedSubString
  174. << " in actual " << actual;
  175. throw failure(ss.str().c_str());
  176. }
  177. }
  178. }