Skip to content

Commit 1bc3fc5

Browse files
committed
add tests
1 parent 62f547a commit 1bc3fc5

File tree

6 files changed

+123
-46
lines changed

6 files changed

+123
-46
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
11
#include "CSVReader.h"
2+
3+
namespace StatsUtils {
4+
CSVTable readCSV(std::istream &istream, char sep, bool removeSpaces) {
5+
std::map<std::string, std::vector<std::string>> parsedCSV;
6+
std::string header;
7+
std::getline(istream, header);
8+
std::vector<std::string> keys = StringUtils::split(header, sep);
9+
if (removeSpaces) {
10+
keys = CollectionUtils::transform(keys, [](std::string s){ StringUtils::trim(s); return s; });
11+
}
12+
for (const auto &key: keys) {
13+
parsedCSV[key] = {};
14+
}
15+
while (true) {
16+
std::string row;
17+
std::getline(istream, row);
18+
if (row.empty()) {
19+
break;
20+
}
21+
std::vector<std::string> values = StringUtils::split(row, sep);
22+
if (values.size() != keys.size()) {
23+
LOG_S(WARNING) << "Cannot parse CSV. Invalid format";
24+
return {};
25+
}
26+
for (std::size_t i = 0; i < values.size(); i++) {
27+
if (removeSpaces) {
28+
StringUtils::trim(values[i]);
29+
}
30+
parsedCSV[keys[i]].push_back(values[i]);
31+
}
32+
}
33+
return parsedCSV;
34+
}
35+
}

server/src/utils/stats/CSVReader.h

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef UNITTESTBOT_CSVREADER_H
2+
#define UNITTESTBOT_CSVREADER_H
3+
14
#include <map>
25
#include <string>
36
#include <vector>
@@ -7,35 +10,7 @@
710

811
namespace StatsUtils {
912
using CSVTable = std::map<std::string, std::vector<std::string>>;
10-
CSVTable readCSV(std::istream &istream, char sep, bool removeSpaces = true) {
11-
std::map<std::string, std::vector<std::string>> parsedCSV;
12-
std::string header;
13-
std::getline(istream, header);
14-
std::vector<std::string> keys = StringUtils::split(header, sep);
15-
if (removeSpaces) {
16-
keys = CollectionUtils::transform(keys, [](std::string s){ StringUtils::trim(s); return s; });
17-
}
18-
for (const auto &key: keys) {
19-
parsedCSV[key] = {};
20-
}
21-
while (true) {
22-
std::string row;
23-
std::getline(istream, row);
24-
if (row.empty()) {
25-
break;
26-
}
27-
std::vector<std::string> values = StringUtils::split(row, sep);
28-
if (values.size() != keys.size()) {
29-
LOG_S(WARNING) << "Cannot parse CSV. Invalid format";
30-
return {};
31-
}
32-
for (std::size_t i = 0; i < values.size(); i++) {
33-
if (removeSpaces) {
34-
StringUtils::trim(values[i]);
35-
}
36-
parsedCSV[keys[i]].push_back(values[i]);
37-
}
38-
}
39-
return parsedCSV;
40-
}
13+
CSVTable readCSV(std::istream &istream, char sep, bool removeSpaces = true);
4114
}
15+
16+
#endif //UNITTESTBOT_CSVREADER_H

server/test/framework/CLI_Tests.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ namespace {
6060
complex_structs_test_h, inner_basic_functions_test_h
6161
};
6262

63+
std::vector<fs::path> allProjectSrcFiles = {
64+
"assertion_failures.c", "basic_functions.c", "complex_structs.c", "inner/inner_basic_functions.c"
65+
};
66+
6367
void SetUp() override {
6468
clearTestDirectory();
6569
clearDirectory(suitePath / resultsDirectoryName);
@@ -91,7 +95,7 @@ namespace {
9195
}
9296
}
9397

94-
void checkCoverageDirectory() {
98+
void checkResultsDirectory() {
9599
FileSystemUtils::RecursiveDirectoryIterator directoryIterator(suitePath /
96100
resultsDirectoryName);
97101
EXPECT_EQ(directoryIterator.size(), 3);
@@ -184,49 +188,60 @@ namespace {
184188
}
185189

186190
TEST_F(CLI_Test, Run_All_Tests) {
187-
runCommandLine({ "./utbot", "generate", "--project-path", suitePath, "--build-dir",
188-
buildDirectoryName, "project" });
191+
clearTestDirectory();
192+
runCommandLine({ "./utbot", "generate", "--project-path", suitePath,
193+
"--results-dir", resultsDirectoryName,
194+
"--build-dir", buildDirectoryName, "project" });
189195
checkTestDirectory(allProjectTestFiles);
196+
testUtils::checkGenerationStatsCSV(suitePath / resultsDirectoryName / "generation-stats.csv", allProjectSrcFiles);
190197
runCommandLine({ "./utbot", "run", "--project-path", suitePath, "--results-dir",
191198
resultsDirectoryName, "--build-dir", buildDirectoryName, "project" });
192-
checkCoverageDirectory();
199+
checkResultsDirectory();
200+
testUtils::checkExecutionStatsCSV(suitePath / resultsDirectoryName / "execution-stats.csv", allProjectSrcFiles);
193201
}
194202

195203
TEST_F(CLI_Test, Run_File_Tests) {
196-
runCommandLine({ "./utbot", "generate", "--project-path", suitePath, "--build-dir",
197-
buildDirectoryName, "file", "--file-path",
198-
suitePath / "basic_functions.c" });
204+
runCommandLine({ "./utbot", "generate", "--project-path", suitePath,
205+
"--results-dir", resultsDirectoryName,
206+
"--build-dir", buildDirectoryName, "file",
207+
"--file-path", suitePath / "basic_functions.c" });
199208
checkTestDirectory({ basic_functions_tests_cpp, basic_functions_tests_h });
209+
testUtils::checkGenerationStatsCSV(suitePath / resultsDirectoryName / "generation-stats.csv",
210+
{"basic_functions.c"});
200211
runCommandLine({ "./utbot", "run", "--project-path", suitePath, "--results-dir",
201212
resultsDirectoryName, "--build-dir", buildDirectoryName, "file",
202213
"--file-path", getTestDirectory() / basic_functions_tests_cpp });
203-
checkCoverageDirectory();
214+
testUtils::checkExecutionStatsCSV(suitePath / resultsDirectoryName / "execution-stats.csv",
215+
{"basic_functions.c"});
216+
checkResultsDirectory();
204217
}
205218

206219
TEST_F(CLI_Test, Run_Specific_Test) {
207-
runCommandLine({ "./utbot", "generate", "--project-path", suitePath, "--build-dir",
208-
buildDirectoryName, "file", "--file-path",
209-
suitePath / "basic_functions.c" });
220+
runCommandLine({ "./utbot", "generate", "--project-path", suitePath,
221+
"--results-dir", resultsDirectoryName,
222+
"--build-dir", buildDirectoryName, "file",
223+
"--file-path", suitePath / "basic_functions.c" });
210224
checkTestDirectory({ basic_functions_tests_cpp, basic_functions_tests_h });
211225
runCommandLine({ "./utbot", "run", "--project-path", suitePath, "--results-dir",
212226
resultsDirectoryName, "--build-dir", buildDirectoryName, "test",
213227
"--file-path", getTestDirectory() / basic_functions_tests_cpp,
214228
"--test-suite", "regression", "--test-name", " max__test_1" });
215-
checkCoverageDirectory();
229+
checkResultsDirectory();
216230
}
217231

218232
TEST_F(CLI_Test, All_Command_Tests) {
219233
runCommandLine({ "./utbot", "all", "--project-path", suitePath, "--build-dir",
220234
buildDirectoryName, "--results-dir", resultsDirectoryName });
221235
checkTestDirectory(allProjectTestFiles);
222-
checkCoverageDirectory();
236+
checkResultsDirectory();
237+
testUtils::checkGenerationStatsCSV(suitePath / resultsDirectoryName / "generation-stats.csv", allProjectSrcFiles);
238+
testUtils::checkExecutionStatsCSV(suitePath / resultsDirectoryName / "execution-stats.csv", allProjectSrcFiles);
223239
}
224240

225241
TEST_F(CLI_Test, Target_Option_Tests) {
226242
runCommandLine({ "./utbot", "generate", "--project-path", suitePath, "--build-dir",
227243
buildDirectoryName, "file", "--file-path",
228244
suitePath / "basic_functions.c", "--target", "cli" });
229245
checkTestDirectory({ basic_functions_tests_cpp, basic_functions_tests_h });
230-
231246
}
232247
}

server/test/framework/Server_Tests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,4 +1595,18 @@ namespace {
15951595
ASSERT_EQ(1, cases.size());
15961596
ASSERT_FALSE(cases[0].isError());
15971597
}
1598+
1599+
TEST_P(Parameterized_Server_Test, Stats_Test) {
1600+
std::string suite = "small-project";
1601+
setSuite(suite);
1602+
srcPaths = {};
1603+
auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths);
1604+
auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE);
1605+
setTargetForFirstSource(testGen);
1606+
1607+
Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get());
1608+
ASSERT_TRUE(status.ok()) << status.error_message();
1609+
1610+
1611+
}
15981612
}

server/test/framework/TestUtils.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
#include <fstream>
12
#include "TestUtils.h"
23

34
#include "environment/EnvironmentPaths.h"
45
#include "tasks/ShellExecTask.h"
5-
6+
#include "utils/stats/CSVReader.h"
7+
#include "utils/CollectionUtils.h"
8+
#include "utils/StringUtils.h"
69

710
namespace testUtils {
811
static std::string getMessageForTestCaseNotMatching(
@@ -362,4 +365,36 @@ namespace testUtils {
362365
testGen.setTargetForSource(sourcePath);
363366
}
364367

368+
static void checkStatsCSV(const fs::path &statsPath, const std::vector<std::string> &header,
369+
const std::vector<fs::path> &containedFiles) {
370+
EXPECT_TRUE(fs::exists(statsPath));
371+
std::ifstream inputStream(statsPath);
372+
StatsUtils::CSVTable csvTable = StatsUtils::readCSV(inputStream, ',');
373+
for (const auto &label : header) {
374+
EXPECT_TRUE(CollectionUtils::containsKey(csvTable, label)) <<
375+
StringUtils::stringFormat("Label %s absent in header in CSV %s", label, statsPath);
376+
}
377+
EXPECT_EQ(csvTable.size(), header.size());
378+
for (const auto &fileName : containedFiles) {
379+
EXPECT_TRUE(CollectionUtils::contains(csvTable["File"], fileName)) <<
380+
StringUtils::stringFormat("File %s is absent in CSV %s", fileName, statsPath);
381+
}
382+
EXPECT_TRUE(CollectionUtils::contains(csvTable["File"], "Total:"));
383+
EXPECT_EQ(csvTable["File"].size(), containedFiles.size() + 1);
384+
}
385+
386+
void checkGenerationStatsCSV(const fs::path &statsPath, const std::vector<fs::path> &containedFiles) {
387+
std::vector<std::string> header = {"File", "Klee Time (s)", "Solver Time (s)", "Resolution Time (s)",
388+
"Regression Tests Generated", "Error Tests Generated",
389+
"Covered Functions", "Total functions"};
390+
checkStatsCSV(statsPath, header, containedFiles);
391+
}
392+
393+
void checkExecutionStatsCSV(const fs::path &statsPath, const std::vector<fs::path> &containedFiles) {
394+
std::vector<std::string> header = {"File", "Google Test Execution Time (s)",
395+
"Total Tests Number", "Passed Tests Number", "Failed Tests Number",
396+
"Death Tests Number", "Interrupted Tests Number",
397+
"Total Lines Number", "Covered Lines Number", "Line Coverage Ratio (%)"};
398+
checkStatsCSV(statsPath, header, containedFiles);
399+
}
365400
}

server/test/framework/TestUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ namespace testUtils {
118118
std::vector<char*> createArgvVector(const std::vector<std::string> &args);
119119

120120
void setTargetForFirstSource(ProjectTestGen &testGen);
121+
122+
void checkGenerationStatsCSV(const fs::path &statsPath, const std::vector<fs::path> &containedFiles);
123+
124+
void checkExecutionStatsCSV(const fs::path &statsPath, const std::vector<fs::path> &containedFiles);
121125
}
122126

123127
#endif // UNITTESTBOT_TESTUTILS_H

0 commit comments

Comments
 (0)