RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
VariableLengthLoadTest.cpp
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2024 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
22#include "adt/Array1DRef.h"
23#include <algorithm>
24#include <numeric>
25#include <ostream>
26#include <vector>
27#include <gmock/gmock.h>
28#include <gtest/gtest.h>
29
30namespace rawspeed {
31
32template <typename T>
35 return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
36}
37
38template <typename T>
39static ::std::ostream& operator<<(::std::ostream& os, const Array1DRef<T>& r) {
40 os << "{";
41 for (int i = 0; i != r.size(); ++i) {
42 if (i != 0)
43 os << ", ";
44 os << r(i);
45 }
46 os << "}";
47 return os;
48}
49
50} // namespace rawspeed
51
53
54namespace rawpeed_test {
55
56TEST(VariableLengthLoadTest, Exhaustive) {
57 static constexpr int MaxBytes = 256;
58
59 for (int numInputBytes = 1; numInputBytes <= MaxBytes; ++numInputBytes) {
60 std::vector<unsigned char> inputStorage(numInputBytes);
61 auto input = Array1DRef(inputStorage.data(), numInputBytes);
62 std::iota(input.begin(), input.end(), 0);
63
64 for (int numOutputBytes = 1;
65 numOutputBytes <= numInputBytes && numOutputBytes <= 8;
66 numOutputBytes *= 2) {
67 for (int inPos = 0; inPos <= 4 * numInputBytes; ++inPos) {
68 std::vector<unsigned char> outputReferenceStorage(numOutputBytes);
69 auto outputReference =
70 Array1DRef(outputReferenceStorage.data(), numOutputBytes);
71 std::fill(outputReference.begin(), outputReference.end(), 0);
72 std::iota(outputReference.begin(),
73 outputReference.addressOf(
74 std::clamp(numInputBytes - inPos, 0, numOutputBytes)),
75 inPos);
76
77 std::vector<unsigned char> outputImpl0Storage(numOutputBytes);
78 auto outputImpl0 =
79 Array1DRef(outputImpl0Storage.data(), numOutputBytes);
80 variableLengthLoadNaiveViaMemcpy(
81 outputImpl0, Array1DRef<const unsigned char>(input), inPos);
82
83 EXPECT_THAT(outputImpl0, testing::ContainerEq(outputReference));
84
85 std::vector<unsigned char> outputImpl1Storage(numOutputBytes);
86 auto outputImpl1 =
87 Array1DRef(outputImpl1Storage.data(), numOutputBytes);
88 variableLengthLoadNaiveViaConditionalLoad(
89 outputImpl1, Array1DRef<const unsigned char>(input), inPos);
90
91 EXPECT_THAT(outputImpl1, testing::ContainerEq(outputReference));
92
93 std::vector<unsigned char> outputImpl2Storage(numOutputBytes);
94 auto outputImpl2 =
95 Array1DRef(outputImpl2Storage.data(), numOutputBytes);
96 variableLengthLoad(outputImpl2, Array1DRef<const unsigned char>(input),
97 inPos);
98
99 EXPECT_THAT(outputImpl2, testing::ContainerEq(outputReference));
100 }
101 }
102 }
103}
104
105} // namespace rawpeed_test
int RAWSPEED_READONLY size() const
TEST(VariableLengthLoadTest, Exhaustive)
bool operator==(const AlignedAllocator< T1, A1 > &, const AlignedAllocator< T2, A2 > &)
static inline ::std::ostream & operator<<(::std::ostream &os, const T &b)