aboutsummaryrefslogtreecommitdiff
path: root/test/lib
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2021-09-25 07:03:06 -0600
committerTom Rini <trini@konsulko.com>2021-10-08 15:53:26 -0400
commit930c887e0fb88dcf1907f268960330c17999b5a3 (patch)
tree9f1bdfd36187f27d0c70b9a981974391b61014bd /test/lib
parent0caf37e973015255a3c5b9439ddb8c6aef1b5001 (diff)
downloadu-boot-930c887e0fb88dcf1907f268960330c17999b5a3.zip
u-boot-930c887e0fb88dcf1907f268960330c17999b5a3.tar.gz
u-boot-930c887e0fb88dcf1907f268960330c17999b5a3.tar.bz2
lib: Add memdup()
Add a function to duplicate a memory region, a little like strdup(). Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'test/lib')
-rw-r--r--test/lib/string.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/test/lib/string.c b/test/lib/string.c
index 64234be..5dcf4d6 100644
--- a/test/lib/string.c
+++ b/test/lib/string.c
@@ -23,6 +23,8 @@
/* Allow for copying up to 32 bytes */
#define BUFLEN (SWEEP + 33)
+#define TEST_STR "hello"
+
/**
* init_buffer() - initialize buffer
*
@@ -193,3 +195,33 @@ static int lib_memmove(struct unit_test_state *uts)
}
LIB_TEST(lib_memmove, 0);
+
+/** lib_memdup() - unit test for memdup() */
+static int lib_memdup(struct unit_test_state *uts)
+{
+ char buf[BUFLEN];
+ size_t len;
+ char *p, *q;
+
+ /* Zero size should do nothing */
+ p = memdup(NULL, 0);
+ ut_assertnonnull(p);
+ free(p);
+
+ p = memdup(buf, 0);
+ ut_assertnonnull(p);
+ free(p);
+
+ strcpy(buf, TEST_STR);
+ len = sizeof(TEST_STR);
+ p = memdup(buf, len);
+ ut_asserteq_mem(p, buf, len);
+
+ q = memdup(p, len);
+ ut_asserteq_mem(q, buf, len);
+ free(q);
+ free(p);
+
+ return 0;
+}
+LIB_TEST(lib_memdup, 0);