RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
CoalescingOutputIteratorTest.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 "adt/Casts.h"
24#include "common/Common.h"
25#include <algorithm>
26#include <concepts>
27#include <cstddef>
28#include <cstdint>
29#include <iterator>
30#include <numeric>
31#include <ostream>
32#include <vector>
33#include <gmock/gmock.h>
34#include <gtest/gtest.h>
35
36namespace rawspeed {
37
38template <typename T>
39static bool operator==(const Array1DRef<T> a, const Array1DRef<T> b) {
40 return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
41}
42
43template <typename T>
44 requires std::same_as<T, std::byte>
45static inline ::std::ostream& operator<<(::std::ostream& os, const T& b) {
46 os << std::to_integer<uint8_t>(b);
47 return os;
48}
49
50template <typename T>
51static ::std::ostream& operator<<(::std::ostream& os, const Array1DRef<T>& r) {
52 os << "{";
53 for (int i = 0; i != r.size(); ++i) {
54 if (i != 0)
55 os << ", ";
56 os << r(i);
57 }
58 os << "}";
59 return os;
60}
61
62namespace rawpeed_test {
63
64namespace {
65
66template <typename CoalescedType, typename PartType>
68 std::vector<CoalescedType> outputStorage;
69 {
70 outputStorage.reserve(implicit_cast<size_t>(roundUpDivisionSafe(
71 sizeof(PartType) * input.size(), sizeof(CoalescedType))));
72 auto subIter = std::back_inserter(outputStorage);
74 static_assert(std::output_iterator<decltype(iter), PartType>);
75 for (const auto& e : input)
76 *iter = e;
77 }
78 return outputStorage;
79}
80
81template <typename T, typename U> struct TypeSpec {
83 using PartType = U;
84};
85
86template <typename T>
87class CoalescingOutputIteratorTest : public testing::Test {};
88
90
92 static constexpr int MaxBytes = 256;
93
94 for (int numInputBytes = 1; numInputBytes <= MaxBytes; ++numInputBytes) {
95 std::vector<uint8_t> inputStorage(numInputBytes);
96 auto input = Array1DRef(inputStorage.data(), numInputBytes);
97 std::iota(input.begin(), input.end(), 0);
98
99 std::vector<typename TypeParam::PartType> intermediateStorage =
101 auto intermediate =
102 Array1DRef(intermediateStorage.data(),
103 implicit_cast<int>(intermediateStorage.size()));
104
105 auto intermediateBytes = Array1DRef<std::byte>(intermediate);
106
107 ASSERT_THAT(intermediateBytes, testing::SizeIs(testing::Ge(input.size())));
108 intermediateBytes =
109 intermediateBytes.getBlock(input.size(), /*index=*/0).getAsArray1DRef();
110 ASSERT_THAT(intermediateBytes,
111 testing::ContainerEq(Array1DRef<std::byte>(input)));
112
113 std::vector<typename TypeParam::CoalescedType> outputStorage =
114 coalesceElts<typename TypeParam::CoalescedType,
115 typename TypeParam::PartType>(intermediate);
116
117 auto output = Array1DRef<std::byte>(Array1DRef(
118 outputStorage.data(), implicit_cast<int>(outputStorage.size())));
119 ASSERT_THAT(output, testing::SizeIs(testing::Ge(input.size())));
120 output = output.getBlock(input.size(), /*index=*/0).getAsArray1DRef();
121 ASSERT_THAT(output, testing::ContainerEq(Array1DRef<std::byte>(input)));
122 }
123}
124
126
128 ::testing::Types<TypeSpec<uint8_t, uint8_t>,
129
131
134
138
141} // namespace
142} // namespace rawpeed_test
143
144} // namespace rawspeed
int RAWSPEED_READONLY size() const
::testing::Types< TypeSpec< uint8_t, uint8_t >, TypeSpec< uint16_t, uint8_t >, TypeSpec< uint16_t, uint16_t >, TypeSpec< uint32_t, uint8_t >, TypeSpec< uint32_t, uint16_t >, TypeSpec< uint32_t, uint32_t >, TypeSpec< uint64_t, uint8_t >, TypeSpec< uint64_t, uint16_t >, TypeSpec< uint64_t, uint32_t >, TypeSpec< uint64_t, uint64_t > > CoalescedPairTypes
INSTANTIATE_TYPED_TEST_SUITE_P(CoalescedTo, CoalescingOutputIteratorTest, CoalescedPairTypes)
constexpr uint64_t RAWSPEED_READNONE roundUpDivisionSafe(uint64_t value, uint64_t div)
Definition Common.h:145
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition Casts.h:32
throw T(buf.data())
CoalescingOutputIterator(T) -> CoalescingOutputIterator< T >
Array1DRef(T *data_, int numElts_) -> Array1DRef< T >
bool operator==(const AlignedAllocator< T1, A1 > &, const AlignedAllocator< T2, A2 > &)
static inline ::std::ostream & operator<<(::std::ostream &os, const T &b)