add Chapter03

This commit is contained in:
Amar Mahmutbegovic
2025-04-02 23:02:38 +02:00
parent 0e79f982a2
commit 8b360ab9fc
3 changed files with 111 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
#include <array>
#include <cstdio>
#include "gtest/gtest.h"
template <class T, std::size_t N> struct ring_buffer {
std::array<T, N> arr;
std::size_t write_idx = 0;
std::size_t read_idx = 0;
std::size_t count = 0;
void push(T t) {
arr.at(write_idx) = t;
write_idx = (write_idx + 1) % N;
if (count < N) {
count++;
} else {
read_idx = (read_idx + 1) % N;
}
}
T pop() {
if (count == 0) {
return T{};
}
T value = arr.at(read_idx);
read_idx = (read_idx + 1) % N;
--count;
return value;
}
bool is_empty() const { return count == 0; }
std::size_t get_count() const { return count; }
};
TEST(RingBufferInt, PushPop) {
ring_buffer<int, 2> rb;
rb.push(1);
rb.push(2);
EXPECT_EQ(rb.pop(), 1);
EXPECT_EQ(rb.pop(), 2);
}
TEST(RingBufferInt, GetCount) {
ring_buffer<int, 20> rb;
for (int i = 0; i < 50; i++) {
rb.push(i);
}
EXPECT_EQ(rb.get_count(), 20);
for (int i = 0; i < 10; i++) {
rb.pop();
}
EXPECT_EQ(rb.get_count(), 10);
}
int main() {
testing::InitGoogleTest();
return RUN_ALL_TESTS();
}