diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..5475a73 --- /dev/null +++ b/.clang-format @@ -0,0 +1,22 @@ +AlignAfterOpenBracket: DontAlign +AlignEscapedNewlines: DontAlign +AlignOperands: DontAlign +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: TopLevel +BreakBeforeTernaryOperators: false +BinPackArguments: false +BinPackParameters: false +BreakBeforeBraces: WebKit +IndentCaseLabels: false +TabWidth: 4 +IndentWidth: 4 +ContinuationIndentWidth: 4 +UseTab: ForContinuationAndIndentation +ColumnLimit: 0 +ReflowComments: false +SortIncludes: false +SpaceBeforeParens: false \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5e82d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..359b8a7 --- /dev/null +++ b/makefile @@ -0,0 +1,18 @@ +RELEASE_flags=-DNDEBUG -O2 -g0 +DEBUG_flags=-std=c23 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined + +clean: + rm -fr bin + +format: + clang-format -i test/niconst_test.c + clang-format -i niconst.h + +test: bin/test + bin/niconst_test + +.PHONY: test clean + +bin/test: test/niconst_test.c + mkdir -p bin/test + cc ${DEBUG_flags} test/niconst_test.c -o bin/niconst_test \ No newline at end of file diff --git a/niconst.h b/niconst.h new file mode 100644 index 0000000..7e21d29 --- /dev/null +++ b/niconst.h @@ -0,0 +1,17 @@ +#pragma once + +#define NICONST_SIZE_T_ZERO ((size_t) 0) + +// MEMORY CONSTANTS: BINARY +#define NICONST_MEM_BINARY_FACTOR ((size_t) 1024) +#define NICONST_KIB_B(n) ((n) * NICONST_MEM_BINARY_FACTOR) +#define NICONST_MIB_B(n) ((NICONST_KIB_B(n)) * NICONST_MEM_BINARY_FACTOR) +#define NICONST_GIB_B(n) ((NICONST_MIB_B(n)) * NICONST_MEM_BINARY_FACTOR) +#define NICONST_TIB_B(n) ((NICONST_GIB_B(n)) * NICONST_MEM_BINARY_FACTOR) + +// MEMORY CONSTANTS: DECIMAL +#define NICONST_MEM_DECIMAL_FACTOR ((size_t) 1000) +#define NICONST_KB_B(n) ((n) * NICONST_MEM_DECIMAL_FACTOR) +#define NICONST_MB_B(n) ((NICONST_KB_B(n)) * NICONST_MEM_DECIMAL_FACTOR) +#define NICONST_GB_B(n) ((NICONST_MB_B(n)) * NICONST_MEM_DECIMAL_FACTOR) +#define NICONST_TB_B(n) ((NICONST_GB_B(n)) * NICONST_MEM_DECIMAL_FACTOR) diff --git a/test/niconst_test.c b/test/niconst_test.c new file mode 100644 index 0000000..1e96873 --- /dev/null +++ b/test/niconst_test.c @@ -0,0 +1,80 @@ +#include "../niconst.h" +#include +#include + +void +test_kib_b() +{ + assert(NICONST_KIB_B(0) == (size_t) 0); + assert(NICONST_KIB_B(1) == (size_t) 1024); +} + +void +test_mib_b() +{ + assert(NICONST_MIB_B(0) == (size_t) 0); + assert(NICONST_MIB_B(1) == (size_t) 1048576); + assert(NICONST_MIB_B(5) == ((size_t) 1024) * NICONST_KIB_B(5)); +} + +void +test_gib_b() +{ + assert(NICONST_GIB_B(0) == (size_t) 0); + assert(NICONST_GIB_B(1) == (size_t) 1073741824); + assert(NICONST_GIB_B(3) == ((size_t) 1024) * NICONST_MIB_B(3)); +} + +void +test_tib_b() +{ + assert(NICONST_TIB_B(0) == (size_t) 0); + assert(NICONST_TIB_B(1) == (size_t) 1099511627776); + assert(NICONST_TIB_B(3) == ((size_t) 1024) * NICONST_GIB_B(3)); +} + +void +test_kb_b() +{ + assert(NICONST_KB_B(0) == (size_t) 0); + assert(NICONST_KB_B(1) == (size_t) 1000); +} + +void +test_mb_b() +{ + assert(NICONST_MB_B(0) == (size_t) 0); + assert(NICONST_MB_B(1) == (size_t) 1000000); + assert(NICONST_MB_B(5) == ((size_t) 1000) * NICONST_KB_B(5)); +} + +void +test_gb_b() +{ + assert(NICONST_GB_B(0) == (size_t) 0); + assert(NICONST_GB_B(1) == (size_t) 1000000000); + assert(NICONST_GB_B(3) == ((size_t) 1000) * NICONST_MB_B(3)); +} + +void +test_tb_b() +{ + assert(NICONST_TB_B(0) == (size_t) 0); + assert(NICONST_TB_B(1) == (size_t) 1000000000000); + assert(NICONST_TB_B(3) == ((size_t) 1000) * NICONST_GB_B(3)); +} + +int +main() +{ + test_kib_b(); + test_mib_b(); + test_gib_b(); + test_tib_b(); + test_kb_b(); + test_mb_b(); + test_gb_b(); + test_tb_b(); + printf("== TESTS SUCCESSFUL ==\n"); + return 0; +}