diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-04 15:11:53 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-04 15:13:55 -0400 |
| commit | 5e2783b6a8a65aba17789aa388c320bb85804d45 (patch) | |
| tree | 13d5ba5802f8fe04ea872da7cbb0a03b7c81ea93 /test | |
| parent | 5715acd12cae75cc5fdf395567cb5603fd906a71 (diff) | |
pkstn: INCONVERTIBLE overhaul
Diffstat (limited to 'test')
| -rw-r--r-- | test/pkstn.c | 21 | ||||
| -rw-r--r-- | test/pkstn.cpp | 166 |
2 files changed, 131 insertions, 56 deletions
diff --git a/test/pkstn.c b/test/pkstn.c index 8884804..032d7e2 100644 --- a/test/pkstn.c +++ b/test/pkstn.c @@ -8,12 +8,13 @@ int main(int argc, char *argv[]) { (void)argc; (void)argv; + char *s; enum PK_STN_RES res = {0}; // stn_int64_t { int64_t i = {0}; - res = pk_stn_int64_t(&i, "-1", 0); + res = pk_stn_int64_t(&i, "-1", &s, 0); fprintf(stdout, "pkstn: stn_int64_t res: %ld\n", i); if (res != PK_STN_RES_SUCCESS) exit(1); if (-1 != i) exit(1); @@ -22,7 +23,7 @@ int main(int argc, char *argv[]) // stn_uint64_t { uint64_t i = {0}; - res = pk_stn_uint64_t(&i, "1", 0); + res = pk_stn_uint64_t(&i, "1", &s, 0); fprintf(stdout, "pkstn: stn_uint64_t res: %lu\n", i); if (res != PK_STN_RES_SUCCESS) exit(1); if (1 != i) exit(1); @@ -31,7 +32,7 @@ int main(int argc, char *argv[]) // stn_int32_t { int32_t i = {0}; - res = pk_stn_int32_t(&i, "-1", 0); + res = pk_stn_int32_t(&i, "-1", &s, 0); fprintf(stdout, "pkstn: stn_int32_t res: %d\n", i); if (res != PK_STN_RES_SUCCESS) exit(1); if (-1 != i) exit(1); @@ -40,7 +41,7 @@ int main(int argc, char *argv[]) // stn_uint32_t { uint32_t i = {0}; - res = pk_stn_uint32_t(&i, "1", 0); + res = pk_stn_uint32_t(&i, "1", &s, 0); fprintf(stdout, "pkstn: stn_uint32_t res: %u\n", i); if (res != PK_STN_RES_SUCCESS) exit(1); if (1 != i) exit(1); @@ -49,7 +50,7 @@ int main(int argc, char *argv[]) // stn_int16_t { int16_t i = {0}; - res = pk_stn_int16_t(&i, "-1", 0); + res = pk_stn_int16_t(&i, "-1", &s, 0); fprintf(stdout, "pkstn: stn_int16_t res: %d\n", i); if (res != PK_STN_RES_SUCCESS) exit(1); if (-1 != i) exit(1); @@ -58,7 +59,7 @@ int main(int argc, char *argv[]) // stn_uint16_t { uint16_t i = {0}; - res = pk_stn_uint16_t(&i, "1", 0); + res = pk_stn_uint16_t(&i, "1", &s, 0); fprintf(stdout, "pkstn: stn_uint16_t res: %u\n", i); if (res != PK_STN_RES_SUCCESS) exit(1); if (1 != i) exit(1); @@ -67,7 +68,7 @@ int main(int argc, char *argv[]) // stn_int8_t { int8_t i = {0}; - res = pk_stn_int8_t(&i, "-1", 0); + res = pk_stn_int8_t(&i, "-1", &s, 0); fprintf(stdout, "pkstn: stn_int8_t res: %d\n", i); if (res != PK_STN_RES_SUCCESS) exit(1); if (-1 != i) exit(1); @@ -76,7 +77,7 @@ int main(int argc, char *argv[]) // stn_uint8_t { uint8_t i = {0}; - res = pk_stn_uint8_t(&i, "1", 0); + res = pk_stn_uint8_t(&i, "1", &s, 0); fprintf(stdout, "pkstn: stn_uint8_t res: %u\n", i); if (res != PK_STN_RES_SUCCESS) exit(1); if (1 != i) exit(1); @@ -85,7 +86,7 @@ int main(int argc, char *argv[]) // stn_float { float f = {0}; - res = pk_stn_float(&f, "-1"); + res = pk_stn_float(&f, "-1", &s); fprintf(stdout, "pkstn: stn_float res: %f\n", f); if (res != PK_STN_RES_SUCCESS) exit(1); if (-1 != f) exit(1); @@ -94,7 +95,7 @@ int main(int argc, char *argv[]) // stn_double { double f = {0}; - res = pk_stn_double(&f, "-1"); + res = pk_stn_double(&f, "-1", &s); fprintf(stdout, "pkstn: stn_double res: %f\n", f); if (res != PK_STN_RES_SUCCESS) exit(1); if (-1 != f) exit(1); diff --git a/test/pkstn.cpp b/test/pkstn.cpp index 8a4d901..b6c9329 100644 --- a/test/pkstn.cpp +++ b/test/pkstn.cpp @@ -1,75 +1,143 @@ #include "../pkstn.h" +#include <cstring> #include <iostream> #include <stdio.h> #include <stdlib.h> #include <type_traits> +const char *STRNUM_NIL = ""; +const char *STRNUM_ZED = "0"; const char *STRNUM_ONE = "1"; const char *STRNUM_ONE_MINUS = "-1"; const char *STRNUM_UNDERFLOW = "-99999999999999999999999999999999"; const char *STRNUM_OVERFLOW = "99999999999999999999999999999999"; +const char *STRNUM_FLOAT_NIL = ""; +const char *STRNUM_FLOAT_ZED = "0.0"; +const char *STRNUM_FLOAT_ONE = "15.0"; +const char *STRNUM_FLOAT_ONE_MINUS = "-15.0"; const char *STRNUM_UNDERFLOW_FLOAT = "-1.0e9999"; const char *STRNUM_OVERFLOW_FLOAT = "1.0e9999"; const char *STRNUM_INCONVERTIBLE = "junk"; -enum STRNUM_TESTS : uint8_t { - TEST_ONE = 0x01, - TEST_ONE_MINUS = 0x02, - TEST_UNDERFLOW = 0x04, - TEST_OVERFLOW = 0x08, - TEST_INCONVERTIBLE = 0x10, - TEST_NO_NEG = TEST_ONE | TEST_OVERFLOW | TEST_INCONVERTIBLE, - TEST_FLOATS = TEST_ONE | TEST_ONE_MINUS | TEST_UNDERFLOW | TEST_OVERFLOW, - TEST_ALL = 0xFF, +#define VAL_SCALAR_ZED 0 +#define VAL_SCALAR_ONE 1 +#define VAL_SCALAR_ONE_MINUS -1 +#define VAL_FLOATING_ZED 0.0 +#define VAL_FLOATING_ONE 0x1.ep+3 +#define VAL_FLOATING_ONE_MINUS -0x1.ep+3 + +enum STRNUM_TESTS : uint32_t { + TEST_SCALAR_NIL = 0x00000001, + TEST_SCALAR_ZED = 0x00000002, + TEST_SCALAR_ONE = 0x00000004, + TEST_SCALAR_ONE_MINUS = 0x00000008, + TEST_SCALAR_UNDERFLOW = 0x00000010, + TEST_SCALAR_OVERFLOW = 0x00000020, + TEST_FLOATING_NIL = 0x00000100, + TEST_FLOATING_ZED = 0x00000200, + TEST_FLOATING_ONE = 0x00000400, + TEST_FLOATING_ONE_MINUS = 0x00000800, + TEST_FLOATING_UNDERFLOW = 0x00001000, + TEST_FLOATING_OVERFLOW = 0x00002000, + TEST_INCONVERTIBLE = 0x00010000, + TEST_SCALAR = 0x000100FF, + TEST_FLOATS = 0x0001FF00, + TEST_ALL = 0xFFFFFFFF, }; template <typename T, enum STRNUM_TESTS t> void test() { + char *s; enum PK_STN_RES res = {}; - T i = {0}; - if constexpr (t & TEST_ONE) { - res = pk_stn(&i, STRNUM_ONE); - std::cout << "pkstn: TEST_ONE res: " << i << "\n"; + T i = {1}; + if constexpr (t & TEST_SCALAR_NIL) { + res = pk_stn(&i, STRNUM_NIL, &s); + std::cout << "pkstn: TEST_SCALAR_NIL res: " << i << "\n"; + if (s != STRNUM_NIL) exit(1); + if (res != PK_STN_RES_INCONVERTIBLE) exit(1); + } + if constexpr (t & TEST_FLOATING_NIL) { + res = pk_stn(&i, STRNUM_FLOAT_NIL, &s); + std::cout << "pkstn: TEST_FLOATING_NIL res: " << i << "\n"; + if (s != STRNUM_FLOAT_NIL) exit(1); + if (res != PK_STN_RES_INCONVERTIBLE) exit(1); + } + if constexpr (t & TEST_SCALAR_ZED) { + res = pk_stn(&i, STRNUM_ZED, &s); + std::cout << "pkstn: TEST_SCALAR_ZED res: " << i << "\n"; + if (s != STRNUM_ZED + strlen(STRNUM_ZED)) exit(1); if (res != PK_STN_RES_SUCCESS) exit(1); - if (1 != i) exit(1); + if (T(VAL_SCALAR_ZED) != i) exit(1); } - if constexpr (t & TEST_ONE_MINUS) { - res = pk_stn(&i, STRNUM_ONE_MINUS); - std::cout << "pkstn: STRNUM_ONE_MINUS res: " << i << "\n"; + if constexpr (t & TEST_FLOATING_ZED) { + res = pk_stn(&i, STRNUM_FLOAT_ZED, &s); + std::cout << "pkstn: TEST_FLOATING_ZED res: " << i << "\n"; + if (s != STRNUM_FLOAT_ZED + strlen(STRNUM_FLOAT_ZED)) exit(1); if (res != PK_STN_RES_SUCCESS) exit(1); - if (T(-1) != i) exit(1); - } - if constexpr (t & TEST_UNDERFLOW) { - const char * str; - if constexpr(std::is_same<T, float>::value == true || std::is_same<T, double>::value == true) { - str = STRNUM_UNDERFLOW_FLOAT; - } else { - str = STRNUM_UNDERFLOW; - } - res = pk_stn(&i, str); - std::cout << "pkstn: TEST_UNDERFLOW res: " << i << "\n"; + if (T(VAL_FLOATING_ZED) != i) exit(1); + } + if constexpr (t & TEST_SCALAR_ONE) { + res = pk_stn(&i, STRNUM_ONE, &s); + std::cout << "pkstn: TEST_SCALAR_ONE res: " << i << "\n"; + if (s != STRNUM_ONE + strlen(STRNUM_ONE)) exit(1); + if (res != PK_STN_RES_SUCCESS) exit(1); + if (T(VAL_SCALAR_ONE) != i) exit(1); + } + if constexpr (t & TEST_FLOATING_ONE) { + res = pk_stn(&i, STRNUM_FLOAT_ONE, &s); + std::cout << "pkstn: TEST_FLOATING_ONE res: " << i << "\n"; + if (s != STRNUM_FLOAT_ONE + strlen(STRNUM_FLOAT_ONE)) exit(1); + if (res != PK_STN_RES_SUCCESS) exit(1); + if (T(VAL_FLOATING_ONE) != i) exit(1); + } + if constexpr (t & TEST_SCALAR_ONE_MINUS) { + res = pk_stn(&i, STRNUM_ONE_MINUS, &s); + std::cout << "pkstn: TEST_SCALAR_ONE_MINUS res: " << i << "\n"; + if (s != STRNUM_ONE_MINUS + strlen(STRNUM_ONE_MINUS)) exit(1); + if (res != PK_STN_RES_SUCCESS) exit(1); + if (T(VAL_SCALAR_ONE_MINUS) != i) exit(1); + } + if constexpr (t & TEST_FLOATING_ONE_MINUS) { + res = pk_stn(&i, STRNUM_FLOAT_ONE_MINUS, &s); + std::cout << "pkstn: TEST_FLOATING_ONE_MINUS res: " << i << "\n"; + if (s != STRNUM_FLOAT_ONE_MINUS + strlen(STRNUM_FLOAT_ONE_MINUS)) exit(1); + if (res != PK_STN_RES_SUCCESS) exit(1); + if (T(VAL_FLOATING_ONE_MINUS) != i) exit(1); + } + if constexpr (t & TEST_SCALAR_UNDERFLOW) { + res = pk_stn(&i, STRNUM_UNDERFLOW, &s); + std::cout << "pkstn: TEST_SCALAR_UNDERFLOW res: " << i << "\n"; + if (s != STRNUM_UNDERFLOW + strlen(STRNUM_UNDERFLOW)) exit(1); if constexpr(std::is_unsigned_v<T> == true) { if (res != PK_STN_RES_OVERFLOW) exit(1); } else { if (res != PK_STN_RES_UNDERFLOW) exit(1); } } - if constexpr (t & TEST_OVERFLOW) { - const char * str; - if constexpr(std::is_same<T, float>::value == true || std::is_same<T, double>::value == true) { - str = STRNUM_OVERFLOW_FLOAT; - } else { - str = STRNUM_OVERFLOW; - } - res = pk_stn(&i, str); - std::cout << "pkstn: TEST_OVERFLOW res: " << i << "\n"; + if constexpr (t & TEST_FLOATING_UNDERFLOW) { + res = pk_stn(&i, STRNUM_UNDERFLOW_FLOAT, &s); + std::cout << "pkstn: TEST_FLOATING_UNDERFLOW res: " << i << "\n"; + if (s != STRNUM_UNDERFLOW_FLOAT + strlen(STRNUM_UNDERFLOW_FLOAT)) exit(1); + if (res != PK_STN_RES_UNDERFLOW) exit(1); + } + if constexpr (t & TEST_SCALAR_OVERFLOW) { + res = pk_stn(&i, STRNUM_OVERFLOW, &s); + std::cout << "pkstn: TEST_SCALAR_OVERFLOW res: " << i << "\n"; + if (s != STRNUM_OVERFLOW + strlen(STRNUM_OVERFLOW)) exit(1); + if (res != PK_STN_RES_OVERFLOW) exit(1); + } + if constexpr (t & TEST_FLOATING_OVERFLOW) { + res = pk_stn(&i, STRNUM_OVERFLOW_FLOAT, &s); + std::cout << "pkstn: TEST_FLOATING_OVERFLOW res: " << i << "\n"; + if (s != STRNUM_OVERFLOW_FLOAT + strlen(STRNUM_OVERFLOW_FLOAT)) exit(1); if (res != PK_STN_RES_OVERFLOW) exit(1); } if constexpr (t & TEST_INCONVERTIBLE) { - res = pk_stn(&i, STRNUM_INCONVERTIBLE); + res = pk_stn(&i, STRNUM_INCONVERTIBLE, &s); std::cout << "pkstn: TEST_INCONVERTIBLE res: " << i << "\n"; + if (s != STRNUM_INCONVERTIBLE) exit(1); if (res != PK_STN_RES_INCONVERTIBLE) exit(1); } }; @@ -83,48 +151,54 @@ int main(int argc, char *argv[]) // stn_int64_t { fprintf(stdout, "\npkstn: starting int64_t\n"); - test<int64_t, TEST_ALL>(); + test<int64_t, TEST_SCALAR>(); } // stn_uint64_t { fprintf(stdout, "\npkstn: starting uint64_t\n"); - test<uint64_t, TEST_ALL>(); } + test<uint64_t, TEST_SCALAR>(); } // stn_int32_t { fprintf(stdout, "\npkstn: starting int32_t\n"); - test<int32_t, TEST_ALL>(); + test<int32_t, TEST_SCALAR>(); } // stn_uint32_t { fprintf(stdout, "\npkstn: starting uint32_t\n"); - test<uint32_t, TEST_ALL>(); + test<uint32_t, TEST_SCALAR>(); } // stn_int16_t { fprintf(stdout, "\npkstn: starting int16_t\n"); - test<int16_t, TEST_ALL>(); + test<int16_t, TEST_SCALAR>(); } // stn_uint16_t { fprintf(stdout, "\npkstn: starting uint16_t\n"); - test<uint16_t, TEST_ALL>(); + test<uint16_t, TEST_SCALAR>(); } // stn_int8_t { fprintf(stdout, "\npkstn: starting int8_t\n"); - test<int8_t, TEST_ALL>(); + test<int8_t, TEST_SCALAR>(); } // stn_uint8_t { fprintf(stdout, "\npkstn: starting uint8_t\n"); - test<uint8_t, TEST_ALL>(); + test<uint8_t, TEST_SCALAR>(); + } + + // bool + { + fprintf(stdout, "\npkstn: starting bool\n"); + test<bool, TEST_SCALAR>(); } // stn_float |
