Git init
[external/pango1.0.git] / tests / test-ot-tags.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* Pango
3  * testscript.c: Test cases for PangoScriptIter
4  *
5  * Copyright (C) 2002 Red Hat Software
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #define PANGO_ENABLE_ENGINE
24 #include <pango/pango-ot.h>
25
26 #undef VERBOSE
27
28 #define ASSERT(stmt) G_STMT_START {                                     \
29     if (stmt) { }                                                       \
30     else                                                                \
31       {                                                                 \
32         g_warning ("%s:%d (%s): assertion '%s' failed",                 \
33                  __FILE__, __LINE__, G_STRFUNC, #stmt);                 \
34         exit (1);                                                       \
35       }                                                                 \
36 } G_STMT_END
37
38 static void
39 test_script_tags (void)
40 {
41   gunichar ch;
42   PangoScript i, max_script;
43
44   /* we need to know what the maximum script number is.  but we don't
45    * provide an api for that.  instead of looking into internal tables,
46    * we'll go over all chars and see what their script is, taking the max!
47    */
48
49   max_script = PANGO_SCRIPT_INVALID_CODE;
50   for (ch = 0; ch <= 0x10FFFF; ch++)
51     max_script = MAX (max_script, pango_script_for_unichar (ch));
52
53   for (i = PANGO_SCRIPT_COMMON; i <= max_script; i++)
54     {
55       PangoOTTag tag = pango_ot_tag_from_script (i);
56       PangoScript j  = pango_ot_tag_to_script (tag);
57
58       if (i <= PANGO_SCRIPT_INHERITED || i == PANGO_SCRIPT_UNKNOWN)
59         {
60           ASSERT (tag == PANGO_OT_TAG_DEFAULT_SCRIPT);
61           ASSERT (j == PANGO_SCRIPT_COMMON);
62         }
63       else if (tag == FT_MAKE_TAG ('k', 'a', 'n', 'a'))
64         {
65           /* Hiragana and Katakana both map to tag 'kana' */
66          ASSERT (i == PANGO_SCRIPT_HIRAGANA || i == PANGO_SCRIPT_KATAKANA);
67          ASSERT (j == PANGO_SCRIPT_HIRAGANA || j == PANGO_SCRIPT_KATAKANA);
68         }
69       else
70         {
71           if (j != i)
72             g_error ("Got back %d for script %d (OT tag '%c%c%c%c')", j, i,
73                      tag>>24, (tag>>16)&255, (tag>>8)&255, tag&255);
74         }
75     }
76
77   ASSERT (pango_ot_tag_to_script (FT_MAKE_TAG ('X', 'Y', 'Z', ' ')) == PANGO_SCRIPT_UNKNOWN);
78 }
79
80 static void
81 test_language_tags (void)
82 {
83   /* just test it for a few known languages to make sure it's working */
84   const char languages[][6] = {
85     "xy", /* hopefully nonexistent */
86     "aa",
87     "az_IR",
88     "en",
89     "en_US",
90     "fa",
91     "fa_IR",
92     "fr",
93     "zh_CN",
94     "zu"
95   };
96   unsigned int i;
97
98   for (i = 0; i < G_N_ELEMENTS (languages); i++)
99     {
100       PangoLanguage *l = pango_language_from_string (languages[i]);
101       PangoOTTag tag   = pango_ot_tag_from_language (l);
102       PangoLanguage *m = pango_ot_tag_to_language (tag);
103
104       if (i == 0)
105         {
106           ASSERT (tag == PANGO_OT_TAG_DEFAULT_LANGUAGE);
107           ASSERT (strcmp (pango_language_to_string (m), "xx") == 0);
108         }
109       else
110         {
111           if (tag == PANGO_OT_TAG_DEFAULT_LANGUAGE)
112             g_error ("Got PANGO_OT_TAG_DEFAULT_LANGUAGE for language '%s'", pango_language_to_string (l));
113
114           if (!pango_language_matches (l, pango_language_to_string (m)))
115             g_error ("Got back %s for language %s (OT tag '%c%c%c%c')",
116                      pango_language_to_string (m), pango_language_to_string (l),
117                      tag>>24, (tag>>16)&255, (tag>>8)&255, tag&255);
118         }
119     }
120 }
121
122 int
123 main (int argc, char **argv)
124 {
125   g_setenv ("PANGO_RC_FILE", "./pangorc", TRUE);
126
127   test_script_tags ();
128   test_language_tags ();
129
130   return 0;
131 }