Skip to content

Commit

Permalink
Add one more case to the unit test of ThreadSafeRingBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Samahu committed Jun 1, 2023
1 parent fa93e0a commit 129bf01
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions ouster-ros/test/ring_buffer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ using namespace std::chrono_literals;

class ThreadSafeRingBufferTest : public ::testing::Test {
protected:
static const int ITEM_SIZE = 4;
static const int ITEM_COUNT = 3;
static const int ITEM_SIZE = 4; // predefined size for all items used in
static const int ITEM_COUNT = 3; // number of item the buffer could hold

void SetUp() override {
buffer = std::make_unique<ThreadSafeRingBuffer>(ITEM_SIZE, ITEM_COUNT);
Expand Down Expand Up @@ -50,6 +50,49 @@ class ThreadSafeRingBufferTest : public ::testing::Test {
std::unique_ptr<ThreadSafeRingBuffer> buffer;
};

TEST_F(ThreadSafeRingBufferTest, ReadWriteToBufferSimple) {

assert (ITEM_COUNT > 1 && "or this test can't run");

const int TOTAL_ITEMS = 10; // total items to process
const std::vector<std::string> source = rand_vector_str(TOTAL_ITEMS, ITEM_SIZE);
std::vector<std::string> target = known_vector_str(TOTAL_ITEMS, "0000");

EXPECT_TRUE(buffer->empty());
EXPECT_FALSE(buffer->full());

for (int i = 0; i < ITEM_COUNT; ++i) {
buffer->write([i, &source](uint8_t* buffer) {
std::memcpy(buffer, &source[i][0], ITEM_SIZE);
});
}

EXPECT_FALSE(buffer->empty());
EXPECT_TRUE(buffer->full());

// remove one item
buffer->read([&target](uint8_t* buffer){
std::memcpy(&target[0][0], buffer, ITEM_SIZE);
});

EXPECT_FALSE(buffer->empty());
EXPECT_FALSE(buffer->full());

for (int i = 1; i < ITEM_COUNT; ++i) {
buffer->read([i, &target](uint8_t* buffer){
std::memcpy(&target[i][0], buffer, ITEM_SIZE);
});
}

EXPECT_TRUE(buffer->empty());
EXPECT_FALSE(buffer->full());

for (int i = 0; i < ITEM_COUNT; ++i) {
std::cout << "source " << source[i] << ", target " << target[i] << std::endl;
EXPECT_EQ(target[i], source[i]);
}
}

TEST_F(ThreadSafeRingBufferTest, ReadWriteToBuffer) {

const int TOTAL_ITEMS = 10; // total items to process
Expand Down Expand Up @@ -127,7 +170,7 @@ TEST_F(ThreadSafeRingBufferTest, ReadWriteToBufferWithOverwrite) {

for (int i = ITEM_COUNT; i < TOTAL_ITEMS; ++i) {
std::cout << "source " << source[i] << ", target " << target[i] << std::endl;
EXPECT_EQ(target[i], "XSDS");
EXPECT_EQ(target[i], "0000");
}
}

Expand Down

0 comments on commit 129bf01

Please sign in to comment.