Browse Source

Initial commit

master
Riyyi 2 weeks ago
commit
d2714daaa2
  1. 41
      .clang-format
  2. 7
      .gitignore
  3. 3
      .gitmodules
  4. 62
      CMakeLists.txt
  5. 1
      compile_commands.json
  6. 5
      src/4-1/color.h
  7. 27
      src/4-1/main.c
  8. 5
      src/4-2/color.h
  9. 27
      src/4-2/main.c
  10. 7
      src/4-3/http.c
  11. 11
      src/4-3/http.h
  12. 27
      src/4-3/main.c
  13. 1
      vendor/munit

41
.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
...

7
.gitignore vendored

@ -0,0 +1,7 @@
# Directories
.cache/
.clangd/
build/
# Files

3
.gitmodules vendored

@ -0,0 +1,3 @@
[submodule "vendor/munit"]
path = vendor/munit
url = https://github.com/nemequ/munit

62
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()

1
compile_commands.json

@ -0,0 +1 @@
build/compile_commands.json

5
src/4-1/color.h

@ -0,0 +1,5 @@
typedef enum Color {
RED,
GREEN,
BLUE,
} color_t;

27
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);
}

5
src/4-2/color.h

@ -0,0 +1,5 @@
typedef enum Color {
RED = 55,
GREEN = 176,
BLUE = 38,
} color_t;

27
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);
}

7
src/4-3/http.c

@ -0,0 +1,7 @@
#include "http.h"
char* http_to_str(http_error_code_t code)
{
// ?
return 0;
}

11
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);

27
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);
}

1
vendor/munit vendored

@ -0,0 +1 @@
Subproject commit fbbdf1467eb0d04a6ee465def2e529e4c87f2118
Loading…
Cancel
Save