Skip to content

Commit 33750ec

Browse files
authored
fix: cover the cases n == 0 and m < n in N_bonacci (TheAlgorithms#2468)
* fix: make N_bonacci return an array of size m * tests: simplify test, add new test cases * style: remove unused include, update include justifications * fix: cover the case n == 0
1 parent 0934ec4 commit 33750ec

File tree

1 file changed

+37
-53
lines changed

1 file changed

+37
-53
lines changed

math/n_bonacci.cpp

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
* @author [Swastika Gupta](https://github.com/Swastyy)
1616
*/
1717

18-
#include <algorithm> /// for std::is_equal, std::swap
19-
#include <cassert> /// for assert
20-
#include <iostream> /// for IO operations
21-
#include <vector> /// for std::vector
18+
#include <cassert> /// for assert
19+
#include <iostream> /// for std::cout
20+
#include <vector> /// for std::vector
2221

2322
/**
2423
* @namespace math
@@ -39,10 +38,17 @@ namespace n_bonacci {
3938
* @returns the n-bonacci sequence as vector array
4039
*/
4140
std::vector<uint64_t> N_bonacci(const uint64_t &n, const uint64_t &m) {
42-
std::vector<uint64_t> a(m, 0); // we create an empty array of size m
41+
std::vector<uint64_t> a(
42+
m, 0); // we create an array of size m filled with zeros
43+
if (m < n || n == 0) {
44+
return a;
45+
}
4346

4447
a[n - 1] = 1; /// we initialise the (n-1)th term as 1 which is the sum of
4548
/// preceding N zeros
49+
if (n == m) {
50+
return a;
51+
}
4652
a[n] = 1; /// similarily the sum of preceding N zeros and the (N+1)th 1 is
4753
/// also 1
4854
for (uint64_t i = n + 1; i < m; i++) {
@@ -61,55 +67,33 @@ std::vector<uint64_t> N_bonacci(const uint64_t &n, const uint64_t &m) {
6167
* @returns void
6268
*/
6369
static void test() {
64-
// n = 1 m = 1 return [1, 1]
65-
std::cout << "1st test";
66-
std::vector<uint64_t> arr1 = math::n_bonacci::N_bonacci(
67-
1, 1); // first input is the param n and second one is the param m for
68-
// N-bonacci func
69-
std::vector<uint64_t> output_array1 = {
70-
1, 1}; // It is the expected output series of length m
71-
assert(std::equal(std::begin(arr1), std::end(arr1),
72-
std::begin(output_array1)));
73-
std::cout << "passed" << std::endl;
74-
75-
// n = 5 m = 15 return [0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236,
76-
// 464]
77-
std::cout << "2nd test";
78-
std::vector<uint64_t> arr2 = math::n_bonacci::N_bonacci(
79-
5, 15); // first input is the param n and second one is the param m for
80-
// N-bonacci func
81-
std::vector<uint64_t> output_array2 = {
82-
0, 0, 0, 0, 1, 1, 2, 4,
83-
8, 16, 31, 61, 120, 236, 464}; // It is the expected output series of
84-
// length m
85-
assert(std::equal(std::begin(arr2), std::end(arr2),
86-
std::begin(output_array2)));
87-
std::cout << "passed" << std::endl;
88-
89-
// n = 6 m = 17 return [0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248,
90-
// 492, 976]
91-
std::cout << "3rd test";
92-
std::vector<uint64_t> arr3 = math::n_bonacci::N_bonacci(
93-
6, 17); // first input is the param n and second one is the param m for
94-
// N-bonacci func
95-
std::vector<uint64_t> output_array3 = {
96-
0, 0, 0, 0, 0, 1, 1, 2, 4,
97-
8, 16, 32, 63, 125, 248, 492, 976}; // It is the expected output series
98-
// of length m
99-
assert(std::equal(std::begin(arr3), std::end(arr3),
100-
std::begin(output_array3)));
101-
std::cout << "passed" << std::endl;
70+
struct TestCase {
71+
const uint64_t n;
72+
const uint64_t m;
73+
const std::vector<uint64_t> expected;
74+
TestCase(const uint64_t in_n, const uint64_t in_m,
75+
std::initializer_list<uint64_t> data)
76+
: n(in_n), m(in_m), expected(data) {
77+
assert(data.size() == m);
78+
}
79+
};
80+
const std::vector<TestCase> test_cases = {
81+
TestCase(0, 0, {}),
82+
TestCase(0, 1, {0}),
83+
TestCase(0, 2, {0, 0}),
84+
TestCase(1, 0, {}),
85+
TestCase(1, 1, {1}),
86+
TestCase(1, 2, {1, 1}),
87+
TestCase(1, 3, {1, 1, 1}),
88+
TestCase(5, 15, {0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, 464}),
89+
TestCase(
90+
6, 17,
91+
{0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, 492, 976}),
92+
TestCase(56, 15, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})};
10293

103-
// n = 56 m = 15 return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
104-
std::cout << "4th test";
105-
std::vector<uint64_t> arr4 = math::n_bonacci::N_bonacci(
106-
56, 15); // first input is the param n and second one is the param m
107-
// for N-bonacci func
108-
std::vector<uint64_t> output_array4 = {
109-
0, 0, 0, 0, 0, 0, 0, 0,
110-
0, 0, 0, 0, 0, 0, 0}; // It is the expected output series of length m
111-
assert(std::equal(std::begin(arr4), std::end(arr4),
112-
std::begin(output_array4)));
94+
for (const auto &tc : test_cases) {
95+
assert(math::n_bonacci::N_bonacci(tc.n, tc.m) == tc.expected);
96+
}
11397
std::cout << "passed" << std::endl;
11498
}
11599

0 commit comments

Comments
 (0)