From bed7360063629c9949cc188c432c566b8a972ce4 Mon Sep 17 00:00:00 2001 From: Tobias Schmidl Date: Mon, 16 Jun 2025 21:22:22 +0200 Subject: [PATCH] feat: first working set --- include/glimpses/glimpses.hpp | 35 ++++++++++++++++++++++++++--------- source/glimpses.cpp | 20 +++++++++++++++++++- test/CMakeLists.txt | 13 ++++++------- test/source/glimpses.cpp | 19 ++++++++----------- test/source/main.cpp | 14 -------------- 5 files changed, 59 insertions(+), 42 deletions(-) delete mode 100644 test/source/main.cpp diff --git a/include/glimpses/glimpses.hpp b/include/glimpses/glimpses.hpp index 7d8fc02..eeed973 100644 --- a/include/glimpses/glimpses.hpp +++ b/include/glimpses/glimpses.hpp @@ -4,22 +4,39 @@ #pragma once +#include +#include #include -namespace glimpses { +#include "glimpses/version.h" -/** - * @brief A class for saying hello in multiple languages - */ -class Glimpses +namespace glimpses { +/** @brief A class for saying retrieving system information */ +class Glimpses +{ public: - /** - * @brief Creates a new glimpses - * @param name the name to greet - */ + /** @brief Type for representing the number of CPU cores. */ + using CPUCount = uint32_t; + + /** @brief Type for representing a set of CPU cores, mapped by their names. */ + using CPUSet = std::map; + + /** @brief Maximum number of CPU cores supported by Glimpses. */ + constexpr static CPUCount MAX_CPU_COUNT = 1024; + + /** @brief Version of the Glimpses library. */ + constexpr static const char *VERSION = GLIMPSES_VERSION; + + /** @brief Creates a new glimpses */ Glimpses(); + + /** + * @brief Returns the number of CPU cores available on the system. + * @return The number of CPU cores available on the system. + */ + CPUSet getCPUCount() const; }; } // namespace glimpses diff --git a/source/glimpses.cpp b/source/glimpses.cpp index 80aed49..b7071c0 100644 --- a/source/glimpses.cpp +++ b/source/glimpses.cpp @@ -3,7 +3,25 @@ // SPDX-License-Identifier: AGPL-3.0-or-later #include +#include +#include +#include using namespace glimpses; -Glimpses::Glimpses() {} +Glimpses::Glimpses() +{ +} + +Glimpses::CPUSet Glimpses::getCPUCount() const +{ + cpu_set_t cpuset; + auto minmax = [](int64_t min, int64_t value, int64_t max) -> CPUCount { + return std::min(std::max(value, min), max); + }; + sched_getaffinity(0, sizeof(cpuset), &cpuset); + return CPUSet{{"configured", minmax(0, sysconf(_SC_NPROCESSORS_CONF), MAX_CPU_COUNT)}, + {"logical", minmax(0, std::thread::hardware_concurrency(), MAX_CPU_COUNT)}, + {"online", minmax(0, sysconf(_SC_NPROCESSORS_ONLN), MAX_CPU_COUNT)}, + {"allowed", minmax(0, CPU_COUNT(&cpuset), MAX_CPU_COUNT)}}; +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0bf5ae3..29beb72 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -18,8 +18,8 @@ include(../cmake/tools.cmake) include(../cmake/get_cpm.cmake) -# CPMAddPackage("gh:doctest/doctest@2.4.9") -CPMAddPackage("gh:TheLartians/Format.cmake@1.7.3") +CPMAddPackage(NAME Catch2 GITHUB_REPOSITORY catchorg/Catch2 VERSION 3.8.1) +CPMAddPackage("gh:TheLartians/Format.cmake@1.8.3") if(TEST_INSTALLED_VERSION) find_package(Glimpses REQUIRED) @@ -31,11 +31,7 @@ endif() file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp) add_executable(${PROJECT_NAME} ${sources}) -target_link_libraries( - ${PROJECT_NAME} - # doctest::doctest - Glimpses::Glimpses -) +target_link_libraries(${PROJECT_NAME} Catch2::Catch2WithMain Glimpses::Glimpses) set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17) # enable compiler warnings @@ -52,6 +48,9 @@ endif() enable_testing() +include(CTest) +add_test(GlimpsesTests GlimpsesTests) + # Note: doctest and similar testing frameworks can automatically configure CMake tests. For other testing frameworks add the tests target # instead: add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME}) diff --git a/test/source/glimpses.cpp b/test/source/glimpses.cpp index 82e9090..29caf68 100644 --- a/test/source/glimpses.cpp +++ b/test/source/glimpses.cpp @@ -2,24 +2,21 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later +#include #include #include - #include -/* -TEST_CASE("Glimpses") { - using namespace greeter; - Glimpses greeter("Tests"); +TEST_CASE("CPU count") +{ - CHECK(greeter.greet(LanguageCode::EN) == "Hello, Tests!"); - CHECK(greeter.greet(LanguageCode::DE) == "Hallo Tests!"); - CHECK(greeter.greet(LanguageCode::ES) == "¡Hola Tests!"); - CHECK(greeter.greet(LanguageCode::FR) == "Bonjour Tests!"); + glimpses::Glimpses glimpses{}; + + CHECK(glimpses.getCPUCount().size() > 0); } -TEST_CASE("Glimpses version") { - static_assert(std::string_view(GREETER_VERSION) == std::string_view("1.0")); +/* +TEST_CASE("Glimpses version") { static_assert(std::string_view(GREETER_VERSION) == std::string_view("0.1.0")); CHECK(std::string(GREETER_VERSION) == std::string("1.0")); } */ diff --git a/test/source/main.cpp b/test/source/main.cpp deleted file mode 100644 index ef34b77..0000000 --- a/test/source/main.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Tobias Schmidl -// -// SPDX-License-Identifier: AGPL-3.0-or-later -#if 0 -#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN - -#include -#endif - -int -main() -{ - return 0; -}