~ubuntu-branches/debian/jessie/glib2.0/jessie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN

#include <stdarg.h>
#include <string.h>
#include <glib.h>

static void test_format (const gchar *format,
			 const gchar *expected, ...) G_GNUC_PRINTF (1, 3);

static gboolean error = FALSE;

static void
test (const gchar *original,
      const gchar *expected)
{
  gchar *result = g_markup_escape_text (original, -1);

  if (strcmp (result, expected) != 0)
    {
      g_printerr ("g_markup_escape_text(): expected '%s', got '%s'\n",
		  expected, result);
      error = TRUE;
    }

  g_free (result);
}

static void
test_unichar (gunichar c, 
              gboolean entity)
{
  gint len;
  gchar outbuf[7], expected[12];

  len = g_unichar_to_utf8 (c, outbuf);
  outbuf[len] = 0;

  if (entity)
    g_snprintf (expected, 12, "&#x%x;", c);
  else
    strcpy (expected, outbuf);

  test (outbuf, expected);
}

static void
test_format (const gchar *format,
	     const gchar *expected,
	     ...)
{
  gchar *result;
  
  va_list args;
  
  va_start (args, expected);
  result = g_markup_vprintf_escaped (format, args);
  va_end (args);

  if (strcmp (result, expected) != 0)
    {
      g_printerr ("g_markup_printf_escaped(): expected '%s', got '%s'\n",
		  expected, result);
      error = TRUE;
    }

  g_free (result);
}

int main (int argc, char **argv)
{
  /* Tests for g_markup_escape_text() */
  test ("&", "&amp;");
  test ("<", "&lt;");
  test (">", "&gt;");
  test ("'", "&apos;");
  test ("\"", "&quot;");
  
  test ("", "");
  test ("A", "A");
  test ("A&", "A&amp;");
  test ("&A", "&amp;A");
  test ("A&A", "A&amp;A");
  test ("&&A", "&amp;&amp;A");
  test ("A&&", "A&amp;&amp;");
  test ("A&&A", "A&amp;&amp;A");
  test ("A&A&A", "A&amp;A&amp;A");
  test ("A&#23;A", "A&amp;#23;A");
  test ("A&#xa;A", "A&amp;#xa;A");
  test_unichar (0x1, TRUE);
  test_unichar (0x8, TRUE);
  test_unichar (0x9, FALSE);
  test_unichar (0xa, FALSE);
  test_unichar (0xb, TRUE);
  test_unichar (0xc, TRUE);
  test_unichar (0xd, FALSE);
  test_unichar (0xe, TRUE);
  test_unichar (0x1f, TRUE);
  test_unichar (0x20, FALSE);
  test_unichar (0x7e, FALSE);
  test_unichar (0x7f, TRUE);
  test_unichar (0x84, TRUE);
  test_unichar (0x85, FALSE);
  test_unichar (0x86, TRUE);
  test_unichar (0x9f, TRUE);
  test_unichar (0xa0, FALSE);
  
  /* Tests for g_markup_printf_escaped() */
  test_format ("A", "A");
  test_format ("A%s", "A&amp;", "&");
  test_format ("%sA", "&amp;A", "&");
  test_format ("A%sA", "A&amp;A", "&");
  test_format ("%s%sA", "&amp;&amp;A", "&", "&");
  test_format ("A%s%s", "A&amp;&amp;", "&", "&");
  test_format ("A%s%sA", "A&amp;&amp;A", "&", "&");
  test_format ("A%sA%sA", "A&amp;A&amp;A", "&", "&");
  
  test_format ("%s", "&lt;B&gt;&amp;",
	       "<B>&");
  test_format ("%c%c", "&lt;&amp;",
	       '<', '&');
  test_format (".%c.%c.", ".&lt;.&amp;.",
	       '<', '&');
  test_format ("%s", "",
	       "");
  test_format ("%-5s", "A    ",
	       "A");
  test_format ("%2$s%1$s", "B.A.",
	       "A.", "B.");
  
  return error ? 1 : 0;
}