forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogletest.cpp
More file actions
81 lines (67 loc) · 2.23 KB
/
googletest.cpp
File metadata and controls
81 lines (67 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Test library configuration for googletest.cfg
//
// Usage:
// $ cppcheck --check-library --library=googletest --enable=style,information --inconclusive --error-exitcode=1 --disable=missingInclude --inline-suppr test/cfg/googletest.cpp
// =>
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
//
#include <gmock/gmock-generated-matchers.h>
#include <gtest/gtest.h>
namespace ExampleNamespace {
constexpr long long TOLERANCE = 10;
// #9397 syntaxError when MATCHER_P is not known
// cppcheck-suppress symbolDatabaseWarning
MATCHER_P(ExampleMatcherPTest, expected, "")
{
// cppcheck-suppress valueFlowBailoutIncompleteVar
return ((arg <= (expected + TOLERANCE)) && (arg >= (expected - TOLERANCE)));
}
// syntaxError when MATCHER is not known
// cppcheck-suppress symbolDatabaseWarning
MATCHER(ExampleMatcherTest, "")
{
return (arg == TOLERANCE);
}
}
TEST(ASSERT, ASSERT)
{
int *a = (int*)calloc(10,sizeof(int)); // cppcheck-suppress cstyleCast
ASSERT_TRUE(a != nullptr);
a[0] = 10;
free(a);
}
// Avoid syntax error: https://sourceforge.net/p/cppcheck/discussion/general/thread/6ccc7283e2/
TEST(test_cppcheck, cppcheck)
{
TestStruct<int> it;
ASSERT_THROW(it.operator->(), std::out_of_range);
}
// #9964 - avoid compareBoolExpressionWithInt false positive
TEST(Test, assert_false_fp)
{
ASSERT_FALSE(errno < 0);
}
// Check that conditions in the ASSERT_* macros are processed correctly.
TEST(Test, warning_in_assert_macros)
{
int i = 5;
int j = 6;
// cppcheck-suppress knownConditionTrueFalse
ASSERT_TRUE(i == 5);
// cppcheck-suppress knownConditionTrueFalse
ASSERT_FALSE(i != 5);
// cppcheck-suppress duplicateExpression
ASSERT_EQ(i, i);
ASSERT_NE(i, j); // expected knownConditionTrueFalse...
ASSERT_LT(i, j); // expected knownConditionTrueFalse...
// cppcheck-suppress duplicateExpression
ASSERT_LE(i, i);
ASSERT_GT(j, i); // expected knownConditionTrueFalse
// cppcheck-suppress duplicateExpression
ASSERT_GE(i, i);
unsigned int u = errno;
// cppcheck-suppress [unsignedPositive]
ASSERT_GE(u, 0);
// cppcheck-suppress [unsignedLessThanZero]
ASSERT_LT(u, 0);
}