]> www.git.momoyon.org Git - commonlib.git/commitdiff
[tests/string_view.c] Add tests for sv to number conversions.
authorahmedsamyh <ahmedsamyh10@gmail.com>
Tue, 8 Apr 2025 23:50:48 +0000 (04:50 +0500)
committerahmedsamyh <ahmedsamyh10@gmail.com>
Tue, 8 Apr 2025 23:50:48 +0000 (04:50 +0500)
tests/string_view.c

index 470e2df94f2a21c43cd7c7aceb6d95f865b388bb..47324685bb6fd1fa85d997f6114bd34c672f1514 100644 (file)
@@ -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;
 }