2019-10-09 19:53:58 +02:00
|
|
|
|
|
|
|
// Test library configuration for googletest.cfg
|
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
// $ cppcheck --check-library --library=googletest --enable=information --error-exitcode=1 --inline-suppr --suppress=missingIncludeSystem test/cfg/googletest.cpp
|
|
|
|
// =>
|
|
|
|
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <gmock/gmock-generated-matchers.h>
|
2019-11-06 21:38:01 +01:00
|
|
|
#include <gtest/gtest.h>
|
2019-10-09 19:53:58 +02:00
|
|
|
|
2020-01-12 08:11:58 +01:00
|
|
|
|
2019-10-09 19:53:58 +02:00
|
|
|
namespace ExampleNamespace {
|
|
|
|
constexpr long long TOLERANCE = 10;
|
|
|
|
|
2020-01-12 08:11:58 +01:00
|
|
|
// #9397 syntaxError when MATCHER_P is not known
|
|
|
|
MATCHER_P(ExampleMatcherPTest, expected, "")
|
2019-10-09 19:53:58 +02:00
|
|
|
{
|
|
|
|
return ((arg <= (expected + TOLERANCE)) && (arg >= (expected - TOLERANCE)));
|
|
|
|
}
|
2019-11-06 21:38:01 +01:00
|
|
|
|
2020-01-12 08:11:58 +01:00
|
|
|
// syntaxError when MATCHER is not known
|
|
|
|
MATCHER(ExampleMatcherTest, "")
|
|
|
|
{
|
|
|
|
return (arg == TOLERANCE);
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 21:38:01 +01:00
|
|
|
|
2019-11-07 11:29:37 +01:00
|
|
|
TEST(ASSERT, ASSERT)
|
|
|
|
{
|
2022-02-10 20:48:51 +01:00
|
|
|
int *a = (int*)calloc(10,sizeof(int)); // cppcheck-suppress cstyleCast
|
2019-11-07 11:29:37 +01:00
|
|
|
ASSERT_TRUE(a != nullptr);
|
2019-11-06 21:38:01 +01:00
|
|
|
|
2019-11-07 11:29:37 +01:00
|
|
|
a[0] = 10;
|
2019-11-06 21:38:01 +01:00
|
|
|
|
2019-11-07 11:29:37 +01:00
|
|
|
free(a);
|
2019-11-06 21:38:01 +01:00
|
|
|
}
|
2020-09-30 18:45:04 +02:00
|
|
|
|
|
|
|
// Avoid syntax error: https://sourceforge.net/p/cppcheck/discussion/general/thread/6ccc7283e2/
|
|
|
|
TEST(test_cppcheck, cppcheck)
|
|
|
|
{
|
2020-10-01 08:33:16 +02:00
|
|
|
TestStruct<int> it;
|
|
|
|
ASSERT_THROW(it.operator->(), std::out_of_range);
|
2020-09-30 18:45:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 10:20:25 +01:00
|
|
|
// #9964 - avoid compareBoolExpressionWithInt false positive
|
|
|
|
TEST(Test, assert_false_fp)
|
|
|
|
{
|
2020-11-03 21:08:21 +01:00
|
|
|
ASSERT_FALSE(errno < 0);
|
2020-11-03 10:20:25 +01:00
|
|
|
}
|
2022-01-12 22:09:17 +01:00
|
|
|
|
|
|
|
// 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;
|
2022-05-21 08:33:42 +02:00
|
|
|
// cppcheck-suppress [unsignedPositive, compareValueOutOfTypeRangeError]
|
2022-01-12 22:09:17 +01:00
|
|
|
ASSERT_GE(u, 0);
|
2022-05-21 08:33:42 +02:00
|
|
|
// cppcheck-suppress [unsignedLessThanZero, compareValueOutOfTypeRangeError]
|
2022-01-12 22:09:17 +01:00
|
|
|
ASSERT_LT(u, 0);
|
|
|
|
}
|