PageRenderTime 53ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/external/fmt-master/test/gtest-extra-test.cc

https://bitbucket.org/ChunkBuster/cis330-project-repo
C++ | 433 lines | 318 code | 53 blank | 62 comment | 9 complexity | a54387b06b03ea65fc627ae2c08aa7f1 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, CC-BY-3.0
  1. /*
  2. Tests of custom Google Test assertions.
  3. Copyright (c) 2012-2014, Victor Zverovich
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. 1. Redistributions of source code must retain the above copyright notice, this
  8. list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  13. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  16. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include "gtest-extra.h"
  24. #include <cstring>
  25. #include <algorithm>
  26. #include <stdexcept>
  27. #include <gtest/gtest-spi.h>
  28. #if defined(_WIN32) && !defined(__MINGW32__)
  29. # include <crtdbg.h> // for _CrtSetReportMode
  30. #endif // _WIN32
  31. #include "util.h"
  32. using testing::internal::scoped_ptr;
  33. namespace {
  34. // This is used to suppress coverity warnings about untrusted values.
  35. std::string sanitize(const std::string &s) {
  36. std::string result;
  37. for (std::string::const_iterator i = s.begin(), end = s.end(); i != end; ++i)
  38. result.push_back(static_cast<char>(*i & 0xff));
  39. return result;
  40. }
  41. // Tests that assertion macros evaluate their arguments exactly once.
  42. class SingleEvaluationTest : public ::testing::Test {
  43. protected:
  44. SingleEvaluationTest() {
  45. p_ = s_;
  46. a_ = 0;
  47. b_ = 0;
  48. }
  49. static const char* const s_;
  50. static const char* p_;
  51. static int a_;
  52. static int b_;
  53. };
  54. const char* const SingleEvaluationTest::s_ = "01234";
  55. const char* SingleEvaluationTest::p_;
  56. int SingleEvaluationTest::a_;
  57. int SingleEvaluationTest::b_;
  58. void do_nothing() {}
  59. void throw_exception() {
  60. throw std::runtime_error("test");
  61. }
  62. void throw_system_error() {
  63. throw fmt::system_error(EDOM, "test");
  64. }
  65. // Tests that when EXPECT_THROW_MSG fails, it evaluates its message argument
  66. // exactly once.
  67. TEST_F(SingleEvaluationTest, FailedEXPECT_THROW_MSG) {
  68. EXPECT_NONFATAL_FAILURE(
  69. EXPECT_THROW_MSG(throw_exception(), std::exception, p_++), "01234");
  70. EXPECT_EQ(s_ + 1, p_);
  71. }
  72. // Tests that when EXPECT_SYSTEM_ERROR fails, it evaluates its message argument
  73. // exactly once.
  74. TEST_F(SingleEvaluationTest, FailedEXPECT_SYSTEM_ERROR) {
  75. EXPECT_NONFATAL_FAILURE(
  76. EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, p_++), "01234");
  77. EXPECT_EQ(s_ + 1, p_);
  78. }
  79. // Tests that when EXPECT_WRITE fails, it evaluates its message argument
  80. // exactly once.
  81. TEST_F(SingleEvaluationTest, FailedEXPECT_WRITE) {
  82. EXPECT_NONFATAL_FAILURE(
  83. EXPECT_WRITE(stdout, std::printf("test"), p_++), "01234");
  84. EXPECT_EQ(s_ + 1, p_);
  85. }
  86. // Tests that assertion arguments are evaluated exactly once.
  87. TEST_F(SingleEvaluationTest, ExceptionTests) {
  88. // successful EXPECT_THROW_MSG
  89. EXPECT_THROW_MSG({ // NOLINT
  90. a_++;
  91. throw_exception();
  92. }, std::exception, (b_++, "test"));
  93. EXPECT_EQ(1, a_);
  94. EXPECT_EQ(1, b_);
  95. // failed EXPECT_THROW_MSG, throws different type
  96. EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG({ // NOLINT
  97. a_++;
  98. throw_exception();
  99. }, std::logic_error, (b_++, "test")), "throws a different type");
  100. EXPECT_EQ(2, a_);
  101. EXPECT_EQ(2, b_);
  102. // failed EXPECT_THROW_MSG, throws an exception with different message
  103. EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG({ // NOLINT
  104. a_++;
  105. throw_exception();
  106. }, std::exception, (b_++, "other")),
  107. "throws an exception with a different message");
  108. EXPECT_EQ(3, a_);
  109. EXPECT_EQ(3, b_);
  110. // failed EXPECT_THROW_MSG, throws nothing
  111. EXPECT_NONFATAL_FAILURE(
  112. EXPECT_THROW_MSG(a_++, std::exception, (b_++, "test")), "throws nothing");
  113. EXPECT_EQ(4, a_);
  114. EXPECT_EQ(4, b_);
  115. }
  116. TEST_F(SingleEvaluationTest, SystemErrorTests) {
  117. // successful EXPECT_SYSTEM_ERROR
  118. EXPECT_SYSTEM_ERROR({ // NOLINT
  119. a_++;
  120. throw_system_error();
  121. }, EDOM, (b_++, "test"));
  122. EXPECT_EQ(1, a_);
  123. EXPECT_EQ(1, b_);
  124. // failed EXPECT_SYSTEM_ERROR, throws different type
  125. EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR({ // NOLINT
  126. a_++;
  127. throw_exception();
  128. }, EDOM, (b_++, "test")), "throws a different type");
  129. EXPECT_EQ(2, a_);
  130. EXPECT_EQ(2, b_);
  131. // failed EXPECT_SYSTEM_ERROR, throws an exception with different message
  132. EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR({ // NOLINT
  133. a_++;
  134. throw_system_error();
  135. }, EDOM, (b_++, "other")),
  136. "throws an exception with a different message");
  137. EXPECT_EQ(3, a_);
  138. EXPECT_EQ(3, b_);
  139. // failed EXPECT_SYSTEM_ERROR, throws nothing
  140. EXPECT_NONFATAL_FAILURE(
  141. EXPECT_SYSTEM_ERROR(a_++, EDOM, (b_++, "test")), "throws nothing");
  142. EXPECT_EQ(4, a_);
  143. EXPECT_EQ(4, b_);
  144. }
  145. // Tests that assertion arguments are evaluated exactly once.
  146. TEST_F(SingleEvaluationTest, WriteTests) {
  147. // successful EXPECT_WRITE
  148. EXPECT_WRITE(stdout, { // NOLINT
  149. a_++;
  150. std::printf("test");
  151. }, (b_++, "test"));
  152. EXPECT_EQ(1, a_);
  153. EXPECT_EQ(1, b_);
  154. // failed EXPECT_WRITE
  155. EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout, { // NOLINT
  156. a_++;
  157. std::printf("test");
  158. }, (b_++, "other")), "Actual: test");
  159. EXPECT_EQ(2, a_);
  160. EXPECT_EQ(2, b_);
  161. }
  162. // Tests that the compiler will not complain about unreachable code in the
  163. // EXPECT_THROW_MSG macro.
  164. TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) {
  165. int n = 0;
  166. using std::runtime_error;
  167. EXPECT_THROW_MSG(throw runtime_error(""), runtime_error, "");
  168. EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(n++, runtime_error, ""), "");
  169. EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(throw 1, runtime_error, ""), "");
  170. EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(
  171. throw runtime_error("a"), runtime_error, "b"), "");
  172. }
  173. // Tests that the compiler will not complain about unreachable code in the
  174. // EXPECT_SYSTEM_ERROR macro.
  175. TEST(ExpectSystemErrorTest, DoesNotGenerateUnreachableCodeWarning) {
  176. int n = 0;
  177. EXPECT_SYSTEM_ERROR(throw fmt::system_error(EDOM, "test"), EDOM, "test");
  178. EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(n++, EDOM, ""), "");
  179. EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(throw 1, EDOM, ""), "");
  180. EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(
  181. throw fmt::system_error(EDOM, "aaa"), EDOM, "bbb"), "");
  182. }
  183. TEST(AssertionSyntaxTest, ExceptionAssertionBehavesLikeSingleStatement) {
  184. if (::testing::internal::AlwaysFalse())
  185. EXPECT_THROW_MSG(do_nothing(), std::exception, "");
  186. if (::testing::internal::AlwaysTrue())
  187. EXPECT_THROW_MSG(throw_exception(), std::exception, "test");
  188. else
  189. do_nothing();
  190. }
  191. TEST(AssertionSyntaxTest, SystemErrorAssertionBehavesLikeSingleStatement) {
  192. if (::testing::internal::AlwaysFalse())
  193. EXPECT_SYSTEM_ERROR(do_nothing(), EDOM, "");
  194. if (::testing::internal::AlwaysTrue())
  195. EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "test");
  196. else
  197. do_nothing();
  198. }
  199. TEST(AssertionSyntaxTest, WriteAssertionBehavesLikeSingleStatement) {
  200. if (::testing::internal::AlwaysFalse())
  201. EXPECT_WRITE(stdout, std::printf("x"), "x");
  202. if (::testing::internal::AlwaysTrue())
  203. EXPECT_WRITE(stdout, std::printf("x"), "x");
  204. else
  205. do_nothing();
  206. }
  207. // Tests EXPECT_THROW_MSG.
  208. TEST(ExpectTest, EXPECT_THROW_MSG) {
  209. EXPECT_THROW_MSG(throw_exception(), std::exception, "test");
  210. EXPECT_NONFATAL_FAILURE(
  211. EXPECT_THROW_MSG(throw_exception(), std::logic_error, "test"),
  212. "Expected: throw_exception() throws an exception of "
  213. "type std::logic_error.\n Actual: it throws a different type.");
  214. EXPECT_NONFATAL_FAILURE(
  215. EXPECT_THROW_MSG(do_nothing(), std::exception, "test"),
  216. "Expected: do_nothing() throws an exception of type std::exception.\n"
  217. " Actual: it throws nothing.");
  218. EXPECT_NONFATAL_FAILURE(
  219. EXPECT_THROW_MSG(throw_exception(), std::exception, "other"),
  220. "throw_exception() throws an exception with a different message.\n"
  221. "Expected: other\n"
  222. " Actual: test");
  223. }
  224. // Tests EXPECT_SYSTEM_ERROR.
  225. TEST(ExpectTest, EXPECT_SYSTEM_ERROR) {
  226. EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "test");
  227. EXPECT_NONFATAL_FAILURE(
  228. EXPECT_SYSTEM_ERROR(throw_exception(), EDOM, "test"),
  229. "Expected: throw_exception() throws an exception of "
  230. "type fmt::system_error.\n Actual: it throws a different type.");
  231. EXPECT_NONFATAL_FAILURE(
  232. EXPECT_SYSTEM_ERROR(do_nothing(), EDOM, "test"),
  233. "Expected: do_nothing() throws an exception of type fmt::system_error.\n"
  234. " Actual: it throws nothing.");
  235. EXPECT_NONFATAL_FAILURE(
  236. EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "other"),
  237. fmt::format(
  238. "throw_system_error() throws an exception with a different message.\n"
  239. "Expected: {}\n"
  240. " Actual: {}",
  241. format_system_error(EDOM, "other"),
  242. format_system_error(EDOM, "test")));
  243. }
  244. // Tests EXPECT_WRITE.
  245. TEST(ExpectTest, EXPECT_WRITE) {
  246. EXPECT_WRITE(stdout, do_nothing(), "");
  247. EXPECT_WRITE(stdout, std::printf("test"), "test");
  248. EXPECT_WRITE(stderr, std::fprintf(stderr, "test"), "test");
  249. EXPECT_NONFATAL_FAILURE(
  250. EXPECT_WRITE(stdout, std::printf("that"), "this"),
  251. "Expected: this\n"
  252. " Actual: that");
  253. }
  254. TEST(StreamingAssertionsTest, EXPECT_THROW_MSG) {
  255. EXPECT_THROW_MSG(throw_exception(), std::exception, "test")
  256. << "unexpected failure";
  257. EXPECT_NONFATAL_FAILURE(
  258. EXPECT_THROW_MSG(throw_exception(), std::exception, "other")
  259. << "expected failure", "expected failure");
  260. }
  261. TEST(StreamingAssertionsTest, EXPECT_SYSTEM_ERROR) {
  262. EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "test")
  263. << "unexpected failure";
  264. EXPECT_NONFATAL_FAILURE(
  265. EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "other")
  266. << "expected failure", "expected failure");
  267. }
  268. TEST(StreamingAssertionsTest, EXPECT_WRITE) {
  269. EXPECT_WRITE(stdout, std::printf("test"), "test")
  270. << "unexpected failure";
  271. EXPECT_NONFATAL_FAILURE(
  272. EXPECT_WRITE(stdout, std::printf("test"), "other")
  273. << "expected failure", "expected failure");
  274. }
  275. TEST(UtilTest, FormatSystemError) {
  276. fmt::memory_buffer out;
  277. fmt::format_system_error(out, EDOM, "test message");
  278. EXPECT_EQ(to_string(out), format_system_error(EDOM, "test message"));
  279. }
  280. #if FMT_USE_FILE_DESCRIPTORS
  281. using fmt::BufferedFile;
  282. using fmt::ErrorCode;
  283. using fmt::File;
  284. TEST(ErrorCodeTest, Ctor) {
  285. EXPECT_EQ(0, ErrorCode().get());
  286. EXPECT_EQ(42, ErrorCode(42).get());
  287. }
  288. TEST(OutputRedirectTest, ScopedRedirect) {
  289. File read_end, write_end;
  290. File::pipe(read_end, write_end);
  291. {
  292. BufferedFile file(write_end.fdopen("w"));
  293. std::fprintf(file.get(), "[[[");
  294. {
  295. OutputRedirect redir(file.get());
  296. std::fprintf(file.get(), "censored");
  297. }
  298. std::fprintf(file.get(), "]]]");
  299. }
  300. EXPECT_READ(read_end, "[[[]]]");
  301. }
  302. // Test that OutputRedirect handles errors in flush correctly.
  303. TEST(OutputRedirectTest, FlushErrorInCtor) {
  304. File read_end, write_end;
  305. File::pipe(read_end, write_end);
  306. int write_fd = write_end.descriptor();
  307. File write_copy = write_end.dup(write_fd);
  308. BufferedFile f = write_end.fdopen("w");
  309. // Put a character in a file buffer.
  310. EXPECT_EQ('x', fputc('x', f.get()));
  311. FMT_POSIX(close(write_fd));
  312. scoped_ptr<OutputRedirect> redir;
  313. EXPECT_SYSTEM_ERROR_NOASSERT(redir.reset(new OutputRedirect(f.get())),
  314. EBADF, "cannot flush stream");
  315. redir.reset();
  316. write_copy.dup2(write_fd); // "undo" close or dtor will fail
  317. }
  318. TEST(OutputRedirectTest, DupErrorInCtor) {
  319. BufferedFile f = open_buffered_file();
  320. int fd = (f.fileno)();
  321. File copy = File::dup(fd);
  322. FMT_POSIX(close(fd));
  323. scoped_ptr<OutputRedirect> redir;
  324. EXPECT_SYSTEM_ERROR_NOASSERT(redir.reset(new OutputRedirect(f.get())),
  325. EBADF, fmt::format("cannot duplicate file descriptor {}", fd));
  326. copy.dup2(fd); // "undo" close or dtor will fail
  327. }
  328. TEST(OutputRedirectTest, RestoreAndRead) {
  329. File read_end, write_end;
  330. File::pipe(read_end, write_end);
  331. BufferedFile file(write_end.fdopen("w"));
  332. std::fprintf(file.get(), "[[[");
  333. OutputRedirect redir(file.get());
  334. std::fprintf(file.get(), "censored");
  335. EXPECT_EQ("censored", sanitize(redir.restore_and_read()));
  336. EXPECT_EQ("", sanitize(redir.restore_and_read()));
  337. std::fprintf(file.get(), "]]]");
  338. file = BufferedFile();
  339. EXPECT_READ(read_end, "[[[]]]");
  340. }
  341. // Test that OutputRedirect handles errors in flush correctly.
  342. TEST(OutputRedirectTest, FlushErrorInRestoreAndRead) {
  343. File read_end, write_end;
  344. File::pipe(read_end, write_end);
  345. int write_fd = write_end.descriptor();
  346. File write_copy = write_end.dup(write_fd);
  347. BufferedFile f = write_end.fdopen("w");
  348. OutputRedirect redir(f.get());
  349. // Put a character in a file buffer.
  350. EXPECT_EQ('x', fputc('x', f.get()));
  351. FMT_POSIX(close(write_fd));
  352. EXPECT_SYSTEM_ERROR_NOASSERT(redir.restore_and_read(),
  353. EBADF, "cannot flush stream");
  354. write_copy.dup2(write_fd); // "undo" close or dtor will fail
  355. }
  356. TEST(OutputRedirectTest, ErrorInDtor) {
  357. File read_end, write_end;
  358. File::pipe(read_end, write_end);
  359. int write_fd = write_end.descriptor();
  360. File write_copy = write_end.dup(write_fd);
  361. BufferedFile f = write_end.fdopen("w");
  362. scoped_ptr<OutputRedirect> redir(new OutputRedirect(f.get()));
  363. // Put a character in a file buffer.
  364. EXPECT_EQ('x', fputc('x', f.get()));
  365. EXPECT_WRITE(stderr, {
  366. // The close function must be called inside EXPECT_WRITE, otherwise
  367. // the system may recycle closed file descriptor when redirecting the
  368. // output in EXPECT_STDERR and the second close will break output
  369. // redirection.
  370. FMT_POSIX(close(write_fd));
  371. SUPPRESS_ASSERT(redir.reset());
  372. }, format_system_error(EBADF, "cannot flush stream"));
  373. write_copy.dup2(write_fd); // "undo" close or dtor of BufferedFile will fail
  374. }
  375. #endif // FMT_USE_FILE_DESCRIPTORS
  376. } // namespace