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