feat: memory constants
This commit is contained in:
22
.clang-format
Normal file
22
.clang-format
Normal 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
|
||||
BIN
bin/niconst_test
Executable file
BIN
bin/niconst_test
Executable file
Binary file not shown.
20
bin/niconst_test.dSYM/Contents/Info.plist
Normal file
20
bin/niconst_test.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>English</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.apple.xcode.dsym.niconst_test</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>dSYM</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
</dict>
|
||||
</plist>
|
||||
BIN
bin/niconst_test.dSYM/Contents/Resources/DWARF/niconst_test
Normal file
BIN
bin/niconst_test.dSYM/Contents/Resources/DWARF/niconst_test
Normal file
Binary file not shown.
@@ -0,0 +1,5 @@
|
||||
---
|
||||
triple: 'arm64-apple-darwin'
|
||||
binary-path: 'bin/niconst_test'
|
||||
relocations: []
|
||||
...
|
||||
18
makefile
Normal file
18
makefile
Normal 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
17
niconst.h
Normal 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
80
test/niconst_test.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user