dovecot-2.2: lib-charset: Added a minimal unit test

dovecot at dovecot.org dovecot at dovecot.org
Sat May 16 09:48:41 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/dcaf508860ad
changeset: 18714:dcaf508860ad
user:      Timo Sirainen <tss at iki.fi>
date:      Sat May 16 12:46:38 2015 +0300
description:
lib-charset: Added a minimal unit test

diffstat:

 src/lib-charset/Makefile.am     |  23 ++++++++++-
 src/lib-charset/charset-iconv.c |   3 +-
 src/lib-charset/test-charset.c  |  90 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+), 3 deletions(-)

diffs (144 lines):

diff -r 1044c55fb4ef -r dcaf508860ad src/lib-charset/Makefile.am
--- a/src/lib-charset/Makefile.am	Sat May 16 11:47:09 2015 +0300
+++ b/src/lib-charset/Makefile.am	Sat May 16 12:46:38 2015 +0300
@@ -1,7 +1,8 @@
 noinst_LTLIBRARIES = libcharset.la
 
 AM_CPPFLAGS = \
-	-I$(top_srcdir)/src/lib
+	-I$(top_srcdir)/src/lib \
+	-I$(top_srcdir)/src/lib-test
 
 libcharset_la_LIBADD = $(LTLIBICONV)
 libcharset_la_SOURCES = \
@@ -13,3 +14,23 @@
 
 pkginc_libdir=$(pkgincludedir)
 pkginc_lib_HEADERS = $(headers)
+
+test_programs = \
+	test-charset
+
+noinst_PROGRAMS = $(test_programs)
+
+test_libs = \
+	../lib-test/libtest.la \
+	../lib/liblib.la
+test_deps = $(noinst_LTLIBRARIES) $(test_libs)
+
+test_charset_SOURCES = test-charset.c
+test_charset_LDADD = libcharset.la $(test_libs)
+test_charset_DEPENDENCIES = libcharset.la $(test_deps)
+
+check: check-am check-test
+check-test: all-am
+	for bin in $(test_programs); do \
+	  if ! $(RUN_TEST) ./$$bin; then exit 1; fi; \
+	done
diff -r 1044c55fb4ef -r dcaf508860ad src/lib-charset/charset-iconv.c
--- a/src/lib-charset/charset-iconv.c	Sat May 16 11:47:09 2015 +0300
+++ b/src/lib-charset/charset-iconv.c	Sat May 16 12:46:38 2015 +0300
@@ -118,8 +118,7 @@
 
 		if (result == CHARSET_RET_INVALID_INPUT) {
 			if (prev_invalid_pos != dest->used) {
-				uni_ucs4_to_utf8_c(UNICODE_REPLACEMENT_CHAR,
-						   dest);
+				str_append(dest, UNICODE_REPLACEMENT_CHAR_UTF8);
 				prev_invalid_pos = dest->used;
 			}
 			pos++;
diff -r 1044c55fb4ef -r dcaf508860ad src/lib-charset/test-charset.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-charset/test-charset.c	Sat May 16 12:46:38 2015 +0300
@@ -0,0 +1,90 @@
+/* Copyright (c) 2015 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "str.h"
+#include "test-common.h"
+#include "charset-utf8.h"
+
+static void test_charset_is_utf8(void)
+{
+	test_begin("charset_is_utf8");
+	test_assert(charset_is_utf8("AScII"));
+	test_assert(charset_is_utf8("us-AScII"));
+	test_assert(charset_is_utf8("uTF8"));
+	test_assert(charset_is_utf8("uTF-8"));
+	test_end();
+}
+
+static void test_charset_utf8_common(const char *input_charset)
+{
+	struct {
+		const char *input;
+		const char *output;
+		enum charset_result result;
+	} tests[] = {
+		{ "päÃ", "pä", CHARSET_RET_INCOMPLETE_INPUT },
+		{ "päÃa", "pä"UNICODE_REPLACEMENT_CHAR_UTF8"a", CHARSET_RET_INVALID_INPUT }
+	};
+	string_t *str = t_str_new(128);
+	enum charset_result result;
+	unsigned int i;
+
+	for (i = 0; i < N_ELEMENTS(tests); i++) {
+		str_truncate(str, 0);
+		test_assert_idx(charset_to_utf8_str(input_charset, NULL,
+						    tests[i].input, str, &result) == 0, i);
+		test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i);
+		test_assert_idx(result == tests[i].result, i);
+	}
+}
+
+static void test_charset_utf8(void)
+{
+	test_begin("charset utf8");
+	test_charset_utf8_common("UTF-8");
+	test_end();
+}
+
+#ifdef HAVE_ICONV
+static void test_charset_iconv(void)
+{
+	struct {
+		const char *charset;
+		const char *input;
+		const char *output;
+		enum charset_result result;
+	} tests[] = {
+		{ "ISO-8859-1", "p\xE4\xE4", "pää", CHARSET_RET_OK }
+	};
+	string_t *str = t_str_new(128);
+	enum charset_result result;
+	unsigned int i;
+
+	test_begin("charset iconv");
+	for (i = 0; i < N_ELEMENTS(tests); i++) {
+		str_truncate(str, 0);
+		test_assert_idx(charset_to_utf8_str(tests[i].charset, NULL,
+						    tests[i].input, str, &result) == 0, i);
+		test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i);
+		test_assert_idx(result == tests[i].result, i);
+	}
+	/* Use //IGNORE just to force handling to be done by iconv
+	   instead of our own UTF-8 routines. */
+	test_charset_utf8_common("UTF-8//IGNORE");
+	test_end();
+}
+#endif
+
+int main(void)
+{
+	static void (*test_functions[])(void) = {
+		test_charset_is_utf8,
+		test_charset_utf8,
+#ifdef HAVE_ICONV
+		test_charset_iconv,
+#endif
+		NULL
+	};
+
+	return test_run(test_functions);
+}


More information about the dovecot-cvs mailing list