RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
HuffmanTableTest.cpp
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2018 Roman Lebedev
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; withexpected even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#include "adt/Array1DRef.h"
24#include "codes/HuffmanCode.h"
26#include "io/Buffer.h"
27#include "io/ByteStream.h"
28#include "io/Endianness.h"
29#include <algorithm>
30#include <array>
31#include <cstdint>
32#include <initializer_list>
33#include <utility>
34#include <vector>
35#include <gtest/gtest.h>
36
37namespace rawspeed {
39}
40
47
48namespace rawspeed_test {
49
50namespace {
51
53 [](std::initializer_list<uint8_t>&& nCodesPerLength,
54 std::initializer_list<uint8_t>&& codeValues) -> PrefixCodeDecoder<> {
56
57 std::vector<uint8_t> lv(nCodesPerLength.begin(), nCodesPerLength.end());
58 lv.resize(16);
59 Buffer lb(lv.data(), lv.size());
60 hc.setNCodesPerLength(lb);
61
62 std::vector<uint8_t> cv(codeValues.begin(), codeValues.end());
63 rawspeed::Array1DRef<uint8_t> cb(cv.data(), cv.size());
64 hc.setCodeValues(cb);
65
67 PrefixCodeDecoder<> ht(std::move(code));
68 return ht;
69};
70
71TEST(PrefixCodeDecoderTest, decodeCodeValueIdentityTest) {
72 static const std::array<uint8_t, 4> data{
73 {0b01010101, 0b01010101, 0b01010101, 0b01010101}};
74 const Buffer b(data.data(), data.size());
75 const DataBuffer db(b, Endianness::little);
76 const ByteStream bs(db);
77
78 BitStreamerMSB p(bs);
79
80 auto ht = genHTFull({2}, {4, 8});
81 ht.setup(false, false);
82
83 for (int i = 0; i < 32; i += 2) {
84 ASSERT_EQ(ht.decodeCodeValue(p), 4);
85 ASSERT_EQ(ht.decodeCodeValue(p), 8);
86 }
87}
88
89TEST(PrefixCodeDecoderTest, decodeDifferenceIdentityTest) {
90 static const std::array<uint8_t, 4> data{
91 {0b00000000, 0b11010101, 0b01010101, 0b01111111}};
92 const Buffer b(data.data(), data.size());
93 const DataBuffer db(b, Endianness::little);
94 const ByteStream bs(db);
95
96 BitStreamerMSB p(bs);
97
98 auto ht = genHTFull({2}, {7, 7 + 8});
99 ht.setup(true, false);
100
101 ASSERT_EQ(ht.decodeDifference(p), -127);
102 ASSERT_EQ(ht.decodeDifference(p), 21845);
103 ASSERT_EQ(ht.decodeDifference(p), 127);
104}
105
106TEST(PrefixCodeDecoderTest, decodeCodeValueBadCodeTest) {
107 static const std::array<uint8_t, 4> data{{0b01000000}};
108 const Buffer b(data.data(), data.size());
109 const DataBuffer db(b, Endianness::little);
110 const ByteStream bs(db);
111
112 BitStreamerMSB p(bs);
113
114 auto ht = genHTFull({1}, {4});
115 ht.setup(false, false);
116
117 ASSERT_EQ(ht.decodeCodeValue(p), 4);
118 ASSERT_THROW(ht.decodeCodeValue(p), rawspeed::RawDecoderException);
119}
120
121TEST(PrefixCodeDecoderTest, decodeDifferenceBadCodeTest) {
122 static const std::array<uint8_t, 4> data{{0b00100000}};
123 const Buffer b(data.data(), data.size());
124 const DataBuffer db(b, Endianness::little);
125 const ByteStream bs(db);
126
127 BitStreamerMSB p(bs);
128
129 auto ht = genHTFull({1}, {1});
130 ht.setup(true, false);
131
132 ASSERT_EQ(ht.decodeDifference(p), -1);
133 ASSERT_THROW(ht.decodeDifference(p), rawspeed::RawDecoderException);
134}
135
136} // namespace
137
138} // namespace rawspeed_test
uint32_t setNCodesPerLength(Buffer data)
Definition HuffmanCode.h:99
void setCodeValues(Array1DRef< const typename Traits::CodeValueTy > data)
TEST(PrefixCodeDecoderTest, decodeCodeValueIdentityTest)
PrefixCodeLUTDecoder< CodeTag, PrefixCodeLookupDecoder< CodeTag > > PrefixCodeDecoder