testutil.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright (c) 2010, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // ---
  30. // This macro mimics a unittest framework, but is a bit less flexible
  31. // than most. It requires a superclass to derive from, and does all
  32. // work in global constructors. The tricky part is implementing
  33. // TYPED_TEST.
  34. #ifndef SPARSEHASH_TEST_UTIL_H_
  35. #define SPARSEHASH_TEST_UTIL_H_
  36. #include <sparsehash/internal/sparseconfig.h>
  37. #include "config.h"
  38. #include <stdio.h>
  39. #include <stdlib.h> // for exit
  40. #include <stdexcept> // for length_error
  41. _START_GOOGLE_NAMESPACE_
  42. namespace testing {
  43. #define EXPECT_TRUE(cond) do { \
  44. if (!(cond)) { \
  45. ::fputs("Test failed: " #cond "\n", stderr); \
  46. ::exit(1); \
  47. } \
  48. } while (0)
  49. #define EXPECT_FALSE(a) EXPECT_TRUE(!(a))
  50. #define EXPECT_EQ(a, b) EXPECT_TRUE((a) == (b))
  51. #define EXPECT_NE(a, b) EXPECT_TRUE((a) != (b))
  52. #define EXPECT_LT(a, b) EXPECT_TRUE((a) < (b))
  53. #define EXPECT_GT(a, b) EXPECT_TRUE((a) > (b))
  54. #define EXPECT_LE(a, b) EXPECT_TRUE((a) <= (b))
  55. #define EXPECT_GE(a, b) EXPECT_TRUE((a) >= (b))
  56. #define EXPECT_DEATH(cmd, expected_error_string) \
  57. try { \
  58. cmd; \
  59. EXPECT_FALSE("did not see expected error: " #expected_error_string); \
  60. } catch (const std::length_error&) { \
  61. /* Good, the cmd failed. */ \
  62. }
  63. #define TEST(suitename, testname) \
  64. class TEST_##suitename##_##testname { \
  65. public: \
  66. TEST_##suitename##_##testname() { \
  67. ::fputs("Running " #suitename "." #testname "\n", stderr); \
  68. Run(); \
  69. } \
  70. void Run(); \
  71. }; \
  72. static TEST_##suitename##_##testname \
  73. test_instance_##suitename##_##testname; \
  74. void TEST_##suitename##_##testname::Run()
  75. template<typename C1, typename C2, typename C3, typename C4, typename C5,
  76. typename C6> struct TypeList6 {
  77. typedef C1 type1;
  78. typedef C2 type2;
  79. typedef C3 type3;
  80. typedef C4 type4;
  81. typedef C5 type5;
  82. typedef C6 type6;
  83. };
  84. // I need to list 18 types here, for code below to compile, though
  85. // only the first 6 are ever used.
  86. #define TYPED_TEST_CASE_6(classname, typelist) \
  87. typedef typelist::type1 classname##_type1; \
  88. typedef typelist::type2 classname##_type2; \
  89. typedef typelist::type3 classname##_type3; \
  90. typedef typelist::type4 classname##_type4; \
  91. typedef typelist::type5 classname##_type5; \
  92. typedef typelist::type6 classname##_type6; \
  93. static const int classname##_numtypes = 6; \
  94. typedef typelist::type1 classname##_type7; \
  95. typedef typelist::type1 classname##_type8; \
  96. typedef typelist::type1 classname##_type9; \
  97. typedef typelist::type1 classname##_type10; \
  98. typedef typelist::type1 classname##_type11; \
  99. typedef typelist::type1 classname##_type12; \
  100. typedef typelist::type1 classname##_type13; \
  101. typedef typelist::type1 classname##_type14; \
  102. typedef typelist::type1 classname##_type15; \
  103. typedef typelist::type1 classname##_type16; \
  104. typedef typelist::type1 classname##_type17; \
  105. typedef typelist::type1 classname##_type18;
  106. template<typename C1, typename C2, typename C3, typename C4, typename C5,
  107. typename C6, typename C7, typename C8, typename C9, typename C10,
  108. typename C11, typename C12, typename C13, typename C14, typename C15,
  109. typename C16, typename C17, typename C18> struct TypeList18 {
  110. typedef C1 type1;
  111. typedef C2 type2;
  112. typedef C3 type3;
  113. typedef C4 type4;
  114. typedef C5 type5;
  115. typedef C6 type6;
  116. typedef C7 type7;
  117. typedef C8 type8;
  118. typedef C9 type9;
  119. typedef C10 type10;
  120. typedef C11 type11;
  121. typedef C12 type12;
  122. typedef C13 type13;
  123. typedef C14 type14;
  124. typedef C15 type15;
  125. typedef C16 type16;
  126. typedef C17 type17;
  127. typedef C18 type18;
  128. };
  129. #define TYPED_TEST_CASE_18(classname, typelist) \
  130. typedef typelist::type1 classname##_type1; \
  131. typedef typelist::type2 classname##_type2; \
  132. typedef typelist::type3 classname##_type3; \
  133. typedef typelist::type4 classname##_type4; \
  134. typedef typelist::type5 classname##_type5; \
  135. typedef typelist::type6 classname##_type6; \
  136. typedef typelist::type7 classname##_type7; \
  137. typedef typelist::type8 classname##_type8; \
  138. typedef typelist::type9 classname##_type9; \
  139. typedef typelist::type10 classname##_type10; \
  140. typedef typelist::type11 classname##_type11; \
  141. typedef typelist::type12 classname##_type12; \
  142. typedef typelist::type13 classname##_type13; \
  143. typedef typelist::type14 classname##_type14; \
  144. typedef typelist::type15 classname##_type15; \
  145. typedef typelist::type16 classname##_type16; \
  146. typedef typelist::type17 classname##_type17; \
  147. typedef typelist::type18 classname##_type18; \
  148. static const int classname##_numtypes = 18;
  149. #define TYPED_TEST(superclass, testname) \
  150. template<typename TypeParam> \
  151. class TEST_onetype_##superclass##_##testname : \
  152. public superclass<TypeParam> { \
  153. public: \
  154. TEST_onetype_##superclass##_##testname() { \
  155. Run(); \
  156. } \
  157. private: \
  158. void Run(); \
  159. }; \
  160. class TEST_typed_##superclass##_##testname { \
  161. public: \
  162. explicit TEST_typed_##superclass##_##testname() { \
  163. if (superclass##_numtypes >= 1) { \
  164. ::fputs("Running " #superclass "." #testname ".1\n", stderr); \
  165. TEST_onetype_##superclass##_##testname<superclass##_type1> t; \
  166. } \
  167. if (superclass##_numtypes >= 2) { \
  168. ::fputs("Running " #superclass "." #testname ".2\n", stderr); \
  169. TEST_onetype_##superclass##_##testname<superclass##_type2> t; \
  170. } \
  171. if (superclass##_numtypes >= 3) { \
  172. ::fputs("Running " #superclass "." #testname ".3\n", stderr); \
  173. TEST_onetype_##superclass##_##testname<superclass##_type3> t; \
  174. } \
  175. if (superclass##_numtypes >= 4) { \
  176. ::fputs("Running " #superclass "." #testname ".4\n", stderr); \
  177. TEST_onetype_##superclass##_##testname<superclass##_type4> t; \
  178. } \
  179. if (superclass##_numtypes >= 5) { \
  180. ::fputs("Running " #superclass "." #testname ".5\n", stderr); \
  181. TEST_onetype_##superclass##_##testname<superclass##_type5> t; \
  182. } \
  183. if (superclass##_numtypes >= 6) { \
  184. ::fputs("Running " #superclass "." #testname ".6\n", stderr); \
  185. TEST_onetype_##superclass##_##testname<superclass##_type6> t; \
  186. } \
  187. if (superclass##_numtypes >= 7) { \
  188. ::fputs("Running " #superclass "." #testname ".7\n", stderr); \
  189. TEST_onetype_##superclass##_##testname<superclass##_type7> t; \
  190. } \
  191. if (superclass##_numtypes >= 8) { \
  192. ::fputs("Running " #superclass "." #testname ".8\n", stderr); \
  193. TEST_onetype_##superclass##_##testname<superclass##_type8> t; \
  194. } \
  195. if (superclass##_numtypes >= 9) { \
  196. ::fputs("Running " #superclass "." #testname ".9\n", stderr); \
  197. TEST_onetype_##superclass##_##testname<superclass##_type9> t; \
  198. } \
  199. if (superclass##_numtypes >= 10) { \
  200. ::fputs("Running " #superclass "." #testname ".10\n", stderr); \
  201. TEST_onetype_##superclass##_##testname<superclass##_type10> t; \
  202. } \
  203. if (superclass##_numtypes >= 11) { \
  204. ::fputs("Running " #superclass "." #testname ".11\n", stderr); \
  205. TEST_onetype_##superclass##_##testname<superclass##_type11> t; \
  206. } \
  207. if (superclass##_numtypes >= 12) { \
  208. ::fputs("Running " #superclass "." #testname ".12\n", stderr); \
  209. TEST_onetype_##superclass##_##testname<superclass##_type12> t; \
  210. } \
  211. if (superclass##_numtypes >= 13) { \
  212. ::fputs("Running " #superclass "." #testname ".13\n", stderr); \
  213. TEST_onetype_##superclass##_##testname<superclass##_type13> t; \
  214. } \
  215. if (superclass##_numtypes >= 14) { \
  216. ::fputs("Running " #superclass "." #testname ".14\n", stderr); \
  217. TEST_onetype_##superclass##_##testname<superclass##_type14> t; \
  218. } \
  219. if (superclass##_numtypes >= 15) { \
  220. ::fputs("Running " #superclass "." #testname ".15\n", stderr); \
  221. TEST_onetype_##superclass##_##testname<superclass##_type15> t; \
  222. } \
  223. if (superclass##_numtypes >= 16) { \
  224. ::fputs("Running " #superclass "." #testname ".16\n", stderr); \
  225. TEST_onetype_##superclass##_##testname<superclass##_type16> t; \
  226. } \
  227. if (superclass##_numtypes >= 17) { \
  228. ::fputs("Running " #superclass "." #testname ".17\n", stderr); \
  229. TEST_onetype_##superclass##_##testname<superclass##_type17> t; \
  230. } \
  231. if (superclass##_numtypes >= 18) { \
  232. ::fputs("Running " #superclass "." #testname ".18\n", stderr); \
  233. TEST_onetype_##superclass##_##testname<superclass##_type18> t; \
  234. } \
  235. } \
  236. }; \
  237. static TEST_typed_##superclass##_##testname \
  238. test_instance_typed_##superclass##_##testname; \
  239. template<class TypeParam> \
  240. void TEST_onetype_##superclass##_##testname<TypeParam>::Run()
  241. // This is a dummy class just to make converting from internal-google
  242. // to opensourcing easier.
  243. class Test { };
  244. } // namespace testing
  245. _END_GOOGLE_NAMESPACE_
  246. #endif // SPARSEHASH_TEST_UTIL_H_