Skip to content

Commit 4b57843

Browse files
Alexander Antonovrdementi
authored andcommitted
Add simle googletest for bdf function
1 parent 38f757d commit 4b57843

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
184184
add_subdirectory(src)
185185
add_subdirectory(examples)
186186
add_subdirectory(tests)
187+
add_subdirectory(utests)
188+
add_subdirectory(googletest)
187189

188190
message(STATUS "Install directory: ${CMAKE_INSTALL_PREFIX}")
189191

src/lspci.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,28 @@ 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+
std::ostringstream oss;
182+
oss << std::hex << std::uppercase << std::setfill('0')
183+
<< std::setw(DOMAIN_WIDTH) << domainno << ":"
184+
<< std::setw(BUS_WIDTH) << static_cast<int>(busno) << ":"
185+
<< std::setw(DEVICE_WIDTH) << static_cast<int>(devno) << "."
186+
<< std::setw(FUNCTION_WIDTH) << static_cast<int>(funcno);
187+
return oss.str();
188+
}
189+
190+
private:
191+
static const int DOMAIN_WIDTH = 4;
192+
static const int BUS_WIDTH = 2;
193+
static const int DEVICE_WIDTH = 2;
194+
static const int FUNCTION_WIDTH = 1;
178195
};
179196

180197
struct pci {

utests/CMakeLists.txt

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) 2022-2025, Intel Corporation
3+
4+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/utests)
5+
6+
if(UNIX)
7+
project(pcm_utests LANGUAGES CXX)
8+
9+
enable_testing()
10+
11+
include_directories(../src/)
12+
include_directories("${GMOCK_DIR}/include")
13+
14+
file(GLOB LSPCI_TEST_FILES lspci-utest.cpp ../src/lspci.cpp)
15+
set(LIBS PCM_STATIC)
16+
add_executable(lspci-utest ${LSPCI_TEST_FILES})
17+
18+
target_link_libraries(
19+
lspci-utest
20+
GTest::gtest_main
21+
GTest::gmock_main
22+
${LIBS}
23+
)
24+
25+
include(GoogleTest)
26+
gtest_discover_tests(lspci-utest)
27+
endif(UNIX)

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)