Skip to content

Commit 4a441a6

Browse files
authored
Merge pull request #775 from intel-innersource/uid_aantonov_add_gtests
Introduce GoogleTests in PCM project
2 parents 3270122 + 1a93c59 commit 4a441a6

File tree

8 files changed

+102
-2
lines changed

8 files changed

+102
-2
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
[submodule "Intel-PMT"]
1111
path = Intel-PMT
1212
url = https://github.com/intel/Intel-PMT.git
13+
[submodule "googletest"]
14+
path = googletest
15+
url = https://github.com/google/googletest.git

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ add_subdirectory(src)
185185
add_subdirectory(examples)
186186
add_subdirectory(tests)
187187

188+
if(UNIX)
189+
add_subdirectory(googletest)
190+
endif(UNIX)
191+
188192
message(STATUS "Install directory: ${CMAKE_INSTALL_PREFIX}")
189193

190194
#######################

googletest

Submodule googletest added at cd430b4

src/lspci.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,29 @@ struct bdf {
170170
uint8_t busno;
171171
uint8_t devno;
172172
uint8_t funcno;
173+
173174
bdf() : domainno(0), busno(0), devno(0), funcno(0) {}
174175
bdf(uint32_t domain, uint8_t bus, uint8_t device, uint8_t function) :
175176
domainno(domain), busno(bus), devno(device), funcno(function) {}
176177
bdf(uint8_t bus, uint8_t device, uint8_t function) :
177178
domainno(0), busno(bus), devno(device), funcno(function) {}
179+
180+
std::string to_string() const
181+
{
182+
std::ostringstream oss;
183+
oss << std::hex << std::uppercase << std::setfill('0')
184+
<< std::setw(DOMAIN_WIDTH) << domainno << ":"
185+
<< std::setw(BUS_WIDTH) << static_cast<int>(busno) << ":"
186+
<< std::setw(DEVICE_WIDTH) << static_cast<int>(devno) << "."
187+
<< std::setw(FUNCTION_WIDTH) << static_cast<int>(funcno);
188+
return oss.str();
189+
}
190+
191+
private:
192+
static const int DOMAIN_WIDTH = 4;
193+
static const int BUS_WIDTH = 2;
194+
static const int DEVICE_WIDTH = 2;
195+
static const int FUNCTION_WIDTH = 1;
178196
};
179197

180198
struct pci {

tests/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# SPDX-License-Identifier: BSD-3-Clause
22
# Copyright (c) 2022-2024, Intel Corporation
33

4-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/tests)
4+
set(CMAKE_TEST_BINARY_DIR ${CMAKE_BINARY_DIR}/bin/tests)
5+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_TEST_BINARY_DIR})
56

67
if(UNIX)
78

@@ -16,6 +17,7 @@ if(UNIX)
1617
target_link_libraries(urltest Threads::Threads PCM_STATIC)
1718
endif(LINUX)
1819

20+
add_subdirectory(utests)
1921
endif(UNIX)
2022

2123
if(PCM_FUZZ)
@@ -33,4 +35,4 @@ if(PCM_FUZZ)
3335
target_link_libraries(pcm-sensor-server-ssl-fuzz Threads::Threads PCM_STATIC ${SSL_LIBS})
3436
target_link_libraries(pcm-fuzz Threads::Threads PCM_STATIC)
3537
target_link_libraries(pcm-memory-fuzz Threads::Threads PCM_STATIC)
36-
endif()
38+
endif()

tests/test.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,5 +408,25 @@ if [ "$?" -ne "0" ]; then
408408
fi
409409
online_offline_cores 1
410410

411+
# Below is UT part
412+
413+
echo "Running Unit Tests"
414+
failed=()
415+
for test_binary in ./tests/utests/*; do
416+
if [ -x "$test_binary" ]; then
417+
echo "Running $test_binary"
418+
"$test_binary"
419+
if [ "$?" -ne "0" ]; then
420+
failed+=("$test_binary")
421+
fi
422+
else
423+
echo "Skipping $test_binary (not executable)"
424+
fi
425+
done
426+
427+
if [ "${#failed[@]}" -ne "0" ]; then
428+
echo "Failed test programs: ${failed[@]}"
429+
exit 1
430+
fi
411431

412432
popd

tests/utests/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2009-2025, Intel Corporation
3+
4+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_TEST_BINARY_DIR}/utests)
5+
6+
project(pcm_utests LANGUAGES CXX)
7+
8+
enable_testing()
9+
10+
include_directories(${CMAKE_SOURCE_DIR}/src/)
11+
include_directories("${GMOCK_DIR}/include")
12+
13+
file(GLOB LSPCI_TEST_FILES lspci-utest.cpp ${CMAKE_SOURCE_DIR}/src/lspci.cpp)
14+
set(LIBS Threads::Threads PCM_STATIC)
15+
add_executable(lspci-utest ${LSPCI_TEST_FILES})
16+
17+
target_link_libraries(
18+
lspci-utest
19+
GTest::gtest_main
20+
GTest::gmock_main
21+
${LIBS}
22+
)
23+
24+
include(GoogleTest)
25+
gtest_discover_tests(lspci-utest)

tests/utests/lspci-utest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
// Copyright (c) 2009-2025, Intel Corporation
3+
// written by Alexander Antonov
4+
5+
#include "lspci.h"
6+
#include <gtest/gtest.h>
7+
#include <string>
8+
9+
using namespace pcm;
10+
11+
TEST(BDFTest, ToStringDefaultConstructor)
12+
{
13+
struct bdf default_bdf;
14+
EXPECT_EQ(default_bdf.to_string(), "0000:00:00.0");
15+
}
16+
17+
TEST(BDFTest, ToStringCustomConstructor)
18+
{
19+
struct bdf custom_bdf(0x1234, 0x56, 0x10, 0x7);
20+
EXPECT_EQ(custom_bdf.to_string(), "1234:56:10.7");
21+
}
22+
23+
TEST(BDFTest, ToStringPartialConstructor)
24+
{
25+
struct bdf partial_bdf(0x56, 0x10, 0x7);
26+
EXPECT_EQ(partial_bdf.to_string(), "0000:56:10.7");
27+
}

0 commit comments

Comments
 (0)