Project
Loading...
Searching...
No Matches
test_ransSymbolTables.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#include <boost/test/unit_test.hpp>
21#include <boost/mpl/vector.hpp>
22#include <vector>
23#include <numeric>
24#include <iterator>
25
26#include "rANS/histogram.h"
31
32using namespace o2::rans;
33using namespace o2::rans::internal;
34
35template <typename T>
36size_t getNUniqueSymbols(const T& container)
37{
38 return std::count_if(container.begin(), container.end(), [](uint32_t value) { return value != 0; });
39};
40
41using histogram_t = boost::mpl::vector<DenseHistogram<uint8_t>,
51
53{
54 using namespace o2::rans;
55
56 using source_type = typename histogram_T::source_type;
57 std::vector<source_type> samples{};
58 histogram_T f{};
59 f.addSamples(gsl::make_span(samples));
60
61 auto makeSymbolTable = [](const histogram_T& hist) {
62 using source_type = typename histogram_T::source_type;
63 if constexpr (std::is_same_v<histogram_T, DenseHistogram<source_type>>) {
65 } else if constexpr (std::is_same_v<histogram_T, AdaptiveHistogram<source_type>>) {
67 } else {
68 static_assert(std::is_same_v<histogram_T, SparseHistogram<source_type>>);
70 }
71 };
72
73 const auto symbolTable = makeSymbolTable(f);
74
75 BOOST_CHECK_EQUAL(symbolTable.size(), 0);
77
78 const auto escapeSymbol = symbolTable.getEscapeSymbol();
79 BOOST_CHECK_EQUAL(escapeSymbol.getFrequency(), 1);
80 BOOST_CHECK_EQUAL(escapeSymbol.getCumulative(), 0);
81 BOOST_CHECK_EQUAL(symbolTable.isEscapeSymbol(escapeSymbol), true);
82
83 // test in range
84 for (auto iter = symbolTable.begin(); iter != symbolTable.end(); ++iter) {
85 BOOST_CHECK_EQUAL(symbolTable.isEscapeSymbol(internal::getValue(iter)), true);
86 }
87
88 // out of range checks:
89 const int outOfRangeSymbols[] = {-100, 100};
90 for (auto symbol : outOfRangeSymbols) {
91 const auto outOfRange = symbolTable[symbol];
92 BOOST_CHECK_EQUAL(symbolTable.isEscapeSymbol(symbol), true);
93 BOOST_CHECK_EQUAL(outOfRange.getFrequency(), escapeSymbol.getFrequency());
94 BOOST_CHECK_EQUAL(outOfRange.getCumulative(), escapeSymbol.getCumulative());
95 }
96}
97
98BOOST_AUTO_TEST_CASE_TEMPLATE(test_symbolTable, histogram_T, histogram_t)
99{
100 using source_type = typename histogram_T::source_type;
101
102 std::vector<uint32_t> frequencies{1, 1, 2, 2, 2, 2, 6, 8, 4, 10, 8, 14, 10, 19, 26, 30, 31, 35, 41, 45, 51, 44, 47, 39, 58, 52, 42, 53, 50, 34, 50, 30, 32, 24, 30, 20, 17, 12, 16, 6, 8, 5, 6, 4, 4, 2, 2, 2, 1};
103 histogram_T histogram{frequencies.begin(), frequencies.end(), static_cast<uint8_t>(0)};
104 const size_t scaleBits = 8;
105
106 auto makeSymbolTable = [](histogram_T&& hist, size_t scaleBits, RenormingPolicy renormingPolicy, size_t cutoff) {
107 using source_type = typename histogram_T::source_type;
108 if constexpr (std::is_same_v<histogram_T, DenseHistogram<source_type>>) {
109 return DenseSymbolTable<source_type, Symbol>(renorm(std::move(hist), scaleBits, renormingPolicy, cutoff));
110 } else if constexpr (std::is_same_v<histogram_T, AdaptiveHistogram<source_type>>) {
111 return AdaptiveSymbolTable<source_type, Symbol>(renorm(std::move(hist), scaleBits, renormingPolicy, cutoff));
112 } else {
113 static_assert(std::is_same_v<histogram_T, SparseHistogram<source_type>>);
114 return SparseSymbolTable<source_type, Symbol>(renorm(std::move(hist), scaleBits, renormingPolicy, cutoff));
115 }
116 };
117
118 const auto symbolTable = makeSymbolTable(std::move(histogram), scaleBits, RenormingPolicy::ForceIncompressible, 1);
119
120 const std::vector<uint32_t> rescaledFrequencies{1, 2, 1, 3, 2, 3, 3, 5, 6, 7, 8, 9, 10, 11, 13, 11, 12, 10, 14, 13, 10, 13, 12, 8, 12, 7, 8, 6, 7, 5, 4, 3, 4, 2, 2, 1, 2, 1, 1};
121 std::vector<uint32_t> cumulativeFrequencies;
122 std::exclusive_scan(rescaledFrequencies.begin(), rescaledFrequencies.end(), std::back_inserter(cumulativeFrequencies), 0);
123
124 BOOST_CHECK_EQUAL(symbolTable.getPrecision(), scaleBits);
125
126 for (source_type index = 0; index < static_cast<source_type>(rescaledFrequencies.size()); ++index) {
127 auto symbol = symbolTable[index + 6];
128 BOOST_CHECK_EQUAL(symbol.getFrequency(), rescaledFrequencies[index]);
129 BOOST_CHECK_EQUAL(symbol.getCumulative(), cumulativeFrequencies[index]);
130 }
131
132 const auto escapeSymbol = symbolTable.getEscapeSymbol();
133 BOOST_CHECK_EQUAL(escapeSymbol.getFrequency(), 4);
134 BOOST_CHECK_EQUAL(escapeSymbol.getCumulative(), cumulativeFrequencies.back() + rescaledFrequencies.back());
135
136 // out of range checks:
137 const int outOfRangeSymbols[] = {-100, 100};
138 for (auto symbol : outOfRangeSymbols) {
139 const auto escapeSymbol = symbolTable.getEscapeSymbol();
140 const auto outOfRange = symbolTable[symbol];
141 BOOST_CHECK_EQUAL(symbolTable.isEscapeSymbol(symbol), true);
142 BOOST_CHECK_EQUAL(outOfRange.getFrequency(), escapeSymbol.getFrequency());
143 BOOST_CHECK_EQUAL(outOfRange.getCumulative(), escapeSymbol.getCumulative());
144 }
145}
Lookup table containing statistical information for each symbol in the alphabet required for encoding...
Lookup table containing statistical information for each symbol in the alphabet required for encoding...
Contains statistical information for one source symbol, required for encoding/decoding.
uint32_t source_type
GLuint index
Definition glcorearb.h:781
GLdouble f
Definition glcorearb.h:310
GLsizei const GLfloat * value
Definition glcorearb.h:819
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)
auto getValue(IT iter) -> typename std::iterator_traits< IT >::value_type::second_type
Definition algorithm.h:35
size_t countNUsedAlphabetSymbols(const AdaptiveHistogram< source_T > &histogram)
decltype(auto) renorm(histogram_T histogram, size_t newPrecision, RenormingPolicy renormingPolicy=RenormingPolicy::Auto, size_t lowProbabilityCutoffBits=0)
Definition renorm.h:203
RenormingPolicy
Definition renorm.h:32
BOOST_CHECK_EQUAL(triggersD.size(), triggers.size())
size_t getNUniqueSymbols(const T &container)
boost::mpl::vector< DenseHistogram< uint8_t >, DenseHistogram< int8_t >, DenseHistogram< uint16_t >, DenseHistogram< int16_t >, DenseHistogram< uint32_t >, DenseHistogram< int32_t >, AdaptiveHistogram< uint32_t >, AdaptiveHistogram< int32_t >, SparseHistogram< uint32_t >, SparseHistogram< int32_t > > histogram_t
BOOST_AUTO_TEST_CASE_TEMPLATE(test_empty, histogram_T, histogram_t)