ratpoison-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RP] [PATCH 3/3 v2] Add a test program to exercise concat_width()


From: Will Storey
Subject: [RP] [PATCH 3/3 v2] Add a test program to exercise concat_width()
Date: Sun, 27 Aug 2017 21:25:59 -0700

This program can be run using `make check`. In order to do this, you
will need to run `autogen.sh` again.
---
 .gitignore      |   2 ++
 src/Makefile.am |  11 +++++++
 src/format.c    | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+)

diff --git a/.gitignore b/.gitignore
index f256246..a053d16 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,6 +13,8 @@ ratpoison-*.tar.gz
 ratpoison-*.tar.gz.sig
 ratpoison-*.tar.xz
 ratpoison-*.tar.xz.sig
+src/format_test
+src/format_test.trs
 TAGS
 /doc/version.texi
 /doc/stamp-vti
diff --git a/src/Makefile.am b/src/Makefile.am
index bc27439..7f4a673 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -75,3 +75,14 @@ ratpoison_SOURCES    = actions.c             \
 if HAVE_XRANDR
 ratpoison_SOURCES += xrandr.c xrandr.h
 endif
+
+TESTS = format_test
+
+check_PROGRAMS = format_test
+
+format_test_SOURCES = actions.c bar.c completions.c editor.c events.c format.c 
\
+                                                                               
        frame.c globals.c group.c history.c hook.c input.c \
+                                                                               
        linkedlist.c manage.c number.c sbuf.c screen.c split.c \
+                                                                               
        window.c xrandr.c
+format_test_CPPFLAGS = $(AM_CPPFLAGS) -DRUN_FORMAT_TEST
+format_test_LDADD = $(ratpoison_LDADD)
diff --git a/src/format.c b/src/format.c
index 71b56c7..31adb69 100644
--- a/src/format.c
+++ b/src/format.c
@@ -344,3 +344,103 @@ fmt_pid (rp_window_elem *elem, struct sbuf *buf)
   else
     sbuf_concat (buf, "?");
 }
+
+#ifdef RUN_FORMAT_TEST
+
+#include <assert.h>
+#include <locale.h>
+
+void rtp_test_concat_width(void);
+
+int main(void)
+{
+  /* We need to set up locale information for multibyte functions to work
+     correctly. */
+  setlocale(LC_ALL, "");
+
+  rtp_test_concat_width();
+  return 0;
+}
+
+void rtp_test_concat_width(void)
+{
+  struct sbuf *buf = NULL;
+
+  /* Zero length string, no limit. */
+
+  buf = sbuf_new(0);
+  concat_width(buf, "", -1);
+  assert(strcmp(sbuf_get(buf), "") == 0);
+  sbuf_free(buf);
+
+  /* Zero length string, non-zero limit. */
+
+  buf = sbuf_new(0);
+  concat_width(buf, "", 5);
+  assert(strcmp(sbuf_get(buf), "") == 0);
+  sbuf_free(buf);
+
+  /* ASCII string, no limit. */
+
+  buf = sbuf_new(0);
+  concat_width(buf, "hi there", -1);
+  assert(strcmp(sbuf_get(buf), "hi there") == 0);
+  concat_width(buf, " you", -1);
+  assert(strcmp(sbuf_get(buf), "hi there you") == 0);
+  sbuf_free(buf);
+
+  /* ASCII string, non-zero limit, truncated. */
+
+  buf = sbuf_new(0);
+  concat_width(buf, "hi there", 4);
+  assert(strcmp(sbuf_get(buf), "hi t") == 0);
+  sbuf_free(buf);
+
+  /* ASCII string, non-zero limit, not truncated. */
+
+  buf = sbuf_new(0);
+  concat_width(buf, "hi", 4);
+  assert(strcmp(sbuf_get(buf), "hi") == 0);
+  sbuf_free(buf);
+
+  /* UTF-8 string, no limit. */
+
+  buf = sbuf_new(0);
+  /* 0xe2 0x84 0xa2 is U+2122, the trademark symbol. */
+  concat_width(buf, "hi \xe2\x84\xa2 there", -1);
+  assert(strcmp(sbuf_get(buf), "hi \xe2\x84\xa2 there") == 0);
+  sbuf_free(buf);
+
+  /* UTF-8 string, non-zero limit, truncated at an okay spot counting either
+     by bytes or by characters. */
+
+  buf = sbuf_new(0);
+  concat_width(buf, "hi \xe2\x84\xa2 there you", 11);
+  assert(strcmp(sbuf_get(buf), "hi \xe2\x84\xa2 there ") == 0);
+  sbuf_free(buf);
+
+  /* UTF-8 string, non-zero limit, truncated such that if the limit were in
+     bytes that we would cut in the middle of a character. */
+
+  buf = sbuf_new(0);
+  concat_width(buf, "hi \xe2\x84\xa2 there you", 5);
+  assert(strcmp(sbuf_get(buf), "hi \xe2\x84\xa2 ") == 0);
+  sbuf_free(buf);
+
+  /* UTF-8 string, non-zero limit, not truncated. */
+
+  buf = sbuf_new(0);
+  concat_width(buf, "hi \xe2\x84\xa2 there you", 20);
+  assert(strcmp(sbuf_get(buf), "hi \xe2\x84\xa2 there you") == 0);
+  sbuf_free(buf);
+
+  /* Invalid character. */
+
+  buf = sbuf_new(0);
+  /* This is an invalid UTF-8 sequence. It's missing 0xa2. */
+  concat_width(buf, "hi \xe2\x84 there you", 20);
+  assert(strcmp(sbuf_get(buf), "hi ") == 0);
+  sbuf_free(buf);
+}
+
+#endif
-- 
2.11.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]