Dummy dynamic type module for testing type plugin code and dynamic types.
[platform/upstream/glib.git] / tests / unicode-caseconv.c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 #include <locale.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <glib.h>
8 #include <string.h>
9
10 int main (int argc, char **argv)
11 {
12   FILE *infile;
13   char buffer[1024];
14   char **strings;
15   char *srcdir = getenv ("srcdir");
16   char *filename;
17   const char *locale;
18   const char *test;
19   char *convert;
20   char *current_locale = setlocale (LC_CTYPE, NULL);
21   gint result = 0;
22
23   if (!srcdir)
24     srcdir = ".";
25   filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "casemap.txt", NULL);
26   
27   infile = fopen (filename, "r");
28   if (!infile)
29     {
30       fprintf (stderr, "Failed to open %s\n", filename );
31       exit (1);
32     }
33   
34   while (fgets (buffer, sizeof(buffer), infile))
35     {
36       if (buffer[0] == '#')
37         continue;
38
39       strings = g_strsplit (buffer, "\t", -1);
40
41       locale = strings[0];
42
43       if (!locale[0])
44         locale = "C";
45         
46       if (strcmp (locale, current_locale) != 0)
47         {
48           setlocale (LC_CTYPE, locale);
49           current_locale = setlocale (LC_CTYPE, NULL);
50
51           if (strncmp (current_locale, locale, 2) != 0)
52             {
53               fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);
54               goto next;
55             }
56         }
57       
58       test = strings[1];
59
60       convert = g_utf8_strup (test, -1);
61       if (strcmp (convert, strings[4]) != 0)
62         {
63           fprintf (stderr, "Failure: toupper(%s) == %s, should have been %s\n",
64                    test, convert, strings[4]);
65           result = 1;
66         }
67       g_free (convert);
68
69       convert = g_utf8_strdown (test, -1);
70       if (strcmp (convert, strings[2]) != 0)
71         {
72           fprintf (stderr, "Failure: tolower(%s) == %s, should have been %s\n",
73                    test, convert, strings[2]);
74           result = 1;
75         }
76       g_free (convert);
77
78     next:
79       g_strfreev (strings);
80     }
81
82   fclose (infile);
83
84   g_free (filename);
85   filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "casefold.txt", NULL);
86   
87   infile = fopen (filename, "r");
88   if (!infile)
89     {
90       fprintf (stderr, "Failed to open %s\n", filename );
91       exit (1);
92     }
93   
94   while (fgets (buffer, sizeof(buffer), infile))
95     {
96       if (buffer[0] == '#')
97         continue;
98
99       buffer[strlen(buffer) - 1] = '\0';
100       strings = g_strsplit (buffer, "\t", -1);
101
102       test = strings[0];
103
104       convert = g_utf8_casefold (test, -1);
105       if (strcmp (convert, strings[1]) != 0)
106         {
107           fprintf (stderr, "Failure: casefold(%s) == '%s', should have been '%s'\n",
108                    test, convert, strings[1]);
109           result = 1;
110         }
111       g_free (convert);
112
113       g_strfreev (strings);
114     }
115
116   fclose (infile);
117
118   return result;
119 }