Skip to content

Commit

Permalink
Add counter of read/write bytes in files
Browse files Browse the repository at this point in the history
  • Loading branch information
sava-cska committed Nov 23, 2022
1 parent 37a8569 commit 70827d6
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 25 deletions.
24 changes: 20 additions & 4 deletions server/src/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,13 +971,29 @@ void KTestObjectParser::processSymbolicStdin(Tests::TestCaseDescription &testCas

void KTestObjectParser::processSymbolicFiles(Tests::TestCaseDescription &testCaseDescription,
const std::vector<RawKleeParam> &rawKleeParams) {
std::vector<Tests::TestCaseParamValue> filesValues(types::Type::symFilesCount);
std::vector<Tests::FileInfo> filesValues(types::Type::symFilesCount);
for (char fileName = 'A'; fileName < 'A' + types::Type::symFilesCount; fileName++) {
std::string readBytesName = PrinterUtils::getFileReadBytesParamKTestJSON(fileName);
auto &&readBytes = getKleeParamOrThrow(rawKleeParams, readBytesName);
filesValues[fileName - 'A'].readBytes = std::stoi(
testParameterView(readBytes, { types::Type::longlongType(), readBytesName },
types::PointerUsage::PARAMETER, testCaseDescription.lazyAddressToName,
testCaseDescription.lazyReferences)
->getEntryValue(nullptr));

std::string writeBytesName = PrinterUtils::getFileWriteBytesParamKTestJSON(fileName);
auto &&writeBytes = getKleeParamOrThrow(rawKleeParams, writeBytesName);
filesValues[fileName - 'A'].writeBytes = std::stoi(
testParameterView(writeBytes, { types::Type::longlongType(), writeBytesName },
types::PointerUsage::PARAMETER, testCaseDescription.lazyAddressToName,
testCaseDescription.lazyReferences)
->getEntryValue(nullptr));

auto &&fileBuffer =
getKleeParamOrThrow(rawKleeParams, PrinterUtils::getFileParamKTestJSON(fileName));
auto &&testParamView = stringLiteralView(fileBuffer.rawData, types::Type::symInputSize);
filesValues[fileName - 'A'] = Tests::TestCaseParamValue(
types::Type::getFileParamName(fileName), std::nullopt, testParamView);
filesValues[fileName - 'A'].data =
stringLiteralView(fileBuffer.rawData, filesValues[fileName - 'A'].readBytes)
->getEntryValue(nullptr);
}
testCaseDescription.filesValues = filesValues;
}
Expand Down
14 changes: 10 additions & 4 deletions server/src/Tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ namespace tests {
view(std::move(_view)) {}
};

struct FileInfo {
std::string data;
int readBytes;
int writeBytes;
};

struct TestCaseDescription {
std::string suiteName;

Expand All @@ -408,8 +414,8 @@ namespace tests {
TestCaseParamValue returnValue;
TestCaseParamValue functionReturnNotNullValue;
TestCaseParamValue kleePathFlagSymbolicValue;
std::optional <TestCaseParamValue> stdinValue = std::nullopt;
std::optional <std::vector<TestCaseParamValue>> filesValues = std::nullopt;
std::optional<TestCaseParamValue> stdinValue = std::nullopt;
std::optional<std::vector<FileInfo>> filesValues;
std::optional<TestCaseParamValue> classPreValues;
std::optional<TestCaseParamValue> classPostValues;
};
Expand All @@ -421,8 +427,8 @@ namespace tests {

std::vector<TestCaseParamValue> globalPreValues;
std::vector<TestCaseParamValue> globalPostValues;
std::optional <TestCaseParamValue> stdinValue;
std::optional <std::vector<TestCaseParamValue>> filesValues = std::nullopt;
std::optional<TestCaseParamValue> stdinValue;
std::optional<std::vector<FileInfo>> filesValues;
std::vector<InitReference> lazyReferences;
std::vector<UTBotKTestObject> objects;

Expand Down
28 changes: 21 additions & 7 deletions server/src/printers/TestsPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,28 @@ void TestsPrinter::initializeFiles(const Tests::MethodDescription &methodDescrip
fs::path pathToSourceFile =
Paths::sourcePathToTestPath(projectContext, methodDescription.sourceFilePath);
fs::path pathToTestDir = Paths::getPathDirRelativeToBuildDir(projectContext, pathToSourceFile);
int numInitFiles = 0;
for (char fileName = 'A'; fileName < 'A' + types::Type::symFilesCount; fileName++) {
if (testCase.filesValues.value()[fileName - 'A'].readBytes == 0) {
continue;
}

numInitFiles++;
std::string strFileName(1, fileName);
strFunctionCall("write_to_file",
{ StringUtils::wrapQuotations(pathToTestDir / strFileName),
testCase.filesValues.value()[fileName - 'A'].view->getEntryValue(this) });
strFunctionCall("write_to_file", { StringUtils::wrapQuotations(pathToTestDir / strFileName),
testCase.filesValues.value()[fileName - 'A'].data });
}
if (numInitFiles != 0) {
ss << NL;
}
ss << NL;
}

void TestsPrinter::openFiles(const Tests::MethodDescription &methodDescription,
const Tests::MethodTestCase &testCase) {
if (!testCase.filesValues.has_value()) {
LOG_S(WARNING) << "There are not symbolic files in the test.";
return;
}
char fileName = 'A';
fs::path pathToSourceFile =
Paths::sourcePathToTestPath(projectContext, methodDescription.sourceFilePath);
Expand All @@ -277,12 +288,15 @@ void TestsPrinter::openFiles(const Tests::MethodDescription &methodDescription,
continue;
}

std::string strFileName(1, fileName++);
std::string strFileName(1, fileName);
std::string fileMode =
testCase.filesValues.value()[fileName - 'A'].writeBytes > 0 ? "\"w\"" : "\"r\"";
strDeclareVar(param.type.typeName(), param.name,
constrFunctionCall(
"(UTBot::FILE *) fopen",
{ StringUtils::wrapQuotations(pathToTestDir / strFileName), "\"r\"" }, "",
std::nullopt, false));
{ StringUtils::wrapQuotations(pathToTestDir / strFileName), fileMode },
"", std::nullopt, false));
fileName++;
}
if (fileName != 'A') {
ss << NL;
Expand Down
4 changes: 0 additions & 4 deletions server/src/types/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,6 @@ const std::string &types::Type::getStdinParamName() {
return stdinParamName;
}

std::string types::Type::getFileParamName(char fileName) {
return StringUtils::stringFormat("%c_file_buf", fileName);
}

bool types::Type::isPointerToPointer() const {
const std::vector<std::shared_ptr<AbstractType>> pointerArrayKinds = this->pointerArrayKinds();
return pointerArrayKinds.size() > 1 &&
Expand Down
1 change: 0 additions & 1 deletion server/src/types/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ namespace types {
static const size_t symFilesCount = 3;

static const std::string &getStdinParamName();
static std::string getFileParamName(char fileName);
private:

explicit Type(const TypeName& type, size_t pointersNum=0);
Expand Down
8 changes: 8 additions & 0 deletions server/src/utils/PrinterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,12 @@ namespace PrinterUtils {
std::string getFileParamKTestJSON(char fileName) {
return StringUtils::stringFormat("%c-data", fileName);
}

std::string getFileReadBytesParamKTestJSON(char fileName) {
return StringUtils::stringFormat("%c-data-read", fileName);
}

std::string getFileWriteBytesParamKTestJSON(char fileName) {
return StringUtils::stringFormat("%c-data-write", fileName);
}
}
2 changes: 2 additions & 0 deletions server/src/utils/PrinterUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ namespace PrinterUtils {
std::string generateNewVar(int cnt);

std::string getFileParamKTestJSON(char fileName);
std::string getFileReadBytesParamKTestJSON(char fileName);
std::string getFileWriteBytesParamKTestJSON(char fileName);

const std::string LAZYRENAME = "utbotInnerVar";
const std::string UTBOT_ARGC = "utbot_argc";
Expand Down
8 changes: 4 additions & 4 deletions server/test/framework/Syntax_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ namespace {
);
}

TEST_F(Syntax_Test, Pointers_In_Structs_2) {
TEST_F(Syntax_Test, DISABLED_Pointers_In_Structs_2) {
auto [testGen, status] = createTestForFunction(structs_with_pointers_c, 17);

ASSERT_TRUE(status.ok()) << status.error_message();
Expand Down Expand Up @@ -2001,7 +2001,7 @@ namespace {
);
}

TEST_F(Syntax_Test, len_bound) {
TEST_F(Syntax_Test, DISABLED_len_bound) {
auto [testGen, status] = createTestForFunction(linked_list_c, 92);

ASSERT_TRUE(status.ok()) << status.error_message();
Expand All @@ -2017,7 +2017,7 @@ namespace {
);
}

TEST_F(Syntax_Test, DISABLED_sort_list) {
TEST_F(Syntax_Test, sort_list) {
auto [testGen, status] = createTestForFunction(linked_list_c, 104, 90);

ASSERT_TRUE(status.ok()) << status.error_message();
Expand All @@ -2039,7 +2039,7 @@ namespace {
);
}

TEST_F(Syntax_Test, DISABLED_sort_list_with_cmp) {
TEST_F(Syntax_Test, sort_list_with_cmp) {
auto [testGen, status] = createTestForFunction(linked_list_c, 135, 90);

ASSERT_TRUE(status.ok()) << status.error_message();
Expand Down
2 changes: 1 addition & 1 deletion submodules/klee

0 comments on commit 70827d6

Please sign in to comment.