check_test.cpp 2.6 KB

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