RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
BitTest.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 "adt/Bit.h"
22#include <algorithm>
23#include <cstdint>
24#include <limits>
25#include <string>
26#include <tuple>
27#include <vector>
28#include <gtest/gtest.h>
29
32using std::make_tuple;
33using std::min;
34using std::numeric_limits;
35using std::string;
36using std::vector;
37
38namespace rawspeed_test {
39
40namespace {
41
42using powerOfTwoType = std::tuple<int, bool>;
43class PowerOfTwoTest : public ::testing::TestWithParam<powerOfTwoType> {
44protected:
45 PowerOfTwoTest() = default;
46 virtual void SetUp() {
47 in = std::get<0>(GetParam());
48 expected = std::get<1>(GetParam());
49 }
50
51 int in; // input
52 bool expected; // expected output
53};
55 make_tuple(0, true), make_tuple(1, true), make_tuple(2, true),
56 make_tuple(3, false), make_tuple(4, true), make_tuple(5, false),
57 make_tuple(6, false), make_tuple(7, false), make_tuple(8, true),
58 make_tuple(9, false), make_tuple(10, false), make_tuple(11, false),
59
60};
62 ::testing::ValuesIn(powerOfTwoValues));
64 ASSERT_EQ(isPowerOfTwo(in), expected);
65}
66
67using ClampBitsType = std::tuple<int, int, uint16_t>;
68class ClampBitsTest : public ::testing::TestWithParam<ClampBitsType> {
69protected:
70 ClampBitsTest() = default;
71 virtual void SetUp() {
72 in = std::get<0>(GetParam());
73 n = std::get<1>(GetParam());
74 expected = std::get<2>(GetParam());
75 }
76
77 int in; // input
78 int n;
79 uint16_t expected; // expected output
80};
81
82#define MIN(a, b) ((a) < (b) ? (a) : (b))
83#define MAX(a, b) ((a) > (b) ? (a) : (b))
84
85#define ROW(v, p, pv) make_tuple((v), (p), MIN(pv, MAX(v, 0))),
86
87#define ROWS(v, p, pv) ROW(-v, p, 0) ROW(v, p, pv)
88
89#define THREEROWS(v, p) \
90 ROWS(((1 << (v)) - 1), (p), ((1 << (p)) - 1)) \
91 ROWS(((1 << (v)) - 0), (p), ((1 << (p)) - 1)) \
92 ROWS(((1 << (v)) + 1), (p), ((1 << (p)) - 1))
93
94#define MOREROWS(v) \
95 THREEROWS(v, 0) \
96 THREEROWS(v, 1) \
97 THREEROWS(v, 2) \
98 THREEROWS(v, 4) \
99 THREEROWS(v, 8) THREEROWS(v, 16)
100
101#define GENERATE() \
102 MOREROWS(0) \
103 MOREROWS(1) \
104 MOREROWS(2) MOREROWS(4) MOREROWS(8) MOREROWS(16) MOREROWS(24) MOREROWS(30)
105
107 make_tuple(0, 0, 0), make_tuple(0, 16, 0),
108 make_tuple(32, 0, 0), make_tuple(32, 16, 32),
109 make_tuple(32, 2, 3), make_tuple(-32, 0, 0),
110 make_tuple(-32, 16, 0), GENERATE()};
112 ::testing::ValuesIn(ClampBitsValues));
113TEST_P(ClampBitsTest, ClampBitsTest) { ASSERT_EQ(clampBits(in, n), expected); }
114TEST(ClampBitsDeathTest, Only16Bit) {
115#ifndef NDEBUG
116 ASSERT_DEATH({ ASSERT_EQ(clampBits(0, 17), 0); }, "nBits <= 16");
117#endif
118}
119
120TEST(ClampBitsUnsignedDeathTest, NoNopClamps) {
121#ifndef NDEBUG
122 ASSERT_DEATH(
123 { ASSERT_EQ(clampBits<uint16_t>(0, 16), 0); },
124 "bitwidth<T>\\(\\) > nBits");
125#endif
126}
127
128} // namespace
129
130} // namespace rawspeed_test
#define GENERATE()
Definition BitTest.cpp:101
constexpr bool RAWSPEED_READNONE isPowerOfTwo(T val)
Definition Bit.h:38
constexpr auto RAWSPEED_READNONE clampBits(T value, unsigned int nBits)
Definition Bit.h:75
INSTANTIATE_TEST_SUITE_P(MD5Test, MD5Test, ::testing::ValuesIn(testCases))
TEST_P(MD5Test, CheckTestCaseSet)
Definition MD5Test.cpp:388
std::tuple< int, int, uint16_t > ClampBitsType
Definition BitTest.cpp:67
TEST(ClampBitsDeathTest, Only16Bit)
Definition BitTest.cpp:114
constexpr bool RAWSPEED_READNONE isPowerOfTwo(T val)
Definition Bit.h:38
constexpr auto RAWSPEED_READNONE clampBits(T value, unsigned int nBits)
Definition Bit.h:75