summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2024-10-11 18:41:47 -0400
committerJonathan Bradley <jcb@pikum.xyz>2024-10-11 18:41:47 -0400
commit887a9f559e71d8a788fbfe210f126eda5ba7969b (patch)
tree23a282241115abad7f62f0c7b4f0973cab0c4952 /test
initial commit: macros, memory, tests
Diffstat (limited to 'test')
-rw-r--r--test/pkmacros.c55
-rw-r--r--test/pkmacros.cpp191
-rw-r--r--test/pkmem-types.c42
-rw-r--r--test/pkmem-types.cpp65
-rw-r--r--test/pkmem.c17
-rw-r--r--test/pkmem.cpp17
6 files changed, 387 insertions, 0 deletions
diff --git a/test/pkmacros.c b/test/pkmacros.c
new file mode 100644
index 0000000..2a1f626
--- /dev/null
+++ b/test/pkmacros.c
@@ -0,0 +1,55 @@
+
+#include "../pkmacros.h"
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+ (void)stdout;
+
+ // MISC
+ {
+ PK_LOGV_INF("PK_HAS_FLAG 000: %b\n", PK_HAS_FLAG(0xFF, 5));
+ PK_LOGV_INF("PK_HAS_FLAG 001: %b\n", PK_HAS_FLAG(0x0F, 5));
+ PK_LOGV_INF("PK_CLAMP 000: %i\n", PK_CLAMP(0, -10, 10));
+ PK_LOGV_INF("PK_CLAMP 001: %i\n", PK_CLAMP(-20, -10, 10));
+ PK_LOGV_INF("PK_CLAMP 002: %i\n", PK_CLAMP(20, -10, 10));
+ PK_LOGV_INF("PK_MIN 000: %i\n", PK_MIN(0, 10));
+ PK_LOGV_INF("PK_MIN 001: %i\n", PK_MIN(0, -10));
+ PK_LOGV_INF("PK_MAX 000: %i\n", PK_MAX(0, -10));
+ PK_LOGV_INF("PK_MAX 001: %i\n", PK_MAX(0, 10));
+ PK_LOGV_INF("CAFEBABE 000: %p\n", CAFE_BABE(void));
+ NULL_CHAR_ARR(c, 16);
+ PK_LOGV_INF("NULL_CHAR_ARR 000: '%s' '%lu'\n", c, strlen(c));
+ }
+
+ // PK_TO_BIN printing
+ {
+ uint8_t u8 = 0x55; // 01010101
+ uint16_t u16 = 0x5555;
+ uint32_t u32 = 0x55555555;
+ uint64_t u64 = 0x5555555555555555;
+ PK_LOGV_INF(PK_TO_BIN_PAT_8 "\n", PK_TO_BIN_8(u8));
+ PK_LOGV_INF(PK_TO_BIN_PAT_16"\n", PK_TO_BIN_16(u16));
+ PK_LOGV_INF(PK_TO_BIN_PAT_32"\n", PK_TO_BIN_32(u32));
+ PK_LOGV_INF(PK_TO_BIN_PAT_64"\n", PK_TO_BIN_64(u64));
+ u8 = 0xAA; // 10101010
+ u16 = 0xAAAA;
+ u32 = 0xAAAAAAAA;
+ u64 = 0xAAAAAAAAAAAAAAAA;
+ PK_LOGV_INF(PK_TO_BIN_PAT_8 "\n", PK_TO_BIN_8(u8));
+ PK_LOGV_INF(PK_TO_BIN_PAT_16"\n", PK_TO_BIN_16(u16));
+ PK_LOGV_INF(PK_TO_BIN_PAT_32"\n", PK_TO_BIN_32(u32));
+ PK_LOGV_INF(PK_TO_BIN_PAT_64"\n", PK_TO_BIN_64(u64));
+ u64 = 0xFFFFFFFF00000000;
+ PK_LOGV_INF(PK_TO_BIN_PAT_64"\n", PK_TO_BIN_64(u64));
+ u64 = 0x00000000FFFFFFFF;
+ PK_LOGV_INF(PK_TO_BIN_PAT_64"\n", PK_TO_BIN_64(u64));
+ }
+
+ return 0;
+}
diff --git a/test/pkmacros.cpp b/test/pkmacros.cpp
new file mode 100644
index 0000000..5e5377f
--- /dev/null
+++ b/test/pkmacros.cpp
@@ -0,0 +1,191 @@
+
+#include "../pkmacros.h"
+
+#include <cstdint>
+#include <cstdio>
+#include <cstring>
+#include <type_traits>
+
+TypeSafeInt_H(MyTSI_8, uint8_t, 0xFF);
+TypeSafeInt_H(MyTSI_16, uint16_t, 0xFFFF);
+TypeSafeInt_H(MyTSI_32, uint32_t, 0xFFFFFFFF);
+TypeSafeInt_H(MyTSI_64, uint64_t, 0xFFFFFFFFFFFFFFFF);
+
+TypeSafeInt_B(MyTSI_8);
+TypeSafeInt_B(MyTSI_16);
+TypeSafeInt_B(MyTSI_32);
+TypeSafeInt_B(MyTSI_64);
+
+TypeSafeInt_constexpr(MyConstexprTSI_8, uint8_t, 0xFF);
+TypeSafeInt_constexpr(MyConstexprTSI_16, uint16_t, 0xFFFF);
+TypeSafeInt_constexpr(MyConstexprTSI_32, uint32_t, 0xFFFFFFFF);
+TypeSafeInt_constexpr(MyConstexprTSI_64, uint64_t, 0xFFFFFFFFFFFFFFFF);
+
+template<typename T, typename T_T>
+bool
+tsi_test_operators()
+{
+ T mtsi0F {0x0F};
+ T mtsiF0 {0xF0};
+ T r;
+ /*
+ r = mtsi0F + T_T{static_cast<T_T>(mtsiF0)};
+ if (r == T{0}) return false;
+ r = mtsi0F - T_T{static_cast<T_T>(mtsiF0)};
+ if (r == T{0}) return false;
+ */
+ r = mtsi0F + mtsiF0;
+ if (r == T{0}) return false;
+ r = mtsi0F - mtsiF0;
+ if (r == T{0}) return false;
+ r = mtsi0F & mtsiF0;
+ if (r != T{0}) return false;
+ r = mtsi0F | mtsiF0;
+ if (r == T{0}) return false;
+ r = mtsi0F ^ mtsiF0;
+ if (r == T{0}) return false;
+ r = mtsi0F++;
+ if (r == T{0}) return false;
+ r = mtsi0F--;
+ if (r == T{0}) return false;
+ r = ++mtsi0F;
+ if (r == T{0}) return false;
+ r = --mtsi0F;
+ if (r == T{0}) return false;
+ r = mtsi0F << T{1};
+ if (r == T{0}) return false;
+ r = mtsi0F >> T{1};
+ if (r == T{0}) return false;
+ r = mtsi0F += mtsiF0;
+ if (r == T{0}) return false;
+ r = mtsi0F -= mtsiF0;
+ if (r == T{0}) return false;
+ r = mtsi0F &= mtsiF0;
+ if (r != T{0}) return false;
+ r = mtsi0F |= mtsiF0;
+ if (r == T{0}) return false;
+ r = mtsi0F ^= mtsiF0;
+ if (r != T{0}) return false;
+ r = mtsi0F = ~mtsiF0;
+ if (r == T{0}) return false;
+ return true;
+}
+
+template<typename T, typename T_T>
+constexpr bool
+tsi_test_operators_constexpr()
+{
+ constexpr T mtsi0F {0x0F};
+ constexpr T mtsiF0 {0xF0};
+ constexpr T mtsi00 {0x00};
+ /*
+ constexpr T r01 = mtsi0F + T_T{static_cast<T_T>(mtsiF0)};
+ if constexpr (r01 == mtsi00) return false;
+ constexpr T r02 = mtsi0F - T_T{static_cast<T_T>(mtsiF0)};
+ if constexpr (r02 == mtsi00) return false;
+ */
+ constexpr T r03 = mtsi0F + mtsiF0;
+ if constexpr (r03 == mtsi00) return false;
+ constexpr T r04 = mtsi0F - mtsiF0;
+ if constexpr (r04 == mtsi00) return false;
+ constexpr T r05 = mtsi0F & mtsiF0;
+ if constexpr (r05 != mtsi00) return false;
+ constexpr T r06 = mtsi0F | mtsiF0;
+ if constexpr (r06 == mtsi00) return false;
+ constexpr T r07 = mtsi0F ^ mtsiF0;
+ if constexpr (r07 == mtsi00) return false;
+ /*
+ constexpr T r08 = mtsi0F++;
+ if constexpr (r08 == mtsi00) return false;
+ constexpr T r09 = mtsi0F--;
+ if constexpr (r09 == mtsi00) return false;
+ constexpr T r10 = ++mtsi0F;
+ if constexpr (r10 == mtsi00) return false;
+ constexpr T r11 = --mtsi0F;
+ if constexpr (r11 == mtsi00) return false;
+ */
+ constexpr T r12 = mtsi0F << T{1};
+ if constexpr (r12 == mtsi00) return false;
+ constexpr T r13 = mtsi0F >> T{1};
+ if constexpr (r13 == mtsi00) return false;
+ /*
+ constexpr T r14 = mtsi0F += mtsiF0;
+ if constexpr (r14 == mtsi00) return false;
+ constexpr T r15 = mtsi0F -= mtsiF0;
+ if constexpr (r15 == mtsi00) return false;
+ constexpr T r16 = mtsi0F &= mtsiF0;
+ if constexpr (r16 != mtsi00) return false;
+ constexpr T r17 = mtsi0F |= mtsiF0;
+ if constexpr (r17 == mtsi00) return false;
+ constexpr T r18 = mtsi0F ^= mtsiF0;
+ if constexpr (r18 != mtsi00) return false;
+ constexpr T r19 = mtsi0F = ~mtsiF0;
+ if constexpr (r19 == mtsi00) return false;
+ */
+ return true;
+}
+
+class sdc {
+};
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+ (void)std::is_const<void>::value;
+
+ // MISC
+ {
+ PK_LOGV_INF("PK_HAS_FLAG 000: %b\n", PK_HAS_FLAG(0xFF, 5));
+ PK_LOGV_INF("PK_HAS_FLAG 001: %b\n", PK_HAS_FLAG(0x0F, 5));
+ PK_LOGV_INF("PK_CLAMP 000: %i\n", PK_CLAMP(0, -10, 10));
+ PK_LOGV_INF("PK_CLAMP 001: %i\n", PK_CLAMP(-20, -10, 10));
+ PK_LOGV_INF("PK_CLAMP 002: %i\n", PK_CLAMP(20, -10, 10));
+ PK_LOGV_INF("PK_MIN 000: %i\n", PK_MIN(0, 10));
+ PK_LOGV_INF("PK_MIN 001: %i\n", PK_MIN(0, -10));
+ PK_LOGV_INF("PK_MAX 000: %i\n", PK_MAX(0, -10));
+ PK_LOGV_INF("PK_MAX 001: %i\n", PK_MAX(0, 10));
+ PK_LOGV_INF("CAFEBABE 000: %p\n", CAFE_BABE(void));
+ NULL_CHAR_ARR(c, 16);
+ PK_LOGV_INF("NULL_CHAR_ARR 000: '%s' '%lu'\n", c, strlen(c));
+ }
+
+ // IS_CONSTRUCTIBLE
+ {
+ bool b = false;
+ if IS_CONSTRUCTIBLE(sdc) b = true;
+ fprintf(stdout, "class sdc IS_CONSTRUCTIBLE: %b\n", b);
+ if IS_DESTRUCTIBLE(sdc) b = false;
+ fprintf(stdout, "class sdc IS_DESTRUCTIBLE: %b\n", b);
+ }
+
+ // TypeSafeInt
+ {
+ PK_LOGV_INF(PK_TO_BIN_PAT_8 "\n", PK_TO_BIN_8 (static_cast<MyTSI_8_T> (MyTSI_8_MAX)));
+ PK_LOGV_INF(PK_TO_BIN_PAT_16"\n", PK_TO_BIN_16(static_cast<MyTSI_16_T>(MyTSI_16_MAX)));
+ PK_LOGV_INF(PK_TO_BIN_PAT_32"\n", PK_TO_BIN_32(static_cast<MyTSI_32_T>(MyTSI_32_MAX)));
+ PK_LOGV_INF(PK_TO_BIN_PAT_64"\n", PK_TO_BIN_64(static_cast<MyTSI_64_T>(MyTSI_64_MAX)));
+ PK_LOGV_INF(PK_TO_BIN_PAT_8 "\n", PK_TO_BIN_8 (static_cast<MyConstexprTSI_8_T> (MyConstexprTSI_8_MAX)));
+ PK_LOGV_INF(PK_TO_BIN_PAT_16"\n", PK_TO_BIN_16(static_cast<MyConstexprTSI_16_T>(MyConstexprTSI_16_MAX)));
+ PK_LOGV_INF(PK_TO_BIN_PAT_32"\n", PK_TO_BIN_32(static_cast<MyConstexprTSI_32_T>(MyConstexprTSI_32_MAX)));
+ PK_LOGV_INF(PK_TO_BIN_PAT_64"\n", PK_TO_BIN_64(static_cast<MyConstexprTSI_64_T>(MyConstexprTSI_64_MAX)));
+ PK_LOGV_INF("tsi_test_operators 08: %b\n", tsi_test_operators<MyTSI_8, MyTSI_8_T>());
+ PK_LOGV_INF("tsi_test_operators 16: %b\n", tsi_test_operators<MyTSI_16, MyTSI_16_T>());
+ PK_LOGV_INF("tsi_test_operators 32: %b\n", tsi_test_operators<MyTSI_32, MyTSI_32_T>());
+ PK_LOGV_INF("tsi_test_operators 64: %b\n", tsi_test_operators<MyTSI_64, MyTSI_64_T>());
+ PK_LOGV_INF("tsi_test_operators c08: %b\n", tsi_test_operators<MyConstexprTSI_8, MyConstexprTSI_8_T>());
+ PK_LOGV_INF("tsi_test_operators c16: %b\n", tsi_test_operators<MyConstexprTSI_16, MyConstexprTSI_16_T>());
+ PK_LOGV_INF("tsi_test_operators c32: %b\n", tsi_test_operators<MyConstexprTSI_32, MyConstexprTSI_32_T>());
+ PK_LOGV_INF("tsi_test_operators c64: %b\n", tsi_test_operators<MyConstexprTSI_64, MyConstexprTSI_64_T>());
+ constexpr bool b1 = tsi_test_operators_constexpr<MyConstexprTSI_8, MyConstexprTSI_8_T>();
+ constexpr bool b2 = tsi_test_operators_constexpr<MyConstexprTSI_16, MyConstexprTSI_16_T>();
+ constexpr bool b3 = tsi_test_operators_constexpr<MyConstexprTSI_32, MyConstexprTSI_32_T>();
+ constexpr bool b4 = tsi_test_operators_constexpr<MyConstexprTSI_64, MyConstexprTSI_64_T>();
+ PK_LOGV_INF("TypeSafeInt_constexpr_08: %b\n", b1);
+ PK_LOGV_INF("TypeSafeInt_constexpr_16: %b\n", b2);
+ PK_LOGV_INF("TypeSafeInt_constexpr_32: %b\n", b3);
+ PK_LOGV_INF("TypeSafeInt_constexpr_64: %b\n", b4);
+ }
+
+ return 0;
+}
diff --git a/test/pkmem-types.c b/test/pkmem-types.c
new file mode 100644
index 0000000..2399308
--- /dev/null
+++ b/test/pkmem-types.c
@@ -0,0 +1,42 @@
+
+#include "../pkmem-types.h"
+#include "../pkmacros.h"
+
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+ (void)stdout;
+
+ // pk_handle_validate
+ {
+ enum PK_HANDLE_VALIDATION res;
+ struct pk_handle h, bh;
+
+ bh.bucketIndex = 2;
+ bh.itemIndex = 2;
+
+ h.bucketIndex = 0;
+ h.itemIndex = 0;
+ res = pk_handle_validate(h, bh, 1024);
+ PK_LOGV_INF("pk_handle_validate 000: %i\n", res);
+
+ h.bucketIndex = 3;
+ h.itemIndex = 0;
+ res = pk_handle_validate(h, bh, 1024);
+ PK_LOGV_INF("pk_handle_validate 001: %i\n", res);
+
+ h.bucketIndex = 2;
+ h.itemIndex = 3;
+ res = pk_handle_validate(h, bh, 1024);
+ PK_LOGV_INF("pk_handle_validate 002: %i\n", res);
+
+ h = pk_handle_MAX;
+ res = pk_handle_validate(h, bh, 1024);
+ PK_LOGV_INF("pk_handle_validate 003: %i\n", res);
+ }
+
+ return 0;
+}
diff --git a/test/pkmem-types.cpp b/test/pkmem-types.cpp
new file mode 100644
index 0000000..b8e19ef
--- /dev/null
+++ b/test/pkmem-types.cpp
@@ -0,0 +1,65 @@
+
+#include "../pkmem-types.h"
+#include "../pkmacros.h"
+
+#include <cstdio>
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+ (void)stdout;
+
+ // pk_handle operator==
+ {
+ struct pk_handle h, bh;
+ bool res;
+
+ constexpr struct pk_handle ch1 { .bucketIndex = 0xAAAAAAAA, .itemIndex = 0x55555555 };
+ constexpr struct pk_handle cbh1 { .bucketIndex = 0xAAAAAAAA, .itemIndex = 0x55555555 };
+ constexpr bool ret1 = (ch1 == cbh1);
+ PK_LOGV_INF("pk_handle constexpr operator== 000: %b\n", ret1);
+
+ constexpr struct pk_handle ch2 { .bucketIndex = 0x00000000, .itemIndex = 0x00000000 };
+ constexpr struct pk_handle cbh2 { .bucketIndex = 0xFFFFFFFF, .itemIndex = 0xFFFFFFFF };
+ constexpr bool ret2 = (ch2 == cbh2);
+ PK_LOGV_INF("pk_handle constexpr operator== 001: %b\n", ret2);
+
+ h.bucketIndex = 0xCAFE;
+ h.itemIndex = 0xBABE;
+ bh.bucketIndex = 0xCAFE;
+ bh.itemIndex = 0xBABE;
+ res = h == bh;
+ PK_LOGV_INF("pk_handle operator== 000: %b\n", res);
+
+ h.bucketIndex = 0x00000000;
+ h.itemIndex = 0x00000000;
+ bh.bucketIndex = 0xFFFFFFFF;
+ bh.itemIndex = 0xFFFFFFFF;
+ res = h == bh;
+ PK_LOGV_INF("pk_handle operator== 001: %b\n", res);
+ }
+
+ // pk_handle_validate_constexpr
+ {
+ constexpr struct pk_handle cbh { .bucketIndex = 2, .itemIndex = 2 };
+
+ constexpr struct pk_handle ch1 { .bucketIndex = 0, .itemIndex = 0 };
+ constexpr enum PK_HANDLE_VALIDATION ret1 = pk_handle_validate_constexpr<ch1, cbh, 1024ULL>();
+ PK_LOGV_INF("pk_handle_validate_constexpr: 000: %i\n", ret1);
+
+ constexpr struct pk_handle ch2 { .bucketIndex = 3, .itemIndex = 0 };
+ constexpr enum PK_HANDLE_VALIDATION ret2 = pk_handle_validate_constexpr<ch2, cbh, 1024ULL>();
+ PK_LOGV_INF("pk_handle_validate_constexpr: 000: %i\n", ret2);
+
+ constexpr struct pk_handle ch3 { .bucketIndex = 2, .itemIndex = 3 };
+ constexpr enum PK_HANDLE_VALIDATION ret3 = pk_handle_validate_constexpr<ch3, cbh, 1024ULL>();
+ PK_LOGV_INF("pk_handle_validate_constexpr: 000: %i\n", ret3);
+
+ constexpr struct pk_handle ch4 = pk_handle_MAX_constexpr;
+ constexpr enum PK_HANDLE_VALIDATION ret4 = pk_handle_validate_constexpr<ch4, cbh, 1024ULL>();
+ PK_LOGV_INF("pk_handle_validate_constexpr: 000: %i\n", ret4);
+ }
+
+ return 0;
+}
diff --git a/test/pkmem.c b/test/pkmem.c
new file mode 100644
index 0000000..b6826a2
--- /dev/null
+++ b/test/pkmem.c
@@ -0,0 +1,17 @@
+
+#include "../pkmem.h"
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+
+ // pk_new_base
+ {
+ char *some_dang_string = (char*)pk_new_base(64, alignof(char*));
+ fprintf(stdout, "some_dang_string: %p\n", some_dang_string);
+ pk_delete_base(some_dang_string, 64);
+ }
+
+ return 0;
+}
diff --git a/test/pkmem.cpp b/test/pkmem.cpp
new file mode 100644
index 0000000..cc7cf9c
--- /dev/null
+++ b/test/pkmem.cpp
@@ -0,0 +1,17 @@
+
+#include "../pkmem.h"
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+
+ // pk_new<T>
+ {
+ char *some_dang_string = pk_new<char>(64);
+ fprintf(stdout, "some_dang_string: %p\n", some_dang_string);
+ pk_delete<char>(some_dang_string, 64);
+ }
+
+ return 0;
+}