Project
Loading...
Searching...
No Matches
test_ransEncodeDecode.cxx
Go to the documentation of this file.
1// Copyright 2019-2023 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#define BOOST_TEST_MODULE Utility test
17#define BOOST_TEST_MAIN
18#define BOOST_TEST_DYN_LINK
19
20#undef NDEBUG
21#include <cassert>
22
23#include <vector>
24#include <cstring>
25
26#include <boost/test/unit_test.hpp>
27#include <boost/mp11.hpp>
28
29#include <gsl/span>
30
31#include "rANS/factory.h"
32#include "rANS/histogram.h"
33#include "rANS/encode.h"
34
35using namespace o2::rans;
36
37inline const std::string str = R"(Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium
38doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis
39et quasi architecto beatae vitae dicta sunt, explicabo. nemo enim ipsam voluptatem,
40quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores
41eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum,
42quia dolor sit, amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
43incidunt, ut labore et dolore magnam aliquam quaerat voluptatem. ut enim ad minima veniam,
44quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea
45commodi consequatur? quis autem vel eum iure reprehenderit, qui in ea voluptate velit
46esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat,
47quo voluptas nulla pariatur?)";
48
49template <typename T>
50struct Empty {
51 inline static std::vector<T> Data{};
52
53 using source_type = T;
54 using iterator_type = decltype(Data.begin());
55};
56
57template <typename T>
58struct Full {
59
60 public:
61 inline static std::vector<T> Data{str.begin(), str.end()};
62
63 using source_type = T;
64 using iterator_type = decltype(Data.begin());
65};
66
67template <typename L>
68struct hasSameTemplateParam : std::is_same<typename boost::mp11::mp_at_c<L, 0>::source_type, typename boost::mp11::mp_at_c<L, 1>::source_type> {
69};
70
71using source_types = boost::mp11::mp_list<int8_t, int16_t, int32_t>;
72
73using testInput_templates = boost::mp11::mp_list<boost::mp11::mp_quote<Empty>, boost::mp11::mp_quote<Full>>;
74
75using testInputAll_types = boost::mp11::mp_product<boost::mp11::mp_invoke_q, testInput_templates, source_types>;
76using testInputProduct_types = boost::mp11::mp_product<boost::mp11::mp_list, testInputAll_types, testInputAll_types>;
77using testInput_types = boost::mp11::mp_copy_if<testInputProduct_types, hasSameTemplateParam>;
78
79using coder_types = boost::mp11::mp_list<std::integral_constant<CoderTag, CoderTag::Compat>
80#ifdef RANS_SINGLE_STREAM
81 ,
82 std::integral_constant<CoderTag, CoderTag::SingleStream>
83#endif /* RANS_SINGLE_STREAM */
84#ifdef RANS_SEE
85 ,
86 std::integral_constant<CoderTag, CoderTag::SSE>
87#endif /*RANS_SSE */
88#ifdef RANS_AVX2
89 ,
90 std::integral_constant<CoderTag, CoderTag::AVX2>
91#endif /* RANS_AVX2 */
92 >;
93
94using testCase_types = boost::mp11::mp_product<boost::mp11::mp_list, coder_types, testInput_types>;
95
96inline constexpr size_t RansRenormingPrecision = 16;
97
99{
100 using coder_type = boost::mp11::mp_at_c<test_types, 0>;
101 using testCase_types = boost::mp11::mp_at_c<test_types, 1>;
102 using dictString_type = boost::mp11::mp_at_c<testCase_types, 0>;
103 using encodeString_type = boost::mp11::mp_at_c<testCase_types, 1>;
104 using stream_type = uint32_t;
105 using source_type = typename dictString_type::source_type;
106
107 constexpr CoderTag coderTag = coder_type::value;
108 const auto& dictString = dictString_type::Data;
109 const auto& encodeString = encodeString_type::Data;
110
111 // TODO(milettri): renorming is not satisfactory.
112 size_t precision = dictString.size() == 0 ? 0 : RansRenormingPrecision;
113 auto renormed = renorm(makeDenseHistogram::fromSamples(dictString.begin(), dictString.end()), precision);
114 auto encoder = makeDenseEncoder<coderTag>::fromRenormed(renormed);
115 auto decoder = makeDecoder<>::fromRenormed(renormed);
116
117 if (dictString == encodeString) {
118 std::vector<stream_type> encodeBuffer(encodeString.size());
119 auto encodeBufferEnd = encoder.process(encodeString.begin(), encodeString.end(), encodeBuffer.begin());
120 std::vector<stream_type> encodeBuffer2(encodeString.size());
121 auto encodeBuffer2End = encoder.process(gsl::span<const source_type>(encodeString), gsl::make_span(encodeBuffer2));
122
123 BOOST_CHECK_EQUAL_COLLECTIONS(encodeBuffer.begin(), encodeBufferEnd, encodeBuffer2.data(), encodeBuffer2End);
124
125 std::vector<source_type> decodeBuffer(encodeString.size());
126 decoder.process(encodeBufferEnd, decodeBuffer.begin(), encodeString.size(), encoder.getNStreams());
127
128 BOOST_CHECK_EQUAL_COLLECTIONS(decodeBuffer.begin(), decodeBuffer.end(), encodeString.begin(), encodeString.end());
129 }
130
131 std::vector<source_type> literals(encodeString.size());
132 std::vector<stream_type> encodeBuffer(encodeString.size());
133 auto [encodeBufferEnd, literalBufferEnd] = encoder.process(encodeString.begin(), encodeString.end(), encodeBuffer.begin(), literals.begin());
134 std::vector<stream_type> encodeBuffer2(encodeString.size());
135 std::vector<source_type> literals2(encodeString.size());
136 auto [encodeBuffer2End, literalBuffer2End] = encoder.process(gsl::span<const source_type>(encodeString), gsl::make_span(encodeBuffer2), literals2.begin());
137
138 BOOST_CHECK_EQUAL_COLLECTIONS(encodeBuffer.begin(), encodeBufferEnd, encodeBuffer2.data(), encodeBuffer2End);
139 BOOST_CHECK_EQUAL_COLLECTIONS(literals.begin(), literalBufferEnd, literals2.begin(), literalBuffer2End);
140
141 std::vector<source_type> decodeBuffer(encodeString.size());
142 decoder.process(encodeBufferEnd, decodeBuffer.begin(), encodeString.size(), encoder.getNStreams(), literalBufferEnd);
143
144 BOOST_CHECK_EQUAL_COLLECTIONS(decodeBuffer.begin(), decodeBuffer.end(), encodeString.begin(), encodeString.end());
145};
146
147#ifndef RANS_SINGLE_STREAM
148BOOST_AUTO_TEST_CASE(test_NoSingleStream)
149{
150 BOOST_TEST_WARN(" Tests were not Compiled for a uint128_t capable CPU, cannot run all tests");
151}
152#endif /* RANS_SINGLE_STREAM */
153#ifndef RANS_SSE
155{
156 BOOST_TEST_WARN("Tests were not Compiled for a SSE 4.2 capable CPU, cannot run all tests");
157}
158#endif /* RANS_SSE */
159#ifndef RANS_AVX2
161{
162 BOOST_TEST_WARN("Tests were not Compiled for a AVX2 capable CPU, cannot run all tests");
163}
164#endif /* RANS_AVX2 */
uint32_t source_type
boost::mp11::mp_list< std::integral_constant< CoderTag, CoderTag::Compat > > coder_types
uint32_t stream_type
static constexpr decltype(auto) fromRenormed(const RenormedHistogramConcept< container_T > &renormed)
Definition factory.h:106
static constexpr decltype(auto) fromRenormed(const RenormedDenseHistogram< source_T > &renormed)
Definition factory.h:195
public interface for encoding.
static factory classes for building histograms, encoders and decoders.
GLenum GLint GLint * precision
Definition glcorearb.h:1899
public interface for building and renorming histograms from source data.
auto make_span(const o2::rans::internal::simd::AlignedArray< T, width_V, size_V > &array)
decltype(auto) renorm(histogram_T histogram, size_t newPrecision, RenormingPolicy renormingPolicy=RenormingPolicy::Auto, size_t lowProbabilityCutoffBits=0)
Definition renorm.h:203
decltype(Data.begin()) iterator_type
static std::vector< T > Data
decltype(Data.begin()) iterator_type
static std::vector< T > Data
static decltype(auto) fromSamples(source_IT begin, source_IT end, typename std::iterator_traits< source_IT >::value_type min, typename std::iterator_traits< source_IT >::value_type max)
Definition factory.h:144
boost::mp11::mp_list< uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t > source_types
std::string decodeBuffer(int feeId, gsl::span< const std::byte > buffer)
boost::mp11::mp_product< boost::mp11::mp_list, coder_types, testInput_types > testCase_types
const std::string str
boost::mp11::mp_copy_if< testInputProduct_types, hasSameTemplateParam > testInput_types
BOOST_AUTO_TEST_CASE_TEMPLATE(test_encodeDecode, test_types, testCase_types)
boost::mp11::mp_product< boost::mp11::mp_list, testInputAll_types, testInputAll_types > testInputProduct_types
BOOST_AUTO_TEST_CASE(test_NoSingleStream)
boost::mp11::mp_list< boost::mp11::mp_quote< Empty >, boost::mp11::mp_quote< Full > > testInput_templates
boost::mp11::mp_product< boost::mp11::mp_invoke_q, testInput_templates, source_types > testInputAll_types
constexpr size_t RansRenormingPrecision
boost::mp11::mp_product< boost::mp11::mp_list, source_types, buffer_types > test_types