Plug leaks reported by valgrind. Same. Same Same. Same. Same.
[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       g_free (filename);
92       exit (1);
93     }
94   
95   while (fgets (buffer, sizeof(buffer), infile))
96     {
97       if (buffer[0] == '#')
98         continue;
99
100       buffer[strlen(buffer) - 1] = '\0';
101       strings = g_strsplit (buffer, "\t", -1);
102
103       test = strings[0];
104
105       convert = g_utf8_casefold (test, -1);
106       if (strcmp (convert, strings[1]) != 0)
107         {
108           fprintf (stderr, "Failure: casefold(%s) == '%s', should have been '%s'\n",
109                    test, convert, strings[1]);
110           result = 1;
111         }
112       g_free (convert);
113
114       g_strfreev (strings);
115     }
116
117   fclose (infile);
118   g_free (filename);
119
120   return result;
121 }