Skip to content

Commit 8ea1fc3

Browse files
Charlie FraschCharlie Frasch
Charlie Frasch
authored and
Charlie Frasch
committed
More unit test cases
1 parent fc33980 commit 8ea1fc3

File tree

3 files changed

+104
-13
lines changed

3 files changed

+104
-13
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
build
2+
*.swp

Fifo5.hpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ class Fifo5 : private Alloc
178178

179179
popper_t(popper_t&& other) noexcept
180180
: fifo_{std::move(other.fifo_)}
181-
, cursor_{std::move(other.other_)} {
181+
, cursor_{std::move(other.cursor_)} {
182182
other.release();
183183
}
184184
popper_t& operator=(popper_t&& other) noexcept {
185185
fifo_ = std::move(other.fifo_);
186-
cursor_ = std::move(other.other_);
186+
cursor_ = std::move(other.cursor_);
187187
other.release();
188188
return *this;
189189
}
@@ -204,6 +204,12 @@ class Fifo5 : private Alloc
204204

205205
/// @name Direct access to the fifo's ring
206206
///@{
207+
208+
// QUESTION
209+
// If std::start_lifetime_as<T> must be called in the pusher_t
210+
// get() calls must it also be applied here? Or, are the
211+
// pusher_t calls to it and the memcpy sufficient to have an
212+
// actual object in the referenced t[] element?
207213
auto& get() noexcept { return *fifo_->element(cursor_); }
208214
auto const& get() const noexcept { return *fifo_->element(cursor_); }
209215

unitTests.cpp

+95-11
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
#include "Fifo3.hpp"
44
#include "Fifo4.hpp"
55
#include "Fifo5.hpp"
6-
// TODO #include "Fifo7.hpp"
76

87
#include <gtest/gtest.h>
98

109
#include <type_traits>
1110

11+
1212
extern "C" {
1313
void __ubsan_on_report() {
1414
FAIL() << "Encountered an undefined behavior sanitizer error";
@@ -31,7 +31,7 @@ class FifoTestBase : public testing::Test {
3131
FifoType fifo{4};
3232
};
3333

34-
using test_type = int;
34+
using test_type = unsigned int;
3535

3636

3737
template<typename FifoT> using FifoTest = FifoTestBase<FifoT>;
@@ -96,7 +96,7 @@ TYPED_TEST(FifoTest, pop) {
9696
for (auto i = 0u; i < this->fifo.size(); ++i) {
9797
auto value = typename TestFixture::value_type{};
9898
EXPECT_TRUE(this->fifo.pop(value));
99-
EXPECT_EQ(42+i, value);
99+
EXPECT_EQ(42 + i, value);
100100
}
101101
EXPECT_FALSE(this->fifo.pop(value));
102102
}
@@ -112,7 +112,7 @@ TYPED_TEST(FifoTest, popFullFifo) {
112112

113113
for (auto i = 0u; i < this->fifo.size()*4; ++i) {
114114
EXPECT_TRUE(this->fifo.pop(value));
115-
EXPECT_EQ(42+i, value);
115+
EXPECT_EQ(42 + i, value);
116116
EXPECT_FALSE(this->fifo.full());
117117

118118
EXPECT_TRUE(this->fifo.push(42 + 4 + i));
@@ -128,13 +128,28 @@ TYPED_TEST(FifoTest, popEmpty) {
128128
EXPECT_TRUE(this->fifo.empty());
129129
EXPECT_TRUE(this->fifo.push(42 + i));
130130
EXPECT_TRUE(this->fifo.pop(value));
131-
EXPECT_EQ(42+i, value);
131+
EXPECT_EQ(42 + i, value);
132132
}
133133

134134
EXPECT_TRUE(this->fifo.empty());
135135
EXPECT_FALSE(this->fifo.pop(value));
136136
}
137137

138+
TYPED_TEST(FifoTest, wrap) {
139+
auto value = typename TestFixture::value_type{};
140+
for (auto i = 0u; i < this->fifo.size() * 2 + 1; ++i) {
141+
this->fifo.push(42 + i);
142+
EXPECT_TRUE(this->fifo.pop(value));
143+
EXPECT_EQ(42 + i, value);
144+
}
145+
146+
for (auto i = 0u; i < 8u; ++i) {
147+
this->fifo.push(42 + i);
148+
EXPECT_TRUE(this->fifo.pop(value));
149+
EXPECT_EQ(42 + i, value);
150+
}
151+
}
152+
138153

139154
template<typename FifoT> using ProxyTest = FifoTestBase<FifoT>;
140155
using ProxyFifoTypes = ::testing::Types<
@@ -184,13 +199,29 @@ TYPED_TEST(ProxyTest, pusherRelease) {
184199

185200
{
186201
auto pusher = this->fifo.push();
202+
ASSERT_TRUE(!!pusher);
187203
pusher = 24;
188204
pusher.release();
205+
EXPECT_FALSE(!!pusher);
189206
}
190207
EXPECT_EQ(42, *this->fifo.pop());
191208
EXPECT_TRUE(this->fifo.empty());
192209
}
193210

211+
TYPED_TEST(ProxyTest, popperRelease) {
212+
this->fifo.push() = 42;
213+
EXPECT_FALSE(this->fifo.empty());
214+
215+
{
216+
auto popper = this->fifo.pop();
217+
ASSERT_TRUE(!!popper);
218+
EXPECT_EQ(42, *popper);
219+
popper.release();
220+
EXPECT_FALSE(!!popper);
221+
}
222+
EXPECT_FALSE(this->fifo.empty());
223+
EXPECT_EQ(42, *this->fifo.pop());
224+
}
194225

195226

196227
struct ABC
@@ -200,6 +231,13 @@ struct ABC
200231
int c;
201232
};
202233

234+
// Specialize ValueSizeTraits not to copy ABC::c.
235+
template<>
236+
inline std::size_t ValueSizeTraits<ABC>::size(value_type const&) {
237+
return sizeof(ABC::a) + sizeof(ABC::b);
238+
}
239+
240+
203241
template<typename FifoT> using ProxyMoveTest = FifoTestBase<FifoT>;
204242
using ProxyMoveFifoTypes = ::testing::Types<
205243
Fifo5<ABC>
@@ -212,22 +250,68 @@ TYPED_TEST(ProxyMoveTest, pusherMove) {
212250
pusher = ABC{100, 200, 300};
213251
EXPECT_TRUE(!!pusher);
214252
EXPECT_EQ(100, pusher->a);
215-
EXPECT_EQ(200, pusher->b);
216-
EXPECT_EQ(300, pusher->c);
217253

218254
// move ctor
219255
auto pusher2 = std::move(pusher);
220256
EXPECT_FALSE(!!pusher);
221257
EXPECT_TRUE(!!pusher2);
222258
EXPECT_EQ(100, pusher2->a);
223-
EXPECT_EQ(200, pusher2->b);
224-
EXPECT_EQ(300, pusher2->c);
225259

226260
// move assignment
227261
pusher = std::move(pusher2);
228262
EXPECT_TRUE(!!pusher);
229263
EXPECT_FALSE(!!pusher2);
230264
EXPECT_EQ(100, pusher->a);
231-
EXPECT_EQ(200, pusher->b);
232-
EXPECT_EQ(300, pusher->c);
265+
}
266+
267+
TYPED_TEST(ProxyMoveTest, popperMove) {
268+
for (auto i = 0; i < static_cast<int>(this->fifo.size()); ++i) {
269+
this->fifo.push(ABC{42 + i, 43, 44});
270+
}
271+
272+
for (auto i = 0u; i < this->fifo.size(); ++i) {
273+
auto popper = this->fifo.pop();
274+
ASSERT_TRUE(!!popper);
275+
EXPECT_EQ(42 + i, popper->a);
276+
277+
// move ctor
278+
auto popper2 = std::move(popper);
279+
EXPECT_FALSE(!!popper);
280+
ASSERT_TRUE(!!popper2);
281+
EXPECT_EQ(42 + i, popper2->a);
282+
283+
// move assignment
284+
popper = std::move(popper2);
285+
ASSERT_TRUE(!!popper);
286+
EXPECT_FALSE(!!popper2);
287+
EXPECT_EQ(42 + i, popper->a);
288+
}
289+
EXPECT_TRUE(this->fifo.empty());
290+
}
291+
292+
TYPED_TEST(ProxyMoveTest, pusherUsesValueSizeTraits) {
293+
// Push and pop a value into every slot of the fifo using assign
294+
// directly to value_type&. This bypasses use of ValueSizeTraits.
295+
for (auto i = 0; i < static_cast<int>(this->fifo.size()); ++i) {
296+
{
297+
auto pusher = this->fifo.push();
298+
*pusher = ABC{1, 2, 3};
299+
}
300+
auto popper = this->fifo.pop();
301+
EXPECT_EQ(1, popper->a);
302+
EXPECT_EQ(2, popper->b);
303+
EXPECT_EQ(3, popper->c);
304+
}
305+
306+
// Now push using operator= and a different value
307+
{
308+
auto pusher = this->fifo.push();
309+
pusher = ABC{100, 200, 300};
310+
}
311+
312+
auto popper = this->fifo.pop();
313+
EXPECT_EQ(100, popper->a);
314+
EXPECT_EQ(200, popper->b);
315+
EXPECT_EQ(3, popper->c);
316+
233317
}

0 commit comments

Comments
 (0)