[Spice-devel] [PATCH spice-gtk 5/5] tests: add some dos2unix tests

Marc-André Lureau marcandre.lureau at gmail.com
Sat Aug 24 07:11:13 PDT 2013


This is probably not exhaustive enough, but better than nothing.
---
 Makefile.am       |  2 +-
 configure.ac      |  1 +
 gtk/spice-util.c  |  1 +
 tests/Makefile.am | 18 +++++++++++
 tests/util.c      | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 110 insertions(+), 1 deletion(-)
 create mode 100644 tests/Makefile.am
 create mode 100644 tests/util.c

diff --git a/Makefile.am b/Makefile.am
index ffa1649..ab10f5f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,7 +1,7 @@
 ACLOCAL_AMFLAGS = -I m4
 NULL =
 
-SUBDIRS = spice-common gtk po doc data
+SUBDIRS = spice-common gtk po doc data tests
 
 if HAVE_INTROSPECTION
 if WITH_VALA
diff --git a/configure.ac b/configure.ac
index 1235f4a..74738a3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -685,6 +685,7 @@ gtk/controller/Makefile
 doc/Makefile
 doc/reference/Makefile
 vapi/Makefile
+tests/Makefile
 ])
 
 dnl ==========================================================================
diff --git a/gtk/spice-util.c b/gtk/spice-util.c
index ebdc6aa..0b6c53a 100644
--- a/gtk/spice-util.c
+++ b/gtk/spice-util.c
@@ -22,6 +22,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <glib.h>
 #include <glib-object.h>
 #include "spice-util-priv.h"
 #include "spice-util.h"
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..9510e2c
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,18 @@
+NULL =
+
+noinst_PROGRAMS = util
+TESTS = $(noinst_PROGRAMS)
+
+AM_CPPFLAGS =					\
+	$(GIO_CFLAGS) -I$(top_srcdir)/gtk	\
+	-DG_LOG_DOMAIN=\"GSpice\"		\
+	$(NULL)
+AM_LDFLAGS = $(GIO_LIBS)
+
+util_SOURCES =					\
+	$(top_srcdir)/gtk/spice-util-priv.h	\
+	$(top_srcdir)/gtk/spice-util.c		\
+	$(top_srcdir)/gtk/spice-util.h  	\
+	util.c					\
+	$(NULL)
+
diff --git a/tests/util.c b/tests/util.c
new file mode 100644
index 0000000..86109aa
--- /dev/null
+++ b/tests/util.c
@@ -0,0 +1,89 @@
+#include <glib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "spice-util-priv.h"
+
+enum {
+    DOS2UNIX = 1 << 0,
+    UNIX2DOS = 1 << 1,
+};
+
+static const struct {
+    const gchar *d;
+    const gchar *u;
+    glong flags;
+} dosunix[] = {
+    { "", "", DOS2UNIX|UNIX2DOS },
+    { "a", "a", DOS2UNIX|UNIX2DOS },
+    { "\r\n", "\n", DOS2UNIX|UNIX2DOS },
+    { "\r\n\r\n", "\n\n", DOS2UNIX|UNIX2DOS },
+    { "a\r\n", "a\n", DOS2UNIX|UNIX2DOS },
+    { "a\r\n\r\n", "a\n\n", DOS2UNIX|UNIX2DOS },
+    { "\r\n\r\na\r\n\r\n", "\n\na\n\n", DOS2UNIX|UNIX2DOS },
+    { "1\r\n\r\na\r\n\r\n2", "1\n\na\n\n2", DOS2UNIX|UNIX2DOS },
+    { "\n", "\n", DOS2UNIX },
+    { "\n\n", "\n\n", DOS2UNIX },
+    { "\r\n", "\r\n", UNIX2DOS },
+    { "\r\r\n", "\r\r\n", UNIX2DOS },
+    { "é\r\né", "é\né", DOS2UNIX|UNIX2DOS },
+    { "\r\né\r\né\r\n", "\né\né\n", DOS2UNIX|UNIX2DOS }
+    /* TODO: add some utf8 test cases */
+};
+
+static void test_dos2unix(void)
+{
+    GError *err = NULL;
+    gchar *tmp;
+    int i;
+
+    for (i = 0; i < G_N_ELEMENTS(dosunix); i++) {
+        if (!(dosunix[i].flags & DOS2UNIX))
+            continue;
+
+        tmp = spice_dos2unix(dosunix[i].d, -1, &err);
+        g_assert_cmpstr(tmp, ==, dosunix[i].u);
+        g_assert_no_error(err);
+        g_free(tmp);
+
+        /* including ending \0 */
+        tmp = spice_dos2unix(dosunix[i].d, strlen(dosunix[i].d) + 1, &err);
+        g_assert_cmpstr(tmp, ==, dosunix[i].u);
+        g_assert_no_error(err);
+        g_free(tmp);
+    }
+}
+
+static void test_unix2dos(void)
+{
+    GError *err = NULL;
+    gchar *tmp;
+    int i;
+
+    for (i = 0; i < G_N_ELEMENTS(dosunix); i++) {
+        if (!(dosunix[i].flags & UNIX2DOS))
+            continue;
+
+        tmp = spice_unix2dos(dosunix[i].u, -1, &err);
+        g_assert_cmpstr(tmp, ==, dosunix[i].d);
+        g_assert_no_error(err);
+        g_free(tmp);
+
+        /* including ending \0 */
+        tmp = spice_unix2dos(dosunix[i].u, strlen(dosunix[i].u) + 1, &err);
+        g_assert_cmpstr(tmp, ==, dosunix[i].d);
+        g_assert_no_error(err);
+        g_free(tmp);
+    }
+}
+
+int main(int argc, char* argv[])
+{
+  g_test_init(&argc, &argv, NULL);
+
+  g_test_add_func("/util/dos2unix", test_dos2unix);
+  g_test_add_func("/util/unix2dos", test_unix2dos);
+
+  return g_test_run ();
+}
-- 
1.8.3.rc1.49.g8d97506



More information about the Spice-devel mailing list