commit d2714daaa2934fdc68231ef6953c86deb447907a Author: Riyyi Date: Mon Aug 25 19:48:17 2025 +0200 Initial commit diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..300ddea --- /dev/null +++ b/.clang-format @@ -0,0 +1,41 @@ +# -*- yaml -*- + +--- +BasedOnStyle: WebKit +IndentWidth: 4 +--- +Language: Cpp + +AlignAfterOpenBracket: Align +AlignEscapedNewlines: Left +AlignOperands: Align +AlignTrailingComments: true + +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortCaseLabelsOnASingleLine: true +AllowShortLambdasOnASingleLine: All + +AlwaysBreakTemplateDeclarations: Yes +IndentPPDirectives: BeforeHash +RequiresClausePosition: SingleLine + +BraceWrapping: + AfterEnum: false + AfterFunction: true + BeforeCatch: true + BeforeElse: true + BeforeLambdaBody: false + SplitEmptyRecord: false +BreakBeforeBraces: Custom +BreakInheritanceList: BeforeComma + +SpaceAfterTemplateKeyword: false +SpaceInEmptyBlock: false +NamespaceIndentation: None +FixNamespaceComments: true +Standard: c++20 +TabWidth: 4 +UseTab: AlignWithSpaces +... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fe4e9e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# Directories + +.cache/ +.clangd/ +build/ + +# Files diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f837dfe --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/munit"] + path = vendor/munit + url = https://github.com/nemequ/munit diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b6f6da2 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,62 @@ +set(PROJECT "bootdev") + +cmake_minimum_required(VERSION 3.16) +project(${PROJECT} C) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) + +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Og -g -pg") + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# ------------------------------------------ +# Library µnit + +file(GLOB_RECURSE LIBRARY_SOURCES "vendor/munit/*.c") + +add_library(munit ${LIBRARY_SOURCES}) + +target_include_directories(munit PUBLIC + "vendor/munit") + +# ------------------------------------------ +# Executable + +file(GLOB LESSONS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src/*) + +set(ALL_RUN "") +set(ALL_TARGETS "") +foreach(LESSON ${LESSONS}) + if(NOT IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${LESSON}) + continue() + endif() + + file(GLOB LESSON_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${LESSON}/*.c) + if(NOT LESSON_SOURCES) + continue() + endif() + + # Get last directory name of the path to use as exe + get_filename_component(EXE_NAME ${LESSON} NAME) + + add_executable(${EXE_NAME} ${LESSON_SOURCES}) + target_include_directories(${EXE_NAME} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/${LESSON} + "vendor/munit") + target_link_libraries(${EXE_NAME} munit) + + # Collect all the directory names for the run target + list(APPEND ALL_RUN COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}) + list(APPEND ALL_TARGETS ${EXE_NAME}) +endforeach() + +# ------------------------------------------ +# Execute target + +add_custom_target(run + ${ALL_RUN}) + +foreach(target ${ALL_TARGETS}) + add_dependencies(run ${target}) +endforeach() diff --git a/compile_commands.json b/compile_commands.json new file mode 120000 index 0000000..25eb4b2 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1 @@ +build/compile_commands.json \ No newline at end of file diff --git a/src/4-1/color.h b/src/4-1/color.h new file mode 100644 index 0000000..9a43aa1 --- /dev/null +++ b/src/4-1/color.h @@ -0,0 +1,5 @@ +typedef enum Color { + RED, + GREEN, + BLUE, +} color_t; diff --git a/src/4-1/main.c b/src/4-1/main.c new file mode 100644 index 0000000..4859406 --- /dev/null +++ b/src/4-1/main.c @@ -0,0 +1,27 @@ +#include "munit.h" + +#include "color.h" + +munit_case(RUN, test_color_enum1, { + assert_int(RED, ==, 0, "RED is defined as 0"); + assert_int(GREEN, ==, 1, "GREEN is defined as 1"); + assert_int(BLUE, ==, 2, "BLUE is defined as 2"); +}); + +munit_case(SUBMIT, test_color_enum2, { + assert_int(RED, !=, 4, "RED is not defined as 4"); + assert_int(GREEN, !=, 2, "GREEN is not defined as 2"); + assert_int(BLUE, !=, 0, "BLUE is not defined as 0"); +}); + +int main() { + MunitTest tests[] = { + munit_test("/are_defined", test_color_enum1), + munit_test("/are_defined_correctly", test_color_enum2), + munit_null_test, + }; + + MunitSuite suite = munit_suite("colors", tests); + + return munit_suite_main(&suite, NULL, 0, NULL); +} diff --git a/src/4-2/color.h b/src/4-2/color.h new file mode 100644 index 0000000..de19a7b --- /dev/null +++ b/src/4-2/color.h @@ -0,0 +1,5 @@ +typedef enum Color { + RED = 55, + GREEN = 176, + BLUE = 38, +} color_t; diff --git a/src/4-2/main.c b/src/4-2/main.c new file mode 100644 index 0000000..e4d8432 --- /dev/null +++ b/src/4-2/main.c @@ -0,0 +1,27 @@ +#include "munit.h" + +#include "color.h" + +munit_case(RUN, test_colors_defined, { + assert_int(RED, ==, 55, "RED is defined as 55 (nvim green!)"); + assert_int(GREEN, ==, 176, "GREEN is defined as 176 (nvim green!)"); + assert_int(BLUE, ==, 38, "BLUE is defined as 38 (nvim green!)"); +}); + +munit_case(SUBMIT, test_colors_defined_correctly, { + assert_int(RED, !=, 0, "RED is not defined as 0 (vsc*de blue!)"); + assert_int(GREEN, !=, 120, "GREEN is not defined as 120 (vsc*de blue!)"); + assert_int(BLUE, !=, 215, "BLUE is not defined as 215 (vsc*de blue!)"); +}); + +int main() { + MunitTest tests[] = { + munit_test("/defined", test_colors_defined), + munit_test("/defined_vscode", test_colors_defined_correctly), + munit_null_test, + }; + + MunitSuite suite = munit_suite("colors", tests); + + return munit_suite_main(&suite, NULL, 0, NULL); +} diff --git a/src/4-3/http.c b/src/4-3/http.c new file mode 100644 index 0000000..583ac8c --- /dev/null +++ b/src/4-3/http.c @@ -0,0 +1,7 @@ +#include "http.h" + +char* http_to_str(http_error_code_t code) +{ + // ? + return 0; +} diff --git a/src/4-3/http.h b/src/4-3/http.h new file mode 100644 index 0000000..584029e --- /dev/null +++ b/src/4-3/http.h @@ -0,0 +1,11 @@ +#pragma once + +typedef enum { + HTTP_BAD_REQUEST = 400, + HTTP_UNAUTHORIZED = 401, + HTTP_NOT_FOUND = 404, + HTTP_TEAPOT = 418, + HTTP_INTERNAL_SERVER_ERROR = 500 +} http_error_code_t; + +char* http_to_str(http_error_code_t code); diff --git a/src/4-3/main.c b/src/4-3/main.c new file mode 100644 index 0000000..6c5973a --- /dev/null +++ b/src/4-3/main.c @@ -0,0 +1,27 @@ +#include "munit.h" + +#include "http.h" + +munit_case(RUN, test_switch_enum, { + assert_string_equal(http_to_str(HTTP_BAD_REQUEST), "400 Bad Request", ""); + assert_string_equal(http_to_str(HTTP_UNAUTHORIZED), "401 Unauthorized", ""); + assert_string_equal(http_to_str(HTTP_NOT_FOUND), "404 Not Found", ""); + assert_string_equal(http_to_str(HTTP_TEAPOT), "418 I AM A TEAPOT!", ""); + assert_string_equal(http_to_str(HTTP_INTERNAL_SERVER_ERROR), "500 Internal Server Error", ""); +}); + +munit_case(SUBMIT, test_switch_enum_default, { + assert_string_equal(http_to_str((http_error_code_t)999), "Unknown HTTP status code", ""); +}); + +int main() { + MunitTest tests[] = { + munit_test("/switch_enum", test_switch_enum), + munit_test("/switch_enum_default", test_switch_enum_default), + munit_null_test, + }; + + MunitSuite suite = munit_suite("http", tests); + + return munit_suite_main(&suite, NULL, 0, NULL); +} diff --git a/vendor/munit b/vendor/munit new file mode 160000 index 0000000..fbbdf14 --- /dev/null +++ b/vendor/munit @@ -0,0 +1 @@ +Subproject commit fbbdf1467eb0d04a6ee465def2e529e4c87f2118