From fc00fbe4385b3ec4b7f8c62cc50236d9cd3565e1 Mon Sep 17 00:00:00 2001 From: ahmedsamyh Date: Wed, 9 Apr 2025 04:50:48 +0500 Subject: [PATCH] [tests/string_view.c] Add tests for sv to number conversions. --- tests/string_view.c | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/tests/string_view.c b/tests/string_view.c index 470e2df..4732468 100644 --- a/tests/string_view.c +++ b/tests/string_view.c @@ -23,6 +23,9 @@ int main(void) { log_info("mc: '"SV_FMT"'", SV_ARG(mc)); + /// Conversions + + // STRING String_view foo = SV("Hello Mister"); char *cstr = sv_to_cstr(foo); @@ -31,5 +34,63 @@ int main(void) { C_FREE(cstr); + // INT + int i_count = -1; + String_view int_sv = SV("-39"); + int64 i = sv_to_int(int_sv, &i_count, 10); + ASSERT(i_count != -1, "We know `int_sv` contains an int!"); + + log_info("int from sv: "SV_FMT" -> %d (len of int str: %d)", SV_ARG(int_sv), i, i_count); + + int_sv = SV("1337 junk"); + i = sv_to_int(int_sv, &i_count, 10); + ASSERT(i_count != -1, "We know `int_sv` contains an int!"); + + log_info("int from sv: "SV_FMT" -> %d (len of int str: %d)", SV_ARG(int_sv), i, i_count); + + int_sv = SV("baka1337 junk"); + i = sv_to_int(int_sv, &i_count, 10); + ASSERT(i_count == -1, "We know `int_sv` does not contain an int!"); + + log_info("int from sv: "SV_FMT" -> %d (len of int str: %d)", SV_ARG(int_sv), i, i_count); + + // UINT + int u_count = -1; + String_view uint_sv = SV("1240"); + uint64 u = sv_to_uint(uint_sv, &u_count, 10); + ASSERT(u_count != -1, "We know `uint_sv` contains an uint!"); + + log_info("uint from sv: "SV_FMT" -> %d (len of uint str: %d)", SV_ARG(uint_sv), u, u_count); + + uint_sv = SV("00000012312strin"); + u = sv_to_uint(uint_sv, &u_count, 10); + ASSERT(u_count != -1, "We know `uint_sv` contains an uint!"); + + log_info("uint from sv: "SV_FMT" -> %d (len of uint str: %d)", SV_ARG(uint_sv), u, u_count); + + uint_sv = SV("burger 123123.23423 junk"); + u = sv_to_uint(uint_sv, &u_count, 10); + ASSERT(u_count == -1, "We know `uint_sv` does not contain an uint!"); + + log_info("uint from sv: "SV_FMT" -> %d (len of uint str: %d)", SV_ARG(uint_sv), u, u_count); + + // FLOAT + int f_count = -1; + String_view float_sv = SV("-2342.045BLAH345"); + float f = sv_to_float(float_sv, &f_count); + ASSERT(f_count != -1, "We know `float_sv` contains an float!"); + + log_info("float from sv: "SV_FMT" -> %f (len of float str: %d)", SV_ARG(float_sv), f, f_count); + + float_sv = SV("1.013012312310f"); + f = sv_to_float(float_sv, &f_count); + ASSERT(f_count != -1, "We know `float_sv` contains an float!"); + log_info("float from sv: "SV_FMT" -> %f (len of float str: %d)", SV_ARG(float_sv), f, f_count); + + float_sv = SV("T.013012312310f"); + f = sv_to_float(float_sv, &f_count); + ASSERT(f_count == -1, "We know `float_sv` does not contain an float!"); + log_info("float from sv: "SV_FMT" -> %f (len of float str: %d)", SV_ARG(float_sv), f, f_count); + return 0; } -- 2.39.5