Skip to content

Commit d0a5879

Browse files
Fix: Exception test termination bug
1 parent 758176b commit d0a5879

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

math/iterative_factorial.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @file
3-
* @brief Iterative implementation of [Factorial](https://en.wikipedia.org/wiki/Factorial)
3+
* @brief Iterative implementation of
4+
* [Factorial](https://en.wikipedia.org/wiki/Factorial)
45
*
56
* @author [Renjian-buchai](https://github.com/Renjian-buchai)
67
*
@@ -25,6 +26,7 @@
2526
*/
2627

2728
#include <cassert> /// for assert
29+
#include <cstdint> /// for integral types
2830
#include <exception> /// for std::invalid_argument
2931
#include <iostream> /// for std::cout
3032

@@ -68,31 +70,31 @@ static void test() {
6870
// Special case test
6971
std::cout << "Exception case test \n"
7072
"Input: 0 \n"
71-
"Expected output: 1 \n";
73+
"Expected output: 1 \n\n";
7274
assert(math::iterativeFactorial(0) == 1);
7375

7476
// Base case
7577
std::cout << "Base case test \n"
7678
"Input: 1 \n"
77-
"Expected output: 1 \n";
79+
"Expected output: 1 \n\n";
7880
assert(math::iterativeFactorial(1) == 1);
7981

8082
// Small case
8183
std::cout << "Small number case test \n"
8284
"Input: 5 \n"
83-
"Expected output: 120 \n";
85+
"Expected output: 120 \n\n";
8486
assert(math::iterativeFactorial(5) == 120);
8587

8688
// Medium case
8789
std::cout << "Medium number case test \n"
8890
"Input: 10 \n"
89-
"Expected output: 3628800 \n";
91+
"Expected output: 3628800 \n\n";
9092
assert(math::iterativeFactorial(10) == 3628800);
9193

9294
// Maximum case
9395
std::cout << "Maximum case test \n"
9496
"Input: 20 \n"
95-
"Expected output: 2432902008176640000\n";
97+
"Expected output: 2432902008176640000\n\n";
9698
assert(math::iterativeFactorial(20) == 2432902008176640000);
9799

98100
// Exception test
@@ -101,9 +103,12 @@ static void test() {
101103
"Expected output: Exception thrown \n";
102104
try {
103105
math::iterativeFactorial(21);
104-
} catch (std::invalid_argument& e) {
105-
std::cout << "Exception thrown successfully \n";
106+
} catch (std::invalid_argument* e) {
107+
std::cout << "Exception thrown successfully \nContent: " << e->what()
108+
<< "\n\n";
106109
}
110+
111+
std::cout << "All tests success.\n";
107112
}
108113

109114
/**

0 commit comments

Comments
 (0)