Skip to content

Commit

Permalink
Fix indentation and style.
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsracz committed Aug 20, 2024
1 parent 754c065 commit 4444eaf
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 58 deletions.
103 changes: 66 additions & 37 deletions src/utils/DataBuffer.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,16 @@ class DataBufferFuzzTest : public ::testing::Test
protected:
DataBufferFuzzTest()
{
for (int i = 0; i < NUM_OP; ++i) {
for (int i = 0; i < NUM_OP; ++i)
{
freq_[i] = 0;
}
freq_[0] = 1;
freq_[NUM_OP] = 0;
}

enum Op {
enum Op
{
OP_APPEND,
OP_READ,
OP_XFERMID,
Expand All @@ -415,7 +417,7 @@ protected:
};

int freq_[NUM_OP + 1];

/// @return a pseudorandom number uniformly distributed between 0 and max -
/// 1.
/// @param max distribution parameter.
Expand All @@ -426,14 +428,16 @@ protected:

/// Setup a fuzz test scenario where we append a given LinkedDataBufferPtr
/// and then read from the same one.
void setup_basic_readwrite() {
void setup_basic_readwrite()
{
freq_[OP_APPEND] = 1;
freq_[OP_READ] = 1;
}

/// Setup a fuzz test scenario where we append one LinkedDataBufferPtr,
/// then move data to a middle one, then read that middle one.
void setup_write_transfer_read() {
void setup_write_transfer_read()
{
freq_[OP_APPEND] = 1;
freq_[OP_XFERMID] = 1;
freq_[OP_READMID] = 1;
Expand All @@ -442,29 +446,35 @@ protected:
/// Setup a fuzz test scenario where we append one LinkedDataBufferPtr,
/// then move data to a middle one, then move data to a third one, then
/// read that last.
void setup_write_transfer_read_transfer_read() {
void setup_write_transfer_read_transfer_read()
{
freq_[OP_APPEND] = 1;
freq_[OP_XFERMID] = 1;
freq_[OP_XFEREND] = 1;
freq_[OP_READEND] = 1;
}

void prep_fuzz() {
void prep_fuzz()
{
int sum = 0;
for (int i = 0; i <= NUM_OP; ++i) {
for (int i = 0; i <= NUM_OP; ++i)
{
sum += freq_[i];
freq_[i] = sum;
}
}

void run_fuzz(unsigned iter) {
void run_fuzz(unsigned iter)
{
prep_fuzz();
size_t idx = 0;
while (--iter && !HasFatalFailure())
{
int oper = get_random_uni(freq_[NUM_OP]);
for (int i = 0; i < NUM_OP; ++i) {
if (freq_[i] > oper) {
for (int i = 0; i < NUM_OP; ++i)
{
if (freq_[i] > oper)
{
SCOPED_TRACE(idx);
run_op((Op)i);
++idx;
Expand All @@ -474,34 +484,42 @@ protected:
}
}

void run_op(Op op) {
switch(op) {
case OP_APPEND: {
void run_op(Op op)
{
switch (op)
{
case OP_APPEND:
{
int len = get_random_uni(22);
append_helper(&lnk_, len);
break;
}
case OP_READ: {
case OP_READ:
{
int len = get_random_uni(22);
consume_helper(&lnk_, len);
break;
}
case OP_XFERMID: {
case OP_XFERMID:
{
int len = get_random_uni(22);
xfer_helper(&lnk_, &mid_, len);
break;
}
case OP_READMID: {
case OP_READMID:
{
int len = get_random_uni(22);
consume_helper(&mid_, len);
break;
}
case OP_XFEREND: {
case OP_XFEREND:
{
int len = get_random_uni(22);
xfer_helper(&mid_, &end_, len);
break;
}
case OP_READEND: {
case OP_READEND:
{
int len = get_random_uni(22);
consume_helper(&end_, len);
break;
Expand All @@ -510,7 +528,7 @@ protected:
return;
}
}

std::string flatten(const LinkedDataBufferPtr &p)
{
std::string ret;
Expand Down Expand Up @@ -556,7 +574,7 @@ protected:
from->data_read_advance(len);
ASSERT_TRUE(to->try_append_from(tmp, true));
}

/// Consumes (reads) a certain number of characters from a ptr. Characters
/// are compared to the expected output sequence.
void consume_helper(LinkedDataBufferPtr *p, size_t len)
Expand All @@ -582,72 +600,83 @@ protected:
}

/// @return the next byte of the generated sequence.
uint8_t generate() {
uint8_t generate()
{
return nextByte_++;
}

/// Take in the next byte that came out at the end. Verifies that it is the
/// correct byte value.
void consume(uint8_t next_byte) {
void consume(uint8_t next_byte)
{
EXPECT_EQ(nextByteRead_, next_byte);
++nextByteRead_;
}

DataBuffer *b_;
unsigned lastFree_;
unsigned int randSeed_{83012475};
uint8_t nextByte_{0};
uint8_t nextByteRead_{0};
unsigned int randSeed_ {83012475};
uint8_t nextByte_ {0};
uint8_t nextByteRead_ {0};

BarrierNotifiable bn_;
BarrierNotifiable bn2_;
LinkedDataBufferPtr lnk_;
LinkedDataBufferPtr mid_;
LinkedDataBufferPtr end_;
std::vector<std::unique_ptr<BarrierNotifiable> > bns_;
std::vector<std::unique_ptr<BarrierNotifiable>> bns_;
};

TEST_F(DataBufferFuzzTest, small_fuzz) {
TEST_F(DataBufferFuzzTest, small_fuzz)
{
setup_basic_readwrite();
run_fuzz(10);
}

TEST_F(DataBufferFuzzTest, medium_fuzz) {
TEST_F(DataBufferFuzzTest, medium_fuzz)
{
setup_basic_readwrite();
run_fuzz(1000);
}

TEST_F(DataBufferFuzzTest, large_fuzz) {
TEST_F(DataBufferFuzzTest, large_fuzz)
{
setup_basic_readwrite();
run_fuzz(100000);
}

TEST_F(DataBufferFuzzTest, small_duo) {
TEST_F(DataBufferFuzzTest, small_duo)
{
setup_write_transfer_read();
run_fuzz(10);
}

TEST_F(DataBufferFuzzTest, medium_duo) {
TEST_F(DataBufferFuzzTest, medium_duo)
{
setup_write_transfer_read();
run_fuzz(1000);
}

TEST_F(DataBufferFuzzTest, large_duo) {
TEST_F(DataBufferFuzzTest, large_duo)
{
setup_write_transfer_read();
run_fuzz(100000);
}

TEST_F(DataBufferFuzzTest, small_tri) {
TEST_F(DataBufferFuzzTest, small_tri)
{
setup_write_transfer_read_transfer_read();
run_fuzz(10);
}

TEST_F(DataBufferFuzzTest, medium_tri) {
TEST_F(DataBufferFuzzTest, medium_tri)
{
setup_write_transfer_read_transfer_read();
run_fuzz(1000);
}

TEST_F(DataBufferFuzzTest, large_tri) {
TEST_F(DataBufferFuzzTest, large_tri)
{
setup_write_transfer_read_transfer_read();
run_fuzz(100000);
}
57 changes: 36 additions & 21 deletions src/utils/DataBuffer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@
#include "utils/LinkedObject.hxx"
#include "utils/macros.h"


#ifdef GTEST
//#define DEBUG_DATA_BUFFER_FREE
// #define DEBUG_DATA_BUFFER_FREE
#endif

class DataBufferPool;


#ifdef DEBUG_DATA_BUFFER_FREE
class DataBuffer;
static void check_db_ownership(DataBuffer* p);
static void check_db_ownership(DataBuffer *p);
#endif

/// Specialization of the Buffer class that is designed for storing untyped
Expand Down Expand Up @@ -179,15 +177,17 @@ public:
return curr->next();
}

#ifdef DEBUG_DATA_BUFFER_FREE
void unref() {
if (references() == 1) {
#ifdef DEBUG_DATA_BUFFER_FREE
void unref()
{
if (references() == 1)
{
check_db_ownership(this);
}
Buffer::unref();
}
#endif
#endif

private:
friend class DataBufferPool;

Expand Down Expand Up @@ -371,7 +371,7 @@ public:
/// at this point.
/// @return the read pointer, or nullptr if there is no data in this
/// buffer.
const uint8_t* data_read_pointer(size_t* len)
const uint8_t *data_read_pointer(size_t *len)
{
if (!head_ || !size_)
{
Expand Down Expand Up @@ -539,7 +539,8 @@ public:
{
return true; // zero bytes, nothing to do.
}
if (!size_) {
if (!size_)
{
// We are empty, so anything can be appended.
reset(o);
return true;
Expand All @@ -559,12 +560,18 @@ public:
// means that we don't depend on the value of free_ anymore for
// correctness. We also check that o starts at the beginning of the
// head buffer.
if (tail_->size() == (size_t)-free_ && o.skip() == 0) {
if (tail_->next() == o.head()) {
if (tail_->size() == (size_t)-free_ && o.skip() == 0)
{
if (tail_->next() == o.head())
{
// link already exists
} else if (add_link && tail_->next() == nullptr) {
}
else if (add_link && tail_->next() == nullptr)
{
tail_->set_next(o.head());
} else {
}
else
{
return false;
}
}
Expand All @@ -581,15 +588,19 @@ public:
// Now we're good, so take over the extra buffers.
// Acquire extra references
o.head_->ref_all(o.skip() + o.size());
if (tail_ == o.head()) {
if (tail_ == o.head())
{
HASSERT(o.head_->references() > 1);
// Release duplicate reference between the two chains.
o.head_->unref();
}
tail_ = o.tail_;
if (o.free_ < 0) {
if (o.free_ < 0)
{
free_ = o.free_;
} else {
}
else
{
free_ = -tail_->size();
}
size_ += o.size_;
Expand Down Expand Up @@ -623,11 +634,15 @@ private:
};

#ifdef DEBUG_DATA_BUFFER_FREE
void check_db_ownership(DataBuffer* b) {
void check_db_ownership(DataBuffer *b)
{
AtomicHolder h(LinkedDataBufferPtr::head_mu());
for (LinkedDataBufferPtr* l = LinkedDataBufferPtr::link_head(); l; l = l->link_next()) {
for (LinkedDataBufferPtr *l = LinkedDataBufferPtr::link_head(); l;
l = l->link_next())
{
ssize_t total = l->skip() + l->size();
for (DataBuffer* curr = l->head(); total > 0;) {
for (DataBuffer *curr = l->head(); total > 0;)
{
HASSERT(curr != b);
total -= curr->size();
curr = curr->next();
Expand Down

0 comments on commit 4444eaf

Please sign in to comment.