billiob pushed a commit to branch master.

http://git.enlightenment.org/apps/terminology.git/commit/?id=04a47e0e19023da0e7bc40d6c5c5f5732c5ebfee

commit 04a47e0e19023da0e7bc40d6c5c5f5732c5ebfee
Author: Boris Faure <bill...@gmail.com>
Date:   Sun May 24 00:05:29 2020 +0200

    sb: add unit tests + fix a few issues
---
 src/bin/meson.build  |   1 +
 src/bin/sb.c         | 135 +++++++++++++++++++++++++++++++++++++++++++++++++--
 src/bin/sb.h         |   6 +--
 src/bin/tytest.c     |   2 +
 src/bin/tytest.h     |   6 ---
 src/bin/unit_tests.h |  11 +++++
 6 files changed, 147 insertions(+), 14 deletions(-)

diff --git a/src/bin/meson.build b/src/bin/meson.build
index 192066a..d8ed7cb 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -78,6 +78,7 @@ tytest_sources = ['termptyesc.c', 'termptyesc.h',
                   'utf8.c', 'utf8.h',
                   'utils.c', 'utils.h',
                   'md5/md5.c', 'md5/md5.h',
+                  'unit_tests.h',
                   'tytest_common.c', 'tytest_common.h',
                   'tytest.c', 'tytest.h']
 
diff --git a/src/bin/sb.c b/src/bin/sb.c
index c91bfd3..defe4b0 100644
--- a/src/bin/sb.c
+++ b/src/bin/sb.c
@@ -1,10 +1,16 @@
 #include "private.h"
-#include "sb.h"
 
+#include <assert.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>
 
+#include "sb.h"
+
+#if defined(ENABLE_TESTS)
+#include "unit_tests.h"
+#endif
+
 int
 ty_sb_add(struct ty_sb *sb, const char *s, size_t len)
 {
@@ -31,7 +37,7 @@ ty_sb_add(struct ty_sb *sb, const char *s, size_t len)
 }
 
 int
-ty_sb_prepend(struct ty_sb *sb, const char *s, size_t  len)
+ty_sb_prepend(struct ty_sb *sb, const char *s, size_t len)
 {
    if (len >= sb->gap)
      {
@@ -127,8 +133,10 @@ ty_sb_steal_buf(struct ty_sb *sb)
 }
 
 void
-ty_sb_lskip(struct ty_sb *sb, int len)
+ty_sb_lskip(struct ty_sb *sb, size_t len)
 {
+   if (len >= sb->len)
+     len = sb->len;
    sb->len -= len;
    if (sb->len)
      {
@@ -144,10 +152,13 @@ ty_sb_lskip(struct ty_sb *sb, int len)
 }
 
 void
-ty_sb_rskip(struct ty_sb *sb, int len)
+ty_sb_rskip(struct ty_sb *sb, size_t len)
 {
+   if (len >= sb->len)
+     len = sb->len;
    sb->len -= len;
-   sb->buf[sb->len] = '\0';
+   if (sb->alloc)
+     sb->buf[sb->len] = '\0';
 }
 
 void
@@ -160,3 +171,117 @@ ty_sb_free(struct ty_sb *sb)
    sb->gap = sb->len = sb->alloc = 0;
    sb->buf = NULL;
 }
+
+#if defined(ENABLE_TESTS)
+static int
+tytest_sb_skip(void)
+{
+   struct ty_sb sb = {};
+   const char *data = "foobar";
+
+   /* lskip normal */
+   assert(ty_sb_add(&sb, data, strlen(data)) == 0);
+   ty_sb_lskip(&sb, 3);
+   assert(sb.len == strlen(data) - 3);
+   assert(sb.gap == 3);
+   assert(strncmp(sb.buf, data+3, sb.len) == 0);
+   /* lskip too large */
+   ty_sb_lskip(&sb, 30);
+   assert(sb.len == 0);
+   assert(sb.gap == 0);
+   ty_sb_free(&sb);
+
+   /* lskip all */
+   assert(ty_sb_add(&sb, data, strlen(data)) == 0);
+   ty_sb_lskip(&sb, strlen(data));
+   assert(sb.len == 0);
+   assert(sb.gap == 0);
+   ty_sb_free(&sb);
+   /* lskip empty */
+   ty_sb_lskip(&sb, 3);
+   assert(sb.len == 0);
+   assert(sb.gap == 0);
+
+   /* rskip normal */
+   assert(ty_sb_add(&sb, data, strlen(data)) == 0);
+   ty_sb_rskip(&sb, 3);
+   assert(sb.len == strlen(data) - 3);
+   assert(sb.gap == 0);
+   assert(strncmp(sb.buf, data, sb.len) == 0);
+   /* rskip too large */
+   ty_sb_rskip(&sb, 30);
+   assert(sb.len == 0);
+   assert(sb.gap == 0);
+   ty_sb_free(&sb);
+
+   /* rskip all */
+   assert(ty_sb_add(&sb, data, strlen(data)) == 0);
+   ty_sb_rskip(&sb, strlen(data));
+   assert(sb.len == 0);
+   assert(sb.gap == 0);
+   ty_sb_free(&sb);
+   /* rskip empty */
+   ty_sb_rskip(&sb, 3);
+   assert(sb.len == 0);
+   assert(sb.gap == 0);
+
+   return 0;
+}
+
+static int
+tytest_sb_trim(void)
+{
+   struct ty_sb sb = {};
+
+   assert(ty_sb_add(&sb,
+                    " \f \t sb_trim_spaces \t ",
+                    strlen(" \f \t sb_trim_spaces \t ")) == 0);
+
+   ty_sb_spaces_ltrim(&sb);
+   assert(sb.gap == 5);
+   assert(strncmp(sb.buf, "sb", 2) == 0);
+   ty_sb_spaces_rtrim(&sb);
+   assert(sb.gap == 5);
+   assert(sb.len == strlen("sb_trim_spaces"));
+   assert(strncmp(sb.buf, "sb_trim_spaces", sb.len) == 0);
+
+   ty_sb_free(&sb);
+   return 0;
+}
+
+static int
+tytest_sb_gap(void)
+{
+   struct ty_sb sb = {};
+   const char *data = "alpha bravo charlie delta";
+
+   assert(ty_sb_add(&sb, data, strlen(data)) == 0);
+   /* prepend with no gap */
+   assert(ty_sb_prepend(&sb, ">>>", strlen(">>>")) == 0);
+   assert(sb.len == strlen(data) + 3);
+   assert(strncmp(sb.buf,">>>alpha", 3+5) == 0);
+   /* make gap */
+   ty_sb_lskip(&sb, 3);
+   /* prepend with enough gap */
+   assert(ty_sb_prepend(&sb, "!!", strlen("!!")) == 0);
+   assert(strncmp(sb.buf,"!!alpha", 2+5) == 0);
+   /* make gap larger */
+   ty_sb_lskip(&sb, 2);
+   /* prepend with not enough gap */
+   assert(ty_sb_prepend(&sb, ">>>>>>", strlen(">>>>>>")) == 0);
+   assert(sb.len == strlen(data) + 6);
+   assert(strncmp(sb.buf,">>>>>>alpha", 6+5) == 0);
+
+   ty_sb_free(&sb);
+   return 0;
+}
+
+int
+tytest_sb(void)
+{
+   assert(tytest_sb_skip() == 0);
+   assert(tytest_sb_trim() == 0);
+   assert(tytest_sb_gap() == 0);
+   return 0;
+}
+#endif
diff --git a/src/bin/sb.h b/src/bin/sb.h
index 3462def..a52be74 100644
--- a/src/bin/sb.h
+++ b/src/bin/sb.h
@@ -13,10 +13,10 @@ struct ty_sb {
 int ty_sb_add(struct ty_sb *sb, const char *s, size_t len);
 void ty_sb_spaces_rtrim(struct ty_sb *sb);
 void ty_sb_spaces_ltrim(struct ty_sb *sb);
-int ty_sb_prepend(struct ty_sb *sb, const char *s, size_t  len);
+int ty_sb_prepend(struct ty_sb *sb, const char *s, size_t len);
 char *ty_sb_steal_buf(struct ty_sb *sb);
-void ty_sb_lskip(struct ty_sb *sb, int len);
-void ty_sb_rskip(struct ty_sb *sb, int len);
+void ty_sb_lskip(struct ty_sb *sb, size_t len);
+void ty_sb_rskip(struct ty_sb *sb, size_t len);
 void ty_sb_free(struct ty_sb *sb);
 
 #endif
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index f05236f..b0521ed 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -15,6 +15,7 @@
 #include "termptyops.h"
 #include "termiointernals.h"
 #include "tytest.h"
+#include "unit_tests.h"
 #include "tytest_common.h"
 
 #include "md5/md5.h"
@@ -28,6 +29,7 @@ static struct {
      tytest_func func;
 } _tytests[] = {
        { "dummy", tytest_dummy },
+       { "sb", tytest_sb},
        { NULL, NULL},
 };
 
diff --git a/src/bin/tytest.h b/src/bin/tytest.h
index cdc2e60..f40a68e 100644
--- a/src/bin/tytest.h
+++ b/src/bin/tytest.h
@@ -27,10 +27,4 @@ test_pointer_canvas_xy_get(int *mx,
 
 void test_set_mouse_pointer(int mx, int my);
 #endif
-
-
-/* Unit tests */
-typedef int (*tytest_func)(void);
-int tytest_dummy(void);
-
 #endif
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
new file mode 100644
index 0000000..ff377d1
--- /dev/null
+++ b/src/bin/unit_tests.h
@@ -0,0 +1,11 @@
+#ifndef _TY_UNIT_TESTS_H__
+#define _TY_UNIT_TESTS_H__ 1
+
+/* Unit tests */
+typedef int (*tytest_func)(void);
+
+/* list of tests */
+int tytest_dummy(void);
+int tytest_sb(void);
+
+#endif

-- 


Reply via email to