Skip to content

Commit 73100da

Browse files
not-matthiasart049
authored andcommitted
feat(google_benchmark): add tests
1 parent c427e50 commit 73100da

File tree

5 files changed

+117
-12
lines changed

5 files changed

+117
-12
lines changed

.github/workflows/ci.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,39 @@ on:
77
workflow_dispatch:
88

99
jobs:
10+
tests:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v3
15+
16+
- name: Cache build
17+
uses: actions/cache@v3
18+
with:
19+
path: core/build-tests
20+
key: ${{ runner.os }}-build-tests-${{ hashFiles('**/CMakeLists.txt', '**/examples/google_benchmark/**') }}
21+
22+
- name: Create build directory
23+
run: mkdir -p core/build-tests
24+
25+
- name: Build tests
26+
run: |
27+
cd core/build-tests
28+
cmake .. -DENABLE_TESTS=ON
29+
make -j
30+
31+
- name: Run tests
32+
run: |
33+
cd core/build-tests
34+
GTEST_OUTPUT=json:test-results/ ctest
35+
36+
- name: Upload test results
37+
uses: actions/upload-artifact@v4
38+
if: failure()
39+
with:
40+
name: test_results
41+
path: ${{runner.workspace}}/core/build-tests/test/test-results/**/*.json
42+
1043
instrumentation:
1144
runs-on: ubuntu-latest
1245

core/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ target_include_directories(
2828
codspeed
2929
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
3030
)
31+
32+
option(ENABLE_TESTS "Enable building the unit tests which depend on gtest" OFF)
33+
if(ENABLE_TESTS)
34+
enable_testing()
35+
add_subdirectory(test)
36+
endif()

core/src/uri.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,46 @@
22
#include <string>
33
#include <iostream>
44

5-
std::string extract_lambda_namespace(const std::string& pretty_func) {
6-
if (pretty_func.find("(anonymous namespace)") != std::string::npos) {
7-
std::cerr << "[ERROR] Anonymous namespace not supported in " << pretty_func << std::endl;
8-
return {};
9-
}
10-
11-
#ifdef __clang__
12-
// Example: auto outer::test12::(anonymous class)::operator()() const
13-
14-
std::size_t anon_class_pos = pretty_func.find("(anonymous class)");
5+
// Example: auto outer::test12::(anonymous class)::operator()() const
6+
// Returns: outer::test12::
7+
std::string extract_namespace_clang(const std::string& pretty_func) {
8+
std::size_t anon_class_pos = pretty_func.find("::(anonymous class)");
159
std::size_t space_pos = pretty_func.find(' ');
1610

1711
if (space_pos == std::string::npos || anon_class_pos == std::string::npos) {
1812
return {};
1913
}
14+
space_pos += 1; // Skip the space
2015

2116
return pretty_func.substr(space_pos, anon_class_pos - space_pos) + "::";
22-
#elif __GNUC__
23-
// Example: outer::test12::<lambda()>
17+
}
2418

19+
// Example: outer::test12::<lambda()>
20+
// Returns: outer::test12::
21+
std::string extract_namespace_gcc(const std::string& pretty_func) {
2522
auto lambda_pos = pretty_func.find("::<lambda()>");
2623
if (lambda_pos == std::string::npos) {
2724
return {};
2825
}
2926

3027
return pretty_func.substr(0, lambda_pos) + "::";
28+
}
29+
30+
// Has to pass the pretty function from a lambda:
31+
// (([]() { return __PRETTY_FUNCTION__; })())
32+
//
33+
// Returns: An empty string if the namespace could not be extracted,
34+
// otherwise the namespace with a trailing "::"
35+
std::string extract_lambda_namespace(const std::string& pretty_func) {
36+
if (pretty_func.find("(anonymous namespace)") != std::string::npos) {
37+
std::cerr << "[ERROR] Anonymous namespace not supported in " << pretty_func << std::endl;
38+
return {};
39+
}
40+
41+
#ifdef __clang__
42+
return extract_namespace_clang(pretty_func);
43+
#elif __GNUC__
44+
return extract_namespace_gcc(pretty_func);
3145
#else
3246
#error "Unsupported compiler"
3347
#endif

core/test/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
include(FetchContent)
2+
FetchContent_Declare(
3+
googletest
4+
GIT_REPOSITORY https://github.com/google/googletest.git
5+
GIT_TAG v1.16.0
6+
)
7+
FetchContent_MakeAvailable(googletest)
8+
9+
add_executable(unit_tests
10+
uri.cpp
11+
)
12+
13+
target_link_libraries(unit_tests
14+
PRIVATE
15+
codspeed
16+
GTest::gtest
17+
GTest::gtest_main
18+
)
19+
20+
include(GoogleTest)
21+
gtest_discover_tests(unit_tests)
22+

core/test/uri.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#include <gtest/gtest.h>
3+
#include "codspeed.h"
4+
5+
// Manual definition (to avoid including it in the public header):
6+
std::string extract_namespace_clang(const std::string& func_str);
7+
std::string extract_namespace_gcc(const std::string& func_str);
8+
9+
TEST(UriTest, TestExtractNamespaceClang) {
10+
EXPECT_EQ(extract_namespace_clang("auto outer::test12::(anonymous class)::operator()() const"), "outer::test12::");
11+
EXPECT_EQ(extract_namespace_clang("auto outer::(anonymous namespace)::test12::(anonymous class)::operator()() const"), "outer::(anonymous namespace)::test12::");
12+
}
13+
14+
TEST(UriTest, TestExtractNamespaceGcc) {
15+
EXPECT_EQ(extract_namespace_gcc("outer::test12::<lambda()>"), "outer::test12::");
16+
EXPECT_EQ(extract_namespace_gcc("outer::(anonymous namespace)::test12::<lambda()>"), "outer::(anonymous namespace)::test12::");
17+
}
18+
19+
20+
namespace a {
21+
namespace b {
22+
namespace c {
23+
static std::string pretty_func = ([]() { return __PRETTY_FUNCTION__; })();
24+
25+
TEST(UriTest, TestExtractNamespace) {
26+
EXPECT_EQ(extract_lambda_namespace(pretty_func), "a::b::c::");
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)