RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
CommonTest.cpp
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2017 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 "common/Common.h"
22#include <algorithm>
23#include <cassert>
24#include <cstddef>
25#include <cstdint>
26#include <limits>
27#include <memory>
28#include <string>
29#include <tuple>
30#include <vector>
31#include <gtest/gtest.h>
32
35using rawspeed::isIn;
42using std::make_tuple;
43using std::min;
44using std::numeric_limits;
45using std::string;
46using std::vector;
47
48namespace rawspeed_test {
49
50namespace {
51
52using RoundDownType = std::tuple<uint64_t, uint64_t, uint64_t>;
53class RoundDownTest : public ::testing::TestWithParam<RoundDownType> {
54protected:
55 RoundDownTest() = default;
56 virtual void SetUp() {
57 in = std::get<0>(GetParam());
58 multiple = std::get<1>(GetParam());
59 expected = std::get<2>(GetParam());
60 }
61
62 uint64_t in; // input
64 uint64_t expected; // expected output
65};
67 make_tuple(0, 0, 0), make_tuple(0, 10, 0), make_tuple(10, 0, 10),
68 make_tuple(10, 10, 10), make_tuple(10, 1, 10), make_tuple(10, 2, 10),
69 make_tuple(10, 3, 9), make_tuple(10, 4, 8), make_tuple(10, 5, 10),
70 make_tuple(10, 6, 6), make_tuple(10, 7, 7), make_tuple(10, 8, 8),
71 make_tuple(10, 9, 9), make_tuple(10, 11, 0), make_tuple(10, 12, 0),
72
73};
75 ::testing::ValuesIn(RoundDownValues));
77 ASSERT_EQ(roundDown(in, multiple), expected);
78}
79
80using RoundUpType = std::tuple<uint64_t, uint64_t, uint64_t>;
81class RoundUpTest : public ::testing::TestWithParam<RoundUpType> {
82protected:
83 RoundUpTest() = default;
84 virtual void SetUp() {
85 in = std::get<0>(GetParam());
86 multiple = std::get<1>(GetParam());
87 expected = std::get<2>(GetParam());
88 }
89
90 uint64_t in; // input
92 uint64_t expected; // expected output
93};
95 make_tuple(0, 0, 0), make_tuple(0, 10, 0), make_tuple(10, 0, 10),
96 make_tuple(10, 10, 10), make_tuple(10, 1, 10), make_tuple(10, 2, 10),
97 make_tuple(10, 3, 12), make_tuple(10, 4, 12), make_tuple(10, 5, 10),
98 make_tuple(10, 6, 12), make_tuple(10, 7, 14), make_tuple(10, 8, 16),
99 make_tuple(10, 9, 18), make_tuple(10, 11, 11), make_tuple(10, 12, 12),
100
101};
103 ::testing::ValuesIn(RoundUpValues));
104TEST_P(RoundUpTest, RoundUpTest) { ASSERT_EQ(roundUp(in, multiple), expected); }
105
106using roundUpDivisionSafeType = std::tuple<uint64_t, uint64_t, uint64_t>;
108 : public ::testing::TestWithParam<roundUpDivisionSafeType> {
109protected:
111 virtual void SetUp() {
112 in = std::get<0>(GetParam());
113 divider = std::get<1>(GetParam());
114 expected = std::get<2>(GetParam());
115 }
116
117 uint64_t in; // input
119 uint64_t expected; // expected output
120};
122 make_tuple(0, 10, 0),
123 make_tuple(10, 10, 1),
124 make_tuple(10, 1, 10),
125 make_tuple(10, 2, 5),
126 make_tuple(10, 3, 4),
127 make_tuple(10, 4, 3),
128 make_tuple(10, 5, 2),
129 make_tuple(10, 6, 2),
130 make_tuple(10, 7, 2),
131 make_tuple(10, 8, 2),
132 make_tuple(10, 9, 2),
133 make_tuple(0, 1, 0),
134 make_tuple(1, 1, 1),
135 make_tuple(numeric_limits<uint64_t>::max() - 1, 1,
136 numeric_limits<uint64_t>::max() - 1),
137 make_tuple(numeric_limits<uint64_t>::max(), 1,
138 numeric_limits<uint64_t>::max()),
139 make_tuple(0, numeric_limits<uint64_t>::max() - 1, 0),
140 make_tuple(1, numeric_limits<uint64_t>::max() - 1, 1),
141 make_tuple(numeric_limits<uint64_t>::max() - 1,
142 numeric_limits<uint64_t>::max() - 1, 1),
143 make_tuple(numeric_limits<uint64_t>::max(),
144 numeric_limits<uint64_t>::max() - 1, 2),
145 make_tuple(0, numeric_limits<uint64_t>::max(), 0),
146 make_tuple(1, numeric_limits<uint64_t>::max(), 1),
147 make_tuple(numeric_limits<uint64_t>::max() - 1,
148 numeric_limits<uint64_t>::max(), 1),
149 make_tuple(numeric_limits<uint64_t>::max(), numeric_limits<uint64_t>::max(),
150 1),
151
152};
154 ::testing::ValuesIn(roundUpDivisionSafeValues));
156 ASSERT_EQ(roundUpDivisionSafe(in, divider), expected);
157}
158
159using IsAlignedType = std::tuple<int, int>;
160class IsAlignedTest : public ::testing::TestWithParam<IsAlignedType> {
161protected:
162 IsAlignedTest() = default;
163 virtual void SetUp() {
164 value = std::get<0>(GetParam());
165 multiple = std::get<1>(GetParam());
166 }
167
168 int value;
170};
172 ::testing::Combine(::testing::Range(0, 32),
173 ::testing::Range(0, 32)));
174TEST_P(IsAlignedTest, IsAlignedAfterRoundUpTest) {
175 ASSERT_TRUE(isAligned(roundUp(value, multiple), multiple));
176}
177
178using IsInType = std::tuple<string, bool>;
179class IsInTest : public ::testing::TestWithParam<IsInType> {
180protected:
181 IsInTest() = default;
182 virtual void SetUp() {
183 in = std::get<0>(GetParam());
184 expected = std::get<1>(GetParam());
185 }
186
187 string in; // input
188 bool expected; // expected output
189};
190
192 make_tuple("foo", true), make_tuple("foo2", true),
193 make_tuple("bar", true), make_tuple("baz", true),
194 make_tuple("foo1", false), make_tuple("bar2", false),
195 make_tuple("baz-1", false), make_tuple("quz", false),
196
197};
200 ASSERT_EQ(isIn(in, {"foo", "foo2", "bar", "baz"}), expected);
201}
202
203using TrimSpacesType = std::tuple<string, string>;
204class TrimSpacesTest : public ::testing::TestWithParam<TrimSpacesType> {
205protected:
206 TrimSpacesTest() = default;
207 virtual void SetUp() {
208 in = std::get<0>(GetParam());
209 out = std::get<1>(GetParam());
210 }
211
212 string in; // input
213 string out; // expected output
214};
215
217#define STR "fo2o 3,24 b5a#r"
218 make_tuple("foo", "foo"),
219 make_tuple(STR, STR),
220 make_tuple(" " STR, STR),
221 make_tuple("\t" STR, STR),
222 make_tuple(" \t " STR, STR),
223 make_tuple(STR " ", STR),
224 make_tuple(STR "\t", STR),
225 make_tuple(STR " \t ", STR),
226 make_tuple(" " STR " ", STR),
227 make_tuple("\t" STR "\t", STR),
228 make_tuple(" \t " STR " \t ", STR),
229 make_tuple(" ", ""),
230 make_tuple(" \t", ""),
231 make_tuple(" \t ", ""),
232 make_tuple("\t ", ""),
233#undef STR
234};
236 ::testing::ValuesIn(TrimSpacesValues));
238
239using splitStringType = std::tuple<string, char, vector<string>>;
240class SplitStringTest : public ::testing::TestWithParam<splitStringType> {
241protected:
242 SplitStringTest() = default;
243 virtual void SetUp() {
244 in = std::get<0>(GetParam());
245 sep = std::get<1>(GetParam());
246 out = std::get<2>(GetParam());
247 }
248
249 string in; // input
250 char sep; // the separator
251 vector<string> out; // expected output
252};
254 make_tuple("", ' ', vector<string>({})),
255 make_tuple(" ", ' ', vector<string>({})),
256 make_tuple(" ini mi,ni moe ", ' ',
257 vector<string>({"ini", "mi,ni", "moe"})),
258 make_tuple(" 412, 542,732 , ", ',',
259 vector<string>({" 412", " 542", "732 ", " "})),
260 make_tuple("\0 412, 542,732 , ", ',', vector<string>({})),
261 make_tuple(" 412, 542\0,732 , ", ',', vector<string>({" 412", " 542"})),
262
263};
265 ::testing::ValuesIn(splitStringValues));
267 auto split = splitString(in, sep);
268 ASSERT_EQ(split.size(), out.size());
269 ASSERT_TRUE(std::equal(split.begin(), split.end(), out.begin()));
270}
271
272TEST(MakeUniqueTest, Test) {
273 ASSERT_NO_THROW({
274 auto s = std::make_unique<int>(0);
275 ASSERT_EQ(*s, 0);
276 });
277 ASSERT_NO_THROW({
278 auto s = std::make_unique<int>(314);
279 ASSERT_EQ(*s, 314);
280 });
281}
282
283using copyPixelsType = std::tuple<int, int, int, int>;
284class CopyPixelsTest : public ::testing::TestWithParam<copyPixelsType> {
285protected:
286 CopyPixelsTest() = default;
287 virtual void SetUp() {
288 dstPitch = std::get<0>(GetParam());
289 srcPitch = std::get<1>(GetParam());
290 rowSize = min({std::get<2>(GetParam()), srcPitch, dstPitch});
291 height = std::get<3>(GetParam());
292
293 assert(srcPitch * height < numeric_limits<uint8_t>::max());
294 assert(dstPitch * height < numeric_limits<uint8_t>::max());
295
296 src.resize(static_cast<size_t>(srcPitch) * height);
297 dst.resize(static_cast<size_t>(dstPitch) * height);
298
299 fill(src.begin(), src.end(), newVal);
300 fill(dst.begin(), dst.end(), origVal);
301 }
302 void copy() {
303 copyPixels(reinterpret_cast<std::byte*>(&(dst[0])), dstPitch,
304 reinterpret_cast<const std::byte*>(&(src[0])), srcPitch, rowSize,
305 height);
306 }
307 void compare() {
308 for (int y = 0; y < height; y++) {
309 for (int x = 0; x < srcPitch; x++)
310 ASSERT_EQ(src[y * srcPitch + x], newVal);
311 for (int x = 0; x < dstPitch; x++)
312 ASSERT_EQ(dst[y * dstPitch + x], x < rowSize ? newVal : origVal);
313 }
314 }
315
316 static constexpr uint8_t newVal = 0;
317 static constexpr uint8_t origVal = -1;
318 vector<uint8_t> src;
319 vector<uint8_t> dst;
324};
326 testing::Combine(testing::Range(1, 4, 1),
327 testing::Range(1, 4, 1),
328 testing::Range(1, 4, 1),
329 testing::Range(1, 4, 1)));
331 copy();
332 compare();
333}
334
335} // namespace
336
337} // namespace rawspeed_test
void copyPixels(std::byte *destPtr, int dstPitch, const std::byte *srcPtr, int srcPitch, int rowSize, int height)
Definition Common.h:79
constexpr uint64_t RAWSPEED_READNONE roundUpDivisionSafe(uint64_t value, uint64_t div)
Definition Common.h:145
std::string trimSpaces(std::string_view str)
Definition Common.h:163
constexpr uint64_t RAWSPEED_READNONE roundDown(uint64_t value, uint64_t multiple)
Definition Common.h:129
bool RAWSPEED_READONLY isIn(const T value, const std::initializer_list< T2 > &list)
Definition Common.h:156
constexpr RAWSPEED_READNONE bool isAligned(T value, size_t multiple)
Definition Common.h:151
std::vector< std::string > splitString(const std::string &input, char c=' ')
Definition Common.h:178
#define s
INSTANTIATE_TEST_SUITE_P(MD5Test, MD5Test, ::testing::ValuesIn(testCases))
TEST_P(MD5Test, CheckTestCaseSet)
Definition MD5Test.cpp:388
#define STR(a)
constexpr uint64_t RAWSPEED_READNONE roundUp(uint64_t value, uint64_t multiple)
Definition Common.h:134
assert(dim.area() >=area)
dim y
Definition Common.cpp:51
dim x
Definition Common.cpp:50
std::tuple< uint64_t, uint64_t, uint64_t > RoundDownType
std::tuple< uint64_t, uint64_t, uint64_t > roundUpDivisionSafeType
std::tuple< uint64_t, uint64_t, uint64_t > RoundUpType
std::tuple< string, char, vector< string > > splitStringType
const roundUpDivisionSafeType roundUpDivisionSafeValues[]
std::tuple< int, int, int, int > copyPixelsType
constexpr uint64_t RAWSPEED_READNONE roundUpDivisionSafe(uint64_t value, uint64_t div)
Definition Common.h:145
constexpr uint64_t RAWSPEED_READNONE roundUp(uint64_t value, uint64_t multiple)
Definition Common.h:134
std::string trimSpaces(std::string_view str)
Definition Common.h:163
void copyPixels(std::byte *destPtr, int dstPitch, const std::byte *srcPtr, int srcPitch, int rowSize, int height)
Definition Common.h:79
constexpr uint64_t RAWSPEED_READNONE roundDown(uint64_t value, uint64_t multiple)
Definition Common.h:129
constexpr bool RAWSPEED_READNONE isPowerOfTwo(T val)
Definition Bit.h:38
bool RAWSPEED_READONLY isIn(const T value, const std::initializer_list< T2 > &list)
Definition Common.h:156
constexpr RAWSPEED_READNONE bool isAligned(T value, size_t multiple)
Definition Common.h:151
std::vector< std::string > splitString(const std::string &input, char c=' ')
Definition Common.h:178