summaryrefslogtreecommitdiff
path: root/test/pkmacros.cpp
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/pkmacros.cpp
initial commit: macros, memory, tests
Diffstat (limited to 'test/pkmacros.cpp')
-rw-r--r--test/pkmacros.cpp191
1 files changed, 191 insertions, 0 deletions
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;
+}