1 /*
2 * Copyright 2019 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "rtc_base/checks.h"
12
13 #include "test/gtest.h"
14
TEST(ChecksTest,ExpressionNotEvaluatedWhenCheckPassing)15 TEST(ChecksTest, ExpressionNotEvaluatedWhenCheckPassing) {
16 int i = 0;
17 RTC_CHECK(true) << "i=" << ++i;
18 RTC_CHECK_EQ(i, 0) << "Previous check passed, but i was incremented!";
19 }
20
21 #if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
TEST(ChecksDeathTest,Checks)22 TEST(ChecksDeathTest, Checks) {
23 #if RTC_CHECK_MSG_ENABLED
24 EXPECT_DEATH(FATAL() << "message",
25 "\n\n#\n"
26 "# Fatal error in: \\S+, line \\w+\n"
27 "# last system error: \\w+\n"
28 "# Check failed: FATAL\\(\\)\n"
29 "# message");
30
31 int a = 1, b = 2;
32 EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
33 "\n\n#\n"
34 "# Fatal error in: \\S+, line \\w+\n"
35 "# last system error: \\w+\n"
36 "# Check failed: a == b \\(1 vs. 2\\)\n"
37 "# 12");
38 RTC_CHECK_EQ(5, 5);
39
40 RTC_CHECK(true) << "Shouldn't crash" << 1;
41 EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
42 "\n\n#\n"
43 "# Fatal error in: \\S+, line \\w+\n"
44 "# last system error: \\w+\n"
45 "# Check failed: false\n"
46 "# Hi there!");
47 #else
48 EXPECT_DEATH(FATAL() << "message",
49 "\n\n#\n"
50 "# Fatal error in: \\S+, line \\w+\n"
51 "# last system error: \\w+\n"
52 "# Check failed.\n"
53 "# ");
54
55 int a = 1, b = 2;
56 EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
57 "\n\n#\n"
58 "# Fatal error in: \\S+, line \\w+\n"
59 "# last system error: \\w+\n"
60 "# Check failed.\n"
61 "# ");
62 RTC_CHECK_EQ(5, 5);
63
64 RTC_CHECK(true) << "Shouldn't crash" << 1;
65 EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
66 "\n\n#\n"
67 "# Fatal error in: \\S+, line \\w+\n"
68 "# last system error: \\w+\n"
69 "# Check failed.\n"
70 "# ");
71 #endif // RTC_CHECK_MSG_ENABLED
72 }
73 #endif // GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
74