Skip to content

Commit 07a8d08

Browse files
Alexey Pleshakovqrort
authored andcommitted
simplified Project_Test, added unit tests for features.
1 parent 9199a0d commit 07a8d08

File tree

11 files changed

+219
-28
lines changed

11 files changed

+219
-28
lines changed

server/test/framework/Server_Tests.cpp

Lines changed: 138 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ namespace {
9999
}
100100
}
101101

102+
struct FileGenResult {
103+
FileTestGen testGen;
104+
Status status;
105+
106+
FileGenResult() = delete;
107+
108+
FileGenResult(FileTestGen ttestGen, Status status) : testGen(std::move(ttestGen)),
109+
status(std::move(status)) {}
110+
};
111+
112+
FileGenResult performFeatureFileTestsRequest(const fs::path &filename) {
113+
auto projectRequest =
114+
createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths);
115+
auto request = GrpcUtils::createFileRequest(std::move(projectRequest), filename);
116+
auto testGen = FileTestGen(*request, writer.get(), TESTMODE);
117+
testGen.setTargetForSource(filename);
118+
Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get());
119+
return FileGenResult(testGen, status);
120+
}
121+
102122
void checkAssertionFailures_C(BaseTestGen &testGen) {
103123
testUtils::checkTestCasePredicates(
104124
testGen.tests.at(assertion_failures_c).methods.begin().value().testCases,
@@ -113,6 +133,8 @@ namespace {
113133
}
114134

115135
void checkBasicFunctions_C(BaseTestGen &testGen) {
136+
EXPECT_EQ(printer::TestsPrinter::needsMathHeader(testGen.tests.at(basic_functions_c)),
137+
false);
116138
for (const auto &[methodName, methodDescription] :
117139
testGen.tests.at(basic_functions_c).methods) {
118140
if (methodName == "max_") {
@@ -467,6 +489,9 @@ namespace {
467489
}
468490
}
469491
void checkFloatingPointPlain_C(BaseTestGen &testGen) {
492+
EXPECT_EQ(
493+
printer::TestsPrinter::needsMathHeader(testGen.tests.at(floating_point_plain_c)),
494+
true);
470495
for (const auto &[methodName, methodDescription] :
471496
testGen.tests.at(floating_point_plain_c).methods) {
472497
if (methodName == "plain_isnan") {
@@ -480,14 +505,6 @@ namespace {
480505
}
481506
}
482507

483-
void checkMathInclude(BaseTestGen &testGen) {
484-
EXPECT_EQ(
485-
printer::TestsPrinter::needsMathHeader(testGen.tests.at(floating_point_plain_c)),
486-
true);
487-
EXPECT_EQ(printer::TestsPrinter::needsMathHeader(testGen.tests.at(basic_functions_c)),
488-
false);
489-
}
490-
491508
void checkLinkage(BaseTestGen &testGen) {
492509
const auto &methods = testGen.tests.at(linkage_c).methods;
493510
EXPECT_EQ(methods.size(), 3);
@@ -535,7 +552,7 @@ namespace {
535552

536553
void checkKeywords(BaseTestGen &testGen) {
537554
auto const &methods = testGen.tests.at(keywords_c).methods;
538-
testUtils::checkMinNumberOfTests(testGen.tests, 14);
555+
testUtils::checkMinNumberOfTests(testGen.tests, 13);
539556
for (const auto &[_, md] : methods) {
540557
if (md.name == "get_size_of_data") {
541558
checkTestCasePredicates(md.testCases,
@@ -737,6 +754,110 @@ namespace {
737754
}
738755
}
739756

757+
TEST_F(Server_Test, Assertions_Failures) {
758+
auto [testGen, status] = performFeatureFileTestsRequest(assertion_failures_c);
759+
ASSERT_TRUE(status.ok()) << status.error_message();
760+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
761+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
762+
checkAssertionFailures_C(testGen);
763+
}
764+
765+
TEST_F(Server_Test, Dependent_Functions) {
766+
auto [testGen, status] = performFeatureFileTestsRequest(dependent_functions_c);
767+
ASSERT_TRUE(status.ok()) << status.error_message();
768+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
769+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
770+
checkDependentFunctions_C(testGen);
771+
}
772+
773+
TEST_F(Server_Test, Simple_Structs) {
774+
auto [testGen, status] = performFeatureFileTestsRequest(simple_structs_c);
775+
ASSERT_TRUE(status.ok()) << status.error_message();
776+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
777+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
778+
checkSimpleStructs_C(testGen);
779+
}
780+
781+
TEST_F(Server_Test, Simple_Unions) {
782+
auto [testGen, status] = performFeatureFileTestsRequest(simple_unions_c);
783+
ASSERT_TRUE(status.ok()) << status.error_message();
784+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
785+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
786+
checkSimpleUnions_C(testGen);
787+
}
788+
789+
TEST_F(Server_Test, Pointer_Parameters) {
790+
auto [testGen, status] = performFeatureFileTestsRequest(pointer_parameters_c);
791+
ASSERT_TRUE(status.ok()) << status.error_message();
792+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
793+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
794+
checkPointerParameters_C(testGen);
795+
}
796+
797+
TEST_F(Server_Test, Types) {
798+
auto [testGen, status] = performFeatureFileTestsRequest(types_c);
799+
ASSERT_TRUE(status.ok()) << status.error_message();
800+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
801+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
802+
checkTypes_C(testGen);
803+
}
804+
805+
TEST_F(Server_Test, Pointer_Return) {
806+
auto [testGen, status] = performFeatureFileTestsRequest(pointer_return_c);
807+
ASSERT_TRUE(status.ok()) << status.error_message();
808+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
809+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
810+
checkPointerReturn_C(testGen);
811+
}
812+
813+
TEST_F(Server_Test, Floating_Point) {
814+
auto [testGen, status] = performFeatureFileTestsRequest(floating_point_c);
815+
ASSERT_TRUE(status.ok()) << status.error_message();
816+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
817+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
818+
checkFloatingPoint_C(testGen);
819+
}
820+
821+
TEST_F(Server_Test, Floating_Point_plain) {
822+
auto [testGen, status] = performFeatureFileTestsRequest(floating_point_plain_c);
823+
ASSERT_TRUE(status.ok()) << status.error_message();
824+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
825+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
826+
checkFloatingPointPlain_C(testGen);
827+
}
828+
829+
TEST_F(Server_Test, Linkage) {
830+
auto [testGen, status] = performFeatureFileTestsRequest(linkage_c);
831+
ASSERT_TRUE(status.ok()) << status.error_message();
832+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
833+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
834+
checkLinkage(testGen);
835+
}
836+
837+
TEST_F(Server_Test, Globals) {
838+
auto [testGen, status] = performFeatureFileTestsRequest(globals_c);
839+
ASSERT_TRUE(status.ok()) << status.error_message();
840+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
841+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
842+
checkGlobals(testGen);
843+
}
844+
845+
TEST_F(Server_Test, Keywords) {
846+
auto [testGen, status] = performFeatureFileTestsRequest(keywords_c);
847+
ASSERT_TRUE(status.ok()) << status.error_message();
848+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
849+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
850+
checkKeywords(testGen);
851+
}
852+
853+
854+
TEST_F(Server_Test, Alignment) {
855+
auto [testGen, status] = performFeatureFileTestsRequest(alignment_c);
856+
ASSERT_TRUE(status.ok()) << status.error_message();
857+
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
858+
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
859+
checkAlignment(testGen);
860+
}
740861

741862
class Parameterized_Server_Test : public Server_Test,
742863
public testing::WithParamInterface<std::tuple<CompilerName>> {
@@ -781,6 +902,9 @@ namespace {
781902
}
782903

783904
TEST_P(Parameterized_Server_Test, Project_Test) {
905+
std::string suite = "small-project";
906+
setSuite(suite);
907+
srcPaths = {suitePath, suitePath / "lib", suitePath / "src"};
784908
auto request = createProjectRequest(projectName, suitePath, buildDirRelativePath, srcPaths);
785909
auto testGen = ProjectTestGen(*request, writer.get(), TESTMODE);
786910
testGen.setTargetForSource(testGen.testingMethodsSourcePaths[0]);
@@ -791,22 +915,11 @@ namespace {
791915
auto testFilePaths = CollectionUtils::getKeys(testGen.tests);
792916
EXPECT_TRUE(!testFilePaths.empty()) << "Generated test files are missing.";
793917

794-
checkAssertionFailures_C(testGen);
795-
checkBasicFunctions_C(testGen);
796-
checkDependentFunctions_C(testGen);
797-
checkInnerBasicFunctions_C(testGen);
798-
checkSimpleStructs_C(testGen);
799-
checkSimpleUnions_C(testGen);
800-
checkPointerParameters_C(testGen);
801-
checkTypes_C(testGen);
802-
checkPointerReturn_C(testGen);
803-
checkFloatingPoint_C(testGen);
804-
checkFloatingPointPlain_C(testGen);
805-
checkMathInclude(testGen);
806-
checkLinkage(testGen);
807-
checkGlobals(testGen);
808-
checkKeywords(testGen);
809-
checkAlignment(testGen);
918+
for (const auto &test : testGen.tests) {
919+
for (const auto &[methodName, methodDescription] : test.second.methods) {
920+
testUtils::checkMinNumberOfTests(methodDescription.testCases, 2);
921+
}
922+
}
810923
}
811924

812925
TEST_P(Parameterized_Server_Test, File_Test) {

server/test/framework/Syntax_Tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace {
7575
}
7676

7777
std::pair<FunctionTestGen, Status> createTestForFunction(const fs::path &pathToFile,
78-
int lineNum, int kleeTimeout = 30) {
78+
int lineNum, int kleeTimeout = 60) {
7979
auto lineRequest = createLineRequest(projectName, suitePath, buildDirRelativePath,
8080
srcPaths, pathToFile, lineNum, false, kleeTimeout);
8181
auto request = GrpcUtils::createFunctionRequest(std::move(lineRequest));

server/test/framework/TestUtils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace testUtils {
6363
const std::vector<fs::path> &srcPaths,
6464
bool useStubs = false,
6565
bool verbose = true,
66-
int kleeTimeout = 30);
66+
int kleeTimeout = 60);
6767

6868
std::unique_ptr<FileRequest> createFileRequest(const std::string &projectName,
6969
const fs::path &projectPath,
@@ -79,7 +79,7 @@ namespace testUtils {
7979
const fs::path &filePath,
8080
int line,
8181
bool verbose = true,
82-
int kleeTimeout = 30);
82+
int kleeTimeout = 60);
8383

8484
std::unique_ptr<CoverageAndResultsRequest>
8585
createCoverageAndResultsRequest(const std::string &projectName,

server/test/framework/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ int main(int argc, char **argv) {
6868
testUtils::tryExecGetBuildCommands(testUtils::getRelativeTestSuitePath("targets"), clang);
6969

7070
testUtils::tryExecGetBuildCommands(testUtils::getRelativeTestSuitePath("object-file"), clang, testUtils::MAKE_BUILD_COMMANDS_TOOL);
71+
72+
testUtils::tryExecGetBuildCommands(testUtils::getRelativeTestSuitePath("small-project"), gcc);
73+
74+
testUtils::tryExecGetBuildCommands(testUtils::getRelativeTestSuitePath("small-project"), clang);
75+
7176
} catch (std::runtime_error const &e) {
7277
return 1;
7378
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(small-project)
3+
4+
set(CMAKE_C_STANDARD 11)
5+
set(CMAKE_CXX_STANDARD 17)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
set(CMAKE_C_STANDARD_REQUIRED ON)
8+
9+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
10+
set(CMAKE_EXPORT_LINK_COMMANDS ON)
11+
12+
add_subdirectory(lib)
13+
add_subdirectory(src)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_library(lib)
2+
target_include_directories(lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
3+
file(GLOB_RECURSE ALL_SOURCES "*.c" "*.h")
4+
target_sources(lib PRIVATE ${ALL_SOURCES})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
3+
*/
4+
#include "libfile.h"
5+
6+
int my_sqr(int x) {
7+
if (x == 0) {
8+
return 1;
9+
} else {
10+
return x * x;
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
3+
*/
4+
#ifndef UNITTESTBOT_LIBFILE_H
5+
#define UNITTESTBOT_LIBFILE_H
6+
7+
int my_sqr(int x);
8+
9+
#endif //UNITTESTBOT_LIBFILE_H
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_executable(src)
2+
target_include_directories(src PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
3+
file(GLOB_RECURSE ALL_SOURCES "*.c" "*.h")
4+
target_sources(src PRIVATE ${ALL_SOURCES})
5+
target_link_libraries(src lib)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
3+
*/
4+
#include "srcfile.h"
5+
#include "libfile.h"
6+
7+
int f(int x) {
8+
if (my_sqr(x) == 1) {
9+
return 2;
10+
} else {
11+
return 3;
12+
}
13+
}
14+
15+
int main(int argc, char* argv[]) {
16+
if (argc == 1) {
17+
return f(2);
18+
} else {
19+
return f(argc);
20+
}
21+
}

0 commit comments

Comments
 (0)