Add tests for accumulate

This commit is contained in:
Roland Reichwein 2026-03-08 21:57:53 +01:00
parent 22e7099bf4
commit 12d85c36dc

View File

@ -2959,6 +2959,116 @@ namespace
}
}
//*************************************************************************
TEST(accumulate_default)
{
int data[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int expected = std::accumulate(std::begin(data), std::end(data), 0);
int result = etl::accumulate(std::begin(data), std::end(data), 0);
CHECK_EQUAL(expected, result);
}
//*************************************************************************
TEST(accumulate_with_initial_value)
{
int data[] = { 1, 2, 3, 4, 5 };
int expected = std::accumulate(std::begin(data), std::end(data), 100);
int result = etl::accumulate(std::begin(data), std::end(data), 100);
CHECK_EQUAL(expected, result);
}
//*************************************************************************
TEST(accumulate_custom_operation)
{
int data[] = { 1, 2, 3, 4, 5 };
int expected = std::accumulate(std::begin(data), std::end(data), 1, std::multiplies<int>());
int result = etl::accumulate(std::begin(data), std::end(data), 1, std::multiplies<int>());
CHECK_EQUAL(expected, result);
}
//*************************************************************************
TEST(accumulate_empty_range)
{
int data[] = { 1 };
int expected = std::accumulate(std::begin(data), std::begin(data), 42);
int result = etl::accumulate(std::begin(data), std::begin(data), 42);
CHECK_EQUAL(expected, result);
}
//*************************************************************************
TEST(accumulate_single_element)
{
int data[] = { 7 };
int expected = std::accumulate(std::begin(data), std::end(data), 0);
int result = etl::accumulate(std::begin(data), std::end(data), 0);
CHECK_EQUAL(expected, result);
}
//*************************************************************************
TEST(accumulate_negative_values)
{
int data[] = { -3, -2, -1, 0, 1, 2, 3 };
int expected = std::accumulate(std::begin(data), std::end(data), 0);
int result = etl::accumulate(std::begin(data), std::end(data), 0);
CHECK_EQUAL(expected, result);
}
//*************************************************************************
TEST(accumulate_custom_operation_subtraction)
{
int data[] = { 1, 2, 3, 4, 5 };
int expected = std::accumulate(std::begin(data), std::end(data), 100, std::minus<int>());
int result = etl::accumulate(std::begin(data), std::end(data), 100, std::minus<int>());
CHECK_EQUAL(expected, result);
}
//*************************************************************************
TEST(accumulate_non_random_iterator)
{
List data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int expected = std::accumulate(data.begin(), data.end(), 0);
int result = etl::accumulate(data.begin(), data.end(), 0);
CHECK_EQUAL(expected, result);
}
//*************************************************************************
TEST(accumulate_non_random_iterator_custom_operation)
{
List data = { 1, 2, 3, 4, 5 };
int expected = std::accumulate(data.begin(), data.end(), 1, std::multiplies<int>());
int result = etl::accumulate(data.begin(), data.end(), 1, std::multiplies<int>());
CHECK_EQUAL(expected, result);
}
//*************************************************************************
TEST(accumulate_double)
{
double data[] = { 1.5, 2.5, 3.5, 4.5, 5.5 };
double expected = std::accumulate(std::begin(data), std::end(data), 0.0);
double result = etl::accumulate(std::begin(data), std::end(data), 0.0);
CHECK_CLOSE(expected, result, 1e-10);
}
//*************************************************************************
TEST(clamp_run_time)
{