Bump version
[platform/upstream/glib.git] / tests / convert-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #undef G_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
29
30 #include <string.h>
31
32 #include <glib.h>
33
34 /* Bug 311337 */
35 static void
36 test_iconv_state (void)
37 {
38   gchar *in = "\xf4\xe5\xf8\xe5\xed";
39   gchar *expected = "\xd7\xa4\xd7\x95\xd7\xa8\xd7\x95\xd7\x9d";
40   gchar *out;
41   gsize bytes_read = 0;
42   gsize bytes_written = 0;
43   GError *error = NULL;
44
45   out = g_convert (in, -1, "UTF-8", "CP1255", 
46                    &bytes_read, &bytes_written, &error);
47   
48   g_assert (error == NULL);
49   g_assert (bytes_read == 5);
50   g_assert (bytes_written == 10);
51   g_assert (strcmp (out, expected) == 0);
52 }
53
54 /* some tests involving "vulgar fraction one half" */
55 static void 
56 test_one_half (void)
57 {
58   gchar *in = "\xc2\xbd";
59   gchar *out;
60   gsize bytes_read = 0;
61   gsize bytes_written = 0;
62   GError *error = NULL;  
63
64   out = g_convert (in, -1, 
65                    "ISO8859-1", "UTF-8",
66                    &bytes_read, &bytes_written,
67                    &error);
68
69   g_assert (error == NULL);
70   g_assert (bytes_read == 2);
71   g_assert (bytes_written == 1);
72   g_assert (strcmp (out, "\xbd") == 0);
73   g_free (out);
74
75   out = g_convert (in, -1, 
76                    "ISO8859-15", "UTF-8",
77                    &bytes_read, &bytes_written,
78                    &error);
79
80   g_assert (error && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE);
81   g_assert (bytes_read == 0);
82   g_assert (bytes_written == 0);
83   g_assert (out == NULL);
84   g_clear_error (&error);
85
86   out = g_convert_with_fallback (in, -1, 
87                                  "ISO8859-15", "UTF-8",
88                                  "a",
89                                  &bytes_read, &bytes_written,
90                                  &error);
91
92   g_assert (error == NULL);
93   g_assert (bytes_read == 2);
94   g_assert (bytes_written == 1);
95   g_assert (strcmp (out, "a") == 0);
96   g_free (out);
97 }
98
99 static void
100 test_byte_order (void)
101 {
102   gchar in_be[4] = { 0xfe, 0xff, 0x03, 0x93}; /* capital gamma */
103   gchar in_le[4] = { 0xff, 0xfe, 0x93, 0x03};
104   gchar *expected = "\xce\x93";
105   gchar *out;
106   gsize bytes_read = 0;
107   gsize bytes_written = 0;
108   GError *error = NULL;  
109
110   out = g_convert (in_be, sizeof (in_be), 
111                    "UTF-8", "UTF-16",
112                    &bytes_read, &bytes_written,
113                    &error);
114
115   g_assert (error == NULL);
116   g_assert (bytes_read == 4);
117   g_assert (bytes_written == 2);
118   g_assert (strcmp (out, expected) == 0);
119   g_free (out);
120
121   out = g_convert (in_le, sizeof (in_le), 
122                    "UTF-8", "UTF-16",
123                    &bytes_read, &bytes_written,
124                    &error);
125
126   g_assert (error == NULL);
127   g_assert (bytes_read == 4);
128   g_assert (bytes_written == 2);
129   g_assert (strcmp (out, expected) == 0);
130   g_free (out);
131 }
132
133 int
134 main (int argc, char *argv[])
135 {
136   test_iconv_state ();
137   test_one_half ();
138   test_byte_order ();
139
140   return 0;
141 }