commit aba374623ea4c420dcb8698c639d95e5bfef812f
parent 5e6241c7ba3bf472fd189e88eb7585b5792db395
Author: vaplv <vaplv@free.fr>
Date: Wed, 7 Jan 2015 11:56:59 +0100
Add and test the str_eq and str_cmp functions
Diffstat:
3 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/src/str.h b/src/str.h
@@ -19,6 +19,7 @@
#include "hash.h"
#include "mem_allocator.h"
#include "rsys.h"
+#include <string.h>
struct str {
/* Internal data. Should not be publicly accessed */
@@ -80,9 +81,23 @@ str_get(struct str* str)
static INLINE const char*
str_cget(const struct str* str)
{
+ ASSERT(str);
return str->cstr;
}
+static INLINE int
+str_cmp(const struct str* str0, const struct str* str1)
+{
+ ASSERT(str0 && str1);
+ return strcmp(str_cget(str0), str_cget(str1));
+}
+
+static INLINE char
+str_eq(const struct str* str0, const struct str* str1)
+{
+ return str_cmp(str0, str1) == 0;
+}
+
BEGIN_DECLS
RSYS_API res_T str_set(struct str* str, const char* cstr);
diff --git a/src/test_binary_heap.c b/src/test_binary_heap.c
@@ -102,13 +102,6 @@ test_primitive_type(void)
mem_shutdown_proxy_allocator(&allocator_proxy);
}
-static int
-str_cmp(const struct str* a, const struct str* b)
-{
- ASSERT(a && b);
- return strcmp(str_cget(a), str_cget(b));
-}
-
#define BHEAP_NAME str
#define BHEAP_DATA struct str
#define BHEAP_FUNCTOR_INIT str_init
diff --git a/src/test_str.c b/src/test_str.c
@@ -98,6 +98,8 @@ main(int argc, char** argv)
CHECK(strcmp(str_cget(&str2), "0Hello world!"), 0);
CHECK(strcmp(str_cget(&str), "0Hello world!"), 0);
+ CHECK(str_eq(&str, &str2), 1);
+
CHECK(str_hash(&str), str_hash(&str2));
str_clear(&str2);
NCHECK(str_hash(&str), str_hash(&str2));
@@ -111,6 +113,9 @@ main(int argc, char** argv)
str_init(&allocator_proxy, &str2);
str_set(&str2, "ABC Hello a0123456789ABCDEFbcdefgh world! insert");
+ CHECK(str_eq(&str, &str2), 0);
+ CHECK(str_cmp(&str, &str2), strcmp(str_cget(&str), str_cget(&str2)));
+
NCHECK(str_hash(&str), str_hash(&str2));
str_copy_and_clear(&str, &str2);
CHECK(0, strcmp