You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
8.6 KiB

  1. // Copyright 2018, 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. // Google Mock - a framework for writing C++ mock classes.
  30. //
  31. // This file tests the internal preprocessor macro library.
  32. #include "gmock/internal/gmock-pp.h"
  33. #include <string>
  34. #include "gmock/gmock.h"
  35. namespace testing {
  36. namespace {
  37. // Matcher to verify that to strings are identical up to whitespace
  38. // Not 100% correct, because it treats "AB" as equal to "A B".
  39. ::testing::Matcher<const std::string&> SameExceptSpaces(const std::string& s) {
  40. auto remove_spaces = [](std::string to_split) {
  41. to_split.erase(std::remove(to_split.begin(), to_split.end(), ' '),
  42. to_split.end());
  43. return to_split;
  44. };
  45. return ::testing::ResultOf(remove_spaces, remove_spaces(s));
  46. }
  47. // Verify that a macro expands to a given text. Ignores whitespace difference.
  48. // In MSVC, GMOCK_PP_STRINGIZE() returns nothing, rather than "". So concatenate
  49. // with an empty string.
  50. #define EXPECT_EXPANSION(Result, Macro) \
  51. EXPECT_THAT("" GMOCK_PP_STRINGIZE(Macro), SameExceptSpaces(Result))
  52. TEST(Macros, Cat) {
  53. EXPECT_EXPANSION("14", GMOCK_PP_CAT(1, 4));
  54. EXPECT_EXPANSION("+=", GMOCK_PP_CAT(+, =));
  55. }
  56. TEST(Macros, Narg) {
  57. EXPECT_EXPANSION("1", GMOCK_PP_NARG());
  58. EXPECT_EXPANSION("1", GMOCK_PP_NARG(x));
  59. EXPECT_EXPANSION("2", GMOCK_PP_NARG(x, y));
  60. EXPECT_EXPANSION("3", GMOCK_PP_NARG(x, y, z));
  61. EXPECT_EXPANSION("4", GMOCK_PP_NARG(x, y, z, w));
  62. EXPECT_EXPANSION("0", GMOCK_PP_NARG0());
  63. EXPECT_EXPANSION("1", GMOCK_PP_NARG0(x));
  64. EXPECT_EXPANSION("2", GMOCK_PP_NARG0(x, y));
  65. }
  66. TEST(Macros, Comma) {
  67. EXPECT_EXPANSION("0", GMOCK_PP_HAS_COMMA());
  68. EXPECT_EXPANSION("1", GMOCK_PP_HAS_COMMA(, ));
  69. EXPECT_EXPANSION("0", GMOCK_PP_HAS_COMMA((, )));
  70. }
  71. TEST(Macros, IsEmpty) {
  72. EXPECT_EXPANSION("1", GMOCK_PP_IS_EMPTY());
  73. EXPECT_EXPANSION("0", GMOCK_PP_IS_EMPTY(, ));
  74. EXPECT_EXPANSION("0", GMOCK_PP_IS_EMPTY(a));
  75. EXPECT_EXPANSION("0", GMOCK_PP_IS_EMPTY(()));
  76. #define GMOCK_PP_INTERNAL_IS_EMPTY_TEST_1
  77. EXPECT_EXPANSION("1", GMOCK_PP_IS_EMPTY(GMOCK_PP_INTERNAL_IS_EMPTY_TEST_1));
  78. }
  79. TEST(Macros, If) {
  80. EXPECT_EXPANSION("1", GMOCK_PP_IF(1, 1, 2));
  81. EXPECT_EXPANSION("2", GMOCK_PP_IF(0, 1, 2));
  82. }
  83. TEST(Macros, HeadTail) {
  84. EXPECT_EXPANSION("1", GMOCK_PP_HEAD(1));
  85. EXPECT_EXPANSION("1", GMOCK_PP_HEAD(1, 2));
  86. EXPECT_EXPANSION("1", GMOCK_PP_HEAD(1, 2, 3));
  87. EXPECT_EXPANSION("", GMOCK_PP_TAIL(1));
  88. EXPECT_EXPANSION("2", GMOCK_PP_TAIL(1, 2));
  89. EXPECT_EXPANSION("2", GMOCK_PP_HEAD(GMOCK_PP_TAIL(1, 2, 3)));
  90. }
  91. TEST(Macros, Parentheses) {
  92. EXPECT_EXPANSION("0", GMOCK_PP_IS_BEGIN_PARENS(sss));
  93. EXPECT_EXPANSION("0", GMOCK_PP_IS_BEGIN_PARENS(sss()));
  94. EXPECT_EXPANSION("0", GMOCK_PP_IS_BEGIN_PARENS(sss() sss));
  95. EXPECT_EXPANSION("1", GMOCK_PP_IS_BEGIN_PARENS((sss)));
  96. EXPECT_EXPANSION("1", GMOCK_PP_IS_BEGIN_PARENS((sss)ss));
  97. EXPECT_EXPANSION("0", GMOCK_PP_IS_ENCLOSED_PARENS(sss));
  98. EXPECT_EXPANSION("0", GMOCK_PP_IS_ENCLOSED_PARENS(sss()));
  99. EXPECT_EXPANSION("0", GMOCK_PP_IS_ENCLOSED_PARENS(sss() sss));
  100. EXPECT_EXPANSION("1", GMOCK_PP_IS_ENCLOSED_PARENS((sss)));
  101. EXPECT_EXPANSION("0", GMOCK_PP_IS_ENCLOSED_PARENS((sss)ss));
  102. EXPECT_EXPANSION("1 + 1", GMOCK_PP_REMOVE_PARENS((1 + 1)));
  103. }
  104. TEST(Macros, Increment) {
  105. EXPECT_EXPANSION("1", GMOCK_PP_INC(0));
  106. EXPECT_EXPANSION("2", GMOCK_PP_INC(1));
  107. EXPECT_EXPANSION("3", GMOCK_PP_INC(2));
  108. EXPECT_EXPANSION("4", GMOCK_PP_INC(3));
  109. EXPECT_EXPANSION("5", GMOCK_PP_INC(4));
  110. EXPECT_EXPANSION("16", GMOCK_PP_INC(15));
  111. }
  112. #define JOINER_CAT(a, b) a##b
  113. #define JOINER(_N, _Data, _Elem) JOINER_CAT(_Data, _N) = _Elem
  114. TEST(Macros, Repeat) {
  115. EXPECT_EXPANSION("", GMOCK_PP_REPEAT(JOINER, X, 0));
  116. EXPECT_EXPANSION("X0=", GMOCK_PP_REPEAT(JOINER, X, 1));
  117. EXPECT_EXPANSION("X0= X1=", GMOCK_PP_REPEAT(JOINER, X, 2));
  118. EXPECT_EXPANSION("X0= X1= X2=", GMOCK_PP_REPEAT(JOINER, X, 3));
  119. EXPECT_EXPANSION("X0= X1= X2= X3=", GMOCK_PP_REPEAT(JOINER, X, 4));
  120. EXPECT_EXPANSION("X0= X1= X2= X3= X4=", GMOCK_PP_REPEAT(JOINER, X, 5));
  121. EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5=", GMOCK_PP_REPEAT(JOINER, X, 6));
  122. EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6=",
  123. GMOCK_PP_REPEAT(JOINER, X, 7));
  124. EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7=",
  125. GMOCK_PP_REPEAT(JOINER, X, 8));
  126. EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8=",
  127. GMOCK_PP_REPEAT(JOINER, X, 9));
  128. EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9=",
  129. GMOCK_PP_REPEAT(JOINER, X, 10));
  130. EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10=",
  131. GMOCK_PP_REPEAT(JOINER, X, 11));
  132. EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11=",
  133. GMOCK_PP_REPEAT(JOINER, X, 12));
  134. EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11= X12=",
  135. GMOCK_PP_REPEAT(JOINER, X, 13));
  136. EXPECT_EXPANSION(
  137. "X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11= X12= X13=",
  138. GMOCK_PP_REPEAT(JOINER, X, 14));
  139. EXPECT_EXPANSION(
  140. "X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11= X12= X13= X14=",
  141. GMOCK_PP_REPEAT(JOINER, X, 15));
  142. }
  143. TEST(Macros, ForEach) {
  144. EXPECT_EXPANSION("", GMOCK_PP_FOR_EACH(JOINER, X, ()));
  145. EXPECT_EXPANSION("X0=a", GMOCK_PP_FOR_EACH(JOINER, X, (a)));
  146. EXPECT_EXPANSION("X0=a X1=b", GMOCK_PP_FOR_EACH(JOINER, X, (a, b)));
  147. EXPECT_EXPANSION("X0=a X1=b X2=c", GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c)));
  148. EXPECT_EXPANSION("X0=a X1=b X2=c X3=d",
  149. GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d)));
  150. EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e",
  151. GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e)));
  152. EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f",
  153. GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f)));
  154. EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f X6=g",
  155. GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g)));
  156. EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h",
  157. GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h)));
  158. EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i",
  159. GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i)));
  160. EXPECT_EXPANSION(
  161. "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j",
  162. GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j)));
  163. EXPECT_EXPANSION(
  164. "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k",
  165. GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k)));
  166. EXPECT_EXPANSION(
  167. "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k X11=l",
  168. GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k, l)));
  169. EXPECT_EXPANSION(
  170. "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k X11=l X12=m",
  171. GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k, l, m)));
  172. EXPECT_EXPANSION(
  173. "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k X11=l X12=m "
  174. "X13=n",
  175. GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k, l, m, n)));
  176. EXPECT_EXPANSION(
  177. "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k X11=l X12=m "
  178. "X13=n X14=o",
  179. GMOCK_PP_FOR_EACH(JOINER, X,
  180. (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)));
  181. }
  182. } // namespace
  183. } // namespace testing