gmock-matchers-arithmetic_test.cc 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. // Copyright 2007, 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 some commonly used argument matchers.
  32. #include <cmath>
  33. #include <limits>
  34. #include <memory>
  35. #include <string>
  36. #include "test/gmock-matchers_test.h"
  37. // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
  38. // possible loss of data and C4100, unreferenced local parameter
  39. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
  40. namespace testing {
  41. namespace gmock_matchers_test {
  42. namespace {
  43. typedef ::std::tuple<long, int> Tuple2; // NOLINT
  44. // Tests that Eq() matches a 2-tuple where the first field == the
  45. // second field.
  46. TEST(Eq2Test, MatchesEqualArguments) {
  47. Matcher<const Tuple2&> m = Eq();
  48. EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
  49. EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
  50. }
  51. // Tests that Eq() describes itself properly.
  52. TEST(Eq2Test, CanDescribeSelf) {
  53. Matcher<const Tuple2&> m = Eq();
  54. EXPECT_EQ("are an equal pair", Describe(m));
  55. }
  56. // Tests that Ge() matches a 2-tuple where the first field >= the
  57. // second field.
  58. TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
  59. Matcher<const Tuple2&> m = Ge();
  60. EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
  61. EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
  62. EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
  63. }
  64. // Tests that Ge() describes itself properly.
  65. TEST(Ge2Test, CanDescribeSelf) {
  66. Matcher<const Tuple2&> m = Ge();
  67. EXPECT_EQ("are a pair where the first >= the second", Describe(m));
  68. }
  69. // Tests that Gt() matches a 2-tuple where the first field > the
  70. // second field.
  71. TEST(Gt2Test, MatchesGreaterThanArguments) {
  72. Matcher<const Tuple2&> m = Gt();
  73. EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
  74. EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
  75. EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
  76. }
  77. // Tests that Gt() describes itself properly.
  78. TEST(Gt2Test, CanDescribeSelf) {
  79. Matcher<const Tuple2&> m = Gt();
  80. EXPECT_EQ("are a pair where the first > the second", Describe(m));
  81. }
  82. // Tests that Le() matches a 2-tuple where the first field <= the
  83. // second field.
  84. TEST(Le2Test, MatchesLessThanOrEqualArguments) {
  85. Matcher<const Tuple2&> m = Le();
  86. EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
  87. EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
  88. EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
  89. }
  90. // Tests that Le() describes itself properly.
  91. TEST(Le2Test, CanDescribeSelf) {
  92. Matcher<const Tuple2&> m = Le();
  93. EXPECT_EQ("are a pair where the first <= the second", Describe(m));
  94. }
  95. // Tests that Lt() matches a 2-tuple where the first field < the
  96. // second field.
  97. TEST(Lt2Test, MatchesLessThanArguments) {
  98. Matcher<const Tuple2&> m = Lt();
  99. EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
  100. EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
  101. EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
  102. }
  103. // Tests that Lt() describes itself properly.
  104. TEST(Lt2Test, CanDescribeSelf) {
  105. Matcher<const Tuple2&> m = Lt();
  106. EXPECT_EQ("are a pair where the first < the second", Describe(m));
  107. }
  108. // Tests that Ne() matches a 2-tuple where the first field != the
  109. // second field.
  110. TEST(Ne2Test, MatchesUnequalArguments) {
  111. Matcher<const Tuple2&> m = Ne();
  112. EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
  113. EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
  114. EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
  115. }
  116. // Tests that Ne() describes itself properly.
  117. TEST(Ne2Test, CanDescribeSelf) {
  118. Matcher<const Tuple2&> m = Ne();
  119. EXPECT_EQ("are an unequal pair", Describe(m));
  120. }
  121. TEST(PairMatchBaseTest, WorksWithMoveOnly) {
  122. using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
  123. Matcher<Pointers> matcher = Eq();
  124. Pointers pointers;
  125. // Tested values don't matter; the point is that matcher does not copy the
  126. // matched values.
  127. EXPECT_TRUE(matcher.Matches(pointers));
  128. }
  129. // Tests that IsNan() matches a NaN, with float.
  130. TEST(IsNan, FloatMatchesNan) {
  131. float quiet_nan = std::numeric_limits<float>::quiet_NaN();
  132. float other_nan = std::nanf("1");
  133. float real_value = 1.0f;
  134. Matcher<float> m = IsNan();
  135. EXPECT_TRUE(m.Matches(quiet_nan));
  136. EXPECT_TRUE(m.Matches(other_nan));
  137. EXPECT_FALSE(m.Matches(real_value));
  138. Matcher<float&> m_ref = IsNan();
  139. EXPECT_TRUE(m_ref.Matches(quiet_nan));
  140. EXPECT_TRUE(m_ref.Matches(other_nan));
  141. EXPECT_FALSE(m_ref.Matches(real_value));
  142. Matcher<const float&> m_cref = IsNan();
  143. EXPECT_TRUE(m_cref.Matches(quiet_nan));
  144. EXPECT_TRUE(m_cref.Matches(other_nan));
  145. EXPECT_FALSE(m_cref.Matches(real_value));
  146. }
  147. // Tests that IsNan() matches a NaN, with double.
  148. TEST(IsNan, DoubleMatchesNan) {
  149. double quiet_nan = std::numeric_limits<double>::quiet_NaN();
  150. double other_nan = std::nan("1");
  151. double real_value = 1.0;
  152. Matcher<double> m = IsNan();
  153. EXPECT_TRUE(m.Matches(quiet_nan));
  154. EXPECT_TRUE(m.Matches(other_nan));
  155. EXPECT_FALSE(m.Matches(real_value));
  156. Matcher<double&> m_ref = IsNan();
  157. EXPECT_TRUE(m_ref.Matches(quiet_nan));
  158. EXPECT_TRUE(m_ref.Matches(other_nan));
  159. EXPECT_FALSE(m_ref.Matches(real_value));
  160. Matcher<const double&> m_cref = IsNan();
  161. EXPECT_TRUE(m_cref.Matches(quiet_nan));
  162. EXPECT_TRUE(m_cref.Matches(other_nan));
  163. EXPECT_FALSE(m_cref.Matches(real_value));
  164. }
  165. // Tests that IsNan() matches a NaN, with long double.
  166. TEST(IsNan, LongDoubleMatchesNan) {
  167. long double quiet_nan = std::numeric_limits<long double>::quiet_NaN();
  168. long double other_nan = std::nan("1");
  169. long double real_value = 1.0;
  170. Matcher<long double> m = IsNan();
  171. EXPECT_TRUE(m.Matches(quiet_nan));
  172. EXPECT_TRUE(m.Matches(other_nan));
  173. EXPECT_FALSE(m.Matches(real_value));
  174. Matcher<long double&> m_ref = IsNan();
  175. EXPECT_TRUE(m_ref.Matches(quiet_nan));
  176. EXPECT_TRUE(m_ref.Matches(other_nan));
  177. EXPECT_FALSE(m_ref.Matches(real_value));
  178. Matcher<const long double&> m_cref = IsNan();
  179. EXPECT_TRUE(m_cref.Matches(quiet_nan));
  180. EXPECT_TRUE(m_cref.Matches(other_nan));
  181. EXPECT_FALSE(m_cref.Matches(real_value));
  182. }
  183. // Tests that IsNan() works with Not.
  184. TEST(IsNan, NotMatchesNan) {
  185. Matcher<float> mf = Not(IsNan());
  186. EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN()));
  187. EXPECT_FALSE(mf.Matches(std::nanf("1")));
  188. EXPECT_TRUE(mf.Matches(1.0));
  189. Matcher<double> md = Not(IsNan());
  190. EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN()));
  191. EXPECT_FALSE(md.Matches(std::nan("1")));
  192. EXPECT_TRUE(md.Matches(1.0));
  193. Matcher<long double> mld = Not(IsNan());
  194. EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN()));
  195. EXPECT_FALSE(mld.Matches(std::nanl("1")));
  196. EXPECT_TRUE(mld.Matches(1.0));
  197. }
  198. // Tests that IsNan() can describe itself.
  199. TEST(IsNan, CanDescribeSelf) {
  200. Matcher<float> mf = IsNan();
  201. EXPECT_EQ("is NaN", Describe(mf));
  202. Matcher<double> md = IsNan();
  203. EXPECT_EQ("is NaN", Describe(md));
  204. Matcher<long double> mld = IsNan();
  205. EXPECT_EQ("is NaN", Describe(mld));
  206. }
  207. // Tests that IsNan() can describe itself with Not.
  208. TEST(IsNan, CanDescribeSelfWithNot) {
  209. Matcher<float> mf = Not(IsNan());
  210. EXPECT_EQ("isn't NaN", Describe(mf));
  211. Matcher<double> md = Not(IsNan());
  212. EXPECT_EQ("isn't NaN", Describe(md));
  213. Matcher<long double> mld = Not(IsNan());
  214. EXPECT_EQ("isn't NaN", Describe(mld));
  215. }
  216. // Tests that FloatEq() matches a 2-tuple where
  217. // FloatEq(first field) matches the second field.
  218. TEST(FloatEq2Test, MatchesEqualArguments) {
  219. typedef ::std::tuple<float, float> Tpl;
  220. Matcher<const Tpl&> m = FloatEq();
  221. EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
  222. EXPECT_TRUE(m.Matches(Tpl(0.3f, 0.1f + 0.1f + 0.1f)));
  223. EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
  224. }
  225. // Tests that FloatEq() describes itself properly.
  226. TEST(FloatEq2Test, CanDescribeSelf) {
  227. Matcher<const ::std::tuple<float, float>&> m = FloatEq();
  228. EXPECT_EQ("are an almost-equal pair", Describe(m));
  229. }
  230. // Tests that NanSensitiveFloatEq() matches a 2-tuple where
  231. // NanSensitiveFloatEq(first field) matches the second field.
  232. TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) {
  233. typedef ::std::tuple<float, float> Tpl;
  234. Matcher<const Tpl&> m = NanSensitiveFloatEq();
  235. EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
  236. EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
  237. std::numeric_limits<float>::quiet_NaN())));
  238. EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
  239. EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
  240. EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
  241. }
  242. // Tests that NanSensitiveFloatEq() describes itself properly.
  243. TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {
  244. Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq();
  245. EXPECT_EQ("are an almost-equal pair", Describe(m));
  246. }
  247. // Tests that DoubleEq() matches a 2-tuple where
  248. // DoubleEq(first field) matches the second field.
  249. TEST(DoubleEq2Test, MatchesEqualArguments) {
  250. typedef ::std::tuple<double, double> Tpl;
  251. Matcher<const Tpl&> m = DoubleEq();
  252. EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));
  253. EXPECT_TRUE(m.Matches(Tpl(0.3, 0.1 + 0.1 + 0.1)));
  254. EXPECT_FALSE(m.Matches(Tpl(1.1, 1.0)));
  255. }
  256. // Tests that DoubleEq() describes itself properly.
  257. TEST(DoubleEq2Test, CanDescribeSelf) {
  258. Matcher<const ::std::tuple<double, double>&> m = DoubleEq();
  259. EXPECT_EQ("are an almost-equal pair", Describe(m));
  260. }
  261. // Tests that NanSensitiveDoubleEq() matches a 2-tuple where
  262. // NanSensitiveDoubleEq(first field) matches the second field.
  263. TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) {
  264. typedef ::std::tuple<double, double> Tpl;
  265. Matcher<const Tpl&> m = NanSensitiveDoubleEq();
  266. EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
  267. EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
  268. std::numeric_limits<double>::quiet_NaN())));
  269. EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
  270. EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
  271. EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
  272. }
  273. // Tests that DoubleEq() describes itself properly.
  274. TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {
  275. Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq();
  276. EXPECT_EQ("are an almost-equal pair", Describe(m));
  277. }
  278. // Tests that FloatEq() matches a 2-tuple where
  279. // FloatNear(first field, max_abs_error) matches the second field.
  280. TEST(FloatNear2Test, MatchesEqualArguments) {
  281. typedef ::std::tuple<float, float> Tpl;
  282. Matcher<const Tpl&> m = FloatNear(0.5f);
  283. EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
  284. EXPECT_TRUE(m.Matches(Tpl(1.3f, 1.0f)));
  285. EXPECT_FALSE(m.Matches(Tpl(1.8f, 1.0f)));
  286. }
  287. // Tests that FloatNear() describes itself properly.
  288. TEST(FloatNear2Test, CanDescribeSelf) {
  289. Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f);
  290. EXPECT_EQ("are an almost-equal pair", Describe(m));
  291. }
  292. // Tests that NanSensitiveFloatNear() matches a 2-tuple where
  293. // NanSensitiveFloatNear(first field) matches the second field.
  294. TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) {
  295. typedef ::std::tuple<float, float> Tpl;
  296. Matcher<const Tpl&> m = NanSensitiveFloatNear(0.5f);
  297. EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
  298. EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));
  299. EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
  300. std::numeric_limits<float>::quiet_NaN())));
  301. EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));
  302. EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
  303. EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
  304. }
  305. // Tests that NanSensitiveFloatNear() describes itself properly.
  306. TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {
  307. Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f);
  308. EXPECT_EQ("are an almost-equal pair", Describe(m));
  309. }
  310. // Tests that FloatEq() matches a 2-tuple where
  311. // DoubleNear(first field, max_abs_error) matches the second field.
  312. TEST(DoubleNear2Test, MatchesEqualArguments) {
  313. typedef ::std::tuple<double, double> Tpl;
  314. Matcher<const Tpl&> m = DoubleNear(0.5);
  315. EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));
  316. EXPECT_TRUE(m.Matches(Tpl(1.3, 1.0)));
  317. EXPECT_FALSE(m.Matches(Tpl(1.8, 1.0)));
  318. }
  319. // Tests that DoubleNear() describes itself properly.
  320. TEST(DoubleNear2Test, CanDescribeSelf) {
  321. Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5);
  322. EXPECT_EQ("are an almost-equal pair", Describe(m));
  323. }
  324. // Tests that NanSensitiveDoubleNear() matches a 2-tuple where
  325. // NanSensitiveDoubleNear(first field) matches the second field.
  326. TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) {
  327. typedef ::std::tuple<double, double> Tpl;
  328. Matcher<const Tpl&> m = NanSensitiveDoubleNear(0.5f);
  329. EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
  330. EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));
  331. EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
  332. std::numeric_limits<double>::quiet_NaN())));
  333. EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));
  334. EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
  335. EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
  336. }
  337. // Tests that NanSensitiveDoubleNear() describes itself properly.
  338. TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {
  339. Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f);
  340. EXPECT_EQ("are an almost-equal pair", Describe(m));
  341. }
  342. // Tests that Not(m) matches any value that doesn't match m.
  343. TEST(NotTest, NegatesMatcher) {
  344. Matcher<int> m;
  345. m = Not(Eq(2));
  346. EXPECT_TRUE(m.Matches(3));
  347. EXPECT_FALSE(m.Matches(2));
  348. }
  349. // Tests that Not(m) describes itself properly.
  350. TEST(NotTest, CanDescribeSelf) {
  351. Matcher<int> m = Not(Eq(5));
  352. EXPECT_EQ("isn't equal to 5", Describe(m));
  353. }
  354. // Tests that monomorphic matchers are safely cast by the Not matcher.
  355. TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
  356. // greater_than_5 is a monomorphic matcher.
  357. Matcher<int> greater_than_5 = Gt(5);
  358. Matcher<const int&> m = Not(greater_than_5);
  359. Matcher<int&> m2 = Not(greater_than_5);
  360. Matcher<int&> m3 = Not(m);
  361. }
  362. // Helper to allow easy testing of AllOf matchers with num parameters.
  363. void AllOfMatches(int num, const Matcher<int>& m) {
  364. SCOPED_TRACE(Describe(m));
  365. EXPECT_TRUE(m.Matches(0));
  366. for (int i = 1; i <= num; ++i) {
  367. EXPECT_FALSE(m.Matches(i));
  368. }
  369. EXPECT_TRUE(m.Matches(num + 1));
  370. }
  371. INSTANTIATE_GTEST_MATCHER_TEST_P(AllOfTest);
  372. // Tests that AllOf(m1, ..., mn) matches any value that matches all of
  373. // the given matchers.
  374. TEST(AllOfTest, MatchesWhenAllMatch) {
  375. Matcher<int> m;
  376. m = AllOf(Le(2), Ge(1));
  377. EXPECT_TRUE(m.Matches(1));
  378. EXPECT_TRUE(m.Matches(2));
  379. EXPECT_FALSE(m.Matches(0));
  380. EXPECT_FALSE(m.Matches(3));
  381. m = AllOf(Gt(0), Ne(1), Ne(2));
  382. EXPECT_TRUE(m.Matches(3));
  383. EXPECT_FALSE(m.Matches(2));
  384. EXPECT_FALSE(m.Matches(1));
  385. EXPECT_FALSE(m.Matches(0));
  386. m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
  387. EXPECT_TRUE(m.Matches(4));
  388. EXPECT_FALSE(m.Matches(3));
  389. EXPECT_FALSE(m.Matches(2));
  390. EXPECT_FALSE(m.Matches(1));
  391. EXPECT_FALSE(m.Matches(0));
  392. m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
  393. EXPECT_TRUE(m.Matches(0));
  394. EXPECT_TRUE(m.Matches(1));
  395. EXPECT_FALSE(m.Matches(3));
  396. // The following tests for varying number of sub-matchers. Due to the way
  397. // the sub-matchers are handled it is enough to test every sub-matcher once
  398. // with sub-matchers using the same matcher type. Varying matcher types are
  399. // checked for above.
  400. AllOfMatches(2, AllOf(Ne(1), Ne(2)));
  401. AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
  402. AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
  403. AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
  404. AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
  405. AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
  406. AllOfMatches(8,
  407. AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8)));
  408. AllOfMatches(
  409. 9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9)));
  410. AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
  411. Ne(9), Ne(10)));
  412. AllOfMatches(
  413. 50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
  414. Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17),
  415. Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25),
  416. Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33),
  417. Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41),
  418. Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),
  419. Ne(50)));
  420. }
  421. // Tests that AllOf(m1, ..., mn) describes itself properly.
  422. TEST(AllOfTest, CanDescribeSelf) {
  423. Matcher<int> m;
  424. m = AllOf(Le(2), Ge(1));
  425. EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
  426. m = AllOf(Gt(0), Ne(1), Ne(2));
  427. std::string expected_descr1 =
  428. "(is > 0) and (isn't equal to 1) and (isn't equal to 2)";
  429. EXPECT_EQ(expected_descr1, Describe(m));
  430. m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
  431. std::string expected_descr2 =
  432. "(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "
  433. "to 3)";
  434. EXPECT_EQ(expected_descr2, Describe(m));
  435. m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
  436. std::string expected_descr3 =
  437. "(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "
  438. "and (isn't equal to 7)";
  439. EXPECT_EQ(expected_descr3, Describe(m));
  440. }
  441. // Tests that AllOf(m1, ..., mn) describes its negation properly.
  442. TEST(AllOfTest, CanDescribeNegation) {
  443. Matcher<int> m;
  444. m = AllOf(Le(2), Ge(1));
  445. std::string expected_descr4 = "(isn't <= 2) or (isn't >= 1)";
  446. EXPECT_EQ(expected_descr4, DescribeNegation(m));
  447. m = AllOf(Gt(0), Ne(1), Ne(2));
  448. std::string expected_descr5 =
  449. "(isn't > 0) or (is equal to 1) or (is equal to 2)";
  450. EXPECT_EQ(expected_descr5, DescribeNegation(m));
  451. m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
  452. std::string expected_descr6 =
  453. "(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";
  454. EXPECT_EQ(expected_descr6, DescribeNegation(m));
  455. m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
  456. std::string expected_desr7 =
  457. "(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "
  458. "(is equal to 7)";
  459. EXPECT_EQ(expected_desr7, DescribeNegation(m));
  460. m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
  461. Ne(10), Ne(11));
  462. AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  463. EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11)"));
  464. AllOfMatches(11, m);
  465. }
  466. // Tests that monomorphic matchers are safely cast by the AllOf matcher.
  467. TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
  468. // greater_than_5 and less_than_10 are monomorphic matchers.
  469. Matcher<int> greater_than_5 = Gt(5);
  470. Matcher<int> less_than_10 = Lt(10);
  471. Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
  472. Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
  473. Matcher<int&> m3 = AllOf(greater_than_5, m2);
  474. // Tests that BothOf works when composing itself.
  475. Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
  476. Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
  477. }
  478. TEST_P(AllOfTestP, ExplainsResult) {
  479. Matcher<int> m;
  480. // Successful match. Both matchers need to explain. The second
  481. // matcher doesn't give an explanation, so only the first matcher's
  482. // explanation is printed.
  483. m = AllOf(GreaterThan(10), Lt(30));
  484. EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
  485. // Successful match. Both matchers need to explain.
  486. m = AllOf(GreaterThan(10), GreaterThan(20));
  487. EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
  488. Explain(m, 30));
  489. // Successful match. All matchers need to explain. The second
  490. // matcher doesn't given an explanation.
  491. m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
  492. EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
  493. Explain(m, 25));
  494. // Successful match. All matchers need to explain.
  495. m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
  496. EXPECT_EQ(
  497. "which is 30 more than 10, and which is 20 more than 20, "
  498. "and which is 10 more than 30",
  499. Explain(m, 40));
  500. // Failed match. The first matcher, which failed, needs to
  501. // explain.
  502. m = AllOf(GreaterThan(10), GreaterThan(20));
  503. EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
  504. // Failed match. The second matcher, which failed, needs to
  505. // explain. Since it doesn't given an explanation, nothing is
  506. // printed.
  507. m = AllOf(GreaterThan(10), Lt(30));
  508. EXPECT_EQ("", Explain(m, 40));
  509. // Failed match. The second matcher, which failed, needs to
  510. // explain.
  511. m = AllOf(GreaterThan(10), GreaterThan(20));
  512. EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
  513. }
  514. // Helper to allow easy testing of AnyOf matchers with num parameters.
  515. static void AnyOfMatches(int num, const Matcher<int>& m) {
  516. SCOPED_TRACE(Describe(m));
  517. EXPECT_FALSE(m.Matches(0));
  518. for (int i = 1; i <= num; ++i) {
  519. EXPECT_TRUE(m.Matches(i));
  520. }
  521. EXPECT_FALSE(m.Matches(num + 1));
  522. }
  523. static void AnyOfStringMatches(int num, const Matcher<std::string>& m) {
  524. SCOPED_TRACE(Describe(m));
  525. EXPECT_FALSE(m.Matches(std::to_string(0)));
  526. for (int i = 1; i <= num; ++i) {
  527. EXPECT_TRUE(m.Matches(std::to_string(i)));
  528. }
  529. EXPECT_FALSE(m.Matches(std::to_string(num + 1)));
  530. }
  531. INSTANTIATE_GTEST_MATCHER_TEST_P(AnyOfTest);
  532. // Tests that AnyOf(m1, ..., mn) matches any value that matches at
  533. // least one of the given matchers.
  534. TEST(AnyOfTest, MatchesWhenAnyMatches) {
  535. Matcher<int> m;
  536. m = AnyOf(Le(1), Ge(3));
  537. EXPECT_TRUE(m.Matches(1));
  538. EXPECT_TRUE(m.Matches(4));
  539. EXPECT_FALSE(m.Matches(2));
  540. m = AnyOf(Lt(0), Eq(1), Eq(2));
  541. EXPECT_TRUE(m.Matches(-1));
  542. EXPECT_TRUE(m.Matches(1));
  543. EXPECT_TRUE(m.Matches(2));
  544. EXPECT_FALSE(m.Matches(0));
  545. m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
  546. EXPECT_TRUE(m.Matches(-1));
  547. EXPECT_TRUE(m.Matches(1));
  548. EXPECT_TRUE(m.Matches(2));
  549. EXPECT_TRUE(m.Matches(3));
  550. EXPECT_FALSE(m.Matches(0));
  551. m = AnyOf(Le(0), Gt(10), 3, 5, 7);
  552. EXPECT_TRUE(m.Matches(0));
  553. EXPECT_TRUE(m.Matches(11));
  554. EXPECT_TRUE(m.Matches(3));
  555. EXPECT_FALSE(m.Matches(2));
  556. // The following tests for varying number of sub-matchers. Due to the way
  557. // the sub-matchers are handled it is enough to test every sub-matcher once
  558. // with sub-matchers using the same matcher type. Varying matcher types are
  559. // checked for above.
  560. AnyOfMatches(2, AnyOf(1, 2));
  561. AnyOfMatches(3, AnyOf(1, 2, 3));
  562. AnyOfMatches(4, AnyOf(1, 2, 3, 4));
  563. AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
  564. AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
  565. AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
  566. AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
  567. AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
  568. AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
  569. }
  570. // Tests the variadic version of the AnyOfMatcher.
  571. TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
  572. // Also make sure AnyOf is defined in the right namespace and does not depend
  573. // on ADL.
  574. Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  575. EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11)"));
  576. AnyOfMatches(11, m);
  577. AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  578. 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  579. 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
  580. 45, 46, 47, 48, 49, 50));
  581. AnyOfStringMatches(
  582. 50, AnyOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
  583. "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
  584. "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
  585. "33", "34", "35", "36", "37", "38", "39", "40", "41", "42",
  586. "43", "44", "45", "46", "47", "48", "49", "50"));
  587. }
  588. TEST(ConditionalTest, MatchesFirstIfCondition) {
  589. Matcher<std::string> eq_red = Eq("red");
  590. Matcher<std::string> ne_red = Ne("red");
  591. Matcher<std::string> m = Conditional(true, eq_red, ne_red);
  592. EXPECT_TRUE(m.Matches("red"));
  593. EXPECT_FALSE(m.Matches("green"));
  594. StringMatchResultListener listener;
  595. StringMatchResultListener expected;
  596. EXPECT_FALSE(m.MatchAndExplain("green", &listener));
  597. EXPECT_FALSE(eq_red.MatchAndExplain("green", &expected));
  598. EXPECT_THAT(listener.str(), Eq(expected.str()));
  599. }
  600. TEST(ConditionalTest, MatchesSecondIfCondition) {
  601. Matcher<std::string> eq_red = Eq("red");
  602. Matcher<std::string> ne_red = Ne("red");
  603. Matcher<std::string> m = Conditional(false, eq_red, ne_red);
  604. EXPECT_FALSE(m.Matches("red"));
  605. EXPECT_TRUE(m.Matches("green"));
  606. StringMatchResultListener listener;
  607. StringMatchResultListener expected;
  608. EXPECT_FALSE(m.MatchAndExplain("red", &listener));
  609. EXPECT_FALSE(ne_red.MatchAndExplain("red", &expected));
  610. EXPECT_THAT(listener.str(), Eq(expected.str()));
  611. }
  612. // Tests that AnyOf(m1, ..., mn) describes itself properly.
  613. TEST(AnyOfTest, CanDescribeSelf) {
  614. Matcher<int> m;
  615. m = AnyOf(Le(1), Ge(3));
  616. EXPECT_EQ("(is <= 1) or (is >= 3)", Describe(m));
  617. m = AnyOf(Lt(0), Eq(1), Eq(2));
  618. EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m));
  619. m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
  620. EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",
  621. Describe(m));
  622. m = AnyOf(Le(0), Gt(10), 3, 5, 7);
  623. EXPECT_EQ(
  624. "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "
  625. "equal to 7)",
  626. Describe(m));
  627. }
  628. // Tests that AnyOf(m1, ..., mn) describes its negation properly.
  629. TEST(AnyOfTest, CanDescribeNegation) {
  630. Matcher<int> m;
  631. m = AnyOf(Le(1), Ge(3));
  632. EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", DescribeNegation(m));
  633. m = AnyOf(Lt(0), Eq(1), Eq(2));
  634. EXPECT_EQ("(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",
  635. DescribeNegation(m));
  636. m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
  637. EXPECT_EQ(
  638. "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "
  639. "equal to 3)",
  640. DescribeNegation(m));
  641. m = AnyOf(Le(0), Gt(10), 3, 5, 7);
  642. EXPECT_EQ(
  643. "(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "
  644. "to 5) and (isn't equal to 7)",
  645. DescribeNegation(m));
  646. }
  647. // Tests that monomorphic matchers are safely cast by the AnyOf matcher.
  648. TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
  649. // greater_than_5 and less_than_10 are monomorphic matchers.
  650. Matcher<int> greater_than_5 = Gt(5);
  651. Matcher<int> less_than_10 = Lt(10);
  652. Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
  653. Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
  654. Matcher<int&> m3 = AnyOf(greater_than_5, m2);
  655. // Tests that EitherOf works when composing itself.
  656. Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
  657. Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
  658. }
  659. TEST_P(AnyOfTestP, ExplainsResult) {
  660. Matcher<int> m;
  661. // Failed match. Both matchers need to explain. The second
  662. // matcher doesn't give an explanation, so only the first matcher's
  663. // explanation is printed.
  664. m = AnyOf(GreaterThan(10), Lt(0));
  665. EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
  666. // Failed match. Both matchers need to explain.
  667. m = AnyOf(GreaterThan(10), GreaterThan(20));
  668. EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
  669. Explain(m, 5));
  670. // Failed match. All matchers need to explain. The second
  671. // matcher doesn't given an explanation.
  672. m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
  673. EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
  674. Explain(m, 5));
  675. // Failed match. All matchers need to explain.
  676. m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
  677. EXPECT_EQ(
  678. "which is 5 less than 10, and which is 15 less than 20, "
  679. "and which is 25 less than 30",
  680. Explain(m, 5));
  681. // Successful match. The first matcher, which succeeded, needs to
  682. // explain.
  683. m = AnyOf(GreaterThan(10), GreaterThan(20));
  684. EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
  685. // Successful match. The second matcher, which succeeded, needs to
  686. // explain. Since it doesn't given an explanation, nothing is
  687. // printed.
  688. m = AnyOf(GreaterThan(10), Lt(30));
  689. EXPECT_EQ("", Explain(m, 0));
  690. // Successful match. The second matcher, which succeeded, needs to
  691. // explain.
  692. m = AnyOf(GreaterThan(30), GreaterThan(20));
  693. EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
  694. }
  695. // The following predicate function and predicate functor are for
  696. // testing the Truly(predicate) matcher.
  697. // Returns non-zero if the input is positive. Note that the return
  698. // type of this function is not bool. It's OK as Truly() accepts any
  699. // unary function or functor whose return type can be implicitly
  700. // converted to bool.
  701. int IsPositive(double x) { return x > 0 ? 1 : 0; }
  702. // This functor returns true if the input is greater than the given
  703. // number.
  704. class IsGreaterThan {
  705. public:
  706. explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
  707. bool operator()(int n) const { return n > threshold_; }
  708. private:
  709. int threshold_;
  710. };
  711. // For testing Truly().
  712. const int foo = 0;
  713. // This predicate returns true if and only if the argument references foo and
  714. // has a zero value.
  715. bool ReferencesFooAndIsZero(const int& n) { return (&n == &foo) && (n == 0); }
  716. // Tests that Truly(predicate) matches what satisfies the given
  717. // predicate.
  718. TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
  719. Matcher<double> m = Truly(IsPositive);
  720. EXPECT_TRUE(m.Matches(2.0));
  721. EXPECT_FALSE(m.Matches(-1.5));
  722. }
  723. // Tests that Truly(predicate_functor) works too.
  724. TEST(TrulyTest, CanBeUsedWithFunctor) {
  725. Matcher<int> m = Truly(IsGreaterThan(5));
  726. EXPECT_TRUE(m.Matches(6));
  727. EXPECT_FALSE(m.Matches(4));
  728. }
  729. // A class that can be implicitly converted to bool.
  730. class ConvertibleToBool {
  731. public:
  732. explicit ConvertibleToBool(int number) : number_(number) {}
  733. operator bool() const { return number_ != 0; }
  734. private:
  735. int number_;
  736. };
  737. ConvertibleToBool IsNotZero(int number) { return ConvertibleToBool(number); }
  738. // Tests that the predicate used in Truly() may return a class that's
  739. // implicitly convertible to bool, even when the class has no
  740. // operator!().
  741. TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
  742. Matcher<int> m = Truly(IsNotZero);
  743. EXPECT_TRUE(m.Matches(1));
  744. EXPECT_FALSE(m.Matches(0));
  745. }
  746. // Tests that Truly(predicate) can describe itself properly.
  747. TEST(TrulyTest, CanDescribeSelf) {
  748. Matcher<double> m = Truly(IsPositive);
  749. EXPECT_EQ("satisfies the given predicate", Describe(m));
  750. }
  751. // Tests that Truly(predicate) works when the matcher takes its
  752. // argument by reference.
  753. TEST(TrulyTest, WorksForByRefArguments) {
  754. Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
  755. EXPECT_TRUE(m.Matches(foo));
  756. int n = 0;
  757. EXPECT_FALSE(m.Matches(n));
  758. }
  759. // Tests that Truly(predicate) provides a helpful reason when it fails.
  760. TEST(TrulyTest, ExplainsFailures) {
  761. StringMatchResultListener listener;
  762. EXPECT_FALSE(ExplainMatchResult(Truly(IsPositive), -1, &listener));
  763. EXPECT_EQ(listener.str(), "didn't satisfy the given predicate");
  764. }
  765. // Tests that Matches(m) is a predicate satisfied by whatever that
  766. // matches matcher m.
  767. TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
  768. EXPECT_TRUE(Matches(Ge(0))(1));
  769. EXPECT_FALSE(Matches(Eq('a'))('b'));
  770. }
  771. // Tests that Matches(m) works when the matcher takes its argument by
  772. // reference.
  773. TEST(MatchesTest, WorksOnByRefArguments) {
  774. int m = 0, n = 0;
  775. EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
  776. EXPECT_FALSE(Matches(Ref(m))(n));
  777. }
  778. // Tests that a Matcher on non-reference type can be used in
  779. // Matches().
  780. TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
  781. Matcher<int> eq5 = Eq(5);
  782. EXPECT_TRUE(Matches(eq5)(5));
  783. EXPECT_FALSE(Matches(eq5)(2));
  784. }
  785. // Tests Value(value, matcher). Since Value() is a simple wrapper for
  786. // Matches(), which has been tested already, we don't spend a lot of
  787. // effort on testing Value().
  788. TEST(ValueTest, WorksWithPolymorphicMatcher) {
  789. EXPECT_TRUE(Value("hi", StartsWith("h")));
  790. EXPECT_FALSE(Value(5, Gt(10)));
  791. }
  792. TEST(ValueTest, WorksWithMonomorphicMatcher) {
  793. const Matcher<int> is_zero = Eq(0);
  794. EXPECT_TRUE(Value(0, is_zero));
  795. EXPECT_FALSE(Value('a', is_zero));
  796. int n = 0;
  797. const Matcher<const int&> ref_n = Ref(n);
  798. EXPECT_TRUE(Value(n, ref_n));
  799. EXPECT_FALSE(Value(1, ref_n));
  800. }
  801. TEST(AllArgsTest, WorksForTuple) {
  802. EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt()));
  803. EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt())));
  804. }
  805. TEST(AllArgsTest, WorksForNonTuple) {
  806. EXPECT_THAT(42, AllArgs(Gt(0)));
  807. EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
  808. }
  809. class AllArgsHelper {
  810. public:
  811. AllArgsHelper() = default;
  812. MOCK_METHOD2(Helper, int(char x, int y));
  813. private:
  814. AllArgsHelper(const AllArgsHelper&) = delete;
  815. AllArgsHelper& operator=(const AllArgsHelper&) = delete;
  816. };
  817. TEST(AllArgsTest, WorksInWithClause) {
  818. AllArgsHelper helper;
  819. ON_CALL(helper, Helper(_, _)).With(AllArgs(Lt())).WillByDefault(Return(1));
  820. EXPECT_CALL(helper, Helper(_, _));
  821. EXPECT_CALL(helper, Helper(_, _)).With(AllArgs(Gt())).WillOnce(Return(2));
  822. EXPECT_EQ(1, helper.Helper('\1', 2));
  823. EXPECT_EQ(2, helper.Helper('a', 1));
  824. }
  825. class OptionalMatchersHelper {
  826. public:
  827. OptionalMatchersHelper() = default;
  828. MOCK_METHOD0(NoArgs, int());
  829. MOCK_METHOD1(OneArg, int(int y));
  830. MOCK_METHOD2(TwoArgs, int(char x, int y));
  831. MOCK_METHOD1(Overloaded, int(char x));
  832. MOCK_METHOD2(Overloaded, int(char x, int y));
  833. private:
  834. OptionalMatchersHelper(const OptionalMatchersHelper&) = delete;
  835. OptionalMatchersHelper& operator=(const OptionalMatchersHelper&) = delete;
  836. };
  837. TEST(AllArgsTest, WorksWithoutMatchers) {
  838. OptionalMatchersHelper helper;
  839. ON_CALL(helper, NoArgs).WillByDefault(Return(10));
  840. ON_CALL(helper, OneArg).WillByDefault(Return(20));
  841. ON_CALL(helper, TwoArgs).WillByDefault(Return(30));
  842. EXPECT_EQ(10, helper.NoArgs());
  843. EXPECT_EQ(20, helper.OneArg(1));
  844. EXPECT_EQ(30, helper.TwoArgs('\1', 2));
  845. EXPECT_CALL(helper, NoArgs).Times(1);
  846. EXPECT_CALL(helper, OneArg).WillOnce(Return(100));
  847. EXPECT_CALL(helper, OneArg(17)).WillOnce(Return(200));
  848. EXPECT_CALL(helper, TwoArgs).Times(0);
  849. EXPECT_EQ(10, helper.NoArgs());
  850. EXPECT_EQ(100, helper.OneArg(1));
  851. EXPECT_EQ(200, helper.OneArg(17));
  852. }
  853. // Tests floating-point matchers.
  854. template <typename RawType>
  855. class FloatingPointTest : public testing::Test {
  856. protected:
  857. typedef testing::internal::FloatingPoint<RawType> Floating;
  858. typedef typename Floating::Bits Bits;
  859. FloatingPointTest()
  860. : max_ulps_(Floating::kMaxUlps),
  861. zero_bits_(Floating(0).bits()),
  862. one_bits_(Floating(1).bits()),
  863. infinity_bits_(Floating(Floating::Infinity()).bits()),
  864. close_to_positive_zero_(
  865. Floating::ReinterpretBits(zero_bits_ + max_ulps_ / 2)),
  866. close_to_negative_zero_(
  867. -Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_ / 2)),
  868. further_from_negative_zero_(-Floating::ReinterpretBits(
  869. zero_bits_ + max_ulps_ + 1 - max_ulps_ / 2)),
  870. close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)),
  871. further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)),
  872. infinity_(Floating::Infinity()),
  873. close_to_infinity_(
  874. Floating::ReinterpretBits(infinity_bits_ - max_ulps_)),
  875. further_from_infinity_(
  876. Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)),
  877. max_(std::numeric_limits<RawType>::max()),
  878. nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),
  879. nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {}
  880. void TestSize() { EXPECT_EQ(sizeof(RawType), sizeof(Bits)); }
  881. // A battery of tests for FloatingEqMatcher::Matches.
  882. // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
  883. void TestMatches(
  884. testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
  885. Matcher<RawType> m1 = matcher_maker(0.0);
  886. EXPECT_TRUE(m1.Matches(-0.0));
  887. EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
  888. EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
  889. EXPECT_FALSE(m1.Matches(1.0));
  890. Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
  891. EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
  892. Matcher<RawType> m3 = matcher_maker(1.0);
  893. EXPECT_TRUE(m3.Matches(close_to_one_));
  894. EXPECT_FALSE(m3.Matches(further_from_one_));
  895. // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
  896. EXPECT_FALSE(m3.Matches(0.0));
  897. Matcher<RawType> m4 = matcher_maker(-infinity_);
  898. EXPECT_TRUE(m4.Matches(-close_to_infinity_));
  899. Matcher<RawType> m5 = matcher_maker(infinity_);
  900. EXPECT_TRUE(m5.Matches(close_to_infinity_));
  901. // This is interesting as the representations of infinity_ and nan1_
  902. // are only 1 DLP apart.
  903. EXPECT_FALSE(m5.Matches(nan1_));
  904. // matcher_maker can produce a Matcher<const RawType&>, which is needed in
  905. // some cases.
  906. Matcher<const RawType&> m6 = matcher_maker(0.0);
  907. EXPECT_TRUE(m6.Matches(-0.0));
  908. EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
  909. EXPECT_FALSE(m6.Matches(1.0));
  910. // matcher_maker can produce a Matcher<RawType&>, which is needed in some
  911. // cases.
  912. Matcher<RawType&> m7 = matcher_maker(0.0);
  913. RawType x = 0.0;
  914. EXPECT_TRUE(m7.Matches(x));
  915. x = 0.01f;
  916. EXPECT_FALSE(m7.Matches(x));
  917. }
  918. // Pre-calculated numbers to be used by the tests.
  919. const Bits max_ulps_;
  920. const Bits zero_bits_; // The bits that represent 0.0.
  921. const Bits one_bits_; // The bits that represent 1.0.
  922. const Bits infinity_bits_; // The bits that represent +infinity.
  923. // Some numbers close to 0.0.
  924. const RawType close_to_positive_zero_;
  925. const RawType close_to_negative_zero_;
  926. const RawType further_from_negative_zero_;
  927. // Some numbers close to 1.0.
  928. const RawType close_to_one_;
  929. const RawType further_from_one_;
  930. // Some numbers close to +infinity.
  931. const RawType infinity_;
  932. const RawType close_to_infinity_;
  933. const RawType further_from_infinity_;
  934. // Maximum representable value that's not infinity.
  935. const RawType max_;
  936. // Some NaNs.
  937. const RawType nan1_;
  938. const RawType nan2_;
  939. };
  940. // Tests floating-point matchers with fixed epsilons.
  941. template <typename RawType>
  942. class FloatingPointNearTest : public FloatingPointTest<RawType> {
  943. protected:
  944. typedef FloatingPointTest<RawType> ParentType;
  945. // A battery of tests for FloatingEqMatcher::Matches with a fixed epsilon.
  946. // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
  947. void TestNearMatches(testing::internal::FloatingEqMatcher<RawType> (
  948. *matcher_maker)(RawType, RawType)) {
  949. Matcher<RawType> m1 = matcher_maker(0.0, 0.0);
  950. EXPECT_TRUE(m1.Matches(0.0));
  951. EXPECT_TRUE(m1.Matches(-0.0));
  952. EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_));
  953. EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_));
  954. EXPECT_FALSE(m1.Matches(1.0));
  955. Matcher<RawType> m2 = matcher_maker(0.0, 1.0);
  956. EXPECT_TRUE(m2.Matches(0.0));
  957. EXPECT_TRUE(m2.Matches(-0.0));
  958. EXPECT_TRUE(m2.Matches(1.0));
  959. EXPECT_TRUE(m2.Matches(-1.0));
  960. EXPECT_FALSE(m2.Matches(ParentType::close_to_one_));
  961. EXPECT_FALSE(m2.Matches(-ParentType::close_to_one_));
  962. // Check that inf matches inf, regardless of the of the specified max
  963. // absolute error.
  964. Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0);
  965. EXPECT_TRUE(m3.Matches(ParentType::infinity_));
  966. EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_));
  967. EXPECT_FALSE(m3.Matches(-ParentType::infinity_));
  968. Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0);
  969. EXPECT_TRUE(m4.Matches(-ParentType::infinity_));
  970. EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_));
  971. EXPECT_FALSE(m4.Matches(ParentType::infinity_));
  972. // Test various overflow scenarios.
  973. Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_);
  974. EXPECT_TRUE(m5.Matches(ParentType::max_));
  975. EXPECT_FALSE(m5.Matches(-ParentType::max_));
  976. Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_);
  977. EXPECT_FALSE(m6.Matches(ParentType::max_));
  978. EXPECT_TRUE(m6.Matches(-ParentType::max_));
  979. Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0);
  980. EXPECT_TRUE(m7.Matches(ParentType::max_));
  981. EXPECT_FALSE(m7.Matches(-ParentType::max_));
  982. Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0);
  983. EXPECT_FALSE(m8.Matches(ParentType::max_));
  984. EXPECT_TRUE(m8.Matches(-ParentType::max_));
  985. // The difference between max() and -max() normally overflows to infinity,
  986. // but it should still match if the max_abs_error is also infinity.
  987. Matcher<RawType> m9 =
  988. matcher_maker(ParentType::max_, ParentType::infinity_);
  989. EXPECT_TRUE(m8.Matches(-ParentType::max_));
  990. // matcher_maker can produce a Matcher<const RawType&>, which is needed in
  991. // some cases.
  992. Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);
  993. EXPECT_TRUE(m10.Matches(-0.0));
  994. EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_));
  995. EXPECT_FALSE(m10.Matches(ParentType::close_to_one_));
  996. // matcher_maker can produce a Matcher<RawType&>, which is needed in some
  997. // cases.
  998. Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
  999. RawType x = 0.0;
  1000. EXPECT_TRUE(m11.Matches(x));
  1001. x = 1.0f;
  1002. EXPECT_TRUE(m11.Matches(x));
  1003. x = -1.0f;
  1004. EXPECT_TRUE(m11.Matches(x));
  1005. x = 1.1f;
  1006. EXPECT_FALSE(m11.Matches(x));
  1007. x = -1.1f;
  1008. EXPECT_FALSE(m11.Matches(x));
  1009. }
  1010. };
  1011. // Instantiate FloatingPointTest for testing floats.
  1012. typedef FloatingPointTest<float> FloatTest;
  1013. TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { TestMatches(&FloatEq); }
  1014. TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
  1015. TestMatches(&NanSensitiveFloatEq);
  1016. }
  1017. TEST_F(FloatTest, FloatEqCannotMatchNaN) {
  1018. // FloatEq never matches NaN.
  1019. Matcher<float> m = FloatEq(nan1_);
  1020. EXPECT_FALSE(m.Matches(nan1_));
  1021. EXPECT_FALSE(m.Matches(nan2_));
  1022. EXPECT_FALSE(m.Matches(1.0));
  1023. }
  1024. TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
  1025. // NanSensitiveFloatEq will match NaN.
  1026. Matcher<float> m = NanSensitiveFloatEq(nan1_);
  1027. EXPECT_TRUE(m.Matches(nan1_));
  1028. EXPECT_TRUE(m.Matches(nan2_));
  1029. EXPECT_FALSE(m.Matches(1.0));
  1030. }
  1031. TEST_F(FloatTest, FloatEqCanDescribeSelf) {
  1032. Matcher<float> m1 = FloatEq(2.0f);
  1033. EXPECT_EQ("is approximately 2", Describe(m1));
  1034. EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
  1035. Matcher<float> m2 = FloatEq(0.5f);
  1036. EXPECT_EQ("is approximately 0.5", Describe(m2));
  1037. EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
  1038. Matcher<float> m3 = FloatEq(nan1_);
  1039. EXPECT_EQ("never matches", Describe(m3));
  1040. EXPECT_EQ("is anything", DescribeNegation(m3));
  1041. }
  1042. TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
  1043. Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
  1044. EXPECT_EQ("is approximately 2", Describe(m1));
  1045. EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
  1046. Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
  1047. EXPECT_EQ("is approximately 0.5", Describe(m2));
  1048. EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
  1049. Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
  1050. EXPECT_EQ("is NaN", Describe(m3));
  1051. EXPECT_EQ("isn't NaN", DescribeNegation(m3));
  1052. }
  1053. // Instantiate FloatingPointTest for testing floats with a user-specified
  1054. // max absolute error.
  1055. typedef FloatingPointNearTest<float> FloatNearTest;
  1056. TEST_F(FloatNearTest, FloatNearMatches) { TestNearMatches(&FloatNear); }
  1057. TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
  1058. TestNearMatches(&NanSensitiveFloatNear);
  1059. }
  1060. TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {
  1061. Matcher<float> m1 = FloatNear(2.0f, 0.5f);
  1062. EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
  1063. EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
  1064. DescribeNegation(m1));
  1065. Matcher<float> m2 = FloatNear(0.5f, 0.5f);
  1066. EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
  1067. EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
  1068. DescribeNegation(m2));
  1069. Matcher<float> m3 = FloatNear(nan1_, 0.0);
  1070. EXPECT_EQ("never matches", Describe(m3));
  1071. EXPECT_EQ("is anything", DescribeNegation(m3));
  1072. }
  1073. TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {
  1074. Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f);
  1075. EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
  1076. EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
  1077. DescribeNegation(m1));
  1078. Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f);
  1079. EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
  1080. EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
  1081. DescribeNegation(m2));
  1082. Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f);
  1083. EXPECT_EQ("is NaN", Describe(m3));
  1084. EXPECT_EQ("isn't NaN", DescribeNegation(m3));
  1085. }
  1086. TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
  1087. // FloatNear never matches NaN.
  1088. Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f);
  1089. EXPECT_FALSE(m.Matches(nan1_));
  1090. EXPECT_FALSE(m.Matches(nan2_));
  1091. EXPECT_FALSE(m.Matches(1.0));
  1092. }
  1093. TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
  1094. // NanSensitiveFloatNear will match NaN.
  1095. Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f);
  1096. EXPECT_TRUE(m.Matches(nan1_));
  1097. EXPECT_TRUE(m.Matches(nan2_));
  1098. EXPECT_FALSE(m.Matches(1.0));
  1099. }
  1100. // Instantiate FloatingPointTest for testing doubles.
  1101. typedef FloatingPointTest<double> DoubleTest;
  1102. TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
  1103. TestMatches(&DoubleEq);
  1104. }
  1105. TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
  1106. TestMatches(&NanSensitiveDoubleEq);
  1107. }
  1108. TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
  1109. // DoubleEq never matches NaN.
  1110. Matcher<double> m = DoubleEq(nan1_);
  1111. EXPECT_FALSE(m.Matches(nan1_));
  1112. EXPECT_FALSE(m.Matches(nan2_));
  1113. EXPECT_FALSE(m.Matches(1.0));
  1114. }
  1115. TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
  1116. // NanSensitiveDoubleEq will match NaN.
  1117. Matcher<double> m = NanSensitiveDoubleEq(nan1_);
  1118. EXPECT_TRUE(m.Matches(nan1_));
  1119. EXPECT_TRUE(m.Matches(nan2_));
  1120. EXPECT_FALSE(m.Matches(1.0));
  1121. }
  1122. TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
  1123. Matcher<double> m1 = DoubleEq(2.0);
  1124. EXPECT_EQ("is approximately 2", Describe(m1));
  1125. EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
  1126. Matcher<double> m2 = DoubleEq(0.5);
  1127. EXPECT_EQ("is approximately 0.5", Describe(m2));
  1128. EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
  1129. Matcher<double> m3 = DoubleEq(nan1_);
  1130. EXPECT_EQ("never matches", Describe(m3));
  1131. EXPECT_EQ("is anything", DescribeNegation(m3));
  1132. }
  1133. TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
  1134. Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
  1135. EXPECT_EQ("is approximately 2", Describe(m1));
  1136. EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
  1137. Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
  1138. EXPECT_EQ("is approximately 0.5", Describe(m2));
  1139. EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
  1140. Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
  1141. EXPECT_EQ("is NaN", Describe(m3));
  1142. EXPECT_EQ("isn't NaN", DescribeNegation(m3));
  1143. }
  1144. // Instantiate FloatingPointTest for testing floats with a user-specified
  1145. // max absolute error.
  1146. typedef FloatingPointNearTest<double> DoubleNearTest;
  1147. TEST_F(DoubleNearTest, DoubleNearMatches) { TestNearMatches(&DoubleNear); }
  1148. TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
  1149. TestNearMatches(&NanSensitiveDoubleNear);
  1150. }
  1151. TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {
  1152. Matcher<double> m1 = DoubleNear(2.0, 0.5);
  1153. EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
  1154. EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
  1155. DescribeNegation(m1));
  1156. Matcher<double> m2 = DoubleNear(0.5, 0.5);
  1157. EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
  1158. EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
  1159. DescribeNegation(m2));
  1160. Matcher<double> m3 = DoubleNear(nan1_, 0.0);
  1161. EXPECT_EQ("never matches", Describe(m3));
  1162. EXPECT_EQ("is anything", DescribeNegation(m3));
  1163. }
  1164. TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) {
  1165. EXPECT_EQ("", Explain(DoubleNear(2.0, 0.1), 2.05));
  1166. EXPECT_EQ("which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2));
  1167. EXPECT_EQ("which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7));
  1168. const std::string explanation =
  1169. Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10);
  1170. // Different C++ implementations may print floating-point numbers
  1171. // slightly differently.
  1172. EXPECT_TRUE(explanation == "which is 1.2e-10 from 2.1" || // GCC
  1173. explanation == "which is 1.2e-010 from 2.1") // MSVC
  1174. << " where explanation is \"" << explanation << "\".";
  1175. }
  1176. TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {
  1177. Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5);
  1178. EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
  1179. EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
  1180. DescribeNegation(m1));
  1181. Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5);
  1182. EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
  1183. EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
  1184. DescribeNegation(m2));
  1185. Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1);
  1186. EXPECT_EQ("is NaN", Describe(m3));
  1187. EXPECT_EQ("isn't NaN", DescribeNegation(m3));
  1188. }
  1189. TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
  1190. // DoubleNear never matches NaN.
  1191. Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1);
  1192. EXPECT_FALSE(m.Matches(nan1_));
  1193. EXPECT_FALSE(m.Matches(nan2_));
  1194. EXPECT_FALSE(m.Matches(1.0));
  1195. }
  1196. TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
  1197. // NanSensitiveDoubleNear will match NaN.
  1198. Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1);
  1199. EXPECT_TRUE(m.Matches(nan1_));
  1200. EXPECT_TRUE(m.Matches(nan2_));
  1201. EXPECT_FALSE(m.Matches(1.0));
  1202. }
  1203. TEST(NotTest, WorksOnMoveOnlyType) {
  1204. std::unique_ptr<int> p(new int(3));
  1205. EXPECT_THAT(p, Pointee(Eq(3)));
  1206. EXPECT_THAT(p, Not(Pointee(Eq(2))));
  1207. }
  1208. TEST(AllOfTest, HugeMatcher) {
  1209. // Verify that using AllOf with many arguments doesn't cause
  1210. // the compiler to exceed template instantiation depth limit.
  1211. EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
  1212. testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
  1213. }
  1214. TEST(AnyOfTest, HugeMatcher) {
  1215. // Verify that using AnyOf with many arguments doesn't cause
  1216. // the compiler to exceed template instantiation depth limit.
  1217. EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
  1218. testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
  1219. }
  1220. namespace adl_test {
  1221. // Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
  1222. // don't issue unqualified recursive calls. If they do, the argument dependent
  1223. // name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
  1224. // as a candidate and the compilation will break due to an ambiguous overload.
  1225. // The matcher must be in the same namespace as AllOf/AnyOf to make argument
  1226. // dependent lookup find those.
  1227. MATCHER(M, "") {
  1228. (void)arg;
  1229. return true;
  1230. }
  1231. template <typename T1, typename T2>
  1232. bool AllOf(const T1& /*t1*/, const T2& /*t2*/) {
  1233. return true;
  1234. }
  1235. TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
  1236. EXPECT_THAT(42,
  1237. testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
  1238. }
  1239. template <typename T1, typename T2>
  1240. bool AnyOf(const T1&, const T2&) {
  1241. return true;
  1242. }
  1243. TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
  1244. EXPECT_THAT(42,
  1245. testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
  1246. }
  1247. } // namespace adl_test
  1248. TEST(AllOfTest, WorksOnMoveOnlyType) {
  1249. std::unique_ptr<int> p(new int(3));
  1250. EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));
  1251. EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));
  1252. }
  1253. TEST(AnyOfTest, WorksOnMoveOnlyType) {
  1254. std::unique_ptr<int> p(new int(3));
  1255. EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));
  1256. EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));
  1257. }
  1258. } // namespace
  1259. } // namespace gmock_matchers_test
  1260. } // namespace testing
  1261. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100