etl/test/test_histogram.cpp
Roland Reichwein 866c8a315e
Extensions for testing (#1380)
* Extensions for testing

Generalize run-tests.sh

Test all C++ versions at once

Fix combination of big endian and -Wsign-conversion

Failed on s390x (as reference for big endian)

Add github workflow for s390x

Add armhf container files

Devcontainers for i386 and riscv

Add github workflows for armhf, i386 and riscv64

Add run-tests.sh for foreign architectures

Document testing in doc/testing.md

Adjustments from clang-format run

Fix .devcontainer/s390x/Dockerfile for linebreak syntax

Fix exit code of run-test.sh

Previously, "exit $?" was used, actually the return value of
FailedCompilation and FailedTest which are always 0.

Now just using 1.

In run-tests.sh at ctest, use -V for printing number of tests unconditionally

While ctest suppresses individual test list by default, it didn't even
print the number of tests anymore, as run_tests.sh does because
it suppresses it output completely.

Now, by default print number of tests, and in verbose mode, print test list
in addition.

* Support powerpc as foreign architecture

* Add SFINAE constraints to etl::begin/end and reverse iterator free functions

The unconstrained etl::begin(), etl::end(), etl::cbegin(), etl::cend(),
etl::rbegin(), etl::rend(), etl::crbegin(), and etl::crend() templates
in the no-STL code path were matching iterator types during ADL, causing
a hard error with GCC 15's std::ranges::begin. When std::ranges performed
ADL on an etl::*::iterator, it found etl::begin() as a candidate; since
the iterator type has a nested iterator typedef, the return type TContainer::iterator
was valid, but calling .begin() on the iterator failed.

Fix: add etl::void_t<decltype(...)> SFINAE guards to each template,
ensuring they only participate in overload resolution when TContainer
actually has the corresponding member function (.begin(), .end(), etc.).

* - Fix red unit tests on 32 bits big-endian platform.

* Document powerpc architecture for testing

* Use Dockerfiles in cross testing github workflows

Synchronizes environment setup for github workflows to what is
defined in the development Dockerfiles. So they don't need to
be maintained separately.

---------

Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
Co-authored-by: Sergei Shirokov <sergej.shirokov@gmail.com>
Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
2026-04-15 10:47:52 +02:00

337 lines
12 KiB
C++

/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2021 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "unit_test_framework.h"
#include "etl/algorithm.h"
#include "etl/histogram.h"
#include <algorithm>
#include <array>
#include <map>
#include <string>
namespace
{
constexpr size_t Size = 10UL;
constexpr int Start = -4;
using IntRuntimeOffsetHistogram = etl::histogram<int32_t, int8_t, Size>;
using IntOffset0Histogram = etl::histogram<int32_t, int8_t, Size, 0>;
using IntOffsetminus4Histogram = etl::histogram<int32_t, int8_t, Size, Start>;
using StringHistogram = etl::sparse_histogram<std::string, int8_t, Size>;
//***********************************
std::array<int8_t, 55> input1 = {5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7,
7, 7, 2, 2, 2, 8, 8, 8, 8, 8, 8, 8, 8, 8, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0};
//***********************************
std::array<int8_t, 55> input2 = {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, 3, 3, 3, 3, 3, 3,
3, 3, -2, -2, -2, 4, 4, 4, 4, 4, 4, 4, 4, 4, -3, -3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, -4};
//***********************************
std::array<std::string, 55> input3 = {"5", "5", "5", "5", "5", "5", "4", "4", "4", "4", "4", "6", "6", "6", "6", "6", "6", "6", "3",
"3", "3", "3", "7", "7", "7", "7", "7", "7", "7", "7", "2", "2", "2", "8", "8", "8", "8", "8",
"8", "8", "8", "8", "1", "1", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "0"};
//***********************************
std::array<int8_t, Size> output1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//***********************************
std::array<ETL_OR_STD::pair<const std::string, int8_t>, Size> output2 = {
ETL_OR_STD::pair<const std::string, int8_t>("0", 1), ETL_OR_STD::pair<const std::string, int8_t>("1", 2),
ETL_OR_STD::pair<const std::string, int8_t>("2", 3), ETL_OR_STD::pair<const std::string, int8_t>("3", 4),
ETL_OR_STD::pair<const std::string, int8_t>("4", 5), ETL_OR_STD::pair<const std::string, int8_t>("5", 6),
ETL_OR_STD::pair<const std::string, int8_t>("6", 7), ETL_OR_STD::pair<const std::string, int8_t>("7", 8),
ETL_OR_STD::pair<const std::string, int8_t>("8", 9), ETL_OR_STD::pair<const std::string, int8_t>("9", 10),
};
//***********************************
std::array<int8_t, Size> zero1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
//***********************************
std::array<ETL_OR_STD::pair<const std::string, int8_t>, Size> zero2 = {
ETL_OR_STD::pair<const std::string, int8_t>("0", 0), ETL_OR_STD::pair<const std::string, int8_t>("1", 0),
ETL_OR_STD::pair<const std::string, int8_t>("2", 0), ETL_OR_STD::pair<const std::string, int8_t>("3", 0),
ETL_OR_STD::pair<const std::string, int8_t>("4", 0), ETL_OR_STD::pair<const std::string, int8_t>("5", 0),
ETL_OR_STD::pair<const std::string, int8_t>("6", 0), ETL_OR_STD::pair<const std::string, int8_t>("7", 0),
ETL_OR_STD::pair<const std::string, int8_t>("8", 0), ETL_OR_STD::pair<const std::string, int8_t>("9", 0),
};
SUITE(test_histogram)
{
//*************************************************************************
TEST(test_int_offset_0_histogram_constructor)
{
IntOffset0Histogram histogram;
CHECK_EQUAL(0U, histogram.count());
CHECK_EQUAL(Size, histogram.size());
bool isEqual = std::equal(zero1.begin(), zero1.end(), histogram.begin());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_int_offset_0_histogram)
{
IntOffset0Histogram histogram(input1.begin(), input1.end());
CHECK_EQUAL(Size, histogram.size());
bool isEqual;
isEqual = std::equal(zero1.begin(), zero1.end(), histogram.begin());
CHECK(!isEqual);
isEqual = std::equal(output1.begin(), output1.end(), histogram.begin());
CHECK(isEqual);
CHECK_EQUAL(55U, histogram.count());
histogram.clear();
isEqual = std::equal(zero1.begin(), zero1.end(), histogram.begin());
CHECK(isEqual);
CHECK_EQUAL(0U, histogram.count());
}
//*************************************************************************
TEST(test_int_functor1_offset_0_histogram)
{
IntOffset0Histogram histogram;
histogram = std::for_each(input1.begin(), input1.end(), histogram);
CHECK_EQUAL(Size, histogram.size());
bool isEqual;
isEqual = std::equal(zero1.begin(), zero1.end(), histogram.begin());
CHECK(!isEqual);
isEqual = std::equal(output1.begin(), output1.end(), histogram.begin());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_int_functor2_offset_0_histogram)
{
IntOffset0Histogram histogram;
histogram(input1.begin(), input1.end());
CHECK_EQUAL(Size, histogram.size());
bool isEqual;
isEqual = std::equal(zero1.begin(), zero1.end(), histogram.begin());
CHECK(!isEqual);
isEqual = std::equal(output1.begin(), output1.end(), histogram.begin());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_int_offset_0_histogram_indexed)
{
IntOffset0Histogram histogram(input1.begin(), input1.end());
CHECK_EQUAL(Size, histogram.size());
for (size_t i = 0UL; i < output1.size(); ++i)
{
CHECK_EQUAL(static_cast<int>(output1[i]), static_cast<int>(histogram[static_cast<int>(i)]));
}
}
//*************************************************************************
TEST(test_int_offset_minus_4_histogram)
{
IntOffsetminus4Histogram histogram(input2.begin(), input2.end());
CHECK_EQUAL(Size, histogram.size());
bool isEqual;
isEqual = std::equal(zero1.begin(), zero1.end(), histogram.begin());
CHECK(!isEqual);
isEqual = std::equal(output1.begin(), output1.end(), histogram.begin());
CHECK(isEqual);
CHECK_EQUAL(55U, histogram.count());
histogram.clear();
isEqual = std::equal(zero1.begin(), zero1.end(), histogram.begin());
CHECK(isEqual);
CHECK_EQUAL(0U, histogram.count());
}
//*************************************************************************
TEST(test_int_offset_minus_4_histogram_indexed)
{
IntOffsetminus4Histogram histogram(input2.begin(), input2.end());
CHECK_EQUAL(Size, histogram.size());
for (size_t i = 0UL; i < output1.size(); ++i)
{
CHECK_EQUAL(static_cast<int>(output1[i]), static_cast<int>(histogram[static_cast<int>(i) - 4]));
}
}
//*************************************************************************
TEST(test_int_runtime_offset_minus_4_histogram)
{
IntRuntimeOffsetHistogram histogram1(Start);
IntRuntimeOffsetHistogram histogram2(Start, input2.begin(), input2.end());
histogram1.add(input2.begin(), input2.end());
CHECK_EQUAL(Size, histogram1.size());
CHECK_EQUAL(Size, histogram2.size());
bool isEqual;
isEqual = std::equal(zero1.begin(), zero1.end(), histogram1.begin());
CHECK(!isEqual);
isEqual = std::equal(zero1.begin(), zero1.end(), histogram2.begin());
CHECK(!isEqual);
isEqual = std::equal(output1.begin(), output1.end(), histogram1.begin());
CHECK(isEqual);
isEqual = std::equal(output1.begin(), output1.end(), histogram2.begin());
CHECK(isEqual);
CHECK_EQUAL(55U, histogram1.count());
CHECK_EQUAL(55U, histogram2.count());
histogram1.clear();
isEqual = std::equal(zero1.begin(), zero1.end(), histogram1.begin());
CHECK(isEqual);
CHECK_EQUAL(0U, histogram1.count());
histogram2.clear();
isEqual = std::equal(zero1.begin(), zero1.end(), histogram2.begin());
CHECK(isEqual);
CHECK_EQUAL(0U, histogram2.count());
}
//*************************************************************************
TEST(test_string_histogram_constructor)
{
StringHistogram histogram;
CHECK_EQUAL(0U, histogram.size());
CHECK_EQUAL(0U, histogram.count());
}
//*************************************************************************
TEST(test_string_histogram)
{
StringHistogram histogram(input3.begin(), input3.end());
CHECK_EQUAL(Size, histogram.size());
CHECK_EQUAL(55U, histogram.count());
bool isEqual;
isEqual = std::equal(zero2.begin(), zero2.end(), histogram.begin());
CHECK(!isEqual);
auto o1 = *output2.begin();
auto h1 = *histogram.begin();
isEqual = std::equal(output2.begin(), output2.end(), histogram.begin());
CHECK(isEqual);
CHECK_EQUAL(55U, histogram.count());
histogram.clear();
CHECK(isEqual);
CHECK_EQUAL(0U, histogram.size());
CHECK_EQUAL(0U, histogram.count());
}
//*************************************************************************
TEST(test_string_histogram_unused_keys)
{
StringHistogram histogram(input3.begin(), input3.end());
CHECK(histogram["5"] == output2[5]);
StringHistogram::value_type unused = histogram[std::string("99")];
CHECK(unused.first == std::string());
CHECK(unused.second == 0U);
}
//*************************************************************************
TEST(test_string_functor1_histogram)
{
StringHistogram histogram;
histogram = std::for_each(input3.begin(), input3.end(), histogram);
CHECK_EQUAL(Size, histogram.size());
bool isEqual;
isEqual = std::equal(zero2.begin(), zero2.end(), histogram.begin());
CHECK(!isEqual);
isEqual = std::equal(output2.begin(), output2.end(), histogram.begin());
CHECK(isEqual);
}
//*************************************************************************
TEST(test_string_functor2_histogram)
{
StringHistogram histogram;
histogram(input3.begin(), input3.end());
CHECK_EQUAL(Size, histogram.size());
bool isEqual;
isEqual = std::equal(zero2.begin(), zero2.end(), histogram.begin());
CHECK(!isEqual);
isEqual = std::equal(output2.begin(), output2.end(), histogram.begin());
CHECK(isEqual);
}
}
} // namespace