1 #undef G_DISABLE_ASSERT 2 #undef G_LOG_DOMAIN 3 4 #include <stdarg.h> 5 #include <string.h> 6 #include <glib.h> 7 8 static void test_format (const gchar *format, 9 const gchar *expected, ...) G_GNUC_PRINTF (1, 3); 10 11 static gboolean error = FALSE; 12 13 static void 14 test (const gchar *original, 15 const gchar *expected) 16 { 17 gchar *result = g_markup_escape_text (original, -1); 18 19 if (strcmp (result, expected) != 0) 20 { 21 g_printerr ("g_markup_escape_text(): expected '%s', got '%s'\n", 22 expected, result); 23 error = TRUE; 24 } 25 26 g_free (result); 27 } 28 29 static void 30 test_unichar (gunichar c, 31 gboolean entity) 32 { 33 gint len; 34 gchar outbuf[7], expected[12]; 35 36 len = g_unichar_to_utf8 (c, outbuf); 37 outbuf[len] = 0; 38 39 if (entity) 40 g_snprintf (expected, 12, "&#x%x;", c); 41 else 42 strcpy (expected, outbuf); 43 44 test (outbuf, expected); 45 } 46 47 static void 48 test_format (const gchar *format, 49 const gchar *expected, 50 ...) 51 { 52 gchar *result; 53 54 va_list args; 55 56 va_start (args, expected); 57 result = g_markup_vprintf_escaped (format, args); 58 va_end (args); 59 60 if (strcmp (result, expected) != 0) 61 { 62 g_printerr ("g_markup_printf_escaped(): expected '%s', got '%s'\n", 63 expected, result); 64 error = TRUE; 65 } 66 67 g_free (result); 68 } 69 70 int main (int argc, char **argv) 71 { 72 /* Tests for g_markup_escape_text() */ 73 test ("&", "&"); 74 test ("<", "<"); 75 test (">", ">"); 76 test ("'", "'"); 77 test ("\"", """); 78 79 test ("", ""); 80 test ("A", "A"); 81 test ("A&", "A&"); 82 test ("&A", "&A"); 83 test ("A&A", "A&A"); 84 test ("&&A", "&&A"); 85 test ("A&&", "A&&"); 86 test ("A&&A", "A&&A"); 87 test ("A&A&A", "A&A&A"); 88 test ("AA", "A&#23;A"); 89 test ("A
A", "A&#xa;A"); 90 test_unichar (0x1, TRUE); 91 test_unichar (0x8, TRUE); 92 test_unichar (0x9, FALSE); 93 test_unichar (0xa, FALSE); 94 test_unichar (0xb, TRUE); 95 test_unichar (0xc, TRUE); 96 test_unichar (0xd, FALSE); 97 test_unichar (0xe, TRUE); 98 test_unichar (0x1f, TRUE); 99 test_unichar (0x20, FALSE); 100 test_unichar (0x7e, FALSE); 101 test_unichar (0x7f, TRUE); 102 test_unichar (0x84, TRUE); 103 test_unichar (0x85, FALSE); 104 test_unichar (0x86, TRUE); 105 test_unichar (0x9f, TRUE); 106 test_unichar (0xa0, FALSE); 107 108 /* Tests for g_markup_printf_escaped() */ 109 test_format ("A", "A"); 110 test_format ("A%s", "A&", "&"); 111 test_format ("%sA", "&A", "&"); 112 test_format ("A%sA", "A&A", "&"); 113 test_format ("%s%sA", "&&A", "&", "&"); 114 test_format ("A%s%s", "A&&", "&", "&"); 115 test_format ("A%s%sA", "A&&A", "&", "&"); 116 test_format ("A%sA%sA", "A&A&A", "&", "&"); 117 118 test_format ("%s", "<B>&", 119 "<B>&"); 120 test_format ("%c%c", "<&", 121 '<', '&'); 122 test_format (".%c.%c.", ".<.&.", 123 '<', '&'); 124 test_format ("%s", "", 125 ""); 126 test_format ("%-5s", "A ", 127 "A"); 128 test_format ("%2$s%1$s", "B.A.", 129 "A.", "B."); 130 131 return error ? 1 : 0; 132 } 133