From 2e2656a02fdc69c08e2db0383d849e57cb305286 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 18 Nov 2022 17:59:19 +0100 Subject: [PATCH] Add static assertions to constexpr functions --- luna/Alignment.h | 19 ++++++++++++++++++- luna/Types.h | 14 +++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/luna/Alignment.h b/luna/Alignment.h index 0cb7ced3..e51af9af 100644 --- a/luna/Alignment.h +++ b/luna/Alignment.h @@ -5,18 +5,35 @@ template constexpr T is_aligned(T value, T align_base) return (value % align_base == 0); } +static_assert(is_aligned(1024, 512)); +static_assert(!is_aligned(235, 32)); +static_assert(is_aligned(40960, 4096)); + template constexpr T align_down(T value, T align_base) { return value - (value % align_base); } +static_assert(align_down(598, 512) == 512); +static_assert(align_down(194, 64) == 192); +static_assert(align_down(62, 31) == 62); + template constexpr T align_up(T value, T align_base) { if (is_aligned(value, align_base)) return value; return align_down(value, align_base) + align_base; } +static_assert(align_up(598, 512) == 1024); +static_assert(align_up(194, 64) == 256); +static_assert(align_up(62, 31) == 62); + template constexpr T get_blocks_from_size(T value, T block_size) { return (value + (block_size - 1)) / block_size; -} \ No newline at end of file +} + +static_assert(get_blocks_from_size(40960, 4096) == 10); +static_assert(get_blocks_from_size(194, 64) == 4); +static_assert(get_blocks_from_size(2, 32) == 1); +static_assert(get_blocks_from_size(0, 256) == 0); \ No newline at end of file diff --git a/luna/Types.h b/luna/Types.h index ad06edfe..a872a576 100644 --- a/luna/Types.h +++ b/luna/Types.h @@ -10,4 +10,16 @@ typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; typedef size_t usize; -typedef long isize; \ No newline at end of file +typedef long isize; + +static_assert(sizeof(u8) == 1UL); +static_assert(sizeof(u16) == 2UL); +static_assert(sizeof(u32) == 4UL); +static_assert(sizeof(u64) == 8UL); +static_assert(sizeof(i8) == 1UL); +static_assert(sizeof(i16) == 2UL); +static_assert(sizeof(i32) == 4UL); +static_assert(sizeof(i64) == 8UL); + +static_assert(sizeof(usize) == 8UL); +static_assert(sizeof(isize) == 8UL); \ No newline at end of file