Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef2959c

Browse files
committed
Add counter of read/write bytes in files
1 parent b537662 commit ef2959c

File tree

8 files changed

+62
-21
lines changed

8 files changed

+62
-21
lines changed

server/src/Tests.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -968,13 +968,29 @@ void KTestObjectParser::processSymbolicStdin(Tests::TestCaseDescription &testCas
968968

969969
void KTestObjectParser::processSymbolicFiles(Tests::TestCaseDescription &testCaseDescription,
970970
const std::vector<RawKleeParam> &rawKleeParams) {
971-
std::vector<Tests::TestCaseParamValue> filesValues(types::Type::symFilesCount);
971+
std::vector<Tests::FileInfo> filesValues(types::Type::symFilesCount);
972972
for (char fileName = 'A'; fileName < 'A' + types::Type::symFilesCount; fileName++) {
973+
std::string readBytesName = PrinterUtils::getFileReadBytesParamKTestJSON(fileName);
974+
auto &&readBytes = getKleeParamOrThrow(rawKleeParams, readBytesName);
975+
filesValues[fileName - 'A'].readBytes = std::stoi(
976+
testParameterView(readBytes, { types::Type::longlongType(), readBytesName },
977+
types::PointerUsage::PARAMETER, testCaseDescription.lazyAddressToName,
978+
testCaseDescription.lazyReferences)
979+
->getEntryValue(nullptr));
980+
981+
std::string writeBytesName = PrinterUtils::getFileWriteBytesParamKTestJSON(fileName);
982+
auto &&writeBytes = getKleeParamOrThrow(rawKleeParams, writeBytesName);
983+
filesValues[fileName - 'A'].writeBytes = std::stoi(
984+
testParameterView(writeBytes, { types::Type::longlongType(), writeBytesName },
985+
types::PointerUsage::PARAMETER, testCaseDescription.lazyAddressToName,
986+
testCaseDescription.lazyReferences)
987+
->getEntryValue(nullptr));
988+
973989
auto &&fileBuffer =
974990
getKleeParamOrThrow(rawKleeParams, PrinterUtils::getFileParamKTestJSON(fileName));
975-
auto &&testParamView = stringLiteralView(fileBuffer.rawData, types::Type::symInputSize);
976-
filesValues[fileName - 'A'] = Tests::TestCaseParamValue(
977-
types::Type::getFileParamName(fileName), std::nullopt, testParamView);
991+
filesValues[fileName - 'A'].data =
992+
stringLiteralView(fileBuffer.rawData, filesValues[fileName - 'A'].readBytes)
993+
->getEntryValue(nullptr);
978994
}
979995
testCaseDescription.filesValues = filesValues;
980996
}

server/src/Tests.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ namespace tests {
390390
view(std::move(_view)) {}
391391
};
392392

393+
struct FileInfo {
394+
std::string data;
395+
int readBytes;
396+
int writeBytes;
397+
};
398+
393399
struct TestCaseDescription {
394400
std::string suiteName;
395401

@@ -408,8 +414,8 @@ namespace tests {
408414
TestCaseParamValue returnValue;
409415
TestCaseParamValue functionReturnNotNullValue;
410416
TestCaseParamValue kleePathFlagSymbolicValue;
411-
std::optional <TestCaseParamValue> stdinValue = std::nullopt;
412-
std::optional <std::vector<TestCaseParamValue>> filesValues = std::nullopt;
417+
std::optional<TestCaseParamValue> stdinValue = std::nullopt;
418+
std::optional<std::vector<FileInfo>> filesValues;
413419
std::optional<TestCaseParamValue> classPreValues;
414420
std::optional<TestCaseParamValue> classPostValues;
415421
};
@@ -421,8 +427,8 @@ namespace tests {
421427

422428
std::vector<TestCaseParamValue> globalPreValues;
423429
std::vector<TestCaseParamValue> globalPostValues;
424-
std::optional <TestCaseParamValue> stdinValue;
425-
std::optional <std::vector<TestCaseParamValue>> filesValues = std::nullopt;
430+
std::optional<TestCaseParamValue> stdinValue;
431+
std::optional<std::vector<FileInfo>> filesValues;
426432
std::vector<InitReference> lazyReferences;
427433
std::vector<UTBotKTestObject> objects;
428434

server/src/printers/TestsPrinter.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,28 @@ void TestsPrinter::initializeFiles(const Tests::MethodDescription &methodDescrip
256256
fs::path pathToSourceFile =
257257
Paths::sourcePathToTestPath(projectContext, methodDescription.sourceFilePath);
258258
fs::path pathToTestDir = Paths::getPathDirRelativeToBuildDir(projectContext, pathToSourceFile);
259+
int numInitFiles = 0;
259260
for (char fileName = 'A'; fileName < 'A' + types::Type::symFilesCount; fileName++) {
261+
if (testCase.filesValues.value()[fileName - 'A'].readBytes == 0) {
262+
continue;
263+
}
264+
265+
numInitFiles++;
260266
std::string strFileName(1, fileName);
261-
strFunctionCall("write_to_file",
262-
{ StringUtils::wrapQuotations(pathToTestDir / strFileName),
263-
testCase.filesValues.value()[fileName - 'A'].view->getEntryValue(this) });
267+
strFunctionCall("write_to_file", { StringUtils::wrapQuotations(pathToTestDir / strFileName),
268+
testCase.filesValues.value()[fileName - 'A'].data });
269+
}
270+
if (numInitFiles != 0) {
271+
ss << NL;
264272
}
265-
ss << NL;
266273
}
267274

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

280-
std::string strFileName(1, fileName++);
291+
std::string strFileName(1, fileName);
292+
std::string fileMode =
293+
testCase.filesValues.value()[fileName - 'A'].writeBytes > 0 ? "\"w\"" : "\"r\"";
281294
strDeclareVar(param.type.typeName(), param.name,
282295
constrFunctionCall(
283296
"(UTBot::FILE *) fopen",
284-
{ StringUtils::wrapQuotations(pathToTestDir / strFileName), "\"r\"" }, "",
285-
std::nullopt, false));
297+
{ StringUtils::wrapQuotations(pathToTestDir / strFileName), fileMode },
298+
"", std::nullopt, false));
299+
fileName++;
286300
}
287301
if (fileName != 'A') {
288302
ss << NL;

server/src/types/Types.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,6 @@ const std::string &types::Type::getStdinParamName() {
284284
return stdinParamName;
285285
}
286286

287-
std::string types::Type::getFileParamName(char fileName) {
288-
return StringUtils::stringFormat("%c_file_buf", fileName);
289-
}
290-
291287
bool types::Type::isPointerToPointer() const {
292288
const std::vector<std::shared_ptr<AbstractType>> pointerArrayKinds = this->pointerArrayKinds();
293289
return pointerArrayKinds.size() > 1 &&

server/src/types/Types.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ namespace types {
242242
static const size_t symFilesCount = 3;
243243

244244
static const std::string &getStdinParamName();
245-
static std::string getFileParamName(char fileName);
246245
private:
247246

248247
explicit Type(const TypeName& type, size_t pointersNum=0);

server/src/utils/PrinterUtils.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,12 @@ namespace PrinterUtils {
136136
std::string getFileParamKTestJSON(char fileName) {
137137
return StringUtils::stringFormat("%c-data", fileName);
138138
}
139+
140+
std::string getFileReadBytesParamKTestJSON(char fileName) {
141+
return StringUtils::stringFormat("%c-data-read", fileName);
142+
}
143+
144+
std::string getFileWriteBytesParamKTestJSON(char fileName) {
145+
return StringUtils::stringFormat("%c-data-write", fileName);
146+
}
139147
}

server/src/utils/PrinterUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ namespace PrinterUtils {
103103
std::string generateNewVar(int cnt);
104104

105105
std::string getFileParamKTestJSON(char fileName);
106+
std::string getFileReadBytesParamKTestJSON(char fileName);
107+
std::string getFileWriteBytesParamKTestJSON(char fileName);
106108

107109
const std::string LAZYRENAME = "utbotInnerVar";
108110
const std::string UTBOT_ARGC = "utbot_argc";

0 commit comments

Comments
 (0)