Skip to content

Commit

Permalink
[BUG] Unnecessary "utbot_abs_error" constant added to the test #405
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
alexey-utkin committed Sep 5, 2022
1 parent 69f5f2d commit 4fdd4d9
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 25 deletions.
21 changes: 14 additions & 7 deletions server/src/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ Tests::MethodDescription::MethodDescription()
{ Tests::ERROR_SUITE_NAME, std::string() }},
modifiers{} { }

static const std::unordered_map<std::string, std::string> FPSpecialValuesMappings = {
{"nan", "NAN"},
{"-nan", "-NAN"},
{"inf", "INFINITY"},
{"-inf", "-INFINITY"}
};

static std::string makeDecimalConstant(std::string value, const std::string &typeName) {
if (typeName == "long") {
if (value == INT64_MIN_STRING) {
Expand All @@ -55,16 +62,16 @@ static std::string makeDecimalConstant(std::string value, const std::string &typ
if (typeName == "unsigned long long") {
return value + "ULL";
}
if (typeName == "long double") {
if ( FPSpecialValuesMappings.find(value) == FPSpecialValuesMappings.end()) {
// we need it to avoid overflow in exponent for const like 1.18973e+4932L
// BUT! Skip the NAN/INFINITY values
return value + "L";
}
}
return value;
}

static const std::unordered_map<std::string, std::string> FPSpecialValuesMappings = {
{"nan", "NAN"},
{"-nan", "-NAN"},
{"inf", "INFINITY"},
{"-inf", "-INFINITY"}
};

namespace tests {
/**
* The function checks for presence of argument in values as it is
Expand Down
1 change: 1 addition & 0 deletions server/src/clang-utils/SourceToHeaderRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ std::string SourceToHeaderRewriter::generateTestHeader(const fs::path &sourceFil
sourceFileToInclude =
fs::canonical(test.sourceFilePath.parent_path() / test.mainHeader.value().path);
}
sourceFileToInclude = fs::relative(sourceFilePath, test.testHeaderFilePath.parent_path());
return StringUtils::stringFormat("#define main main__\n\n"
"#include \"%s\"\n\n",
sourceFileToInclude);
Expand Down
2 changes: 0 additions & 2 deletions server/src/printers/TestsPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ void TestsPrinter::joinToFinalCode(Tests &tests, const fs::path& generatedHeader
genHeaders(tests, generatedHeaderPath);
ss << "namespace " << PrinterUtils::TEST_NAMESPACE << " {\n";

strDeclareAbsError(PrinterUtils::ABS_ERROR);

for (const auto &commentBlock : tests.commentBlocks) {
strComment(commentBlock) << NL;
}
Expand Down
2 changes: 2 additions & 0 deletions server/src/utils/PrinterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace PrinterUtils {
const std::string ACTUAL = "actual";
const std::string ABS_ERROR = "utbot_abs_error";
const std::string EXPECT_ = "EXPECT_";
const std::string EXPECT_FLOAT_EQ = "EXPECT_FLOAT_EQ";
const std::string EXPECT_DOUBLE_EQ = "EXPECT_DOUBLE_EQ";
const std::string EQ = "EQ";

std::string convertToBytesFunctionName(const std::string &typeName) {
Expand Down
4 changes: 4 additions & 0 deletions server/src/utils/PrinterUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ namespace PrinterUtils {
extern const std::string KLEE_PATH_FLAG;
extern const std::string KLEE_PATH_FLAG_SYMBOLIC;
extern const std::string EQ_OPERATOR;

extern const std::string ASSIGN_OPERATOR;
extern const std::string TAB;

extern const std::string EXPECTED;
extern const std::string EXPECT_FLOAT_EQ;
extern const std::string EXPECT_DOUBLE_EQ;

extern const std::string ACTUAL;
extern const std::string ABS_ERROR;
extern const std::string EXPECT_;
Expand Down
15 changes: 9 additions & 6 deletions server/src/visitors/AssertsVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ namespace visitor {

AssertsVisitor::FunctionSignature AssertsVisitor::processExpect(
const types::Type &type, const std::string &gtestMacro, std::vector<std::string> &&args) {
bool changePredicate = types::TypesHandler::isFloatingPointType(type) && (gtestMacro == PrinterUtils::EQ);
std::string targetMacro = gtestMacro;
if (changePredicate) {
targetMacro = "NEAR";
args.emplace_back(PrinterUtils::ABS_ERROR);
if (types::TypesHandler::isFloatingPointType(type) && gtestMacro == PrinterUtils::EQ) {
const types::TypeName &typeName = type.baseType();
if (typeName == "float") {
return VerboseAssertsVisitor::FunctionSignature{ PrinterUtils::EXPECT_FLOAT_EQ, std::move(args) };
}
if (typeName == "double" || typeName == "long double") {
return VerboseAssertsVisitor::FunctionSignature{ PrinterUtils::EXPECT_DOUBLE_EQ, std::move(args) };
}
}
return VerboseAssertsVisitor::FunctionSignature{ PrinterUtils::EXPECT_ + targetMacro, std::move(args) };
return VerboseAssertsVisitor::FunctionSignature{ PrinterUtils::EXPECT_ + gtestMacro, std::move(args) };
}

std::string AssertsVisitor::getDecorateActualVarName(const std::string &access) {
Expand Down
14 changes: 10 additions & 4 deletions server/test/framework/Syntax_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1693,12 +1693,18 @@ namespace {


ASSERT_TRUE(status.ok()) << status.error_message();

printer::TestsPrinter testsPrinter(nullptr, utbot::Language::C);
const auto &tests = testGen.tests.at(floats_special_c)
.methods.begin().value().testCases;
checkTestCasePredicates(
testGen.tests.at(floats_special_c).methods.begin().value().testCases,
std::vector<TestCasePredicate>(
{[](const tests::Tests::MethodTestCase &testCase) {
tests, std::vector<TestCasePredicate>(
{ [](const tests::Tests::MethodTestCase &testCase) {
return testCase.paramValues[0].view->getEntryValue(nullptr) == "NAN";
}}));
},
[](const tests::Tests::MethodTestCase &testCase) {
return testCase.paramValues[0].view->getEntryValue(nullptr) == "0.000000e+00L";
} }));
}

TEST_F(Syntax_Test, Floats_Special_Values_Inf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
ACCESS_PRIVATE_FIELD(Point_2d, int, x);

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



Expand Down Expand Up @@ -67,7 +66,7 @@ namespace UTBot {
{
Point_2d Point_2d_obj;
double actual = Point_2d_obj.get_dist_to_zero();
EXPECT_NEAR(-0.000000e+00, actual, utbot_abs_error);
EXPECT_DOUBLE_EQ(-0.000000e+00, actual);
}


Expand Down Expand Up @@ -106,7 +105,7 @@ namespace UTBot {
class Point_2d lhs = {0, 0};
class Point_2d rhs = {0, 0};
double actual = get_dist(lhs, rhs);
EXPECT_NEAR(0.000000e+00, actual, utbot_abs_error);
EXPECT_DOUBLE_EQ(0.000000e+00, actual);
class Point_2d expected_lhs = {0, 0};
EXPECT_EQ(access_private::x(expected_lhs), access_private::x(lhs));
EXPECT_EQ(expected_lhs.y, lhs.y);
Expand Down
2 changes: 1 addition & 1 deletion server/test/suites/syntax/floats_special.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int is_nanf(float x) {
}
}

int is_nan(double x) {
int is_nan(long double x) {
if (x != x) {
return 1;
} else {
Expand Down
2 changes: 1 addition & 1 deletion server/test/suites/syntax/floats_special.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define UNITTESTBOT_FLOATS_SPECIAL_H

int is_nanf(float x);
int is_nan(double x);
int is_nan(long double x);
int is_inf(float x);

#endif //UNITTESTBOT_FLOATS_SPECIAL_H

0 comments on commit 4fdd4d9

Please sign in to comment.