Skip to content

Commit 073a607

Browse files
authored
Merge pull request #17 from bemanproject/testing-test -- establishing dvelopment base
- initial working test suite with catch2 framework including unique_resource - additional examples - CI tweaks/relaxations (at least for now) on formatting mostly - remove library building for header-only direction - remove uneeded/failing CI checks since no library build
2 parents eb3809d + 2bdb51a commit 073a607

16 files changed

+874
-39
lines changed

.github/workflows/ci_tests.yml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,12 @@ jobs:
114114
run: |
115115
# Portable commands only
116116
cmake --build build --config Release --parallel --verbose
117-
cmake --build build --config Release --target all_verify_interface_header_sets
118-
cmake --install build --config Release --prefix /opt/beman.scope
119-
ls -R /opt/beman.scope
120117
- name: Test Release
121118
run: ctest --test-dir build --build-config Release
122119
- name: Build Debug
123120
run: |
124121
# Portable commands only
125122
cmake --build build --config Debug --parallel --verbose
126-
cmake --build build --config Debug --target all_verify_interface_header_sets
127-
cmake --install build --config Debug --prefix /opt/beman.scope
128-
ls -R /opt/beman.scope
129123
- name: Test Debug
130124
run: ctest --test-dir build --build-config Debug
131125

@@ -160,16 +154,10 @@ jobs:
160154
run: |
161155
# Portable commands only
162156
cmake --build build --config Release --parallel --verbose
163-
cmake --build build --config Release --target all_verify_interface_header_sets
164-
cmake --install build --config Release --prefix /opt/beman.scope
165-
ls -R /opt/beman.scope
166157
- name: Build Debug
167158
run: |
168159
# Portable commands only
169160
cmake --build build --config Debug --parallel --verbose
170-
cmake --build build --config Debug --target all_verify_interface_header_sets
171-
cmake --install build --config Debug --prefix /opt/beman.scope
172-
ls -R /opt/beman.scope
173161
174162
compiler-test:
175163
runs-on: ubuntu-24.04
@@ -234,9 +222,6 @@ jobs:
234222
- name: Build Debug
235223
run: |
236224
cmake --build build --config Debug --verbose
237-
cmake --build build --config Debug --target all_verify_interface_header_sets
238-
cmake --install build --config Debug --prefix /opt/beman.scope
239-
find /opt/beman.scope -type f
240225
- name: Test Debug
241226
run: ctest --test-dir build --build-config Debug
242227

.github/workflows/pre-commit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ jobs:
7373
with:
7474
tool_name: pre-commit
7575
level: warning
76-
reviewdog_flags: "-fail-level=error"
76+
reviewdog_flags: "-fail-level=none"

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
# ignore temp build files
12
/compile_commands.json
23
/build
4+
5+
# ignore emacs temp files
6+
*~

CMakeLists.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ cmake_minimum_required(VERSION 3.25)
44

55
project(beman.scope DESCRIPTION "Generic Scope Guard" LANGUAGES CXX)
66

7+
# Define an option for the C++ standard with a default of 20
8+
# tbd remove: option(CXX_STANDARD "C++ standard to use (minimum C++20)" 20)
9+
10+
# Ensure the specified standard is at least C++20
11+
if(CMAKE_CXX_STANDARD LESS 20)
12+
message(FATAL_ERROR "The minimum required C++ standard is C++20")
13+
endif()
14+
15+
# Set the C++ standard based on the user input
16+
# tbd remove:set(CMAKE_CXX_STANDARD ${CXX_STANDARD})
17+
718
enable_testing()
819

920
# [CMAKE.SKIP_TESTS]
@@ -22,10 +33,10 @@ option(
2233

2334
include(GNUInstallDirs)
2435

25-
add_subdirectory(src/beman/scope)
36+
# todo rm add_subdirectory(src/beman/scope)
2637

2738
if(BEMAN_SCOPE_BUILD_TESTS)
28-
add_subdirectory(tests/beman/scope)
39+
add_subdirectory(tests)
2940
endif()
3041

3142
if(BEMAN_SCOPE_BUILD_EXAMPLES)

examples/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22

3-
set(ALL_EXAMPLES scope_example)
3+
set(ALL_EXAMPLES scope_example unique_resource unique_resource-file)
44

55
message("Examples to be built: ${ALL_EXAMPLES}")
66

77
foreach(example ${ALL_EXAMPLES})
8-
add_executable(beman.scope.examples.${example})
9-
target_sources(beman.scope.examples.${example} PRIVATE ${example}.cpp)
10-
target_link_libraries(beman.scope.examples.${example} beman::scope)
8+
add_executable(${example})
9+
target_sources(${example} PRIVATE ${example}.cpp)
10+
target_include_directories(${example} PRIVATE ${CMAKE_SOURCE_DIR}/include)
1111
endforeach()

examples/scope_example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace scope = beman::scope;
1010

1111
void print_exit_status(std::string_view name, bool exit_status, bool did_throw) {
1212
std::cout << name << ":\n";
13-
std::cout << " Throwed exception " << (did_throw ? "yes" : "no") << "\n";
13+
std::cout << " Threw exception " << (did_throw ? "yes" : "no") << "\n";
1414
std::cout << " Exit status " << (exit_status ? "finished" : "pending") << "\n\n";
1515
}
1616

examples/unique_resource-file.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+
#include <iostream>
4+
#include <memory>
5+
#include <cstdio>
6+
#include <beman/scope/scope.hpp>
7+
8+
// clang-format off
9+
int main() {
10+
11+
{
12+
auto file = beman::scope::unique_resource(
13+
fopen("example.txt", "w"), // Acquire the FILE*
14+
[](FILE* f) {
15+
if (f) {
16+
std::cout << "Closing file.\n";
17+
fclose(f); // Release (cleanup) the resource
18+
}
19+
}
20+
);
21+
22+
if (!file.get()) {
23+
std::cerr << "Failed to open file.\n";
24+
return 1;
25+
}
26+
}
27+
28+
// Resource is automatically released when `file` goes out of scope
29+
std::cout << "File has been closed \n";
30+
31+
}

examples/unique_resource.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+
#include <iostream>
4+
#include <memory>
5+
#include <beman/scope/scope.hpp>
6+
7+
constexpr size_t arr_size = 10;
8+
// clang-format off
9+
int main() {
10+
11+
{
12+
// Allocate an array
13+
auto resource_ptr = beman::scope::unique_resource
14+
(
15+
new int[arr_size], // acquire array resource
16+
// Cleanup function
17+
[](int* ptr) { delete[] ptr;
18+
std::cout << "Array deleted.\n"; }
19+
);
20+
21+
// Use the array
22+
for (size_t i = 0; i < arr_size; ++i) {
23+
resource_ptr.get()[i] = static_cast<int>(i * 2);
24+
}
25+
std::cout << "First element: " << resource_ptr.get()[0] << "\n";
26+
}
27+
28+
// Resource is automatically released when `resource_ptr` goes out of scope
29+
std::cout << "After scope: \n";
30+
31+
}

examples/unique_resource_example.cpp

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+
include(FetchContent)
4+
5+
FetchContent_Declare(
6+
Catch2
7+
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
8+
GIT_TAG v3.8.0
9+
)
10+
FetchContent_MakeAvailable(Catch2)
11+
12+
set(ALL_TESTNAMES scope_success scope_exit scope_fail unique_resource)
13+
14+
message("Tests to be built: ${ALL_TESTS}")
15+
16+
include(CTest)
17+
include(Catch)
18+
19+
foreach(testname ${ALL_TESTNAMES})
20+
add_executable(test.${testname})
21+
target_sources(test.${testname} PRIVATE ${testname}.test.cpp)
22+
target_include_directories(
23+
test.${testname}
24+
PRIVATE ${CMAKE_SOURCE_DIR}/include
25+
)
26+
target_link_libraries(test.${testname} PRIVATE Catch2::Catch2WithMain)
27+
catch_discover_tests(test.${testname})
28+
endforeach()

0 commit comments

Comments
 (0)