Static assertions test

This commit is contained in:
2026-05-11 19:40:33 -05:00
parent 80fb72b701
commit b025bc6b50
5 changed files with 149 additions and 9 deletions

View File

@@ -24,13 +24,11 @@ SOFTWARE.
#define CI2_BASE_H #define CI2_BASE_H
#include "ci2_assert.h" // ci2_os and ci2_compiler #include "ci2_assert.h" // ci2_os and ci2_compiler
// #include "ci2_syntax.h" // exclude for now #include "ci2_syntax.h" // short syntax is guarded.
#include "ci2_macros.h" #include "ci2_macros.h" // platform general macros
#define CI2_GUI 1 // -mwindows flag is in makefile
typedef int ci2_bool;
#define ci2_false 0
#define ci2_true 1
// os specific macros and platform types guarded by defines
#if defined CI2_WINDOWS #if defined CI2_WINDOWS
#define CI2_WIN_APIVER 0x0600 // Windows Vista and Windows Server 2008 #define CI2_WIN_APIVER 0x0600 // Windows Vista and Windows Server 2008
@@ -42,7 +40,7 @@ typedef int ci2_bool;
#define _CRT_SECURE_NO_WARNINGS // No c warnings #define _CRT_SECURE_NO_WARNINGS // No c warnings
#define _CRT_RAND_S #define _CRT_RAND_S
#define OEMRESOURCE // GUI #define OEMRESOURCE // GUI
// Now we define our system types for unix and windows.
#include <windows.h> #include <windows.h>
typedef HANDLE ci2_fd; typedef HANDLE ci2_fd;
typedef wchar_t ci2_sys_char; typedef wchar_t ci2_sys_char;

View File

@@ -23,7 +23,12 @@ SOFTWARE.
#ifndef CI2_MACROS_H #ifndef CI2_MACROS_H
#define CI2_MACROS_H #define CI2_MACROS_H
#define CI2_GUI 1 // -mwindows flag is in makefile
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
typedef int ci2_bool;
#define ci2_false 0
#define ci2_true 1
#endif // ci2_macros.h #endif // ci2_macros.h

View File

@@ -23,25 +23,33 @@ SOFTWARE.
#ifndef CI2_SYNTAX_H #ifndef CI2_SYNTAX_H
#define CI2_SYNTAX_H #define CI2_SYNTAX_H
#ifdef CI2_SHORT_SYNTAX
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
// Integers // Integers
typedef int8_t i8;
typedef uint8_t u8; typedef uint8_t u8;
typedef char16_t c16; typedef int16_t i16;
typedef int32_t b32; typedef uint16_t u16;
typedef int32_t i32; typedef int32_t i32;
typedef uint32_t u32; typedef uint32_t u32;
typedef int64_t i64;
typedef uint64_t u64; typedef uint64_t u64;
// Floats // Floats
typedef float f32; typedef float f32;
typedef double f64; typedef double f64;
typedef float float32_t; typedef float float32_t;
typedef double float64_t; typedef double float64_t;
// Pointers and Chars
typedef uintptr_t uptr; typedef uintptr_t uptr;
typedef char16_t c16;
// typedef char byte; problem? // typedef char byte; problem?
typedef ptrdiff_t size; typedef ptrdiff_t size;
typedef size_t usize; typedef size_t usize;
#endif
#endif // ci2_syntax.h #endif // ci2_syntax.h

127
tests/01_static_asserts.c Normal file
View File

@@ -0,0 +1,127 @@
/* - | Copyright | --------------------------------------------------------------
Copyright (c) 2026 Randy Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* --------------------------------------------------------------------------*/
#define DEBUG
#define GUI_DEBUG_CLOSE
#include "../include/ci2.h"
#include <stdlib.h>
#include <limits.h> /* INT_MAX, CHAR_BIT — C standard */
#include <float.h> /* FLT_MANT_DIG — C standard */
#include <stdint.h> /* integer types — C standard */
static int ci2_static_assertions(void){
/* Fundamental type sizes that almost all C code silently assumes. */
CI2_STATIC_ASSERT(CHAR_BIT == 8);
CI2_STATIC_ASSERT(sizeof(char) == 1);
CI2_STATIC_ASSERT(sizeof(short) == 2);
CI2_STATIC_ASSERT(sizeof(int) == 4);
CI2_STATIC_ASSERT(sizeof(float) == 4);
CI2_STATIC_ASSERT(sizeof(double) == 8);
/* Pointer and size_t width — catches ILP64 oddities. */
CI2_STATIC_ASSERT(sizeof(void *) == sizeof(size_t));
/* Fixed-width types from <stdint.h> */
CI2_STATIC_ASSERT(sizeof(int8_t) == 1);
CI2_STATIC_ASSERT(sizeof(int16_t) == 2);
CI2_STATIC_ASSERT(sizeof(int32_t) == 4);
CI2_STATIC_ASSERT(sizeof(int64_t) == 8);
CI2_STATIC_ASSERT(sizeof(uint8_t) == 1);
CI2_STATIC_ASSERT(sizeof(uint16_t) == 2);
CI2_STATIC_ASSERT(sizeof(uint32_t) == 4);
CI2_STATIC_ASSERT(sizeof(uint64_t) == 8);
/* IEEE-754 single precision: 24 significand bits. */
CI2_STATIC_ASSERT(FLT_MANT_DIG == 24);
return EXIT_SUCCESS;
}
#ifdef CI2_WINDOWS
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
(void)hInstance;
(void)hPrevInstance;
(void)lpCmdLine;
(void) nShowCmd;
if (!ci2_init()) {
MessageBoxA(NULL, "ci2_init failed", "Error", MB_OK | MB_ICONERROR);
return EXIT_FAILURE;
}
static ci2_sys_char *ci2_window_title = L"Win32 CI2 Window Title";
CI2_Window* window = ci2_create_window(ci2_window_title, 800, 600);
if (!window) {
MessageBoxA(NULL, "ci2_create_window failed", "Error", MB_OK | MB_ICONERROR);
ci2_shutdown();
return EXIT_FAILURE;
}
// 4) Main loop: run until ci2_poll_events reports the window should close.
// This loop is friendly to rendering or other per-frame work.
int result;
while (ci2_poll_events(window)) {
// Per-frame update/render would go here.
// Example: simple sleep to avoid busy-looping; adjust to suit your app's timing.
result = ci2_static_assertions();
Sleep(1);
#ifdef GUI_DEBUG_CLOSE
break;
#endif
}
// 5) Destroy window and shutdown ci2.
ci2_destroy_window(window);
ci2_shutdown();
return result;
}
#else /* non-Windows (Linux, macOS, etc.) */
#include <unistd.h>
int main(int argc, ci2_sys_char *argv[])
{
UNUSED(argc);
UNUSED(argv);
if (!ci2_init()) return 1;
static ci2_sys_char *ci2_window_title = "Linux CI2 Window Title";
CI2_Window* window = ci2_create_window(ci2_window_title, 800, 600);
if (!window) return 1;
int result;
while (ci2_poll_events(window)) {
// Your rendering / game loop goes here
result = ci2_static_assertions();
sleep(1);
#ifdef GUI_DEBUG_CLOSE
break;
#endif
}
ci2_destroy_window(window);
ci2_shutdown();
return result;
}
#endif

View File

@@ -62,6 +62,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
return 0; return 0;
} }
#else /* non-Windows (Linux, macOS, etc.) */ #else /* non-Windows (Linux, macOS, etc.) */
#include <unistd.h>
int main(int argc, ci2_sys_char *argv[]) int main(int argc, ci2_sys_char *argv[])
{ {
UNUSED(argc); UNUSED(argc);
@@ -74,6 +75,7 @@ int main(int argc, ci2_sys_char *argv[])
while (ci2_poll_events(window)) { while (ci2_poll_events(window)) {
// Your rendering / game loop goes here // Your rendering / game loop goes here
sleep(1);
#ifdef GUI_DEBUG_CLOSE #ifdef GUI_DEBUG_CLOSE
break; break;
#endif #endif