Skip to content

Commit

Permalink
Merge pull request #2 from dr8co/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dr8co authored Apr 6, 2024
2 parents 6667b56 + f316636 commit 9c3da62
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 253 deletions.
52 changes: 31 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
cmake_minimum_required(VERSION 3.28)
cmake_minimum_required(VERSION 3.27)

project(Cachetron C CXX)

set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Google Test requires at least C++14
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Check if the system is Linux
if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
message(FATAL_ERROR "This program only targets Linux.")
Expand All @@ -22,6 +27,7 @@ endif ()

# Additional options for the Debug build
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -Wall -Wextra -Werror -Wpedantic")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wpedantic")

# Sanitizers for debugging (Disabled by default, slows down the program. Uncomment to enable.)
# GCC does not support all sanitizers, so Clang is recommended for this purpose. Requires llvm-symbolizer.
Expand All @@ -37,36 +43,40 @@ if (${CMAKE_C_COMPILER_ID} STREQUAL "Clang")
# set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS} -fsanitize=memory -fPIE -fno-optimize-sibling-calls -fno-omit-frame-pointer -g -O1")
endif ()

# Tests
add_subdirectory(tests)
# Google Test
include(FetchContent)

FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/f10e11fb27301fba21caa71030bb5024e67aa135.zip
)

FetchContent_MakeAvailable(googletest)

enable_testing()

# Data structures
file(GLOB_RECURSE DS_SOURCES
data_structures/*.c
data_structures/*.h)

# server executable
add_executable(server
server.c
common.h
thread_pool.c
thread_pool.h
commands.h
data_structures/trees/avl.c
data_structures/trees/avl.h
data_structures/vector/vector_c.c
data_structures/vector/vector_c.h
data_structures/string/string_c.c
data_structures/string/string_c.h
data_structures/hashmap/hashtable.c
data_structures/hashmap/hashtable.h
data_structures/set/zset.c
data_structures/set/zset.h
data_structures/list/list.c
data_structures/list/list.h
data_structures/trees/heap.c
data_structures/trees/heap.h
data_structures/queue/deque_c.c
data_structures/queue/deque_c.h)
${DS_SOURCES})

# client executable
add_executable(client
client.c
common.h
data_structures/vector/vector_c.c
data_structures/vector/vector_c.h
data_structures/string/string_c.c
data_structures/string/string_c.h
common.h)
data_structures/string/string_c.h)

# Tests
add_subdirectory(tests)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Cachetron uses CMake as its build system, and **it is intended for Linux systems

To build Cachetron, you will need to have the following installed:

- `CMake` 3.28 or later
- `CMake` 3.27 or later
- `Clang 18` and later or `GCC 13` and later. **C23 support is required**.
- `Ninja` 1.11 or later (optional, but recommended)
- `Python` 3.11 or later (optional, for running tests)
Expand Down
28 changes: 25 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
add_executable(test_vector test_vector.c ../data_structures/vector/vector_c.c ../data_structures/vector/vector_c.h)
# Test executables
add_executable(test_vector
test_vector.cpp
../data_structures/vector/vector_c.c
../data_structures/vector/vector_c.h)

add_executable(test_string test_string.c ../data_structures/string/string_c.c ../data_structures/string/string_c.h)
add_executable(test_string
test_string.cpp
../data_structures/string/string_c.c
../data_structures/string/string_c.h)

add_executable(test_deque test_deque.c ../data_structures/queue/deque_c.c ../data_structures/queue/deque_c.h)
add_executable(test_deque
test_deque.cpp
../data_structures/queue/deque_c.c
../data_structures/queue/deque_c.h)

# Link test executables with gtest
target_link_libraries(test_vector gtest gtest_main)
target_link_libraries(test_string gtest gtest_main)
target_link_libraries(test_deque gtest gtest_main)

include(GoogleTest)

# Discover tests
gtest_discover_tests(test_vector)
gtest_discover_tests(test_string)
gtest_discover_tests(test_deque)
68 changes: 0 additions & 68 deletions tests/test_deque.c

This file was deleted.

58 changes: 58 additions & 0 deletions tests/test_deque.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <gtest/gtest.h>
#include "../data_structures/queue/deque_c.h"

TEST(DequeCTest, CreateDequeShouldInitializeEmptyDeque) {
Deque *deque = create_deque();
ASSERT_NE(deque, nullptr);
ASSERT_TRUE(deque_empty(deque));
ASSERT_EQ(deque_size(deque), 0);
destroy_deque(deque);
}

TEST(DequeCTest, DequePushFrontShouldAddElementToFront) {
Deque *deque = create_deque();
int data = 5;
ASSERT_TRUE(deque_push_front(deque, &data));
ASSERT_EQ(deque_size(deque), 1);
ASSERT_EQ(*(int *) deque_front(deque), data);
destroy_deque(deque);
}

TEST(DequeCTest, DequePushBackShouldAddElementToBack) {
Deque *deque = create_deque();
int data = 5;
ASSERT_TRUE(deque_push_back(deque, &data));
ASSERT_EQ(deque_size(deque), 1);
ASSERT_EQ(*(int *) deque_back(deque), data);
destroy_deque(deque);
}

TEST(DequeCTest, DequePopFrontShouldRemoveAndReturnFrontElement) {
Deque *deque = create_deque();
int data = 5;
deque_push_front(deque, &data);
ASSERT_EQ(*(int *) deque_pop_front(deque), data);
ASSERT_TRUE(deque_empty(deque));
destroy_deque(deque);
}

TEST(DequeCTest, DequePopBackShouldRemoveAndReturnBackElement) {
Deque *deque = create_deque();
int data = 5;
deque_push_back(deque, &data);
ASSERT_EQ(*(int *) deque_pop_back(deque), data);
ASSERT_TRUE(deque_empty(deque));
destroy_deque(deque);
}

TEST(DequeCTest, DequeResizeShouldDoubleCapacityWhenFull) {
Deque *deque = create_deque();
for (int i = 0; i < 16; ++i) {
deque_push_back(deque, &i);
}
ASSERT_EQ(deque_size(deque), 16);
int data = 17;
ASSERT_TRUE(deque_push_back(deque, &data));
ASSERT_EQ(deque_size(deque), 17);
destroy_deque(deque);
}
9 changes: 4 additions & 5 deletions tests/test_string.c → tests/test_string.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include "../data_structures/string/string_c.h"

void string_new_creates_empty_string() {
Expand Down
Loading

0 comments on commit 9c3da62

Please sign in to comment.