commit 78ff352c3eaa852ff2173606420a9e4d559c68b3
parent bd791557ff5d67313946e7d24b5494df1cdad5df
Author: vaplv <vincent.forest@meso-star.com>
Date: Tue, 7 Feb 2017 16:17:17 +0100
Add and test the cstr_to_list_uint function
Diffstat:
4 files changed, 110 insertions(+), 17 deletions(-)
diff --git a/src/cstr.c b/src/cstr.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 2013-2016 Vincent Forest (vaplv@free.fr)
+/* Copyright (C) 2013-2017 Vincent Forest (vaplv@free.fr)
*
* The RSys library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
@@ -20,3 +20,7 @@
#define CSTR_LIST_TYPE float
#include "cstr_to_list.h"
+
+#define CSTR_LIST_TYPE unsigned
+#define CSTR_LIST_SUFFIX uint
+#include "cstr_to_list.h"
diff --git a/src/cstr.h b/src/cstr.h
@@ -149,5 +149,13 @@ cstr_to_list_float
size_t* length, /* May be NULL. Length of the filled list */
const size_t max_length); /* Maximum size of dst */
+RSYS_API res_T
+cstr_to_list_uint
+ (const char* str,
+ const char delimiter,
+ unsigned dst[],
+ size_t* length,
+ const size_t max_length);
+
#endif /* CSTR_H */
diff --git a/src/cstr_to_list.h b/src/cstr_to_list.h
@@ -32,8 +32,12 @@
#error "Missing the CSTR_LIST_TYPE macro defining the type of the list"
#endif
+#ifndef CSTR_LIST_SUFFIX
+ #define CSTR_LIST_SUFFIX CSTR_LIST_TYPE
+#endif
+
res_T
-CONCAT(cstr_to_list_, CSTR_LIST_TYPE)
+CONCAT(cstr_to_list_, CSTR_LIST_SUFFIX)
(const char* str,
const char delimiter,
CSTR_LIST_TYPE dst[],
@@ -73,10 +77,10 @@ CONCAT(cstr_to_list_, CSTR_LIST_TYPE)
res = RES_BAD_ARG;
goto error;
}
- res = CONCAT(cstr_to_, CSTR_LIST_TYPE)(tk, dst + i);
+ res = CONCAT(cstr_to_, CSTR_LIST_SUFFIX)(tk, dst + i);
} else {
CSTR_LIST_TYPE d;
- res = CONCAT(cstr_to_, CSTR_LIST_TYPE)(tk, &d);
+ res = CONCAT(cstr_to_, CSTR_LIST_SUFFIX)(tk, &d);
}
if(res != RES_OK) goto error;
}
@@ -92,6 +96,7 @@ error:
}
#undef CSTR_LIST_TYPE
+#undef CSTR_LIST_SUFFIX
#endif /* defined(CSTR_LIST_TYPE) */
diff --git a/src/test_cstr.c b/src/test_cstr.c
@@ -16,21 +16,10 @@
#include "cstr.h"
#include "math.h"
-int
-main(int argc, char** argv)
+static void
+test_double(void)
{
- char buf[128];
- double dlist[4];
- float flist[4];
- size_t len;
double d;
- float f;
- long l;
- int i;
- unsigned u;
- unsigned long ul;
- (void)argc, (void)argv;
-
CHECK(cstr_to_double(NULL, &d), RES_BAD_ARG);
CHECK(cstr_to_double("a", &d), RES_BAD_ARG);
CHECK(cstr_to_double(STR(PI), &d), RES_OK);
@@ -44,7 +33,12 @@ main(int argc, char** argv)
CHECK(cstr_to_double("INFINITY", &d), RES_OK);
CHECK(d, INF);
CHECK(cstr_to_double("", &d), RES_BAD_ARG);
+}
+static void
+test_float(void)
+{
+ float f;
CHECK(cstr_to_float(NULL, &f), RES_BAD_ARG);
CHECK(cstr_to_float("a", &f), RES_BAD_ARG);
CHECK(cstr_to_float(STR(PI), &f), RES_OK);
@@ -63,7 +57,12 @@ main(int argc, char** argv)
CHECK(f, 0.f);
CHECK(cstr_to_float("-0.0", &f), RES_OK);
CHECK(f, 0.f);
+}
+static void
+test_long(void)
+{
+ long l;
CHECK(cstr_to_long(NULL, &l), RES_BAD_ARG);
CHECK(cstr_to_long("a", &l), RES_BAD_ARG);
CHECK(cstr_to_long("1.e-3", &l), RES_BAD_ARG);
@@ -76,6 +75,13 @@ main(int argc, char** argv)
CHECK(cstr_to_long(" \t+1234567890 \t ", &l), RES_OK);
CHECK(l, 1234567890);
CHECK(cstr_to_long(" \t+1234567890 \t a", &l), RES_BAD_ARG);
+}
+
+static void
+test_int(void)
+{
+ char buf[128];
+ int i;
CHECK(cstr_to_int(NULL, &i), RES_BAD_ARG);
CHECK(cstr_to_int("a", &i), RES_BAD_ARG);
@@ -96,6 +102,13 @@ main(int argc, char** argv)
CHECK(cstr_to_int(buf, &i), RES_BAD_ARG);
sprintf(buf, "%lld", (long long)INT_MIN-1);
CHECK(cstr_to_int(buf, &i), RES_BAD_ARG);
+}
+
+static void
+test_uint(void)
+{
+ char buf[128];
+ unsigned u;
CHECK(cstr_to_uint(NULL, &u), RES_BAD_ARG);
CHECK(cstr_to_uint("a", &u), RES_BAD_ARG);
@@ -113,6 +126,13 @@ main(int argc, char** argv)
CHECK(u, UINT_MAX);
sprintf(buf, "%llu", (unsigned long long)UINT_MAX+1);
CHECK(cstr_to_uint(buf, &u), RES_BAD_ARG);
+}
+
+static void
+test_ulong(void)
+{
+ char buf[128];
+ unsigned long ul;
CHECK(cstr_to_ulong(NULL, &ul), RES_BAD_ARG);
CHECK(cstr_to_ulong("a", &ul), RES_BAD_ARG);
@@ -130,6 +150,13 @@ main(int argc, char** argv)
CHECK(ul, ULONG_MAX);
sprintf(buf, "%lu%c", ULONG_MAX, '0');
CHECK(cstr_to_ulong(buf, &ul), RES_BAD_ARG);
+}
+
+static void
+test_list_double(void)
+{
+ double dlist[4];
+ size_t len;
CHECK(cstr_to_list_double(NULL, ':', dlist, NULL, 3), RES_BAD_ARG);
CHECK(cstr_to_list_double("a", ':', dlist, NULL, 3), RES_BAD_ARG);
@@ -168,6 +195,13 @@ main(int argc, char** argv)
CHECK(dlist[1], -1.0);
CHECK(dlist[2], -1.0);
CHECK(dlist[3], -1.0);
+}
+
+static void
+test_list_float(void)
+{
+ float flist[4];
+ size_t len;
CHECK(cstr_to_list_float(NULL, ':', flist, NULL, 3), RES_BAD_ARG);
CHECK(cstr_to_list_float("a", ':', flist, NULL, 3), RES_BAD_ARG);
@@ -194,6 +228,48 @@ main(int argc, char** argv)
CHECK(flist[1], 0.5f);
CHECK(flist[2], 1.2f);
CHECK(flist[3], (float)INF);
+}
+
+static void
+test_list_uint(void)
+{
+ unsigned ulist[4];
+ size_t len;
+
+ CHECK(cstr_to_list_uint(NULL, ':', ulist, NULL, 3), RES_BAD_ARG);
+ CHECK(cstr_to_list_uint("a", ':', ulist, NULL, 3), RES_BAD_ARG);
+ CHECK(cstr_to_list_uint("1:4:-1", ':', ulist, NULL, 3), RES_OK);
+ CHECK(ulist[0], 1);
+ CHECK(ulist[1], 4);
+ CHECK(ulist[2], (unsigned)-1);
+ CHECK(cstr_to_list_uint("1:2::", ':', ulist, &len, 3), RES_OK);
+ CHECK(len, 2);
+ CHECK(ulist[0], 1);
+ CHECK(ulist[1], 2);
+ CHECK(cstr_to_list_uint("1:5:2:4", ':', ulist, &len, 2), RES_BAD_ARG);
+ CHECK(cstr_to_list_uint("1:5:2:4.2", ':', NULL, &len, 0), RES_BAD_ARG);
+ CHECK(cstr_to_list_uint("1:5:2:4", ':', NULL, &len, 0), RES_OK);
+ CHECK(len, 4);
+ CHECK(cstr_to_list_uint("-1.5.2.3", ':', ulist, NULL, len), RES_BAD_ARG);
+ CHECK(cstr_to_list_uint("-1.5.2.3", '.', ulist, NULL, len), RES_OK);
+ CHECK(ulist[0], (unsigned)-1);
+ CHECK(ulist[1], 5);
+ CHECK(ulist[2], 2);
+ CHECK(ulist[3], 3);
+}
+int
+main(int argc, char** argv)
+{
+ (void)argc, (void)argv;
+ test_double();
+ test_float();
+ test_long();
+ test_int();
+ test_uint();
+ test_ulong();
+ test_list_double();
+ test_list_float();
+ test_list_uint();
return 0;
}