From 33484cccdea2790721fb20f75588d1ed4fb53017 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Sat, 14 Dec 2024 13:50:09 -0500 Subject: pkstn: more tests + pk_stn --- test/pkstn.cpp | 143 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 91 insertions(+), 52 deletions(-) (limited to 'test') diff --git a/test/pkstn.cpp b/test/pkstn.cpp index 920c6ac..8a4d901 100644 --- a/test/pkstn.cpp +++ b/test/pkstn.cpp @@ -1,103 +1,142 @@ #include "../pkstn.h" +#include #include #include +#include + +const char *STRNUM_ONE = "1"; +const char *STRNUM_ONE_MINUS = "-1"; +const char *STRNUM_UNDERFLOW = "-99999999999999999999999999999999"; +const char *STRNUM_OVERFLOW = "99999999999999999999999999999999"; +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, +}; + +template +void test() { + 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"; + if (res != PK_STN_RES_SUCCESS) exit(1); + if (1 != 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 (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::value == true || std::is_same::value == true) { + str = STRNUM_UNDERFLOW_FLOAT; + } else { + str = STRNUM_UNDERFLOW; + } + res = pk_stn(&i, str); + std::cout << "pkstn: TEST_UNDERFLOW res: " << i << "\n"; + if constexpr(std::is_unsigned_v == 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::value == true || std::is_same::value == true) { + str = STRNUM_OVERFLOW_FLOAT; + } else { + str = STRNUM_OVERFLOW; + } + res = pk_stn(&i, str); + std::cout << "pkstn: TEST_OVERFLOW res: " << i << "\n"; + if (res != PK_STN_RES_OVERFLOW) exit(1); + } + if constexpr (t & TEST_INCONVERTIBLE) { + res = pk_stn(&i, STRNUM_INCONVERTIBLE); + std::cout << "pkstn: TEST_INCONVERTIBLE res: " << i << "\n"; + if (res != PK_STN_RES_INCONVERTIBLE) exit(1); + } +}; int main(int argc, char *argv[]) { (void)argc; (void)argv; - enum PK_STN_RES res = {}; + fprintf(stdout, "\n"); // stn_int64_t { - int64_t i = {0}; - res = pk_stn_int64_t(&i, "-1", 0); - fprintf(stdout, "pkstn: stn_int64_t res: %ld\n", i); - if (res != PK_STN_RES_SUCCESS) exit(1); - if (-1 != i) exit(1); + fprintf(stdout, "\npkstn: starting int64_t\n"); + test(); } // stn_uint64_t { - uint64_t i = {0}; - res = pk_stn_uint64_t(&i, "1", 0); - fprintf(stdout, "pkstn: stn_uint64_t res: %lu\n", i); - if (res != PK_STN_RES_SUCCESS) exit(1); - if (1 != i) exit(1); - } + fprintf(stdout, "\npkstn: starting uint64_t\n"); + test(); } // stn_int32_t { - int32_t i = {0}; - res = pk_stn_int32_t(&i, "-1", 0); - fprintf(stdout, "pkstn: stn_int32_t res: %d\n", i); - if (res != PK_STN_RES_SUCCESS) exit(1); - if (-1 != i) exit(1); + fprintf(stdout, "\npkstn: starting int32_t\n"); + test(); } // stn_uint32_t { - uint32_t i = {0}; - res = pk_stn_uint32_t(&i, "1", 0); - fprintf(stdout, "pkstn: stn_uint32_t res: %u\n", i); - if (res != PK_STN_RES_SUCCESS) exit(1); - if (1 != i) exit(1); + fprintf(stdout, "\npkstn: starting uint32_t\n"); + test(); } // stn_int16_t { - int16_t i = {0}; - res = pk_stn_int16_t(&i, "-1", 0); - fprintf(stdout, "pkstn: stn_int16_t res: %d\n", i); - if (res != PK_STN_RES_SUCCESS) exit(1); - if (-1 != i) exit(1); + fprintf(stdout, "\npkstn: starting int16_t\n"); + test(); } // stn_uint16_t { - uint16_t i = {0}; - res = pk_stn_uint16_t(&i, "1", 0); - fprintf(stdout, "pkstn: stn_uint16_t res: %u\n", i); - if (res != PK_STN_RES_SUCCESS) exit(1); - if (1 != i) exit(1); + fprintf(stdout, "\npkstn: starting uint16_t\n"); + test(); } // stn_int8_t { - int8_t i = {0}; - res = pk_stn_int8_t(&i, "-1", 0); - fprintf(stdout, "pkstn: stn_int8_t res: %d\n", i); - if (res != PK_STN_RES_SUCCESS) exit(1); - if (-1 != i) exit(1); + fprintf(stdout, "\npkstn: starting int8_t\n"); + test(); } // stn_uint8_t { - uint8_t i = {0}; - res = pk_stn_uint8_t(&i, "1", 0); - fprintf(stdout, "pkstn: stn_uint8_t res: %u\n", i); - if (res != PK_STN_RES_SUCCESS) exit(1); - if (1 != i) exit(1); + fprintf(stdout, "\npkstn: starting uint8_t\n"); + test(); } // stn_float { - float f = {0}; - res = pk_stn_float(&f, "-1"); - fprintf(stdout, "pkstn: stn_float res: %f\n", f); - if (res != PK_STN_RES_SUCCESS) exit(1); - if (-1 != f) exit(1); + fprintf(stdout, "\npkstn: starting float\n"); + test(); } // stn_double { - double f = {0}; - res = pk_stn_double(&f, "-1"); - fprintf(stdout, "pkstn: stn_double res: %f\n", f); - if (res != PK_STN_RES_SUCCESS) exit(1); - if (-1 != f) exit(1); + fprintf(stdout, "\npkstn: starting double\n"); + test(); } return 0; -- cgit v1.2.3