Change in libosmocore[master]: utils_test: add osmo_print_n_test()

2019-11-24 Thread neels
neels has submitted this change. ( 
https://gerrit.osmocom.org/c/libosmocore/+/16165 )

Change subject: utils_test: add osmo_print_n_test()
..

utils_test: add osmo_print_n_test()

A couple of times recently I've needed to copy out a substring to a buffer with
limited size. Use of strncpy() or osmo_strlcpy() are nontrivial here.
I wanted to have a dedicated function.

After I wrote that function with a test, I noticed that I had already
implemented the same thing a while ago, as osmo_print_n() :P
So here is just the test.

Change-Id: Ia716abdc1f58af6065b84f4f567388a32a7b39fc
---
M src/utils.c
M tests/utils/utils_test.c
M tests/utils/utils_test.ok
3 files changed, 82 insertions(+), 0 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/src/utils.c b/src/utils.c
index 904f6e4..4378431 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -509,6 +509,8 @@
  * Copy at most \a siz bytes from \a src to \a dst, ensuring that the result is
  * NUL terminated. The NUL character is included in \a siz, i.e. passing the
  * actual sizeof(*dst) is correct.
+ *
+ * Note, a similar function that also limits the input buffer size is 
osmo_print_n().
  */
 size_t osmo_strlcpy(char *dst, const char *src, size_t siz)
 {
diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c
index 55c9e7f..69510af 100644
--- a/tests/utils/utils_test.c
+++ b/tests/utils/utils_test.c
@@ -1138,6 +1138,59 @@
talloc_free(ctx);
 }

+static void osmo_print_n_test(void)
+{
+   struct token_test {
+   const char *src;
+   size_t token_len;
+   size_t buf_size;
+   const char *expect_token;
+   int expect_rc;
+   };
+   struct token_test tests[] = {
+   { "foo=bar", 3, 100, "foo", 3 },
+   { "foo", 10, 100, "foo", 3 },
+   { "foo", 3, 100, "foo", 3 },
+   { NULL, 10, 100, "", 0 },
+   { "", 10, 100, "", 0 },
+   { "foo=bar", 0, 100, "", 0 },
+
+   { "foo=bar", 3, 2, "f", 3 },
+   { "foo", 10, 2, "f", 3 },
+   { "foo", 3, 2, "f", 3 },
+   { NULL, 10, 2, "", 0 },
+   { "", 10, 2, "", 0 },
+   { "foo=bar", 0, 2, "", 0 },
+
+   { "foo=bar", 3, 1, "", 3 },
+   { "foo", 10, 1, "", 3 },
+   { "foo", 3, 1, "", 3 },
+   { NULL, 10, 1, "", 0 },
+   { "", 10, 1, "", 0 },
+   { "foo=bar", 0, 1, "", 0 },
+
+   { "foo=bar", 3, 0, "unchanged", 3 },
+   { "foo", 10, 0, "unchanged", 3 },
+   { "foo", 3, 0, "unchanged", 3 },
+   { NULL, 10, 0, "unchanged", 0 },
+   { "", 10, 0, "unchanged", 0 },
+   { "foo=bar", 0, 0, "unchanged", 0 },
+   };
+   struct token_test *t;
+   printf("\n%s()\n", __func__);
+   for (t = tests; t - tests < ARRAY_SIZE(tests); t++) {
+   char buf[100] = "unchanged";
+   int rc = osmo_print_n(buf, t->buf_size, t->src, t->token_len);
+   printf("%s token_len=%zu buf_size=%zu", osmo_quote_str(t->src, 
-1), t->token_len, t->buf_size);
+   printf(" -> token=%s rc=%d", osmo_quote_str(buf, -1), rc);
+   if (strcmp(buf, t->expect_token))
+   printf(" ERROR: expected token %s", 
osmo_quote_str(t->expect_token, -1));
+   if (rc != t->expect_rc)
+   printf(" ERROR: expected rc %d", t->expect_rc);
+   printf("\n");
+   }
+}
+
 int main(int argc, char **argv)
 {
static const struct log_info log_info = {};
@@ -1159,5 +1212,6 @@
strbuf_test_nolen();
startswith_test();
name_c_impl_test();
+   osmo_print_n_test();
return 0;
 }
diff --git a/tests/utils/utils_test.ok b/tests/utils/utils_test.ok
index b603647..d5cf491 100644
--- a/tests/utils/utils_test.ok
+++ b/tests/utils/utils_test.ok
@@ -377,3 +377,29 @@
   OSMO_NAME_C_IMPL(10, NULL) -> NULL  allocated 0
 OSMO_NAME_C_IMPL(0, "ERROR") -> "ERROR"  allocated 1  6 bytes, name 
'foo_name_c_zero'
OSMO_NAME_C_IMPL(0, NULL) -> NULL  allocated 0
+
+osmo_print_n_test()
+"foo=bar" token_len=3 buf_size=100 -> token="foo" rc=3
+"foo" token_len=10 buf_size=100 -> token="foo" rc=3
+"foo" token_len=3 buf_size=100 -> token="foo" rc=3
+NULL token_len=10 buf_size=100 -> token="" rc=0
+"" token_len=10 buf_size=100 -> token="" rc=0
+"foo=bar" token_len=0 buf_size=100 -> token="" rc=0
+"foo=bar" token_len=3 buf_size=2 -> token="f" rc=3
+"foo" token_len=10 buf_size=2 -> token="f" rc=3
+"foo" token_len=3 buf_size=2 -> token="f" rc=3
+NULL token_len=10 buf_size=2 -> token="" rc=0
+"" token_len=10 buf_size=2 -> token="" rc=0
+"foo=bar" token_len=0 buf_size=2 -> token="" rc=0
+"foo=bar" token_len=3 buf_size=1 -> token="" rc=3
+"foo" token_len=10 

Change in libosmocore[master]: utils_test: add osmo_print_n_test()

2019-11-22 Thread laforge
laforge has posted comments on this change. ( 
https://gerrit.osmocom.org/c/libosmocore/+/16165 )

Change subject: utils_test: add osmo_print_n_test()
..


Patch Set 2: Code-Review+2


--
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/16165
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Ia716abdc1f58af6065b84f4f567388a32a7b39fc
Gerrit-Change-Number: 16165
Gerrit-PatchSet: 2
Gerrit-Owner: neels 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge 
Gerrit-Comment-Date: Fri, 22 Nov 2019 12:32:55 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


Change in libosmocore[master]: utils_test: add osmo_print_n_test()

2019-11-21 Thread neels
neels has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/libosmocore/+/16165 )


Change subject: utils_test: add osmo_print_n_test()
..

utils_test: add osmo_print_n_test()

A couple of times recently I've needed to copy out a substring to a buffer with
limited size. Use of strncpy() or osmo_strlcpy() are nontrivial here.
I wanted to have a dedicated function.

After I wrote that function with a test, I noticed that I had already
implemented the same thing a while ago, as osmo_print_n() :P
So here is just the test.

Change-Id: Ia716abdc1f58af6065b84f4f567388a32a7b39fc
---
M src/utils.c
M tests/utils/utils_test.c
M tests/utils/utils_test.ok
3 files changed, 82 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/65/16165/1

diff --git a/src/utils.c b/src/utils.c
index 8dfa7ec..4e55a89 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -509,6 +509,8 @@
  * Copy at most \a siz bytes from \a src to \a dst, ensuring that the result is
  * NUL terminated. The NUL character is included in \a siz, i.e. passing the
  * actual sizeof(*dst) is correct.
+ *
+ * Note, a similar function that also limits the input buffer size is 
osmo_print_n().
  */
 size_t osmo_strlcpy(char *dst, const char *src, size_t siz)
 {
diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c
index 3c6d17b..458458e 100644
--- a/tests/utils/utils_test.c
+++ b/tests/utils/utils_test.c
@@ -1264,6 +1264,59 @@
talloc_free(ctx);
 }

+static void osmo_print_n_test(void)
+{
+   struct token_test {
+   const char *src;
+   size_t token_len;
+   size_t buf_size;
+   const char *expect_token;
+   int expect_rc;
+   };
+   struct token_test tests[] = {
+   { "foo=bar", 3, 100, "foo", 3 },
+   { "foo", 10, 100, "foo", 3 },
+   { "foo", 3, 100, "foo", 3 },
+   { NULL, 10, 100, "", 0 },
+   { "", 10, 100, "", 0 },
+   { "foo=bar", 0, 100, "", 0 },
+
+   { "foo=bar", 3, 2, "f", 3 },
+   { "foo", 10, 2, "f", 3 },
+   { "foo", 3, 2, "f", 3 },
+   { NULL, 10, 2, "", 0 },
+   { "", 10, 2, "", 0 },
+   { "foo=bar", 0, 2, "", 0 },
+
+   { "foo=bar", 3, 1, "", 3 },
+   { "foo", 10, 1, "", 3 },
+   { "foo", 3, 1, "", 3 },
+   { NULL, 10, 1, "", 0 },
+   { "", 10, 1, "", 0 },
+   { "foo=bar", 0, 1, "", 0 },
+
+   { "foo=bar", 3, 0, "unchanged", 3 },
+   { "foo", 10, 0, "unchanged", 3 },
+   { "foo", 3, 0, "unchanged", 3 },
+   { NULL, 10, 0, "unchanged", 0 },
+   { "", 10, 0, "unchanged", 0 },
+   { "foo=bar", 0, 0, "unchanged", 0 },
+   };
+   struct token_test *t;
+   printf("\n%s()\n", __func__);
+   for (t = tests; t - tests < ARRAY_SIZE(tests); t++) {
+   char buf[100] = "unchanged";
+   int rc = osmo_print_n(buf, t->buf_size, t->src, t->token_len);
+   printf("%s token_len=%zu buf_size=%zu", osmo_quote_str(t->src, 
-1), t->token_len, t->buf_size);
+   printf(" -> token=%s rc=%d", osmo_quote_str(buf, -1), rc);
+   if (strcmp(buf, t->expect_token))
+   printf(" ERROR: expected token %s", 
osmo_quote_str(t->expect_token, -1));
+   if (rc != t->expect_rc)
+   printf(" ERROR: expected rc %d", t->expect_rc);
+   printf("\n");
+   }
+}
+
 int main(int argc, char **argv)
 {
static const struct log_info log_info = {};
@@ -1287,5 +1340,6 @@
strbuf_test_nolen();
startswith_test();
name_c_impl_test();
+   osmo_print_n_test();
return 0;
 }
diff --git a/tests/utils/utils_test.ok b/tests/utils/utils_test.ok
index 10436ce..bd43365 100644
--- a/tests/utils/utils_test.ok
+++ b/tests/utils/utils_test.ok
@@ -449,3 +449,29 @@
   OSMO_NAME_C_IMPL(10, NULL) -> NULL  allocated 0
 OSMO_NAME_C_IMPL(0, "ERROR") -> "ERROR"  allocated 1  6 bytes, name 
'foo_name_c_zero'
OSMO_NAME_C_IMPL(0, NULL) -> NULL  allocated 0
+
+osmo_print_n_test()
+"foo=bar" token_len=3 buf_size=100 -> token="foo" rc=3
+"foo" token_len=10 buf_size=100 -> token="foo" rc=3
+"foo" token_len=3 buf_size=100 -> token="foo" rc=3
+NULL token_len=10 buf_size=100 -> token="" rc=0
+"" token_len=10 buf_size=100 -> token="" rc=0
+"foo=bar" token_len=0 buf_size=100 -> token="" rc=0
+"foo=bar" token_len=3 buf_size=2 -> token="f" rc=3
+"foo" token_len=10 buf_size=2 -> token="f" rc=3
+"foo" token_len=3 buf_size=2 -> token="f" rc=3
+NULL token_len=10 buf_size=2 -> token="" rc=0
+"" token_len=10 buf_size=2 -> token="" rc=0
+"foo=bar" token_len=0 buf_size=2 -> token="" rc=0
+"foo=bar" token_len=3 buf_size=1 -> token="" rc=3
+"foo"