check_test.cpp 2.6 KB

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