Skip to content

Commit

Permalink
act on reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
m0dB authored and m0dB committed Nov 11, 2024
1 parent 70d223b commit b2bcb3d
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 179 deletions.
245 changes: 126 additions & 119 deletions src/test/fifotest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,176 +7,183 @@

namespace {

class FifoTest : public testing::Test {
struct Param {
std::size_t requestedBufferSize;
std::size_t expectedBufferSize;
int offset;
};

TEST_F(FifoTest, writeAvailableTest) {
std::vector<float> data(1024);
FIFO<float> fifo(1024);
ASSERT_EQ(1024, fifo.writeAvailable());
class FifoTest : public testing::TestWithParam<Param> {
};

TEST_P(FifoTest, writeAvailableTest) {
const auto param = FifoTest::GetParam();
std::vector<float> data(param.requestedBufferSize);
FIFO<float> fifo(param.requestedBufferSize);
fifo.releaseReadRegions(param.offset >= 0
? param.offset
: std::numeric_limits<std::size_t>::max() + param.offset +
1);
fifo.releaseWriteRegions(param.offset >= 0
? param.offset
: std::numeric_limits<std::size_t>::max() + param.offset +
1);

ASSERT_EQ(param.expectedBufferSize, fifo.writeAvailable());
ASSERT_EQ(100, fifo.write(data.data(), 100));
ASSERT_EQ(924, fifo.writeAvailable());
ASSERT_EQ(param.expectedBufferSize - 100, fifo.writeAvailable());
ASSERT_EQ(50, fifo.read(data.data(), 50));
ASSERT_EQ(974, fifo.writeAvailable());
ASSERT_EQ(974, fifo.write(data.data(), 10000));
ASSERT_EQ(param.expectedBufferSize - 50, fifo.writeAvailable());
ASSERT_EQ(param.expectedBufferSize - 50, fifo.write(data.data(), 1000000));
ASSERT_EQ(0, fifo.writeAvailable());
}

TEST_F(FifoTest, readAvailableTest) {
std::vector<float> data(1024);
FIFO<float> fifo(1024);
TEST_P(FifoTest, readAvailableTest) {
const auto param = FifoTest::GetParam();
std::vector<float> data(param.requestedBufferSize);
FIFO<float> fifo(param.requestedBufferSize);
fifo.releaseReadRegions(param.offset >= 0
? param.offset
: std::numeric_limits<std::size_t>::max() + param.offset +
1);
fifo.releaseWriteRegions(param.offset >= 0
? param.offset
: std::numeric_limits<std::size_t>::max() + param.offset +
1);

ASSERT_EQ(0, fifo.readAvailable());
ASSERT_EQ(100, fifo.write(data.data(), 100));
ASSERT_EQ(100, fifo.readAvailable());
ASSERT_EQ(50, fifo.read(data.data(), 50));
ASSERT_EQ(50, fifo.readAvailable());
ASSERT_EQ(974, fifo.write(data.data(), 10000));
ASSERT_EQ(1024, fifo.readAvailable());
ASSERT_EQ(param.expectedBufferSize - 50, fifo.write(data.data(), 1000000));
ASSERT_EQ(param.expectedBufferSize, fifo.readAvailable());
}

TEST_F(FifoTest, flushReadTest) {
std::vector<float> data(1024);
FIFO<float> fifo(1024);
TEST_P(FifoTest, flushReadTest) {
const auto param = FifoTest::GetParam();
std::vector<float> data(param.requestedBufferSize);
FIFO<float> fifo(param.requestedBufferSize);
fifo.releaseReadRegions(param.offset >= 0
? param.offset
: std::numeric_limits<std::size_t>::max() + param.offset +
1);
fifo.releaseWriteRegions(param.offset >= 0
? param.offset
: std::numeric_limits<std::size_t>::max() + param.offset +
1);

ASSERT_EQ(0, fifo.readAvailable());
ASSERT_EQ(100, fifo.write(data.data(), 100));
ASSERT_EQ(50, fifo.flushReadData(50));
ASSERT_EQ(100, fifo.flushReadData(10000));
ASSERT_EQ(1024, fifo.write(data.data(), 10000));
ASSERT_EQ(1024, fifo.readAvailable());
}

TEST_F(FifoTest, readWriteStressTest) {
std::vector<uint32_t> wdata(1024);
std::vector<uint32_t> rdata(1024);
FIFO<uint32_t> fifo(1024);
uint32_t k = 0;
uint32_t j = 0;

std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(0, 300);

while (k < 1000000) {
uint32_t n = dist(mt);
if (n < fifo.writeAvailable()) {
for (uint32_t i = 0; i < n; i++) {
wdata[i] = k++;
}
ASSERT_EQ(n, fifo.write(wdata.data(), n));
}
n = dist(mt);
if (n < fifo.readAvailable()) {
ASSERT_EQ(n, fifo.read(rdata.data(), n));
for (uint32_t i = 0; i < n; i++) {
ASSERT_EQ(j++, rdata[i]);
}
}
}
ASSERT_EQ((param.offset + 50) % param.expectedBufferSize, fifo.flushReadData(50));
ASSERT_EQ((param.offset + 100) % param.expectedBufferSize, fifo.flushReadData(1000000));
ASSERT_EQ(param.expectedBufferSize, fifo.write(data.data(), 1000000));
ASSERT_EQ(param.expectedBufferSize, fifo.readAvailable());
}

TEST_F(FifoTest, readWriteStressTestWrapUint32) {
std::vector<uint32_t> wdata(1024);
std::vector<uint32_t> rdata(1024);
FIFO<uint32_t> fifo(1024);
TEST_P(FifoTest, readWriteStressTest) {
const auto param = FifoTest::GetParam();
std::vector<uint32_t> data(param.expectedBufferSize);
FIFO<uint32_t> fifo(param.requestedBufferSize);
fifo.releaseReadRegions(param.offset >= 0
? param.offset
: std::numeric_limits<std::size_t>::max() + param.offset +
1);
fifo.releaseWriteRegions(param.offset >= 0
? param.offset
: std::numeric_limits<std::size_t>::max() + param.offset +
1);

std::vector<uint32_t> wdata(param.expectedBufferSize + param.expectedBufferSize / 10);
std::vector<uint32_t> rdata(param.expectedBufferSize + param.expectedBufferSize / 10);
uint32_t k = 0;
uint32_t j = 0;

std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(0, 300);

fifo.releaseReadRegions(std::numeric_limits<uint32_t>::max() - 1000);
fifo.releaseWriteRegions(std::numeric_limits<uint32_t>::max() - 1000);
std::uniform_int_distribution<std::size_t> dist(
0, param.expectedBufferSize + param.expectedBufferSize / 10);

while (k < 1000000) {
uint32_t n = dist(mt);
if (n < fifo.writeAvailable()) {
for (uint32_t i = 0; i < n; i++) {
wdata[i] = k++;
}
ASSERT_EQ(n, fifo.write(wdata.data(), n));
std::size_t n = dist(mt);
std::size_t m = std::min(n, fifo.writeAvailable());
for (std::size_t i = 0; i < m; i++) {
wdata[i] = k++;
}
ASSERT_EQ(m, fifo.write(wdata.data(), n));
n = dist(mt);
if (n < fifo.readAvailable()) {
ASSERT_EQ(n, fifo.read(rdata.data(), n));
for (uint32_t i = 0; i < n; i++) {
ASSERT_EQ(j++, rdata[i]);
}
m = std::min(n, fifo.readAvailable());
ASSERT_EQ(m, fifo.read(rdata.data(), n));
for (std::size_t i = 0; i < m; i++) {
ASSERT_EQ(j++, rdata[i]);
}
}
}

TEST_F(FifoTest, readWriteStressTestRegions) {
FIFO<uint32_t> fifo(1024);
TEST_P(FifoTest, readWriteStressTestRegions) {
const auto param = FifoTest::GetParam();
std::vector<uint32_t> data(param.expectedBufferSize);
FIFO<uint32_t> fifo(param.requestedBufferSize);
fifo.releaseReadRegions(param.offset >= 0
? param.offset
: std::numeric_limits<std::size_t>::max() + param.offset +
1);
fifo.releaseWriteRegions(param.offset >= 0
? param.offset
: std::numeric_limits<std::size_t>::max() + param.offset +
1);

std::vector<uint32_t> wdata(param.expectedBufferSize + param.expectedBufferSize / 10);
std::vector<uint32_t> rdata(param.expectedBufferSize + param.expectedBufferSize / 10);
uint32_t k = 0;
uint32_t j = 0;

std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(0, 300);
std::uniform_int_distribution<std::size_t> dist(
0, param.expectedBufferSize + param.expectedBufferSize / 10);

while (k < 1000000) {
uint32_t n = dist(mt);
std::size_t n = dist(mt);
std::size_t m = std::min(n, fifo.writeAvailable());
uint32_t* ptr1;
uint32_t size1;
std::size_t size1;
uint32_t* ptr2;
uint32_t size2;
n = fifo.aquireWriteRegions(n, &ptr1, &size1, &ptr2, &size2);
for (uint32_t i = 0; i < size1; i++) {
std::size_t size2;
ASSERT_EQ(m, fifo.aquireWriteRegions(n, &ptr1, &size1, &ptr2, &size2));
ASSERT_EQ(m, size1 + size2);
for (std::size_t i = 0; i < size1; i++) {
ptr1[i] = k++;
}
for (uint32_t i = 0; i < size2; i++) {
for (std::size_t i = 0; i < size2; i++) {
ptr2[i] = k++;
}
fifo.releaseWriteRegions(n);
fifo.releaseWriteRegions(m);
n = dist(mt);
n = fifo.aquireReadRegions(n, &ptr1, &size1, &ptr2, &size2);
for (uint32_t i = 0; i < size1; i++) {
m = std::min(n, fifo.readAvailable());
ASSERT_EQ(m, fifo.aquireReadRegions(n, &ptr1, &size1, &ptr2, &size2));
ASSERT_EQ(m, size1 + size2);
for (std::size_t i = 0; i < size1; i++) {
ASSERT_EQ(j++, ptr1[i]);
}
for (uint32_t i = 0; i < size2; i++) {
for (std::size_t i = 0; i < size2; i++) {
ASSERT_EQ(j++, ptr2[i]);
}
fifo.releaseReadRegions(n);
fifo.releaseReadRegions(m);
}
}

TEST_F(FifoTest, readWriteStressTestRegionsWrapUint32) {
FIFO<uint32_t> fifo(1024);
uint32_t k = 0;
uint32_t j = 0;

std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(0, 300);

fifo.releaseReadRegions(std::numeric_limits<uint32_t>::max() - 1000);
fifo.releaseWriteRegions(std::numeric_limits<uint32_t>::max() - 1000);

while (k < 1000000) {
uint32_t n = dist(mt);
uint32_t* ptr1;
uint32_t size1;
uint32_t* ptr2;
uint32_t size2;
n = fifo.aquireWriteRegions(n, &ptr1, &size1, &ptr2, &size2);
for (uint32_t i = 0; i < size1; i++) {
ptr1[i] = k++;
}
for (uint32_t i = 0; i < size2; i++) {
ptr2[i] = k++;
}
fifo.releaseWriteRegions(n);
n = dist(mt);
n = fifo.aquireReadRegions(n, &ptr1, &size1, &ptr2, &size2);
for (uint32_t i = 0; i < size1; i++) {
ASSERT_EQ(j++, ptr1[i]);
}
for (uint32_t i = 0; i < size2; i++) {
ASSERT_EQ(j++, ptr2[i]);
}
fifo.releaseReadRegions(n);
}
}
INSTANTIATE_TEST_SUITE_P(FifoTestSuite,
FifoTest,
testing::Values(
Param{1024, 1024, 0},
Param{1024, 1024, 1200},
Param{1024, 1024, -1200},
Param{65536, 65536, 0},
Param{65536, 65536, 1200},
Param{65536, 65536, -1200},
Param{1234, 2048, 0},
Param{1234, 2048, -1200},
Param{1234, 2048, 1200}));

} // namespace
Loading

0 comments on commit b2bcb3d

Please sign in to comment.