From 4fc81c4c049aea4f6075b2e12ae01b9df6520a1e Mon Sep 17 00:00:00 2001 From: chatlanin Date: Fri, 3 Jan 2025 11:53:18 +0300 Subject: [PATCH] add logger and some tests --- bin/main.cpp | 19 +++---- src/hack/{log/log.hpp => logger/logger.hpp} | 8 +++ src/hack/patterns/ring_buffer.hpp | 62 +++++++++++---------- src/meson.build | 2 +- tests/meson.build | 2 +- tests/mt/max.cpp | 13 ----- tests/mt/mt.cpp | 31 +++++++++++ tests/patterns/ring_buffer.cpp | 1 + 8 files changed, 83 insertions(+), 55 deletions(-) rename src/hack/{log/log.hpp => logger/logger.hpp} (96%) delete mode 100644 tests/mt/max.cpp create mode 100644 tests/mt/mt.cpp diff --git a/bin/main.cpp b/bin/main.cpp index 6dfd177..699b32f 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -1,7 +1,7 @@ -#include #include #include -#include + +#include "hack/logger/logger.hpp" #include "hack/mt/algorithms/sort.hpp" #include "hack/mt/algorithms/max.hpp" @@ -14,7 +14,7 @@ auto main(int argc, char *argv[]) -> int { hack::patterns::ring_buffer rb; for (int i = 1; i < 12; ++i) rb.put(i); - while(!rb.empty()) std::cout << rb.get().value() << std::endl; + hack::log()(rb); } // mt::sort @@ -25,20 +25,15 @@ auto main(int argc, char *argv[]) -> int hack::mt::algorithms::sort(v); hack::mt::algorithms::sort(l); - for (auto d : v) - std::cout << d << " "; - std::cout << std::endl; - - for (auto d : l) - std::cout << d << " "; - std::cout << std::endl; + hack::log()(v); + hack::log()(l); } // mt::max { int a = 4, b = 5; int& c = a; - std::cout << hack::mt::algorithms::max(4, 5) << std::endl; - std::cout << hack::mt::algorithms::max(c, b) << std::endl; + hack::log()(hack::mt::algorithms::max(4, 5)); + hack::log()(hack::mt::algorithms::max(c, b)); } } diff --git a/src/hack/log/log.hpp b/src/hack/logger/logger.hpp similarity index 96% rename from src/hack/log/log.hpp rename to src/hack/logger/logger.hpp index 4f54f06..bcdc816 100755 --- a/src/hack/log/log.hpp +++ b/src/hack/logger/logger.hpp @@ -8,6 +8,8 @@ #include "hack/iterators/sequence_ostream_iterator.hpp" #include "hack/iterators/associative_ostream_iterator.hpp" +#include "hack/patterns/ring_buffer.hpp" + namespace hack { class log @@ -133,6 +135,12 @@ namespace hack std::cout << data << (count != 0 ? devider : ""); } + template + static void print_t(const hack::patterns::ring_buffer& rb) + { + print_t(rb.get_src()); + } + friend class warn; friend class error; }; diff --git a/src/hack/patterns/ring_buffer.hpp b/src/hack/patterns/ring_buffer.hpp index 84d8ebd..16a264e 100644 --- a/src/hack/patterns/ring_buffer.hpp +++ b/src/hack/patterns/ring_buffer.hpp @@ -1,3 +1,5 @@ +#pragma once + #include #include #include @@ -56,41 +58,46 @@ namespace hack::patterns return val; } - bool empty() noexcept + std::array& get_src() const noexcept + { + return m_data; + } + + bool empty() const noexcept { MUTEX lock(m_mutex); return (!m_full && (m_head == m_tail)); } - size_t size() noexcept - { - MUTEX lock(m_mutex); + std::size_t size() const noexcept + { + MUTEX lock(m_mutex); - std::size_t size; - if(!m_full) - size = (m_head >= m_tail) ? m_head - m_tail : BUFFER_SIZE - (m_tail - m_head); - else - size = BUFFER_SIZE; + std::size_t size; + if(!m_full) + size = (m_head >= m_tail) ? m_head - m_tail : BUFFER_SIZE - (m_tail - m_head); + else + size = BUFFER_SIZE; - return size; - } + return size; + } - void reset() noexcept - { - MUTEX lock(m_mutex); - m_head = m_tail; - m_full = false; - } + void reset() noexcept + { + MUTEX lock(m_mutex); + m_head = m_tail; + m_full = false; + } - bool full() const noexcept - { - return m_full; - } + bool full() const noexcept + { + return m_full; + } - std::size_t capacity() const noexcept - { - return BUFFER_SIZE; - } + std::size_t capacity() const noexcept + { + return BUFFER_SIZE; + } private: void head_refresh() @@ -106,8 +113,7 @@ namespace hack::patterns std::size_t m_tail{ 0 }; private: - std::recursive_mutex m_mutex; - std::array m_data; + mutable std::recursive_mutex m_mutex; + mutable std::array m_data; }; } - diff --git a/src/meson.build b/src/meson.build index 46567d2..b98fa57 100755 --- a/src/meson.build +++ b/src/meson.build @@ -6,7 +6,7 @@ headers = [ 'hack/iterators/associative_ostream_iterator.hpp', 'hack/iterators/sequence_ostream_iterator.hpp', - 'hack/log/log.hpp', + 'hack/logger/logger.hpp', 'hack/mt/algorithms/max.hpp', 'hack/mt/algorithms/sort.hpp', diff --git a/tests/meson.build b/tests/meson.build index 6a32200..ac2a1ca 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -2,5 +2,5 @@ catch2_with_main_dep = dependency('catch2-with-main') dep = [hack_dep, catch2_with_main_dep] test('patterns', executable('patterns', ['patterns/ring_buffer.cpp'], dependencies : dep)) -test('mt', executable('mt', ['mt/max.cpp'], dependencies : dep)) +test('mt', executable('mt', ['mt/mt.cpp'], dependencies : dep)) diff --git a/tests/mt/max.cpp b/tests/mt/max.cpp deleted file mode 100644 index 93256b4..0000000 --- a/tests/mt/max.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "catch2/catch_test_macros.hpp" -#include "hack/mt/algorithms/max.hpp" - -TEST_CASE("mt") -{ - SECTION("max buffer") - { - int a = 4, b = 5; - int& c = a; - REQUIRE(hack::mt::algorithms::max(4, 5) == 5); - REQUIRE(hack::mt::algorithms::max(c, b) == 5); - } -} diff --git a/tests/mt/mt.cpp b/tests/mt/mt.cpp new file mode 100644 index 0000000..6a85921 --- /dev/null +++ b/tests/mt/mt.cpp @@ -0,0 +1,31 @@ +#include "catch2/catch_test_macros.hpp" + +#include + +#include "hack/mt/algorithms/max.hpp" +#include "hack/mt/algorithms/sort.hpp" + +TEST_CASE("mt") +{ + SECTION("max") + { + int a = 4, b = 5; + int& c = a; + REQUIRE(hack::mt::algorithms::max(4, 5) == 5); + REQUIRE(hack::mt::algorithms::max(c, b) == 5); + } + + SECTION("sort") + { + std::vector v { 4, 4, 6, 1, 4, 3, 2 }; + std::vector vs { 1, 2, 3, 4, 4, 4, 6 }; + + std::forward_list l { 8, 7, 5, 9, 0, 1, 3, 2, 6, 4 }; + std::forward_list ls { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + + hack::mt::algorithms::sort(v); + hack::mt::algorithms::sort(l); + REQUIRE(v == vs); + REQUIRE(l == ls); + } +} diff --git a/tests/patterns/ring_buffer.cpp b/tests/patterns/ring_buffer.cpp index c4ce2d2..34e9a7b 100644 --- a/tests/patterns/ring_buffer.cpp +++ b/tests/patterns/ring_buffer.cpp @@ -1,4 +1,5 @@ #include "catch2/catch_test_macros.hpp" + #include "hack/patterns/ring_buffer.hpp" TEST_CASE("patterns")