Skip to content

Commit 931c948

Browse files
authored
[BUG] Unnecessary "utbot_abs_error" constant added to the test #405 (#431)
* [BUG] Unnecessary "utbot_abs_error" constant added to the test #405 - use `EXPECT_FLOAT_EQ` and `EXPECT_DOUBLE_EQ` instead of `EXPECT_NEAR` - remove insertion obsolete `utbot_abs_error` to the code - support comparison `NAN` and `INFINITY` consts - make inclusion of `main.c` wrapper with relative path - remove obsolete `ABS_ERROR` - printer optimization
1 parent 69f5f2d commit 931c948

File tree

11 files changed

+44
-27
lines changed

11 files changed

+44
-27
lines changed

server/src/Tests.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ Tests::MethodDescription::MethodDescription()
3333
{ Tests::ERROR_SUITE_NAME, std::string() }},
3434
modifiers{} { }
3535

36+
static const std::unordered_map<std::string, std::string> FPSpecialValuesMappings = {
37+
{"nan", "NAN"},
38+
{"-nan", "-NAN"},
39+
{"inf", "INFINITY"},
40+
{"-inf", "-INFINITY"}
41+
};
42+
3643
static std::string makeDecimalConstant(std::string value, const std::string &typeName) {
3744
if (typeName == "long") {
3845
if (value == INT64_MIN_STRING) {
@@ -55,16 +62,16 @@ static std::string makeDecimalConstant(std::string value, const std::string &typ
5562
if (typeName == "unsigned long long") {
5663
return value + "ULL";
5764
}
65+
if (typeName == "long double") {
66+
if ( FPSpecialValuesMappings.find(value) == FPSpecialValuesMappings.end()) {
67+
// we need it to avoid overflow in exponent for const like 1.18973e+4932L
68+
// BUT! Skip the NAN/INFINITY values
69+
return value + "L";
70+
}
71+
}
5872
return value;
5973
}
6074

61-
static const std::unordered_map<std::string, std::string> FPSpecialValuesMappings = {
62-
{"nan", "NAN"},
63-
{"-nan", "-NAN"},
64-
{"inf", "INFINITY"},
65-
{"-inf", "-INFINITY"}
66-
};
67-
6875
namespace tests {
6976
/**
7077
* The function checks for presence of argument in values as it is

server/src/clang-utils/SourceToHeaderRewriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ std::string SourceToHeaderRewriter::generateTestHeader(const fs::path &sourceFil
7171
sourceFileToInclude =
7272
fs::canonical(test.sourceFilePath.parent_path() / test.mainHeader.value().path);
7373
}
74+
sourceFileToInclude = fs::relative(sourceFilePath, test.testHeaderFilePath.parent_path());
7475
return StringUtils::stringFormat("#define main main__\n\n"
7576
"#include \"%s\"\n\n",
7677
sourceFileToInclude);

server/src/printers/TestsPrinter.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ void TestsPrinter::joinToFinalCode(Tests &tests, const fs::path& generatedHeader
4848
genHeaders(tests, generatedHeaderPath);
4949
ss << "namespace " << PrinterUtils::TEST_NAMESPACE << " {\n";
5050

51-
strDeclareAbsError(PrinterUtils::ABS_ERROR);
52-
5351
for (const auto &commentBlock : tests.commentBlocks) {
5452
strComment(commentBlock) << NL;
5553
}

server/src/utils/PrinterUtils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ namespace PrinterUtils {
3636

3737
const std::string EXPECTED = "expected";
3838
const std::string ACTUAL = "actual";
39-
const std::string ABS_ERROR = "utbot_abs_error";
4039
const std::string EXPECT_ = "EXPECT_";
40+
const std::string EXPECT_FLOAT_EQ = "EXPECT_FLOAT_EQ";
41+
const std::string EXPECT_DOUBLE_EQ = "EXPECT_DOUBLE_EQ";
4142
const std::string EQ = "EQ";
4243

4344
std::string convertToBytesFunctionName(const std::string &typeName) {

server/src/utils/PrinterUtils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ namespace PrinterUtils {
2323
extern const std::string KLEE_PATH_FLAG;
2424
extern const std::string KLEE_PATH_FLAG_SYMBOLIC;
2525
extern const std::string EQ_OPERATOR;
26+
2627
extern const std::string ASSIGN_OPERATOR;
2728
extern const std::string TAB;
2829

2930
extern const std::string EXPECTED;
31+
extern const std::string EXPECT_FLOAT_EQ;
32+
extern const std::string EXPECT_DOUBLE_EQ;
33+
3034
extern const std::string ACTUAL;
31-
extern const std::string ABS_ERROR;
3235
extern const std::string EXPECT_;
3336
extern const std::string EQ;
3437

server/src/visitors/AssertsVisitor.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ namespace visitor {
3030

3131
AssertsVisitor::FunctionSignature AssertsVisitor::processExpect(
3232
const types::Type &type, const std::string &gtestMacro, std::vector<std::string> &&args) {
33-
bool changePredicate = types::TypesHandler::isFloatingPointType(type) && (gtestMacro == PrinterUtils::EQ);
34-
std::string targetMacro = gtestMacro;
35-
if (changePredicate) {
36-
targetMacro = "NEAR";
37-
args.emplace_back(PrinterUtils::ABS_ERROR);
33+
std::string macroName = PrinterUtils::EXPECT_ + gtestMacro;
34+
if (types::TypesHandler::isFloatingPointType(type) && gtestMacro == PrinterUtils::EQ) {
35+
const types::TypeName &typeName = type.baseType();
36+
if (typeName == "float") {
37+
macroName = PrinterUtils::EXPECT_FLOAT_EQ;
38+
} else if (typeName == "double" || typeName == "long double") {
39+
macroName = PrinterUtils::EXPECT_DOUBLE_EQ;
40+
}
3841
}
39-
return VerboseAssertsVisitor::FunctionSignature{ PrinterUtils::EXPECT_ + targetMacro, std::move(args) };
42+
return VerboseAssertsVisitor::FunctionSignature{ macroName, std::move(args) };
4043
}
4144

4245
std::string AssertsVisitor::getDecorateActualVarName(const std::string &access) {

server/test/framework/Syntax_Tests.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,12 +1693,18 @@ namespace {
16931693

16941694

16951695
ASSERT_TRUE(status.ok()) << status.error_message();
1696+
1697+
printer::TestsPrinter testsPrinter(nullptr, utbot::Language::C);
1698+
const auto &tests = testGen.tests.at(floats_special_c)
1699+
.methods.begin().value().testCases;
16961700
checkTestCasePredicates(
1697-
testGen.tests.at(floats_special_c).methods.begin().value().testCases,
1698-
std::vector<TestCasePredicate>(
1699-
{[](const tests::Tests::MethodTestCase &testCase) {
1701+
tests, std::vector<TestCasePredicate>(
1702+
{ [](const tests::Tests::MethodTestCase &testCase) {
17001703
return testCase.paramValues[0].view->getEntryValue(nullptr) == "NAN";
1701-
}}));
1704+
},
1705+
[](const tests::Tests::MethodTestCase &testCase) {
1706+
return testCase.paramValues[0].view->getEntryValue(nullptr) == "0.000000e+00L";
1707+
} }));
17021708
}
17031709

17041710
TEST_F(Syntax_Test, Floats_Special_Values_Inf) {

server/test/suites/coverage/pregenerated_tests/dependent_functions_dot_c_test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "gtest/gtest.h"
44
namespace UTBot {
5-
static const float utbot_abs_error = 1e-6;
65

76

87

server/test/suites/coverage/pregenerated_tests/simple_class_dot_cpp_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
ACCESS_PRIVATE_FIELD(Point_2d, int, x);
88

99
namespace UTBot {
10-
static const float utbot_abs_error = 1e-6;
1110

1211

1312

@@ -67,7 +66,7 @@ namespace UTBot {
6766
{
6867
Point_2d Point_2d_obj;
6968
double actual = Point_2d_obj.get_dist_to_zero();
70-
EXPECT_NEAR(-0.000000e+00, actual, utbot_abs_error);
69+
EXPECT_DOUBLE_EQ(-0.000000e+00, actual);
7170
}
7271

7372

@@ -106,7 +105,7 @@ namespace UTBot {
106105
class Point_2d lhs = {0, 0};
107106
class Point_2d rhs = {0, 0};
108107
double actual = get_dist(lhs, rhs);
109-
EXPECT_NEAR(0.000000e+00, actual, utbot_abs_error);
108+
EXPECT_DOUBLE_EQ(0.000000e+00, actual);
110109
class Point_2d expected_lhs = {0, 0};
111110
EXPECT_EQ(access_private::x(expected_lhs), access_private::x(lhs));
112111
EXPECT_EQ(expected_lhs.y, lhs.y);

server/test/suites/syntax/floats_special.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ int is_nanf(float x) {
99
}
1010
}
1111

12-
int is_nan(double x) {
12+
int is_nan(long double x) {
1313
if (x != x) {
1414
return 1;
1515
} else {

server/test/suites/syntax/floats_special.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define UNITTESTBOT_FLOATS_SPECIAL_H
33

44
int is_nanf(float x);
5-
int is_nan(double x);
5+
int is_nan(long double x);
66
int is_inf(float x);
77

88
#endif //UNITTESTBOT_FLOATS_SPECIAL_H

0 commit comments

Comments
 (0)