Skip to content

Commit a4cffb9

Browse files
author
Zind
committed
_
1 parent dcef01d commit a4cffb9

File tree

7 files changed

+154
-4
lines changed

7 files changed

+154
-4
lines changed

CMakeLists.txt

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1-
cmake_minimum_required(VERSION 3.16)
2-
project(hello_cpp_codespace VERSION 0.1.0 LANGUAGES CXX)
1+
cmake_minimum_required(VERSION 3.22)
2+
project(hello_cpp_codespace LANGUAGES CXX)
33

44
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6-
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
6+
set(CMAKE_CXX_EXTENSIONS OFF)
77

8-
add_executable(hello src/main.cpp)
8+
# Warnings for GCC/Clang
9+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
10+
add_compile_options(-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion)
11+
endif()
12+
13+
# Library (example)
14+
add_library(mylib src/lib.cpp)
15+
target_include_directories(mylib PUBLIC include)
16+
17+
# Tests with Catch2 (via FetchContent)
18+
include(FetchContent)
19+
FetchContent_Declare(
20+
Catch2
21+
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
22+
GIT_TAG v3.5.4
23+
)
24+
FetchContent_MakeAvailable(Catch2)
25+
26+
add_executable(tests
27+
tests/test_sum.cpp
28+
tests/test_ring_buffer.cpp
29+
)
30+
target_link_libraries(tests PRIVATE mylib Catch2::Catch2WithMain)
31+
include(CTest)
32+
include(Catch)
33+
catch_discover_tests(tests)

CMakePresets_Version2.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": 5,
3+
"cmakeMinimumRequired": { "major": 3, "minor": 22 },
4+
"configurePresets": [
5+
{
6+
"name": "dev-debug",
7+
"displayName": "Dev Debug",
8+
"generator": "Ninja",
9+
"binaryDir": "build",
10+
"cacheVariables": {
11+
"CMAKE_BUILD_TYPE": "Debug",
12+
"CMAKE_CXX_STANDARD": "17",
13+
"CMAKE_CXX_STANDARD_REQUIRED": "ON",
14+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
15+
}
16+
}
17+
],
18+
"buildPresets": [
19+
{ "name": "dev", "configurePreset": "dev-debug" }
20+
],
21+
"testPresets": [
22+
{ "name": "dev", "configurePreset": "dev-debug", "output": { "outputOnFailure": true } }
23+
]
24+
}

include/lib.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
#include <vector>
3+
4+
int sum(const std::vector<int>& v);

include/ring_buffer.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
#include <array>
3+
#include <cstddef>
4+
#include <optional>
5+
#include <utility>
6+
7+
template <typename T, std::size_t N>
8+
class RingBuffer {
9+
static_assert(N > 0, "N must be > 0");
10+
public:
11+
RingBuffer() = default;
12+
13+
bool push(const T& v) {
14+
if (full()) return false;
15+
buf_[head_] = v;
16+
head_ = (head_ + 1) % N;
17+
++count_;
18+
return true;
19+
}
20+
bool push(T&& v) {
21+
if (full()) return false;
22+
buf_[head_] = std::move(v);
23+
head_ = (head_ + 1) % N;
24+
++count_;
25+
return true;
26+
}
27+
std::optional<T> pop() {
28+
if (empty()) return std::nullopt;
29+
T out = std::move(buf_[tail_]);
30+
tail_ = (tail_ + 1) % N;
31+
--count_;
32+
return out;
33+
}
34+
const T* peek() const {
35+
if (empty()) return nullptr;
36+
return &buf_[tail_];
37+
}
38+
39+
bool empty() const noexcept { return count_ == 0; }
40+
bool full() const noexcept { return count_ == N; }
41+
std::size_t size() const noexcept { return count_; }
42+
std::size_t capacity() const noexcept { return N; }
43+
44+
private:
45+
std::array<T, N> buf_{};
46+
std::size_t head_ = 0; // next write
47+
std::size_t tail_ = 0; // next read
48+
std::size_t count_ = 0; // items in buffer
49+
};

src/libb.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "lib.hpp"
2+
3+
int sum(const std::vector<int>& v) {
4+
int s = 0;
5+
for (int x : v) s += x;
6+
return s;
7+
}

tests/test_ring_buffer.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
#include "ring_buffer.hpp"
3+
#include <string>
4+
5+
TEST_CASE("RingBuffer basic push/pop") {
6+
RingBuffer<int, 3> rb;
7+
REQUIRE(rb.empty());
8+
REQUIRE(rb.push(1));
9+
REQUIRE(rb.push(2));
10+
REQUIRE(rb.push(3));
11+
REQUIRE(rb.full());
12+
REQUIRE_FALSE(rb.push(4));
13+
14+
auto a = rb.pop(); REQUIRE(a && *a == 1);
15+
auto b = rb.pop(); REQUIRE(b && *b == 2);
16+
auto c = rb.pop(); REQUIRE(c && *c == 3);
17+
REQUIRE(rb.empty());
18+
REQUIRE_FALSE(rb.pop().has_value());
19+
}
20+
21+
TEST_CASE("RingBuffer wraparound and peek") {
22+
RingBuffer<std::string, 2> rb;
23+
REQUIRE(rb.push("A"));
24+
REQUIRE(rb.push("B"));
25+
REQUIRE(rb.full());
26+
REQUIRE(std::string(*rb.peek()) == "A");
27+
REQUIRE(rb.pop().value() == "A");
28+
REQUIRE(rb.push("C"));
29+
REQUIRE(rb.full());
30+
REQUIRE(rb.pop().value() == "B");
31+
REQUIRE(rb.pop().value() == "C");
32+
REQUIRE(rb.empty());
33+
}

tests/test_sum.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
#include "lib.hpp"
3+
4+
TEST_CASE("sum works") {
5+
REQUIRE(sum({}) == 0);
6+
REQUIRE(sum({1}) == 1);
7+
REQUIRE(sum({1,2,3}) == 6);
8+
}

0 commit comments

Comments
 (0)