Skip to content

Commit f858054

Browse files
authored
Merge pull request #2 from devscafecommunity/feature/fase-1-test-system
Feature/fase 1 test system
2 parents 8d58880 + 56b6975 commit f858054

29 files changed

Lines changed: 20389 additions & 0 deletions

.github/workflows/test.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [ main, develop, 'feature/**' ]
6+
pull_request:
7+
branches: [ main, develop ]
8+
9+
jobs:
10+
build-and-test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Install compiler
18+
run: sudo apt-get update && sudo apt-get install -y g++-13
19+
20+
- name: Configure
21+
run: mkdir -p build && cd build && cmake .. -DCMAKE_CXX_COMPILER=g++-13 -DCMAKE_CXX_STANDARD=20
22+
23+
- name: Build
24+
run: cd build && cmake --build .
25+
26+
- name: Run Tests
27+
run: cd build && ./tests/CaffeineTest ~[stress] --success

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,23 @@
3939

4040
# debug information files
4141
*.dwo
42+
43+
# Build directories
44+
build/
45+
build-*/
46+
47+
# CMake
48+
CMakeCache.txt
49+
CMakeFiles/
50+
cmake_install.cmake
51+
Makefile
52+
*.sln
53+
*.vcxproj
54+
*.vcxproj.filters
55+
56+
# Compiled commands
57+
compile_commands.json
58+
59+
# Test output
60+
Testing/
61+
CTestTestfile.cmake

CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(Caffeine VERSION 0.1.0 LANGUAGES CXX)
4+
5+
set(CMAKE_CXX_STANDARD 20)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
set(CMAKE_CXX_EXTENSIONS OFF)
8+
9+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
10+
11+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
12+
add_compile_definitions(CF_DEBUG)
13+
endif()
14+
15+
add_library(Caffeine INTERFACE)
16+
target_include_directories(Caffeine INTERFACE
17+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
18+
$<INSTALL_INTERFACE:include>
19+
)
20+
21+
add_library(Caffeine::Core ALIAS Caffeine)
22+
23+
add_subdirectory(tests)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Test System Design - Caffeine Engine
2+
3+
## Overview
4+
5+
This document describes the test system implementation for Caffeine Engine, including framework selection, test organization, CI integration, and success criteria.
6+
7+
## Framework Selection
8+
9+
- **Catch2 v3.x** - Header-only C++ testing framework
10+
- Rationale: Simple integration, no external dependencies, follows Caffeine's "zero dependency" philosophy as much as a test framework allows
11+
12+
## File Structure
13+
14+
```
15+
tests/
16+
├── Catch2/ # Header-only framework
17+
│ └── catch.hpp # Main header
18+
├── test_allocators.cpp # Memory allocator tests
19+
├── test_containers.cpp # Container tests
20+
├── test_math.cpp # Math library tests
21+
├── test_core.cpp # Core module tests
22+
├── benchmarks.cpp # Performance benchmarks
23+
├── CMakeLists.txt # Test build configuration
24+
└── README.md # Test documentation
25+
```
26+
27+
## Test Categories
28+
29+
### 1. Functional Tests
30+
31+
Verify correct behavior of all components under normal and edge conditions.
32+
33+
### 2. Stress Tests
34+
35+
- Memory: 1M allocations, verify zero leaks, fragmentation < 0.1%
36+
- Containers: Large dataset handling
37+
- Math: Precision under extreme values
38+
39+
### 3. Benchmark Tests
40+
41+
Establish performance baselines for CI regression detection.
42+
43+
## GitHub Actions CI
44+
45+
```yaml
46+
name: Tests
47+
on: [push, pull_request]
48+
jobs:
49+
build-and-test:
50+
runs-on: ubuntu-latest
51+
strategy:
52+
matrix:
53+
compiler: [gcc-13, clang-17]
54+
steps:
55+
- uses: actions/checkout@v4
56+
- name: Install compiler
57+
run: sudo apt-get install ${{ matrix.compiler }}
58+
- name: Configure
59+
run: cmake -B build -DCMAKE_CXX_COMPILER=${{ matrix.compiler }}
60+
- name: Build
61+
run: cmake --build build
62+
- name: Run Tests
63+
run: ./build/CaffeineTest
64+
- name: Run Benchmarks
65+
run: ./build/CaffeineBenchmarks
66+
```
67+
68+
## Success Criteria
69+
70+
- [x] Design approved
71+
- [ ] Catch2 integrated
72+
- [ ] All module tests functional
73+
- [ ] Stress tests pass (1M allocs)
74+
- [ ] Benchmarks established
75+
- [ ] CI pipeline green

src/Caffeine.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include "core/Types.hpp"
4+
#include "core/Platform.hpp"
5+
#include "core/Compiler.hpp"
6+
#include "core/Assertions.hpp"
7+
8+
#include "memory/Allocator.hpp"
9+
#include "memory/LinearAllocator.hpp"
10+
#include "memory/PoolAllocator.hpp"
11+
#include "memory/StackAllocator.hpp"
12+
13+
#include "containers/Vector.hpp"
14+
#include "containers/HashMap.hpp"
15+
#include "containers/StringView.hpp"
16+
#include "containers/FixedString.hpp"
17+
18+
#include "math/Vec2.hpp"
19+
#include "math/Vec3.hpp"
20+
#include "math/Vec4.hpp"
21+
#include "math/Mat4.hpp"
22+
#include "math/Math.hpp"

src/containers/FixedString.hpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#pragma once
2+
3+
#include "../core/Types.hpp"
4+
5+
namespace Caffeine {
6+
7+
template<usize N>
8+
class FixedString {
9+
public:
10+
FixedString() = default;
11+
12+
FixedString(const char* str) {
13+
if (str) {
14+
usize len = 0;
15+
while (str[len] && len < N - 1) {
16+
m_data[len] = str[len];
17+
++len;
18+
}
19+
m_data[len] = '\0';
20+
m_length = len;
21+
}
22+
}
23+
24+
const char* cStr() const { return m_data; }
25+
char* data() { return m_data; }
26+
const char* data() const { return m_data; }
27+
28+
usize length() const { return m_length; }
29+
usize size() const { return m_length; }
30+
usize capacity() const { return N; }
31+
bool empty() const { return m_length == 0; }
32+
bool full() const { return m_length >= N - 1; }
33+
34+
void clear() {
35+
m_data[0] = '\0';
36+
m_length = 0;
37+
}
38+
39+
void append(const char* str) {
40+
if (!str) return;
41+
while (*str && m_length < N - 1) {
42+
m_data[m_length++] = *str++;
43+
}
44+
m_data[m_length] = '\0';
45+
}
46+
47+
void append(char c) {
48+
if (m_length < N - 1) {
49+
m_data[m_length++] = c;
50+
m_data[m_length] = '\0';
51+
}
52+
}
53+
54+
bool operator==(const FixedString& other) const {
55+
if (m_length != other.m_length) return false;
56+
for (usize i = 0; i < m_length; ++i) {
57+
if (m_data[i] != other.m_data[i]) return false;
58+
}
59+
return true;
60+
}
61+
62+
bool operator!=(const FixedString& other) const {
63+
return !(*this == other);
64+
}
65+
66+
char& operator[](usize idx) { return m_data[idx]; }
67+
const char& operator[](usize idx) const { return m_data[idx]; }
68+
69+
private:
70+
char m_data[N] = {};
71+
usize m_length = 0;
72+
};
73+
74+
} // namespace Caffeine

src/containers/HashMap.hpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#pragma once
2+
3+
#include "../core/Types.hpp"
4+
#include "Vector.hpp"
5+
6+
namespace Caffeine {
7+
8+
template<typename Key, typename Value>
9+
class HashMap {
10+
public:
11+
using Pair = struct { Key key; Value value; };
12+
13+
HashMap() = default;
14+
explicit HashMap(usize capacity);
15+
16+
Value* get(const Key& key) {
17+
for (auto& pair : m_data) {
18+
if (pair.key == key) return &pair.value;
19+
}
20+
return nullptr;
21+
}
22+
23+
const Value* get(const Key& key) const {
24+
for (const auto& pair : m_data) {
25+
if (pair.key == key) return &pair.value;
26+
}
27+
return nullptr;
28+
}
29+
30+
void set(const Key& key, const Value& value) {
31+
for (auto& pair : m_data) {
32+
if (pair.key == key) {
33+
pair.value = value;
34+
return;
35+
}
36+
}
37+
m_data.pushBack({key, value});
38+
}
39+
40+
bool contains(const Key& key) const {
41+
for (const auto& pair : m_data) {
42+
if (pair.key == key) return true;
43+
}
44+
return false;
45+
}
46+
47+
void remove(const Key& key) {
48+
for (usize i = 0; i < m_data.size(); ++i) {
49+
if (m_data[i].key == key) {
50+
m_data[i] = m_data.back();
51+
m_data.popBack();
52+
return;
53+
}
54+
}
55+
}
56+
57+
void clear() { m_data.clear(); }
58+
usize size() const { return m_data.size(); }
59+
bool empty() const { return m_data.empty(); }
60+
61+
private:
62+
Vector<Pair> m_data;
63+
};
64+
65+
template<typename Key, typename Value>
66+
HashMap<Key, Value>::HashMap(usize capacity) {
67+
m_data.reserve(capacity);
68+
}
69+
70+
} // namespace Caffeine

src/containers/StringView.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include "../core/Types.hpp"
4+
5+
namespace Caffeine {
6+
7+
class StringView {
8+
public:
9+
StringView() = default;
10+
StringView(const char* str, usize len) : m_data(str), m_length(len) {}
11+
StringView(const char* str) : m_data(str), m_length(str ? calculateLength(str) : 0) {}
12+
13+
const char* data() const { return m_data; }
14+
usize length() const { return m_length; }
15+
usize size() const { return m_length; }
16+
bool empty() const { return m_length == 0; }
17+
18+
const char& operator[](usize idx) const { return m_data[idx]; }
19+
20+
int compare(StringView other) const {
21+
usize minLen = (m_length < other.m_length) ? m_length : other.m_length;
22+
for (usize i = 0; i < minLen; ++i) {
23+
if (m_data[i] != other.m_data[i]) {
24+
return (m_data[i] < other.m_data[i]) ? -1 : 1;
25+
}
26+
}
27+
return (m_length == other.m_length) ? 0 : (m_length < other.m_length) ? -1 : 1;
28+
}
29+
30+
bool operator==(StringView other) const { return compare(other) == 0; }
31+
bool operator!=(StringView other) const { return compare(other) != 0; }
32+
bool operator<(StringView other) const { return compare(other) < 0; }
33+
34+
private:
35+
static usize calculateLength(const char* str) {
36+
usize len = 0;
37+
while (str[len]) ++len;
38+
return len;
39+
}
40+
41+
const char* m_data = nullptr;
42+
usize m_length = 0;
43+
};
44+
45+
} // namespace Caffeine

0 commit comments

Comments
 (0)