diff --git a/.clang-format b/.clang-format index 047ab92..0a39f0f 100644 --- a/.clang-format +++ b/.clang-format @@ -42,26 +42,26 @@ AllowShortLambdasOnASingleLine: All AllowShortLoopsOnASingleLine: false AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None -AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: MultiLine +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes AttributeMacros: - __capability -BinPackArguments: true -BinPackParameters: true +BinPackArguments: false +BinPackParameters: false BitFieldColonSpacing: Both BraceWrapping: AfterCaseLabel: false AfterClass: false - AfterControlStatement: Never + AfterControlStatement: Always AfterEnum: false AfterExternBlock: false - AfterFunction: false + AfterFunction: true AfterNamespace: false AfterObjCDeclaration: false AfterStruct: false AfterUnion: false BeforeCatch: false - BeforeElse: false + BeforeElse: true BeforeLambdaBody: false BeforeWhile: false IndentBraces: false @@ -73,7 +73,7 @@ BreakAfterJavaFieldAnnotations: false BreakArrays: true BreakBeforeBinaryOperators: All BreakBeforeConceptDeclarations: Always -BreakBeforeBraces: Attach +BreakBeforeBraces: Custom BreakBeforeInlineASMColon: OnlyMultiline BreakBeforeTernaryOperators: false BreakConstructorInitializers: AfterColon @@ -122,9 +122,9 @@ IndentPPDirectives: None IndentRequiresClause: true IndentWidth: 4 IndentWrappedFunctionNames: false -InsertBraces: false +InsertBraces: true InsertNewlineAtEOF: true -InsertTrailingCommas: None +InsertTrailingCommas: Wrapped IntegerLiteralSeparator: Binary: 0 Decimal: 0 diff --git a/benchmark/first_missing_positive_benchmark.cpp b/benchmark/first_missing_positive_benchmark.cpp index 5346e7b..a860b06 100644 --- a/benchmark/first_missing_positive_benchmark.cpp +++ b/benchmark/first_missing_positive_benchmark.cpp @@ -16,7 +16,8 @@ TEST_CASE( "forfun::first_missing_positive benchmarking", - "[benchmark][first_missing_positive]") { + "[benchmark][first_missing_positive]") +{ using ContainerType = std::array; ankerl::nanobench::Bench() diff --git a/benchmark/lru_cache_benchmark.cpp b/benchmark/lru_cache_benchmark.cpp index 83470b5..bc9d251 100644 --- a/benchmark/lru_cache_benchmark.cpp +++ b/benchmark/lru_cache_benchmark.cpp @@ -18,11 +18,13 @@ template std::enable_if_t, void> -test(int const capacity) { +test(int const capacity) +{ int volatile val{}; T cache(capacity); - for (int i{0}; i < capacity; ++i) { + for (int i{0}; i < capacity; ++i) + { cache.put(i, i); } @@ -44,13 +46,16 @@ test(int const capacity) { val = cache.get(3); - for (int i{0}; i < capacity; ++i) { + for (int i{0}; i < capacity; ++i) + { val = cache.get(i); } } -TEST_CASE("forfun::lrucache benchmarking", "[benchmark][lrucache]") { - SECTION("small") { +TEST_CASE("forfun::lrucache benchmarking", "[benchmark][lrucache]") +{ + SECTION("small") + { static constexpr int const lrucache_capacity{32}; ankerl::nanobench::Bench() @@ -73,7 +78,8 @@ TEST_CASE("forfun::lrucache benchmarking", "[benchmark][lrucache]") { ; } - SECTION("large") { + SECTION("large") + { static constexpr int const lrucache_capacity{128}; ankerl::nanobench::Bench() diff --git a/benchmark/palindrome_benchmark.cpp b/benchmark/palindrome_benchmark.cpp index 9eeb949..367541d 100644 --- a/benchmark/palindrome_benchmark.cpp +++ b/benchmark/palindrome_benchmark.cpp @@ -19,8 +19,10 @@ inline constexpr std::string_view const palindrome{ "oooooooooooooooooooooooooooooooooooooooooooooooooo" "oooooooooooooooooooooooooooooooooooooooooooooooooo"}; -TEST_CASE("forfun::palindrome benchmarking", "[benchmark][palindrome]") { - SECTION("case-sensitive") { +TEST_CASE("forfun::palindrome benchmarking", "[benchmark][palindrome]") +{ + SECTION("case-sensitive") + { ankerl::nanobench::Bench() .title("Palindrome (case-sensitive)") @@ -67,7 +69,8 @@ TEST_CASE("forfun::palindrome benchmarking", "[benchmark][palindrome]") { ; } - SECTION("case-insensitive") { + SECTION("case-insensitive") + { ankerl::nanobench::Bench() .title("Palindrome (case-insensitive)") diff --git a/benchmark/palindromic_number_benchmark.cpp b/benchmark/palindromic_number_benchmark.cpp index 131ce46..78dd0c1 100644 --- a/benchmark/palindromic_number_benchmark.cpp +++ b/benchmark/palindromic_number_benchmark.cpp @@ -16,8 +16,8 @@ inline constexpr int const p{1234554321}; -TEST_CASE( - "palindromic_number benchmarking", "[benchmark][palindromic_number]") { +TEST_CASE("palindromic_number benchmarking", "[benchmark][palindromic_number]") +{ static_assert(p >= 0 && p <= std::numeric_limits::max()); ankerl::nanobench::Bench() diff --git a/include/forfun/first_missing_positive.hpp b/include/forfun/first_missing_positive.hpp index 645a4b6..c7072b4 100644 --- a/include/forfun/first_missing_positive.hpp +++ b/include/forfun/first_missing_positive.hpp @@ -26,12 +26,15 @@ namespace forfun::first_missing_positive { namespace { template -constexpr inline void quasi_sort(RandomIt first, RandomIt const src) noexcept { +constexpr inline void quasi_sort(RandomIt first, RandomIt const src) noexcept +{ auto const n{*src}; - if (n > 0) { + if (n > 0) + { RandomIt const dest{first + std::max(0, n - 1)}; - if (auto const tmp{*dest}; tmp != n) { + if (auto const tmp{*dest}; tmp != n) + { *dest = n; *src = tmp; @@ -43,27 +46,36 @@ constexpr inline void quasi_sort(RandomIt first, RandomIt const src) noexcept { } // namespace template -[[nodiscard]] constexpr inline int lowest_missing(T& numbers) noexcept { +[[nodiscard]] constexpr inline int lowest_missing(T& numbers) noexcept +{ auto begin{numbers.begin()}; auto end{numbers.end()}; auto max{numbers.size()}; - for (auto it{begin}; it != end; ++it) { + for (auto it{begin}; it != end; ++it) + { int const current{*it}; - if (current < 1) { + if (current < 1) + { --max; - } else if (static_cast(current) > max) { + } + else if (static_cast(current) > max) + { --max; *it = 0; - } else { + } + else + { quasi_sort(begin, it); } } int min_num{1}; auto const endIt = begin + max; - for (auto it{begin}; it != endIt; ++it) { - if (*it == min_num) { + for (auto it{begin}; it != endIt; ++it) + { + if (*it == min_num) + { ++min_num; } } diff --git a/include/forfun/lru_cache.hpp b/include/forfun/lru_cache.hpp index fba6674..f1d3a44 100644 --- a/include/forfun/lru_cache.hpp +++ b/include/forfun/lru_cache.hpp @@ -16,7 +16,8 @@ namespace forfun::lrucache { class LRUCacheBase { public: - virtual ~LRUCacheBase() { + virtual ~LRUCacheBase() + { } virtual int get(int const key) noexcept = 0; diff --git a/include/forfun/palindrome.hpp b/include/forfun/palindrome.hpp index 176d652..f01a6c0 100644 --- a/include/forfun/palindrome.hpp +++ b/include/forfun/palindrome.hpp @@ -20,12 +20,15 @@ namespace forfun::palindrome { namespace raw { [[nodiscard]] constexpr inline bool -is_palindrome(std::string_view const& s) noexcept { +is_palindrome(std::string_view const& s) noexcept +{ auto const end{s.length() - 1}; auto const mid{s.length() / 2}; - for (std::size_t i{0}; i < mid; ++i) { - if (s[i] != s[end - i]) { + for (std::size_t i{0}; i < mid; ++i) + { + if (s[i] != s[end - i]) + { return false; } } @@ -33,13 +36,16 @@ is_palindrome(std::string_view const& s) noexcept { return true; } -[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept { +[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept +{ auto const end{s.length() - 1}; auto const mid{s.length() / 2}; - for (std::size_t i{0}; i < mid; ++i) { + for (std::size_t i{0}; i < mid; ++i) + { if (std::tolower(static_cast(s[i])) - != std::tolower(static_cast(s[end - i]))) { + != std::tolower(static_cast(s[end - i]))) + { return false; } } @@ -52,12 +58,15 @@ is_palindrome(std::string_view const& s) noexcept { namespace fast { [[nodiscard]] constexpr inline bool -is_palindrome(std::string_view const& s) noexcept { +is_palindrome(std::string_view const& s) noexcept +{ auto upper{s.cend() - 1}; auto const mid{s.cbegin() + (s.length() / 2)}; - for (auto lower{s.cbegin()}; lower < mid; ++lower) { - if ((*lower) != (*upper)) { + for (auto lower{s.cbegin()}; lower < mid; ++lower) + { + if ((*lower) != (*upper)) + { return false; } @@ -67,13 +76,16 @@ is_palindrome(std::string_view const& s) noexcept { return true; } -[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept { +[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept +{ auto upper{s.cend() - 1}; auto const mid{s.cbegin() + (s.length() / 2)}; - for (auto lower{s.cbegin()}; lower < mid; ++lower) { + for (auto lower{s.cbegin()}; lower < mid; ++lower) + { if (std::tolower(static_cast((*lower))) - != std::tolower(static_cast((*upper)))) { + != std::tolower(static_cast((*upper)))) + { return false; } @@ -90,22 +102,27 @@ namespace stl_bloated { /// Adapted from original source: /// https://en.cppreference.com/w/cpp/algorithm/equal [[nodiscard]] constexpr inline bool -is_palindrome(std::string_view const& s) noexcept { +is_palindrome(std::string_view const& s) noexcept +{ return std::equal( s.cbegin(), std::next(s.cbegin(), s.size() / 2), s.crbegin()); } namespace { [[nodiscard]] inline bool -equal_case_insensitive(char const a, char const b) noexcept { +equal_case_insensitive(char const a, char const b) noexcept +{ return std::tolower(static_cast(a)) == std::tolower(static_cast(b)); } } // namespace -[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept { +[[nodiscard]] inline bool is_palindrome_ci(std::string_view const& s) noexcept +{ return std::equal( - s.cbegin(), std::next(s.cbegin(), s.size() / 2), s.crbegin(), + s.cbegin(), + std::next(s.cbegin(), s.size() / 2), + s.crbegin(), equal_case_insensitive); } @@ -116,7 +133,8 @@ namespace stl_fast { /// Adapted from original source: /// https://en.cppreference.com/w/cpp/algorithm/equal [[nodiscard]] constexpr inline bool -is_palindrome(std::string_view const& s) noexcept { +is_palindrome(std::string_view const& s) noexcept +{ auto const begin{s.cbegin()}; return std::equal(begin, begin + (s.size() / 2), s.crbegin()); } diff --git a/include/forfun/palindromic_number.hpp b/include/forfun/palindromic_number.hpp index 59faaf5..a7d0cbb 100644 --- a/include/forfun/palindromic_number.hpp +++ b/include/forfun/palindromic_number.hpp @@ -18,10 +18,12 @@ namespace fast { template requires std::integral -[[nodiscard]] constexpr bool is_palindrome(T const n) noexcept { +[[nodiscard]] constexpr bool is_palindrome(T const n) noexcept +{ T nn{}; auto d{n}; - while (d > T{0}) { + while (d > T{0}) + { nn = (nn * T{10}) + d % T{10}; d /= T{10}; } diff --git a/include/forfun/project_euler/p0001_multiples_of_3_or_5.hpp b/include/forfun/project_euler/p0001_multiples_of_3_or_5.hpp index ca7680e..052f55a 100644 --- a/include/forfun/project_euler/p0001_multiples_of_3_or_5.hpp +++ b/include/forfun/project_euler/p0001_multiples_of_3_or_5.hpp @@ -20,14 +20,16 @@ namespace { [[nodiscard]] inline /* constexpr */ int -sum_2x(int const n, int const q) noexcept { +sum_2x(int const n, int const q) noexcept +{ auto const d = std::div(n, q); return d.quot * (q + n - d.rem); } } // namespace -[[nodiscard]] /* constexpr */ int find_sum_mult_three_five(int n) noexcept { +[[nodiscard]] /* constexpr */ int find_sum_mult_three_five(int n) noexcept +{ --n; return (sum_2x(n, 3) + sum_2x(n, 5) - sum_2x(n, 15)) / 2; diff --git a/include/forfun/sorting/insertion.hpp b/include/forfun/sorting/insertion.hpp index 0f19e3a..51c2243 100644 --- a/include/forfun/sorting/insertion.hpp +++ b/include/forfun/sorting/insertion.hpp @@ -15,11 +15,15 @@ namespace forfun::sorting { template -constexpr inline void insertion_sort( - T& container, RandomIt const begin, RandomIt const end) noexcept { - if (begin != end) { - for (RandomIt i{begin + 1}; i != end; ++i) { - for (RandomIt j{i}; *j < *(j - 1) && j != begin; --j) { +constexpr inline void +insertion_sort(T& container, RandomIt const begin, RandomIt const end) noexcept +{ + if (begin != end) + { + for (RandomIt i{begin + 1}; i != end; ++i) + { + for (RandomIt j{i}; *j < *(j - 1) && j != begin; --j) + { std::iter_swap(j, j - 1); } } diff --git a/include/forfun/sorting/quicksort.hpp b/include/forfun/sorting/quicksort.hpp index 3176df6..1d4bd28 100644 --- a/include/forfun/sorting/quicksort.hpp +++ b/include/forfun/sorting/quicksort.hpp @@ -19,12 +19,15 @@ namespace { template [[nodiscard]] constexpr inline RandomIt -partition(T& container, RandomIt const lo, RandomIt const hi) noexcept { +partition(T& container, RandomIt const lo, RandomIt const hi) noexcept +{ auto const pivot = *hi; auto i{std::prev(lo)}; - for (auto j{lo}; j < hi; ++j) { - if (*j < pivot) { + for (auto j{lo}; j < hi; ++j) + { + if (*j < pivot) + { ++i; std::iter_swap(i, j); } @@ -40,8 +43,10 @@ partition(T& container, RandomIt const lo, RandomIt const hi) noexcept { template constexpr inline void -quicksort(T& container, RandomIt const lo, RandomIt const hi) noexcept { - if (hi > lo) { +quicksort(T& container, RandomIt const lo, RandomIt const hi) noexcept +{ + if (hi > lo) + { auto const p{partition(container, lo, hi)}; quicksort(container, lo, std::prev(p)); diff --git a/src/fizzbuzz.cpp b/src/fizzbuzz.cpp index 0d57410..11b6d01 100644 --- a/src/fizzbuzz.cpp +++ b/src/fizzbuzz.cpp @@ -9,19 +9,25 @@ #include #include -std::string fizzbuzz(int const n) noexcept { +std::string fizzbuzz(int const n) noexcept +{ std::ostringstream buffer; - for (int i{1}; i <= n; ++i) { + for (int i{1}; i <= n; ++i) + { bool f{true}; - if (i % 3 == 0) { + if (i % 3 == 0) + { buffer << "Fizz"; f = false; } - if (i % 5 == 0) { + if (i % 5 == 0) + { buffer << "Buzz"; - } else if (f) { + } + else if (f) + { buffer << i; } } diff --git a/src/lru_cache.cpp b/src/lru_cache.cpp index 72bbc92..a1d4e49 100644 --- a/src/lru_cache.cpp +++ b/src/lru_cache.cpp @@ -13,28 +13,35 @@ namespace forfun::lrucache { namespace naive { LRUCache::LRUCache(int const capacity) noexcept : - capacity_{capacity}, cache_{std::make_unique(capacity)} { + capacity_{capacity}, cache_{std::make_unique(capacity)} +{ } -[[nodiscard]] int LRUCache::get(int const key) noexcept { +[[nodiscard]] int LRUCache::get(int const key) noexcept +{ int result{-1}; std::int64_t lowest_tick_count{ticks_}; int i{0}; - for (; i < size_; ++i) { - if (cache_[i].key_ == key) { + for (; i < size_; ++i) + { + if (cache_[i].key_ == key) + { result = cache_[i].value_; cache_[i].ticks_ = ticks_; break; } - if (lowest_tick_count >= cache_[i].ticks_) { + if (lowest_tick_count >= cache_[i].ticks_) + { lowest_tick_count = cache_[i].ticks_; least_recent_idx_ = i; } } - for (; i < size_; ++i) { - if (lowest_tick_count >= cache_[i].ticks_) { + for (; i < size_; ++i) + { + if (lowest_tick_count >= cache_[i].ticks_) + { lowest_tick_count = cache_[i].ticks_; least_recent_idx_ = i; } @@ -45,18 +52,22 @@ LRUCache::LRUCache(int const capacity) noexcept : return result; } -void LRUCache::put(int const key, int const value) noexcept { +void LRUCache::put(int const key, int const value) noexcept +{ assert(size_ <= capacity_); - for (int i{0}; i < size_; ++i) { - if (cache_[i].key_ == key) { + for (int i{0}; i < size_; ++i) + { + if (cache_[i].key_ == key) + { cache_[i].value_ = value; return; } } - if (size_ < capacity_) { + if (size_ < capacity_) + { cache_[size_].key_ = key; cache_[size_].value_ = value; cache_[size_].ticks_ = ticks_; @@ -75,13 +86,16 @@ void LRUCache::put(int const key, int const value) noexcept { namespace stl { -LRUCache::LRUCache(int const capacity) noexcept : capacity_{capacity} { +LRUCache::LRUCache(int const capacity) noexcept : capacity_{capacity} +{ lookup_.reserve(capacity); } -[[nodiscard]] int LRUCache::get(int const key) noexcept { +[[nodiscard]] int LRUCache::get(int const key) noexcept +{ auto const existing_key_iter{lookup_.find(key)}; - if (existing_key_iter == lookup_.end()) { + if (existing_key_iter == lookup_.end()) + { return -1; } @@ -90,14 +104,16 @@ LRUCache::LRUCache(int const capacity) noexcept : capacity_{capacity} { return existing_key_iter->second->second; } -void LRUCache::put(int const key, int const value) noexcept { +void LRUCache::put(int const key, int const value) noexcept +{ assert(size_ <= capacity_); assert(size_ == lookup_.size()); assert(size_ == cache_.size()); // Update if key exists. auto const iter_existing_key{lookup_.find(key)}; - if (iter_existing_key != lookup_.end()) { + if (iter_existing_key != lookup_.end()) + { iter_existing_key->second->second = value; return; @@ -105,7 +121,8 @@ void LRUCache::put(int const key, int const value) noexcept { // Add new if cache is not full. auto const cache_end{cache_.end()}; - if (size_ < capacity_) { + if (size_ < capacity_) + { auto const emplaced_key_iter{cache_.emplace(cache_end, key, value)}; assert(cache_.end() == cache_end); [[maybe_unused]] auto const new_key_insert_result{ diff --git a/src/palindrome.c b/src/palindrome.c index 68a09f8..cff73f9 100644 --- a/src/palindrome.c +++ b/src/palindrome.c @@ -10,13 +10,16 @@ found in the LICENSE file. #include -int is_palindrome(char const* const str, size_t const size) { +int is_palindrome(char const* const str, size_t const size) +{ size_t const end = size - 1; size_t const mid = size / 2; size_t i; - for (i = 0; i < mid; ++i) { - if (str[i] != str[end - i]) { + for (i = 0; i < mid; ++i) + { + if (str[i] != str[end - i]) + { return 0; } } @@ -24,14 +27,17 @@ int is_palindrome(char const* const str, size_t const size) { return 1; } -int is_palindrome_ci(char const* const str, size_t const size) { +int is_palindrome_ci(char const* const str, size_t const size) +{ size_t const end = size - 1; size_t const mid = size / 2; size_t i; - for (i = 0; i < mid; ++i) { + for (i = 0; i < mid; ++i) + { if (tolower((unsigned char)str[i]) - != tolower((unsigned char)str[end - i])) { + != tolower((unsigned char)str[end - i])) + { return 0; } } diff --git a/src/palindromic_number.cpp b/src/palindromic_number.cpp index 8cc91f4..3c06312 100644 --- a/src/palindromic_number.cpp +++ b/src/palindromic_number.cpp @@ -12,10 +12,12 @@ namespace forfun::palindromic_number { namespace stl { -[[nodiscard]] bool is_palindrome(int const n) noexcept { +[[nodiscard]] bool is_palindrome(int const n) noexcept +{ int nn{0}; std::div_t d{.quot = n, .rem = 0}; - while (d.quot > 0) { + while (d.quot > 0) + { d = std::div(d.quot, 10); nn = (nn * 10) + d.rem; } diff --git a/src/sonar.cpp b/src/sonar.cpp index c9002da..92abb6b 100644 --- a/src/sonar.cpp +++ b/src/sonar.cpp @@ -10,15 +10,18 @@ #include #include -int count_ships(Sonar const& sonar, Area const area) { - if (!sonar.ping(area)) { +int count_ships(Sonar const& sonar, Area const area) +{ + if (!sonar.ping(area)) + { return 0; } int const width{std::abs(area.right - area.left)}; int const height{std::abs(area.bottom - area.top)}; - if ((width + height) == 0) { + if ((width + height) == 0) + { return 1; } @@ -61,7 +64,8 @@ int count_ships(Sonar const& sonar, Area const area) { }); } -bool Sonar::ping(Area area) const { +bool Sonar::ping(Area area) const +{ return std::ranges::any_of(coords, [&area](Coord const& coord) { return coord.x >= area.top && coord.x <= area.bottom && coord.y >= area.left && coord.y <= area.right; diff --git a/test/first_missing_positive_test.cpp b/test/first_missing_positive_test.cpp index 5d2a7ee..71a0890 100644 --- a/test/first_missing_positive_test.cpp +++ b/test/first_missing_positive_test.cpp @@ -12,10 +12,12 @@ #include "forfun/first_missing_positive.hpp" -TEST_CASE("first_missing_positive") { +TEST_CASE("first_missing_positive") +{ using forfun::first_missing_positive::lowest_missing; - SECTION("Empty container") { + SECTION("Empty container") + { std::array test_input{}; CAPTURE(test_input); @@ -25,7 +27,8 @@ TEST_CASE("first_missing_positive") { STATIC_REQUIRE(lowest_missing(test_input) == 1); } - SECTION("One number") { + SECTION("One number") + { auto [test_input, expected_output]{ GENERATE(table, int>({ {{-2}, 1}, @@ -43,7 +46,8 @@ TEST_CASE("first_missing_positive") { REQUIRE(lowest_missing(test_input) == expected_output); } - SECTION("Two numbers") { + SECTION("Two numbers") + { auto [test_input, expected_output]{ GENERATE(table, int>({ {{-2, -2}, 1}, @@ -65,7 +69,8 @@ TEST_CASE("first_missing_positive") { REQUIRE(lowest_missing(test_input) == expected_output); } - SECTION("Three numbers") { + SECTION("Three numbers") + { auto [test_input, expected_output]{ GENERATE(table, int>({ {{-1, -1, -1}, 1}, @@ -88,7 +93,8 @@ TEST_CASE("first_missing_positive") { REQUIRE(lowest_missing(test_input) == expected_output); } - SECTION("Ten numbers") { + SECTION("Ten numbers") + { auto [test_input, expected_output]{ GENERATE(table, int>({ {{-1, -1, -1, -1, -1, -1, -1, -1, -1, 10}, 1}, diff --git a/test/fizzbuzz_test.cpp b/test/fizzbuzz_test.cpp index bdc390e..f688b27 100644 --- a/test/fizzbuzz_test.cpp +++ b/test/fizzbuzz_test.cpp @@ -9,8 +9,10 @@ #include "forfun/fizzbuzz.hpp" -TEST_CASE("fizzbuzz") { - SECTION("Numbers other than three and five are concatenated as-is") { +TEST_CASE("fizzbuzz") +{ + SECTION("Numbers other than three and five are concatenated as-is") + { REQUIRE_THAT( fizzbuzz(1), Catch::Matchers::Equals("1", Catch::CaseSensitive::Yes)); @@ -20,7 +22,8 @@ TEST_CASE("fizzbuzz") { Catch::Matchers::Equals("12", Catch::CaseSensitive::Yes)); } - SECTION("Multiples of three are concatenated as Fizz") { + SECTION("Multiples of three are concatenated as Fizz") + { REQUIRE_THAT( fizzbuzz(3), Catch::Matchers::Equals("12Fizz", Catch::CaseSensitive::Yes)); @@ -36,7 +39,8 @@ TEST_CASE("fizzbuzz") { "12Fizz4BuzzFizz78Fizz", Catch::CaseSensitive::Yes)); } - SECTION("Multiples of five are concatenated as Buzz") { + SECTION("Multiples of five are concatenated as Buzz") + { REQUIRE_THAT( fizzbuzz(5), Catch::Matchers::Equals("12Fizz4Buzz", Catch::CaseSensitive::Yes)); @@ -53,7 +57,8 @@ TEST_CASE("fizzbuzz") { Catch::CaseSensitive::Yes)); } - SECTION("Multiples of both three and five are concatenated as FizzBuzz") { + SECTION("Multiples of both three and five are concatenated as FizzBuzz") + { REQUIRE_THAT( fizzbuzz(15), Catch::Matchers::Equals( @@ -74,7 +79,8 @@ TEST_CASE("fizzbuzz") { Catch::CaseSensitive::Yes)); } - SECTION("Prime number input is computed correctly") { + SECTION("Prime number input is computed correctly") + { REQUIRE_THAT( fizzbuzz(11), Catch::Matchers::Equals( diff --git a/test/lru_cache.cpp b/test/lru_cache.cpp index 35b8764..13266f6 100644 --- a/test/lru_cache.cpp +++ b/test/lru_cache.cpp @@ -10,7 +10,8 @@ template std::enable_if_t, void> -test() { +test() +{ int volatile val{}; { @@ -130,7 +131,8 @@ test() { } } -int main() { +int main() +{ test(); test(); diff --git a/test/palindrome.c b/test/palindrome.c index a73d461..7c41ff9 100644 --- a/test/palindrome.c +++ b/test/palindrome.c @@ -11,7 +11,8 @@ found in the LICENSE file. #include #include -void test_palindrome() { +void test_palindrome() +{ { char const* const s = ""; size_t const len = strlen(s); @@ -69,7 +70,8 @@ void test_palindrome() { } } -void test_palindrome_ci() { +void test_palindrome_ci() +{ { char const* const s = "Aa"; size_t const len = strlen(s); @@ -85,7 +87,8 @@ void test_palindrome_ci() { } } -int main() { +int main() +{ test_palindrome(); test_palindrome_ci(); diff --git a/test/palindrome.cpp b/test/palindrome.cpp index 9754c41..ae4f032 100644 --- a/test/palindrome.cpp +++ b/test/palindrome.cpp @@ -13,7 +13,8 @@ using is_palindrome_func_t = std::function; -void test_palindrome(is_palindrome_func_t is_palindrome_func) { +void test_palindrome(is_palindrome_func_t is_palindrome_func) +{ using namespace std::string_literals; { @@ -57,7 +58,8 @@ void test_palindrome(is_palindrome_func_t is_palindrome_func) { } } -void test_palindrome_ci(is_palindrome_func_t is_palindrome_func) { +void test_palindrome_ci(is_palindrome_func_t is_palindrome_func) +{ using namespace std::string_literals; { @@ -71,7 +73,8 @@ void test_palindrome_ci(is_palindrome_func_t is_palindrome_func) { } } -int main() { +int main() +{ test_palindrome(forfun::palindrome::raw::is_palindrome); test_palindrome(forfun::palindrome::fast::is_palindrome); test_palindrome(forfun::palindrome::stl_bloated::is_palindrome); diff --git a/test/palindromic_number_test.cpp b/test/palindromic_number_test.cpp index aafc65b..805912b 100644 --- a/test/palindromic_number_test.cpp +++ b/test/palindromic_number_test.cpp @@ -11,11 +11,16 @@ #include "forfun/palindromic_number.hpp" TEMPLATE_TEST_CASE_SIG( - "palindromic_number", "[palindromic_number]", ((auto sut), sut), + "palindromic_number", + "[palindromic_number]", + ((auto sut), sut), (forfun::palindromic_number::fast::is_palindrome), - (forfun::palindromic_number::stl::is_palindrome)) { - SECTION("Palindromic numbers") { - GIVEN("a palindromic number") { + (forfun::palindromic_number::stl::is_palindrome)) +{ + SECTION("Palindromic numbers") + { + GIVEN("a palindromic number") + { auto const [palindromic_number]{GENERATE(table({ {0}, {1}, @@ -32,16 +37,20 @@ TEMPLATE_TEST_CASE_SIG( CAPTURE(palindromic_number); - WHEN("is_palindrome checked") { - THEN("true") { + WHEN("is_palindrome checked") + { + THEN("true") + { REQUIRE(sut(palindromic_number)); } } } } - SECTION("Non-palindromic numbers") { - GIVEN("a non-palindromic number") { + SECTION("Non-palindromic numbers") + { + GIVEN("a non-palindromic number") + { auto const [non_palindromic_number]{GENERATE(table({ {std::numeric_limits::min()}, {-1}, @@ -56,8 +65,10 @@ TEMPLATE_TEST_CASE_SIG( CAPTURE(non_palindromic_number); - WHEN("is_palindrome checked") { - THEN("false") { + WHEN("is_palindrome checked") + { + THEN("false") + { REQUIRE_FALSE(sut(non_palindromic_number)); } } @@ -65,8 +76,10 @@ TEMPLATE_TEST_CASE_SIG( } } -TEST_CASE("palindromic_number_static", "[palindromic_number][static]") { - SECTION("Palindromic integrals") { +TEST_CASE("palindromic_number_static", "[palindromic_number][static]") +{ + SECTION("Palindromic integrals") + { STATIC_REQUIRE( forfun::palindromic_number::fast::is_palindrome(char{33})); @@ -79,7 +92,8 @@ TEST_CASE("palindromic_number_static", "[palindromic_number][static]") { STATIC_REQUIRE(forfun::palindromic_number::fast::is_palindrome(33ull)); } - SECTION("Non-palindromic integrals") { + SECTION("Non-palindromic integrals") + { STATIC_REQUIRE_FALSE( forfun::palindromic_number::fast::is_palindrome(-1)); diff --git a/test/project_euler/p0001_multiples_of_3_or_5.cpp b/test/project_euler/p0001_multiples_of_3_or_5.cpp index 05b8ee4..b94a4d4 100644 --- a/test/project_euler/p0001_multiples_of_3_or_5.cpp +++ b/test/project_euler/p0001_multiples_of_3_or_5.cpp @@ -8,7 +8,8 @@ #include -int main() { +int main() +{ { int const s{find_sum_mult_three_five(1)}; assert(s == 0); diff --git a/test/sonar.cpp b/test/sonar.cpp index d494a3f..1951d88 100644 --- a/test/sonar.cpp +++ b/test/sonar.cpp @@ -9,7 +9,8 @@ #include #include -int main() { +int main() +{ { [[maybe_unused]] int num_ships; Sonar s{{{0, 0}}}; diff --git a/test/sorting/insertion_test.cpp b/test/sorting/insertion_test.cpp index 7f6006e..6025ea9 100644 --- a/test/sorting/insertion_test.cpp +++ b/test/sorting/insertion_test.cpp @@ -11,12 +11,12 @@ #include "forfun/sorting/insertion.hpp" -TEST_CASE("Insertion sort", "[sorting]") { +TEST_CASE("Insertion sort", "[sorting]") +{ using forfun::sorting::insertion_sort; - // clang-format off - - SECTION("Empty container") { + SECTION("Empty container") + { std::array test_input{}; static constexpr std::array const expected_output{}; insertion_sort(test_input, test_input.begin(), test_input.end()); @@ -26,7 +26,8 @@ TEST_CASE("Insertion sort", "[sorting]") { STATIC_REQUIRE(test_input == expected_output); } - SECTION("One element") { + SECTION("One element") + { std::array test_input{7}; static constexpr std::array const expected_output{7}; insertion_sort(test_input, test_input.begin(), test_input.end()); @@ -36,7 +37,8 @@ TEST_CASE("Insertion sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Two elements") { + SECTION("Two elements") + { std::array test_input{1, 1}; static constexpr std::array const expected_output{1, 1}; insertion_sort(test_input, test_input.begin(), test_input.end()); @@ -46,7 +48,8 @@ TEST_CASE("Insertion sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Three elements") { + SECTION("Three elements") + { std::array test_input{-6, 3, 11}; static constexpr std::array const expected_output{-6, 3, 11}; insertion_sort(test_input, test_input.begin(), test_input.end()); @@ -56,9 +59,11 @@ TEST_CASE("Insertion sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Best case") { + SECTION("Best case") + { std::array test_input{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - static constexpr std::array const expected_output{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + static constexpr std::array const expected_output{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; insertion_sort(test_input, test_input.begin(), test_input.end()); STATIC_CHECK(test_input.size() == 10); @@ -66,9 +71,11 @@ TEST_CASE("Insertion sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Worst case") { + SECTION("Worst case") + { std::array test_input{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; - static constexpr std::array const expected_output{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + static constexpr std::array const expected_output{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; insertion_sort(test_input, test_input.begin(), test_input.end()); STATIC_CHECK(test_input.size() == 10); @@ -76,9 +83,11 @@ TEST_CASE("Insertion sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Arbitrary test with std::array") { + SECTION("Arbitrary test with std::array") + { std::array test_input{200, 4, 7, 0, 9, -10, 2}; - static constexpr std::array const expected_output{-10, 0, 2, 4, 7, 9, 200}; + static constexpr std::array const expected_output{ + -10, 0, 2, 4, 7, 9, 200}; insertion_sort(test_input, test_input.begin(), test_input.end()); STATIC_CHECK(test_input.size() == 7); @@ -86,7 +95,8 @@ TEST_CASE("Insertion sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Arbitrary test with std::vector 01") { + SECTION("Arbitrary test with std::vector 01") + { std::vector test_input{8, 4, 7, 0, 9, 5, 2}; std::vector const expected_output{0, 2, 4, 5, 7, 8, 9}; insertion_sort(test_input, test_input.begin(), test_input.end()); @@ -96,7 +106,8 @@ TEST_CASE("Insertion sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Arbitrary test with std::vector 02") { + SECTION("Arbitrary test with std::vector 02") + { std::vector test_input{8, 4, 7, 0, 9, 5, 2}; std::vector const expected_output{0, 2, 4, 5, 7, 8, 9}; insertion_sort(test_input, test_input.begin(), test_input.end()); @@ -106,7 +117,8 @@ TEST_CASE("Insertion sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Negative numbers") { + SECTION("Negative numbers") + { std::vector test_input{-8, -4, -7, -4, -9, -5, -2}; std::vector const expected_output{-9, -8, -7, -5, -4, -4, -2}; insertion_sort(test_input, test_input.begin(), test_input.end()); @@ -115,6 +127,4 @@ TEST_CASE("Insertion sort", "[sorting]") { REQUIRE(test_input == expected_output); } - - // clang-format on } diff --git a/test/sorting/quicksort_test.cpp b/test/sorting/quicksort_test.cpp index 0b2a936..d91ad2b 100644 --- a/test/sorting/quicksort_test.cpp +++ b/test/sorting/quicksort_test.cpp @@ -11,12 +11,12 @@ #include "forfun/sorting/quicksort.hpp" -TEST_CASE("Quicksort sort", "[sorting]") { +TEST_CASE("Quicksort sort", "[sorting]") +{ using forfun::sorting::quicksort; - // clang-format off - - SECTION("Empty container") { + SECTION("Empty container") + { std::array test_input{}; static constexpr std::array const expected_output{}; quicksort(test_input, test_input.begin(), test_input.end()); @@ -26,7 +26,8 @@ TEST_CASE("Quicksort sort", "[sorting]") { STATIC_REQUIRE(test_input == expected_output); } - SECTION("One element") { + SECTION("One element") + { std::array test_input{7}; static constexpr std::array const expected_output{7}; quicksort(test_input, test_input.begin(), std::prev(test_input.end())); @@ -36,7 +37,8 @@ TEST_CASE("Quicksort sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Two elements") { + SECTION("Two elements") + { std::array test_input{1, 1}; static constexpr std::array const expected_output{1, 1}; quicksort(test_input, test_input.begin(), std::prev(test_input.end())); @@ -46,7 +48,8 @@ TEST_CASE("Quicksort sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Three elements") { + SECTION("Three elements") + { std::array test_input{-6, 3, 11}; static constexpr std::array const expected_output{-6, 3, 11}; quicksort(test_input, test_input.begin(), std::prev(test_input.end())); @@ -56,9 +59,11 @@ TEST_CASE("Quicksort sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Best case") { + SECTION("Best case") + { std::array test_input{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - static constexpr std::array const expected_output{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + static constexpr std::array const expected_output{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; quicksort(test_input, test_input.begin(), std::prev(test_input.end())); STATIC_CHECK(test_input.size() == 10); @@ -66,9 +71,11 @@ TEST_CASE("Quicksort sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Worst case") { + SECTION("Worst case") + { std::array test_input{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; - static constexpr std::array const expected_output{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + static constexpr std::array const expected_output{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; quicksort(test_input, test_input.begin(), std::prev(test_input.end())); STATIC_CHECK(test_input.size() == 10); @@ -76,9 +83,11 @@ TEST_CASE("Quicksort sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Arbitrary test with std::array") { + SECTION("Arbitrary test with std::array") + { std::array test_input{200, 4, 7, 0, 9, -10, 2}; - static constexpr std::array const expected_output{-10, 0, 2, 4, 7, 9, 200}; + static constexpr std::array const expected_output{ + -10, 0, 2, 4, 7, 9, 200}; quicksort(test_input, test_input.begin(), std::prev(test_input.end())); STATIC_CHECK(test_input.size() == 7); @@ -86,7 +95,8 @@ TEST_CASE("Quicksort sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Arbitrary test with std::vector 01") { + SECTION("Arbitrary test with std::vector 01") + { std::vector test_input{8, 4, 7, 0, 9, 5, 2}; std::vector const expected_output{0, 2, 4, 5, 7, 8, 9}; quicksort(test_input, test_input.begin(), std::prev(test_input.end())); @@ -96,7 +106,8 @@ TEST_CASE("Quicksort sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Arbitrary test with std::vector 02") { + SECTION("Arbitrary test with std::vector 02") + { std::vector test_input{8, 4, 7, 0, 9, 5, 2}; std::vector const expected_output{0, 2, 4, 5, 7, 8, 9}; quicksort(test_input, test_input.begin(), test_input.end() - 1); @@ -106,7 +117,8 @@ TEST_CASE("Quicksort sort", "[sorting]") { REQUIRE(test_input == expected_output); } - SECTION("Negative numbers") { + SECTION("Negative numbers") + { std::vector test_input{-8, -4, -7, -4, -9, -5, -2}; std::vector const expected_output{-9, -8, -7, -5, -4, -4, -2}; quicksort(test_input, test_input.begin(), test_input.end() - 1); @@ -115,6 +127,4 @@ TEST_CASE("Quicksort sort", "[sorting]") { REQUIRE(test_input == expected_output); } - - // clang-format on }