From 9a32031816b9e566a8d9973bef89df32b6d96100 Mon Sep 17 00:00:00 2001 From: Chris Dalke Date: Fri, 8 Nov 2024 12:55:47 -0500 Subject: [PATCH 1/8] Add cmake build --- .github/workflows/ccpp.yml | 58 ++++++++++++++++ .gitignore | 7 +- CMakeLists.txt | 66 +++++++++++++++++++ .../rigtorp/TokenBucket.h | 28 +++++++- src/TokenBucketTest.cpp | 50 ++++++++++++++ test.cpp | 28 -------- 6 files changed, 206 insertions(+), 31 deletions(-) create mode 100644 .github/workflows/ccpp.yml create mode 100644 CMakeLists.txt rename TokenBucket.h => include/rigtorp/TokenBucket.h (51%) create mode 100644 src/TokenBucketTest.cpp delete mode 100644 test.cpp diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml new file mode 100644 index 0000000..32cf3af --- /dev/null +++ b/.github/workflows/ccpp.yml @@ -0,0 +1,58 @@ +name: C/C++ CI + +on: [push] + +jobs: + build-ubuntu: + + runs-on: ubuntu-latest + strategy: + matrix: + config: [Debug, Release] + standard: [11, 17] + + steps: + - uses: actions/checkout@v1 + - name: Build & Test + run: | + cmake -E remove_directory build + cmake -B build -S . -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DCMAKE_CXX_FLAGS="-Werror -fsanitize=address,undefined" + cmake --build build + cd build + ctest --output-on-failure + + build-windows: + + runs-on: windows-latest + strategy: + matrix: + config: [Debug, Release] + standard: [11, 17] + + steps: + - uses: actions/checkout@v1 + - name: Build & Test + run: | + cmake -E remove_directory build + cmake -B build -S . -DCMAKE_CXX_STANDARD=${{ matrix.standard }} # -DCMAKE_CXX_FLAGS="/WX" + cmake --build build --config ${{ matrix.config }} + cd build + ctest --output-on-failure + + build-macos: + + runs-on: macOS-latest + strategy: + matrix: + config: [Debug, Release] + standard: [11, 17] + + steps: + - uses: actions/checkout@v1 + - name: Build & Test + run: | + cmake -E remove_directory build + cmake -B build -S . -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DCMAKE_CXX_FLAGS="-Werror -fsanitize=address,undefined" + cmake --build build + cd build + ctest --output-on-failure \ No newline at end of file diff --git a/.gitignore b/.gitignore index f6b4931..1ad1abb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ -build +build/ +cmake-build-* +.vs/ +.vscode/ +.idea/ +.project *~ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..998cd4f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,66 @@ +cmake_minimum_required(VERSION 3.5) + +project(TokenBucket VERSION 1.0 LANGUAGES CXX) + +add_library(${PROJECT_NAME} INTERFACE) +add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) + +target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_11) + +target_include_directories(${PROJECT_NAME} INTERFACE + $ + $) + +# Tests and examples +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + if (MSVC) + add_compile_options(/permissive- /W4 /wd4172 /wd4324 /wd4530) + else() + add_compile_options(-Wall -Wextra -Wpedantic) + endif() + + add_executable(TokenBucketTest src/TokenBucketTest.cpp) + target_link_libraries(TokenBucketTest TokenBucket) + + enable_testing() + add_test(TokenBucketTest TokenBucketTest) +endif() + +# Install +include(GNUInstallDirs) +include(CMakePackageConfigHelpers) + +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + COMPATIBILITY SameMajorVersion +) + +export( + TARGETS ${PROJECT_NAME} + NAMESPACE ${PROJECT_NAME}:: + FILE "${PROJECT_NAME}Config.cmake" +) + +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + install( + DIRECTORY "include/" + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) + + install( + TARGETS ${PROJECT_NAME} + EXPORT "${PROJECT_NAME}Config" + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) + + install( + EXPORT "${PROJECT_NAME}Config" + NAMESPACE ${PROJECT_NAME}:: + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" + ) + + install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" + ) +endif() \ No newline at end of file diff --git a/TokenBucket.h b/include/rigtorp/TokenBucket.h similarity index 51% rename from TokenBucket.h rename to include/rigtorp/TokenBucket.h index 0594733..758b31d 100644 --- a/TokenBucket.h +++ b/include/rigtorp/TokenBucket.h @@ -1,11 +1,33 @@ -// © 2023 Erik Rigtorp -// SPDX-License-Identifier: MIT +/* +Copyright (c) 2023 Erik Rigtorp +SPDX-License-Identifier: MIT + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ #pragma once #include #include +namespace rigtorp { + template class TokenBucket { public: TokenBucket() = default; @@ -44,3 +66,5 @@ template class TokenBucket { std::chrono::nanoseconds timePerToken_; std::chrono::nanoseconds timePerBurst_; }; + +}; \ No newline at end of file diff --git a/src/TokenBucketTest.cpp b/src/TokenBucketTest.cpp new file mode 100644 index 0000000..45fdfca --- /dev/null +++ b/src/TokenBucketTest.cpp @@ -0,0 +1,50 @@ +/* +Copyright (c) 2023 Erik Rigtorp +SPDX-License-Identifier: MIT + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include +#include +#include + +int main(int arv, char *argv[]) { + + using namespace rigtorp; + + { + TokenBucket tb(350, 1050); + + assert(tb.consume(1050)); + assert(!tb.consume(1)); + sleep(1); + assert(tb.consume(350)); + assert(!tb.consume(1)); + } + + { + TokenBucket tb(350, 1050); + + assert(!tb.consume(1051)); + } + + return 0; +} \ No newline at end of file diff --git a/test.cpp b/test.cpp deleted file mode 100644 index ff2c063..0000000 --- a/test.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// © 2023 Erik Rigtorp -// SPDX-License-Identifier: MIT - -#include "TokenBucket.h" -#include -#include -#include - -int main(int arv, char *argv[]) { - - { - TokenBucket tb(350, 1050); - - assert(tb.consume(1050)); - assert(!tb.consume(1)); - sleep(1); - assert(tb.consume(350)); - assert(!tb.consume(1)); - } - - { - TokenBucket tb(350, 1050); - - assert(!tb.consume(1051)); - } - - return 0; -} \ No newline at end of file From 334a7881353b47f48f936bf50ab565e6a298ef17 Mon Sep 17 00:00:00 2001 From: Chris Dalke Date: Fri, 8 Nov 2024 13:05:15 -0500 Subject: [PATCH 2/8] Fix warning error --- src/TokenBucketTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TokenBucketTest.cpp b/src/TokenBucketTest.cpp index 45fdfca..5ec5ef7 100644 --- a/src/TokenBucketTest.cpp +++ b/src/TokenBucketTest.cpp @@ -26,7 +26,7 @@ SOFTWARE. #include #include -int main(int arv, char *argv[]) { +int main() { using namespace rigtorp; From af1b70cb8aae269e5e2309d86e709149d38ba1de Mon Sep 17 00:00:00 2001 From: Chris Dalke Date: Fri, 8 Nov 2024 13:07:50 -0500 Subject: [PATCH 3/8] Fix template type --- src/TokenBucketTest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TokenBucketTest.cpp b/src/TokenBucketTest.cpp index 5ec5ef7..509cb37 100644 --- a/src/TokenBucketTest.cpp +++ b/src/TokenBucketTest.cpp @@ -31,7 +31,7 @@ int main() { using namespace rigtorp; { - TokenBucket tb(350, 1050); + TokenBucket<> tb(350, 1050); assert(tb.consume(1050)); assert(!tb.consume(1)); @@ -41,7 +41,7 @@ int main() { } { - TokenBucket tb(350, 1050); + TokenBucket<> tb(350, 1050); assert(!tb.consume(1051)); } From 95fab5c27d57487660909af88db2de55c9f47988 Mon Sep 17 00:00:00 2001 From: Chris Dalke Date: Fri, 8 Nov 2024 13:14:45 -0500 Subject: [PATCH 4/8] Use windows sleep for win test --- src/TokenBucketTest.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/TokenBucketTest.cpp b/src/TokenBucketTest.cpp index 509cb37..8a16954 100644 --- a/src/TokenBucketTest.cpp +++ b/src/TokenBucketTest.cpp @@ -24,7 +24,13 @@ SOFTWARE. #include #include #include -#include + +#ifdef __unix__ +# include +#elif defined _WIN32 +# include +#define sleep(x) Sleep(1000 * (x)) +#endif int main() { From 24f2ebddda1299ecfd319bbb9b42d7ea2a7c4ebb Mon Sep 17 00:00:00 2001 From: Chris Dalke Date: Fri, 8 Nov 2024 13:16:38 -0500 Subject: [PATCH 5/8] Flip conditional to account for mac --- src/TokenBucketTest.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/TokenBucketTest.cpp b/src/TokenBucketTest.cpp index 8a16954..d3e15ac 100644 --- a/src/TokenBucketTest.cpp +++ b/src/TokenBucketTest.cpp @@ -25,11 +25,11 @@ SOFTWARE. #include #include -#ifdef __unix__ -# include -#elif defined _WIN32 +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) # include #define sleep(x) Sleep(1000 * (x)) +#else +# include #endif int main() { From 1930818f380161accb8da4e1399ab0939bcbda50 Mon Sep 17 00:00:00 2001 From: Chris Dalke Date: Fri, 8 Nov 2024 13:22:46 -0500 Subject: [PATCH 6/8] Use stdlib for sleep --- src/TokenBucketTest.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/TokenBucketTest.cpp b/src/TokenBucketTest.cpp index d3e15ac..35dbdc8 100644 --- a/src/TokenBucketTest.cpp +++ b/src/TokenBucketTest.cpp @@ -24,13 +24,8 @@ SOFTWARE. #include #include #include - -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) -# include -#define sleep(x) Sleep(1000 * (x)) -#else -# include -#endif +#include +#include int main() { @@ -41,7 +36,9 @@ int main() { assert(tb.consume(1050)); assert(!tb.consume(1)); - sleep(1); + + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + assert(tb.consume(350)); assert(!tb.consume(1)); } From 20dc72400ce052a0a714cf46636ec8b368ed2b88 Mon Sep 17 00:00:00 2001 From: Chris Dalke Date: Fri, 8 Nov 2024 13:25:24 -0500 Subject: [PATCH 7/8] Disable debug mode for mac --- .github/workflows/ccpp.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index 32cf3af..ab9bbc6 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -44,7 +44,7 @@ jobs: runs-on: macOS-latest strategy: matrix: - config: [Debug, Release] + config: [Release] standard: [11, 17] steps: From d556fda81013fe2fde0f2dfe7cc6b2d5cae54c6b Mon Sep 17 00:00:00 2001 From: Chris Dalke Date: Fri, 8 Nov 2024 13:37:50 -0500 Subject: [PATCH 8/8] Disable debug builds on windows --- .github/workflows/ccpp.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index ab9bbc6..efd986a 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -26,7 +26,7 @@ jobs: runs-on: windows-latest strategy: matrix: - config: [Debug, Release] + config: [Release] standard: [11, 17] steps: