chiark / gitweb /
Add a test program for charset.c.
authorRichard Kettlewell <rjk@greenend.org.uk>
Tue, 20 May 2008 19:51:12 +0000 (20:51 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Tue, 20 May 2008 19:51:12 +0000 (20:51 +0100)
Fix truncate_for_display() to get the right length.

.bzrignore
lib/Makefile.am
lib/charset.c
lib/t-charset.c [new file with mode: 0644]

index 1f06458..1c02594 100644 (file)
@@ -188,3 +188,5 @@ doc/disorder_options.5.html
 doc/disorder_actions.5.in
 doc/disorder_templates.5.in
 lib/t-arcfour
+lib/t-charset
+
index 70cf8dd..2058de2 100644 (file)
 # USA
 #
 
-TESTS=t-addr t-arcfour t-basen t-bits t-cache t-casefold t-cookies \
-               t-filepart t-hash t-heap t-hex t-kvp t-mime t-printf \
-               t-regsub t-selection t-signame t-sink t-split t-syscalls \
-               t-trackname t-unicode t-url t-utf8 t-vector t-words t-wstat \
-               t-macros t-cgi
+TESTS=t-addr t-arcfour t-basen t-bits t-cache t-casefold t-charset     \
+       t-cookies t-filepart t-hash t-heap t-hex t-kvp t-mime t-printf  \
+       t-regsub t-selection t-signame t-sink t-split t-syscalls        \
+       t-trackname t-unicode t-url t-utf8 t-vector t-words t-wstat     \
+       t-macros t-cgi
 
 noinst_LIBRARIES=libdisorder.a
 include_HEADERS=disorder.h
@@ -149,6 +149,10 @@ t_casefold_SOURCES=t-casefold.c test.c test.h
 t_casefold_LDADD=libdisorder.a $(LIBPCRE) $(LIBICONV) $(LIBGC)
 t_casefold_DEPENDENCIES=libdisorder.a
 
+t_charset_SOURCES=t-charset.c test.c test.h
+t_charset_LDADD=libdisorder.a $(LIBPCRE) $(LIBICONV) $(LIBGC)
+t_charset_DEPENDENCIES=libdisorder.a
+
 t_cgi_SOURCES=t-cgi.c test.c test.h
 t_cgi_LDADD=libdisorder.a $(LIBPCRE) $(LIBICONV) $(LIBGC)
 t_cgi_DEPENDENCIES=libdisorder.a
index bacaea9..1dc4231 100644 (file)
@@ -115,20 +115,22 @@ const char *truncate_for_display(const char *s, long max) {
   uint32_t *s32;
   size_t l32, cut;
   utf32_iterator it;
+  long graphemes;
 
   /* Convert to UTF-32 for processing */
   if(!(s32 = utf8_to_utf32(s, strlen(s), &l32)))
     return 0;
   it = utf32_iterator_new(s32, l32);
   cut = l32;
-  while(max && utf32_iterator_where(it) < l32) {
+  graphemes = 0;                        /* # of graphemes left of it */
+  while(graphemes <= max && utf32_iterator_where(it) < l32) {
+    if(graphemes == max - 1)
+      cut = utf32_iterator_where(it);
     utf32_iterator_advance(it, 1);
     if(utf32_iterator_grapheme_boundary(it))
-      --max;
-    if(max == 1)
-      cut = utf32_iterator_where(it);
+      ++graphemes;
   }
-  if(max == 0) {                        /* we need to cut */
+  if(graphemes > max) {                 /* we need to cut */
     s32[cut] = 0x2026;                  /* HORIZONTAL ELLIPSIS */
     l32 = cut + 1;
     s = utf32_to_utf8(s32, l32, 0);
diff --git a/lib/t-charset.c b/lib/t-charset.c
new file mode 100644 (file)
index 0000000..1ee2a46
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * This file is part of DisOrder.
+ * Copyright (C) 2008 Richard Kettlewell
+ *
+ * This program 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 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+#include "test.h"
+#include "charset.h"
+
+static void test_charset(void) {
+  check_string(any2any("UTF-8", "ISO-8859-1", "\xC2\xA3"),
+              "\xA3");
+  check_string(any2any("ISO-8859-1", "UTF-8", "\xA3"),
+              "\xC2\xA3");
+  fprintf(stderr, "Expect a conversion error:\n");
+  insist(any2any("UTF-8", "ISO-8859-1", "\xC2""a") == 0);
+
+#define EL "\xE2\x80\xA6"      /* 2026 HORIZONTAL ELLIPSIS */
+  check_string(truncate_for_display("", 0), "");
+  check_string(truncate_for_display("", 1), "");
+  check_string(truncate_for_display("x", 1), "x");
+  check_string(truncate_for_display("xx", 1), EL);
+  check_string(truncate_for_display("xx", 2), "xx");
+  check_string(truncate_for_display("xxx", 2), "x"EL);
+  check_string(truncate_for_display("wibble", 6), "wibble");
+  check_string(truncate_for_display("wibble", 5), "wibb"EL);
+}
+
+TEST(charset);
+
+/*
+Local Variables:
+c-basic-offset:2
+comment-column:40
+fill-column:79
+indent-tabs-mode:nil
+End:
+*/