Project
Loading...
Searching...
No Matches
test_ransBitPtr.cxx
Go to the documentation of this file.
1// Copyright 2019-2023 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#define BOOST_TEST_MODULE Utility test
17#define BOOST_TEST_MAIN
18#define BOOST_TEST_DYN_LINK
19
20#undef NDEBUG
21#include <cassert>
22
23#include <boost/test/unit_test.hpp>
24#include <boost/mp11.hpp>
25
27
28using namespace o2::rans;
29using namespace o2::rans::internal;
30using namespace o2::rans::utils;
31
32using source_types = boost::mp11::mp_list<int8_t, int16_t, int32_t>;
33
35{
36 BitPtr bitPtr{};
37 BOOST_CHECK_EQUAL(bitPtr.getBitAddress(), 0);
38 BOOST_CHECK_EQUAL(bitPtr.toPtr<source_type>(), nullptr);
39 BOOST_CHECK_EQUAL(bitPtr.getOffset<source_type>(), 0);
40
41 source_type* ptr = nullptr;
42 bitPtr = BitPtr{ptr};
43 BOOST_CHECK_EQUAL(bitPtr.getBitAddress(), 0);
44 BOOST_CHECK_EQUAL(bitPtr.toPtr<source_type>(), nullptr);
45 BOOST_CHECK_EQUAL(bitPtr.getOffset<source_type>(), 0);
46
47 source_type v{};
48 ptr = &v;
49 bitPtr = BitPtr{ptr};
50 BOOST_CHECK_EQUAL(bitPtr.getBitAddress(), adr2Bits(ptr));
51 BOOST_CHECK_EQUAL(bitPtr.toPtr<source_type>(), ptr);
52 BOOST_CHECK_EQUAL(bitPtr.getOffset<source_type>(), 0);
53
54 intptr_t offset = 6;
55 bitPtr = BitPtr{ptr, offset};
56 BOOST_CHECK_EQUAL(bitPtr.getBitAddress(), adr2Bits(ptr) + offset);
57 BOOST_CHECK_EQUAL(bitPtr.toPtr<source_type>(), ptr);
58 BOOST_CHECK_EQUAL(bitPtr.getOffset<source_type>(), offset);
59
60 offset = 13;
61 bitPtr = BitPtr{ptr, offset};
62 BOOST_CHECK_EQUAL(bitPtr.getBitAddress(), adr2Bits(ptr) + offset);
63
64 if constexpr (sizeof(source_type) == 1) {
65 BOOST_CHECK_EQUAL(bitPtr.toPtr<source_type>(), ptr + 1);
66 BOOST_CHECK_EQUAL(bitPtr.getOffset<source_type>(), offset - 8);
67 } else {
68 BOOST_CHECK_EQUAL(bitPtr.toPtr<source_type>(), ptr);
69 BOOST_CHECK_EQUAL(bitPtr.getOffset<source_type>(), offset);
70 }
71};
72
74{
75 source_type v[5]{};
76 source_type* smallPtr = &v[0];
77 source_type* bigPtr = &v[4];
78 BitPtr smallBitPtr{smallPtr};
79 BitPtr bigBitPtr{bigPtr};
80
81 BitPtr smallBitPtrPlusOne{smallPtr, 1};
82
83 // test equal
84 BOOST_CHECK_EQUAL(smallBitPtr, smallBitPtr);
85 // test not equal
86 BOOST_CHECK_NE(smallBitPtr, bigBitPtr);
87 // test smaller
88 BOOST_CHECK_LT(smallBitPtr, bigBitPtr);
89 // test greater
90 BOOST_CHECK_GT(bigBitPtr, smallBitPtr);
91 // test greater-equals
92 BOOST_CHECK_GE(bigBitPtr, smallBitPtr);
93 BOOST_CHECK_GE(smallBitPtr, smallBitPtr);
94 // test smaller-equals
95 BOOST_CHECK_LE(smallBitPtr, bigBitPtr);
96 BOOST_CHECK_LE(smallBitPtr, smallBitPtr);
97
98 // test pre-increment
99 BitPtr ptr = smallBitPtr;
100 ++ptr;
101 BOOST_CHECK_EQUAL(ptr, smallBitPtrPlusOne);
102 // test post-increment
103 ptr = smallBitPtr;
104 BOOST_CHECK_EQUAL(ptr++, smallBitPtr);
105 BOOST_CHECK_EQUAL(ptr, smallBitPtrPlusOne);
106 // test pre-decrement
107 ptr = smallBitPtrPlusOne;
108 --ptr;
109 BOOST_CHECK_EQUAL(ptr, smallBitPtr);
110 // test post-decrement
111 ptr = smallBitPtrPlusOne;
112 BOOST_CHECK_EQUAL(ptr--, smallBitPtrPlusOne);
113 BOOST_CHECK_EQUAL(ptr, smallBitPtr);
114
115 intptr_t increment = toBits((bigPtr - smallPtr) * sizeof(source_type));
116
117 // test +=
118 ptr = smallBitPtr;
119 ptr += increment;
120 BOOST_CHECK_EQUAL(ptr, bigBitPtr);
121 // test +
122 ptr = smallBitPtr;
123 BOOST_CHECK_EQUAL(smallBitPtr + increment, bigBitPtr);
124 BOOST_CHECK_EQUAL(increment + smallBitPtr, bigBitPtr);
125
126 // check -=
127 ptr = bigBitPtr;
128 ptr -= increment;
129 BOOST_CHECK_EQUAL(ptr, smallBitPtr);
130
131 // check -
132 BOOST_CHECK_EQUAL(bigBitPtr - increment, smallBitPtr);
133
134 // check -
135 BOOST_CHECK_EQUAL(bigBitPtr - smallBitPtr, increment);
136}
137
139{
140 source_type v[5]{};
141
142 intptr_t distance = 3;
143 intptr_t bitDistance = toBits<source_type>();
144
145 const BitPtr begin{&v[0]};
146 const BitPtr belowMinus{&v[1], -distance};
147 const BitPtr below{&v[1]};
148 BOOST_CHECK_EQUAL(begin.toPtr<source_type>(), belowMinus.toPtr<source_type>());
149 const BitPtr ptr{&v[2]};
150 const BitPtr above{&v[3]};
151 const BitPtr abovePlus{&v[3], distance};
152 BOOST_CHECK_EQUAL(abovePlus.toPtr<source_type>(), above.toPtr<source_type>());
153
154 BOOST_CHECK_EQUAL(ptr - (bitDistance + distance), belowMinus);
155 BOOST_CHECK_EQUAL(ptr - (bitDistance), below);
156 BOOST_CHECK_EQUAL(ptr + (bitDistance), above);
157 BOOST_CHECK_EQUAL(ptr + (bitDistance + distance), abovePlus);
158}
Pointer type helper class for bitwise Packing.
TBranch * ptr
uint32_t source_type
const GLdouble * v
Definition glcorearb.h:832
GLsizei GLsizei GLfloat distance
Definition glcorearb.h:5506
GLintptr offset
Definition glcorearb.h:660
constexpr uintptr_t adr2Bits(T *address) noexcept
Definition utils.h:84
constexpr size_t toBits() noexcept
Definition utils.h:173
boost::mp11::mp_list< uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t > source_types
BOOST_CHECK_EQUAL(triggersD.size(), triggers.size())
BOOST_AUTO_TEST_CASE_TEMPLATE(test_BitPtrConstructors, source_type, source_types)