[gnome-control-center] printers: Add function shift_string_left() and its test
- From: Marek Kašík <mkasik src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-control-center] printers: Add function shift_string_left() and its test
- Date: Thu, 31 Jul 2014 09:46:42 +0000 (UTC)
commit 88fa618aca5a1c4c5865dfd688c3c31fb0cb745b
Author: Marek Kasik <mkasik redhat com>
Date: Tue Jul 29 16:59:37 2014 +0200
printers: Add function shift_string_left() and its test
shift_string_left() shifts given string by 1 character to the left.
test-shift.c tests whether function shift_string_left() works correctly.
https://bugzilla.gnome.org/show_bug.cgi?id=695564
panels/printers/Makefile.am | 8 ++++
panels/printers/pp-utils.c | 12 ++++++
panels/printers/pp-utils.h | 2 +
panels/printers/shift-test.txt | 9 ++++
panels/printers/test-shift.c | 84 ++++++++++++++++++++++++++++++++++++++++
5 files changed, 115 insertions(+), 0 deletions(-)
---
diff --git a/panels/printers/Makefile.am b/panels/printers/Makefile.am
index 3e34194..590601a 100644
--- a/panels/printers/Makefile.am
+++ b/panels/printers/Makefile.am
@@ -66,4 +66,12 @@ desktop_DATA = $(desktop_in_files:.desktop.in=.desktop)
CLEANFILES = $(desktop_in_files) $(desktop_DATA) $(BUILT_SOURCES)
EXTRA_DIST = $(resource_files) printers.gresource.xml
+noinst_PROGRAMS = test-shift
+test_shift_SOURCES = pp-utils.c pp-utils.h test-shift.c
+test_shift_LDADD = $(PANEL_LIBS) $(PRINTERS_PANEL_LIBS) $(CUPS_LIBS)
+
+EXTRA_DIST += shift-test.txt
+check-local: test-shift
+ $(builddir)/test-shift $(srcdir)/shift-test.txt > /dev/null
+
-include $(top_srcdir)/git.mk
diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c
index d4e4d3b..b354679 100644
--- a/panels/printers/pp-utils.c
+++ b/panels/printers/pp-utils.c
@@ -4244,3 +4244,15 @@ canonicalize_device_name (GList *devices,
return new_name;
}
+
+void
+shift_string_left (gchar *str)
+{
+ gchar *next;
+
+ if (str != NULL && str[0] != '\0')
+ {
+ next = g_utf8_find_next_char (str, NULL);
+ memmove (str, next, strlen (next) + 1);
+ }
+}
diff --git a/panels/printers/pp-utils.h b/panels/printers/pp-utils.h
index 3850976..6543181 100644
--- a/panels/printers/pp-utils.h
+++ b/panels/printers/pp-utils.h
@@ -318,6 +318,8 @@ gchar *canonicalize_device_name (GList *devices,
gint num_of_dests,
PpPrintDevice *device);
+void shift_string_left (gchar *str);
+
G_END_DECLS
#endif /* __PP_UTILS_H */
diff --git a/panels/printers/shift-test.txt b/panels/printers/shift-test.txt
new file mode 100644
index 0000000..b9eced4
--- /dev/null
+++ b/panels/printers/shift-test.txt
@@ -0,0 +1,9 @@
+# String, tab, string shifted by 1 position left
+Canon anon
+-JetDirect JetDirect
+localhost ocalhost
+Šípková Růženka ípková Růženka
+ space space
+[][ ][
+…... ...
+
diff --git a/panels/printers/test-shift.c b/panels/printers/test-shift.c
new file mode 100644
index 0000000..630ae16
--- /dev/null
+++ b/panels/printers/test-shift.c
@@ -0,0 +1,84 @@
+#include "config.h"
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <locale.h>
+
+#include "pp-utils.h"
+
+int
+main (int argc, char **argv)
+{
+ guint i;
+ char *str;
+ char *contents;
+ char **lines;
+ char *locale;
+
+ /* Running in some locales will
+ * break the tests as "ü" will be transliterated to
+ * "ue" in de_DE, and 'u"' in the C locale.
+ *
+ * Work around that by forcing en_US with UTF-8 in
+ * our tests
+ * https://bugzilla.gnome.org/show_bug.cgi?id=650342 */
+
+ locale = setlocale (LC_ALL, "en_US.UTF-8");
+ if (locale == NULL)
+ {
+ g_debug("Missing en_US.UTF-8 locale, ignoring test.");
+ return 0;
+ }
+
+ bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+
+ if (g_file_get_contents (argv[1], &contents, NULL, NULL) == FALSE)
+ {
+ g_warning ("Failed to load '%s'", argv[1]);
+ return 1;
+ }
+
+ lines = g_strsplit (contents, "\n", -1);
+ if (lines == NULL)
+ {
+ g_warning ("Test file is empty");
+ return 1;
+ }
+
+ for (i = 0; lines[i] != NULL; i++)
+ {
+ char **items;
+ char *utf8;
+
+ if (*lines[i] == '#')
+ continue;
+
+ if (*lines[i] == '\0')
+ break;
+
+ items = g_strsplit (lines[i], "\t", -1);
+ str = g_strdup (items[0]);
+ shift_string_left (str);
+ utf8 = g_locale_from_utf8 (items[0], -1, NULL, NULL, NULL);
+ if (g_strcmp0 (str, items[1]) != 0)
+ {
+ g_error ("Result for '%s' doesn't match '%s' (got: '%s')",
+ utf8, items[1], str);
+ }
+ else
+ {
+ g_debug ("Result for '%s' matches '%s'",
+ utf8, str);
+ }
+
+ g_free (str);
+ g_free (utf8);
+
+ g_strfreev (items);
+ }
+
+ g_strfreev (lines);
+ g_free (contents);
+
+ return 0;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]