This is a follow-up question for A non-nested test_vectors_generator Function for arithmetic_mean Function Testing in C++ and An arithmetic_mean Function For Various Type Arbitrary Nested Iterable Implementation in C++. Besides std::vector
test cases generated from test_vectors_generator
template function, I am trying to implement std::deque
and std::list
test cases with test_deques_generator
and test_lists_generator
functions.
The usage description
Similar to the usage of test_vectors_generator
, there are also four parameters in both test_deques_generator
function and test_lists_generator
function. The first one is a start iteration number of each element, the second one is a end iteration number of each element, the third one is a step size and the fourth one is the element count of each container.
The experimental implementation
The experimental implementation of test_deques_generator
and test_lists_generator
functions are as below.
namespace ts
{
//...
template<class T> requires (!is_iterable<T>)
constexpr auto test_deques_generator(T start, T end, T step, std::size_t element_count)
{
if (element_count == 1)
{
std::list<std::deque<T>> output(((end - start) / step) + 1);
T i = 0; // incrementor
std::for_each(output.begin(), output.end(), [&](auto& item) { i+=step; item = std::deque<T>{ i }; });
return output;
}
else
{
std::list<std::deque<T>> output{};
auto test_deques = test_deques_generator(start, end, step, element_count - 1);
std::for_each(test_deques.begin(), test_deques.end(), [&](const auto item) {
for (T i = start; i <= end; i += step)
{
auto new_element = item;
new_element.push_back(i);
output.push_back(new_element);
}
});
return output;
}
}
template<class T> requires (!is_iterable<T>)
constexpr auto test_lists_generator(T start, T end, T step, std::size_t element_count)
{
if (element_count == 1)
{
std::list<std::list<T>> output(((end - start) / step) + 1);
T i = 0; // incrementor
std::for_each(output.begin(), output.end(), [&](auto& item) { i+=step; item = std::list<T>{ i }; });
return output;
}
else
{
std::list<std::list<T>> output{};
auto test_deques = test_lists_generator(start, end, step, element_count - 1);
std::for_each(test_deques.begin(), test_deques.end(), [&](const auto item) {
for (T i = start; i <= end; i += step)
{
auto new_element = item;
new_element.push_back(i);
output.push_back(new_element);
}
});
return output;
}
}
}
The used is_iterable
concept:
template<typename T>
concept is_iterable = requires(T x)
{
*std::begin(x);
std::end(x);
};
Test cases
- Test cases of
test_deques_generator
andtest_lists_generator
template functions
The recursive_print
template function (refer to the previous question A recursive_print Function For Various Type Arbitrary Nested Iterable Implementation in C++) is also used here. The output of recursive_print(ts::test_deques_generator(1, 3, 1, 3));
is as follows.
Level 0:
Level 1:
1
1
1
Level 1:
1
1
2
Level 1:
1
1
3
Level 1:
1
2
1
Level 1:
1
2
2
Level 1:
1
2
3
Level 1:
1
3
1
Level 1:
1
3
2
Level 1:
1
3
3
Level 1:
2
1
1
Level 1:
2
1
2
Level 1:
2
1
3
Level 1:
2
2
1
Level 1:
2
2
2
Level 1:
2
2
3
Level 1:
2
3
1
Level 1:
2
3
2
Level 1:
2
3
3
Level 1:
3
1
1
Level 1:
3
1
2
Level 1:
3
1
3
Level 1:
3
2
1
Level 1:
3
2
2
Level 1:
3
2
3
Level 1:
3
3
1
Level 1:
3
3
2
Level 1:
3
3
3
The output of recursive_print(ts::test_lists_generator(1, 3, 1, 3));
is as follows.
Level 0:
Level 1:
1
1
1
Level 1:
1
1
2
Level 1:
1
1
3
Level 1:
1
2
1
Level 1:
1
2
2
Level 1:
1
2
3
Level 1:
1
3
1
Level 1:
1
3
2
Level 1:
1
3
3
Level 1:
2
1
1
Level 1:
2
1
2
Level 1:
2
1
3
Level 1:
2
2
1
Level 1:
2
2
2
Level 1:
2
2
3
Level 1:
2
3
1
Level 1:
2
3
2
Level 1:
2
3
3
Level 1:
3
1
1
Level 1:
3
1
2
Level 1:
3
1
3
Level 1:
3
2
1
Level 1:
3
2
2
Level 1:
3
2
3
Level 1:
3
3
1
Level 1:
3
3
2
Level 1:
3
3
3
- Test cases for
arithmetic_mean
template function
With Boost.Test tool, the std::deque
and std::list
test cases for arithmetic_mean
template function can be implemented as the following code.
BOOST_AUTO_TEST_CASE(test_deques_generator_char)
{
typedef char TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_deques_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_deques_generator_int)
{
typedef int TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_deques_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_deques_generator_short)
{
typedef short TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_deques_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_deques_generator_long)
{
typedef long TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_deques_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_deques_generator_long_long_int)
{
typedef long long int TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_deques_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_deques_generator_unsigned_char)
{
typedef unsigned char TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_deques_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_deques_generator_float)
{
typedef float TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_deques_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_deques_generator_double)
{
typedef double TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_deques_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_deques_generator_long_double)
{
typedef long double TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_deques_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_lists_generator_char)
{
typedef char TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_lists_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_lists_generator_int)
{
typedef int TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_lists_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_lists_generator_short)
{
typedef short TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_lists_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_lists_generator_long)
{
typedef long TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_lists_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_lists_generator_long_long_int)
{
typedef long long int TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_lists_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_lists_generator_unsigned_char)
{
typedef unsigned char TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_lists_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_lists_generator_float)
{
typedef float TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_lists_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_lists_generator_double)
{
typedef double TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_lists_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
BOOST_AUTO_TEST_CASE(test_lists_generator_long_double)
{
typedef long double TestType;
TestType start_num = 1;
TestType end_num = 50;
TestType step_num = 1;
for (auto& each_test_vector : ts::test_lists_generator(start_num, end_num, step_num, 3))
{
// Generate expected_value
double expected_value = 0;
for (auto& each_item : each_test_vector)
{
expected_value += each_item;
}
expected_value = expected_value / static_cast<double>(each_test_vector.size());
BOOST_TEST(expected_value == arithmetic_mean(each_test_vector));
}
BOOST_TEST(true);
}
Note: The compiling output from Godbolt is <Compilation failed>
and the error message is Killed - processing time exceeded
. This seems to be caused by Boost.Test tool is too large.
All suggestions are welcome.
The summary information:
Which question it is a follow-up to?
A non-nested test_vectors_generator Function for arithmetic_mean Function Testing in C++ and An arithmetic_mean Function For Various Type Arbitrary Nested Iterable Implementation in C++
What changes has been made in the code since last question?
The
std::deque
test cases andstd::list
test cases forarithmetic_mean
template function have been added.Why a new review is being asked for?
I am trying to verify the correctness of
arithmetic_mean
template function as complete as possible, including various container, various element type. If there is any suggestion or possible improvement of these test cases, please let me know.