From f0cfe69d4fc5b0d209d608d9a1661f23714c20d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Courr=C3=A8ges-Anglas?= Date: Sun, 17 Sep 2017 19:40:25 +0200 Subject: Move the sbuf_utf8_nconcat test to its own file --- src/Makefile.am | 19 +++---- src/format.c | 100 ------------------------------------ src/test-sbuf-utf8-nconcat.c | 117 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 109 deletions(-) create mode 100644 src/test-sbuf-utf8-nconcat.c (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index d61c4b9..5bb1934 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -80,13 +80,14 @@ if HAVE_XRANDR ratpoison_SOURCES += xrandr.c xrandr.h endif -TESTS = format_test +TESTS = test-sbuf-utf8-nconcat +check_PROGRAMS = test-sbuf-utf8-nconcat -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 utf8.c utf8.h util.c util.h -format_test_CPPFLAGS = $(AM_CPPFLAGS) -DRUN_FORMAT_TEST -format_test_LDADD = $(ratpoison_LDADD) +test_sbuf_utf8_nconcat_SOURCES = \ + test-sbuf-utf8-nconcat.c \ + sbuf.c \ + sbuf.h \ + utf8.c \ + utf8.h \ + util.c \ + util.h diff --git a/src/format.c b/src/format.c index ed1f571..170f88f 100644 --- a/src/format.c +++ b/src/format.c @@ -296,103 +296,3 @@ fmt_pid (rp_window_elem *elem, struct sbuf *buf) else sbuf_concat (buf, "?"); } - -#ifdef RUN_FORMAT_TEST - -#include -#include - -void test_sbuf_utf8_nconcat(void); - -int main(void) -{ - /* We need to set up locale information for multibyte functions to work - correctly. */ - setlocale(LC_ALL, ""); - - test_sbuf_utf8_nconcat(); - return 0; -} - -void test_sbuf_utf8_nconcat(void) -{ - struct sbuf *buf = NULL; - - /* Zero length string, no limit. */ - - buf = sbuf_new(0); - sbuf_utf8_nconcat(buf, "", -1); - assert(strcmp(sbuf_get(buf), "") == 0); - sbuf_free(buf); - - /* Zero length string, non-zero limit. */ - - buf = sbuf_new(0); - sbuf_utf8_nconcat(buf, "", 5); - assert(strcmp(sbuf_get(buf), "") == 0); - sbuf_free(buf); - - /* ASCII string, no limit. */ - - buf = sbuf_new(0); - sbuf_utf8_nconcat(buf, "hi there", -1); - assert(strcmp(sbuf_get(buf), "hi there") == 0); - sbuf_utf8_nconcat(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); - sbuf_utf8_nconcat(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); - sbuf_utf8_nconcat(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. */ - sbuf_utf8_nconcat(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); - sbuf_utf8_nconcat(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); - sbuf_utf8_nconcat(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); - sbuf_utf8_nconcat(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. */ - sbuf_utf8_nconcat(buf, "hi \xe2\x84 there you", 20); - assert(strcmp(sbuf_get(buf), "hi ") == 0); - sbuf_free(buf); -} - -#endif diff --git a/src/test-sbuf-utf8-nconcat.c b/src/test-sbuf-utf8-nconcat.c new file mode 100644 index 0000000..d886594 --- /dev/null +++ b/src/test-sbuf-utf8-nconcat.c @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2017 Will Storey + * + * This file is part of ratpoison. + * + * ratpoison is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * ratpoison is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA + */ + +#include "ratpoison.h" + +#include +#include + +void test_sbuf_utf8_nconcat(void); + +int main(void) +{ + utf8_locale = 1; + + test_sbuf_utf8_nconcat(); + return 0; +} + +void +test_sbuf_utf8_nconcat(void) +{ + struct sbuf *buf = NULL; + + /* Zero length string, no limit. */ + + buf = sbuf_new(0); + sbuf_utf8_nconcat(buf, "", -1); + assert(strcmp(sbuf_get(buf), "") == 0); + sbuf_free(buf); + + /* Zero length string, non-zero limit. */ + + buf = sbuf_new(0); + sbuf_utf8_nconcat(buf, "", 5); + assert(strcmp(sbuf_get(buf), "") == 0); + sbuf_free(buf); + + /* ASCII string, no limit. */ + + buf = sbuf_new(0); + sbuf_utf8_nconcat(buf, "hi there", -1); + assert(strcmp(sbuf_get(buf), "hi there") == 0); + sbuf_utf8_nconcat(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); + sbuf_utf8_nconcat(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); + sbuf_utf8_nconcat(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. */ + sbuf_utf8_nconcat(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); + sbuf_utf8_nconcat(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); + sbuf_utf8_nconcat(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); + sbuf_utf8_nconcat(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. */ + sbuf_utf8_nconcat(buf, "hi \xe2\x84 there you", 20); + assert(strcmp(sbuf_get(buf), "hi \xe2\x84 there you") == 0); + sbuf_free(buf); +} -- cgit v1.2.3