check_test.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "common/check.h"
  5. #include <gtest/gtest.h>
  6. namespace Carbon {
  7. namespace {
  8. // Non-constexpr functions that always return true and false, to bypass constant
  9. // condition checking.
  10. auto AlwaysTrue() -> bool { return true; }
  11. auto AlwaysFalse() -> bool { return false; }
  12. TEST(CheckTest, CheckTrue) { CARBON_CHECK(AlwaysTrue()); }
  13. TEST(CheckTest, CheckFalse) {
  14. ASSERT_DEATH({ CARBON_CHECK(AlwaysFalse()); },
  15. R"(
  16. CHECK failure at common/check_test.cpp:\d+: AlwaysFalse\(\)
  17. )");
  18. }
  19. TEST(CheckTest, CheckFalseHasStackDump) {
  20. ASSERT_DEATH({ CARBON_CHECK(AlwaysFalse()); }, "\nStack dump:\n");
  21. }
  22. TEST(CheckTest, CheckTrueCallbackNotUsed) {
  23. bool called = false;
  24. auto callback = [&]() {
  25. called = true;
  26. return "called";
  27. };
  28. CARBON_CHECK(AlwaysTrue(), "{0}", callback());
  29. EXPECT_FALSE(called);
  30. }
  31. TEST(CheckTest, CheckFalseMessage) {
  32. ASSERT_DEATH({ CARBON_CHECK(AlwaysFalse(), "msg"); },
  33. R"(
  34. CHECK failure at common/check_test.cpp:.+: AlwaysFalse\(\): msg
  35. )");
  36. }
  37. TEST(CheckTest, CheckFalseFormattedMessage) {
  38. const char msg[] = "msg";
  39. std::string str = "str";
  40. int i = 1;
  41. ASSERT_DEATH(
  42. { CARBON_CHECK(AlwaysFalse(), "{0} {1} {2} {3}", msg, str, i, 0); },
  43. R"(
  44. CHECK failure at common/check_test.cpp:.+: AlwaysFalse\(\): msg str 1 0
  45. )");
  46. }
  47. TEST(CheckTest, CheckOutputForms) {
  48. const char msg[] = "msg";
  49. std::string str = "str";
  50. int i = 1;
  51. CARBON_CHECK(AlwaysTrue(), "{0} {1} {2} {3}", msg, str, i, 0);
  52. }
  53. TEST(CheckTest, Fatal) {
  54. ASSERT_DEATH({ CARBON_FATAL("msg"); },
  55. "\nFATAL failure at common/check_test.cpp:.+: msg\n");
  56. }
  57. TEST(CheckTest, FatalHasStackDump) {
  58. ASSERT_DEATH({ CARBON_FATAL("msg"); }, "\nStack dump:\n");
  59. }
  60. auto FatalNoReturnRequired() -> int { CARBON_FATAL("msg"); }
  61. TEST(ErrorTest, FatalNoReturnRequired) {
  62. ASSERT_DEATH({ FatalNoReturnRequired(); },
  63. "\nFATAL failure at common/check_test.cpp:.+: msg\n");
  64. }
  65. // Detects whether `CARBON_CHECK(F())` compiles.
  66. template <auto F>
  67. concept CheckCompilesWithCondition = requires { CARBON_CHECK(F()); };
  68. TEST(CheckTest, CheckConstantCondition) {
  69. EXPECT_TRUE(CheckCompilesWithCondition<[] { return AlwaysTrue(); }>);
  70. EXPECT_TRUE(CheckCompilesWithCondition<[] { return AlwaysFalse(); }>);
  71. EXPECT_FALSE(CheckCompilesWithCondition<[] { return true; }>);
  72. EXPECT_FALSE(CheckCompilesWithCondition<[] { return false; }>);
  73. }
  74. } // namespace
  75. } // namespace Carbon