Skip to content

Commit

Permalink
Generate stubs for function pointers of inner structs (#607)
Browse files Browse the repository at this point in the history
* Generate stubs for function pointers of inner structs
  • Loading branch information
IDKWNTCMF authored Aug 4, 2023
1 parent 593344f commit 03d7cf2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
4 changes: 1 addition & 3 deletions server/src/visitors/FunctionPointerForStubsVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ namespace visitor {
const tests::AbstractValueView *view,
const std::string &access,
int depth) {
if (depth == 0) {
AbstractValueViewVisitor::visitPointer(type, name, view, access, depth);
}
AbstractValueViewVisitor::visitPointer(type, name, view, access, depth);
}

void FunctionPointerForStubsVisitor::visitArray(const types::Type &type,
Expand Down
38 changes: 38 additions & 0 deletions server/test/framework/Server_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2141,4 +2141,42 @@ namespace {
StatusCountMap expectedStatusCountMap{ { testsgen::TEST_PASSED, 3 } };
testUtils::checkStatuses(resultsMap, tests);
}

TEST_F(Server_Test, Run_Tests_For_Structs_With_Pointers) {
fs::path structs_with_pointers_c = getTestFilePath("structs_with_pointers.c");
auto request = testUtils::createFileRequest(projectName, suitePath, buildDirRelativePath,
srcPaths, structs_with_pointers_c,
GrpcUtils::UTBOT_AUTO_TARGET_PATH, true, false);
auto testGen = FileTestGen(*request, writer.get(), TESTMODE);
Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get());
ASSERT_TRUE(status.ok()) << status.error_message();
EXPECT_GE(testUtils::getNumberOfTests(testGen.tests), 3);

fs::path testsDirPath = getTestFilePath("tests");

fs::path structs_with_pointers_test_cpp = Paths::sourcePathToTestPath(
utbot::ProjectContext(projectName, suitePath, testsDirPath, buildDirRelativePath, clientProjectPath),
structs_with_pointers_c);
auto testFilter = GrpcUtils::createTestFilterForFile(structs_with_pointers_test_cpp);
auto runRequest = testUtils::createCoverageAndResultsRequest(
projectName, suitePath, testsDirPath, buildDirRelativePath, std::move(testFilter));

static auto coverageAndResultsWriter =
std::make_unique<ServerCoverageAndResultsWriter>(nullptr);
CoverageAndResultsGenerator coverageGenerator{ runRequest.get(),
coverageAndResultsWriter.get() };
utbot::SettingsContext settingsContext{
true, false, 45, 0, false, false, ErrorMode::FAILING, false
};
coverageGenerator.generate(false, settingsContext);

EXPECT_FALSE(coverageGenerator.hasExceptions());
ASSERT_TRUE(coverageGenerator.getCoverageMap().empty());

auto resultsMap = coverageGenerator.getTestResultMap();
auto tests = coverageGenerator.getTestsToLaunch();

StatusCountMap expectedStatusCountMap{ { testsgen::TEST_PASSED, 3 } };
testUtils::checkStatuses(resultsMap, tests);
}
}
1 change: 1 addition & 0 deletions server/test/suites/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_executable(server
simple_structs.c
simple_unions.c
struct_with_union.c
structs_with_pointers.c
complex_structs.c
typedefs.c
types.c
Expand Down
8 changes: 8 additions & 0 deletions server/test/suites/server/structs_with_pointers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "structs_with_pointers.h"

int process_struct_with_func_pointer(struct MainStruct* str) {
if (str && str->inner && str->inner->func_id != 0) {
return 1;
}
return 0;
}
17 changes: 17 additions & 0 deletions server/test/suites/server/structs_with_pointers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef UNITTESTBOT_STRUCTS_WITH_POINTERS_H
#define UNITTESTBOT_STRUCTS_WITH_POINTERS_H

typedef int (*some_func)();

struct InnerStruct {
some_func func;
int func_id;
};

struct MainStruct {
struct InnerStruct* inner;
};

int process_struct_with_func_pointer(struct MainStruct* str);

#endif // UNITTESTBOT_STRUCTS_WITH_POINTERS_H

0 comments on commit 03d7cf2

Please sign in to comment.