gmock-matchers-arithmetic_test.cc 59 KB

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