feat: memory constants

This commit is contained in:
2026-01-08 21:51:55 +02:00
parent 266dd86878
commit 6508cc6ee2
5 changed files with 138 additions and 0 deletions

22
.clang-format Normal file
View File

@@ -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

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
bin

18
makefile Normal file
View File

@@ -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

17
niconst.h Normal file
View File

@@ -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)

80
test/niconst_test.c Normal file
View File

@@ -0,0 +1,80 @@
#include "../niconst.h"
#include <stdio.h>
#include <assert.h>
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;
}