From 8a368240e20b4bbacb67e1ec2283900396b3ef4c Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Fri, 30 Aug 2024 19:20:08 +0200 Subject: [PATCH] fix: remove memory leak `iterative_factorial.cpp` (#2535) * fix: remove memory leak * tests: check properly if `math::iterativeFactorial` throws --- math/iterative_factorial.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/math/iterative_factorial.cpp b/math/iterative_factorial.cpp index 00cdb18fd..12f6afe06 100644 --- a/math/iterative_factorial.cpp +++ b/math/iterative_factorial.cpp @@ -46,7 +46,7 @@ namespace math { */ uint64_t iterativeFactorial(uint8_t n) { if (n > 20) { - throw new std::invalid_argument("Maximum n value is 20"); + throw std::invalid_argument("Maximum n value is 20"); } // 1 because it is the identity number of multiplication. @@ -101,12 +101,14 @@ static void test() { std::cout << "Exception test \n" "Input: 21 \n" "Expected output: Exception thrown \n"; + + bool wasExceptionThrown = false; try { math::iterativeFactorial(21); - } catch (std::invalid_argument* e) { - std::cout << "Exception thrown successfully \nContent: " << e->what() - << "\n"; + } catch (const std::invalid_argument&) { + wasExceptionThrown = true; } + assert(wasExceptionThrown); std::cout << "All tests have passed successfully.\n"; }