tests: Fix some minor leaks in the unit tests
[platform/upstream/glib.git] / glib / tests / utf8-performance.c
1 /* GLIB - Library of useful routines for C programming
2  *
3  * Copyright (C) 2010 Mikhail Zabaluev <mikhail.zabaluev@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <string.h>
20
21 #include <glib.h>
22
23 #define NUM_ITERATIONS 500000
24
25 static const char str_ascii[] =
26     "The quick brown fox jumps over the lazy dog";
27
28 static const gchar str_latin1[] =
29     "Zwölf Boxkämpfer jagen Viktor quer über den großen Sylter Deich";
30
31 /* Energizing GOELRO-talk in Russian, used by KDE */
32 static const char str_cyrillic[] =
33     "Широкая электрификация южных губерний даст мощный толчок подъёму "
34     "сельского хозяйства.";
35
36 /* First sentence from the Wikipedia article:
37  * http://zh.wikipedia.org/w/index.php?title=%E6%B1%89%E5%AD%97&oldid=13053137 */
38 static const char str_chinese[] =
39     "漢字,亦稱中文字、中国字,在台灣又被稱為國字,是漢字文化圈廣泛使用的一種文字,屬於表意文字的詞素音節文字";
40
41 typedef int (* GrindFunc) (const char *, gsize);
42
43 static int
44 grind_get_char (const char *str, gsize len)
45 {
46   gunichar acc = 0;
47   int i;
48   for (i = 0; i < NUM_ITERATIONS; i++)
49     {
50       const char *p = str;
51       while (*p) {
52         acc += g_utf8_get_char (p);
53         p = g_utf8_next_char (p);
54       }
55     }
56   return acc;
57 }
58
59 static int
60 grind_get_char_validated (const char *str, gsize len)
61 {
62   gunichar acc = 0;
63   int i;
64   for (i = 0; i < NUM_ITERATIONS; i++)
65     {
66       const char *p = str;
67       while (*p) {
68         acc += g_utf8_get_char_validated (p, -1);
69         p = g_utf8_next_char (p);
70       }
71     }
72   return acc;
73 }
74
75 static int
76 grind_utf8_to_ucs4 (const char *str, gsize len)
77 {
78   int i;
79   for (i = 0; i < NUM_ITERATIONS; i++)
80     {
81       gunichar *ustr;
82       ustr = g_utf8_to_ucs4 (str, -1, NULL, NULL, NULL);
83       g_free (ustr);
84     }
85   return 0;
86 }
87
88 static int
89 grind_get_char_backwards (const char *str, gsize len)
90 {
91   gunichar acc = 0;
92   int i;
93   for (i = 0; i < NUM_ITERATIONS; i++)
94     {
95       const char *p = str + len;
96       do
97         {
98           p = g_utf8_prev_char (p);
99           acc += g_utf8_get_char (p);
100         }
101       while (p != str);
102     }
103   return acc;
104 }
105
106 static int
107 grind_utf8_to_ucs4_sized (const char *str, gsize len)
108 {
109   int i;
110   for (i = 0; i < NUM_ITERATIONS; i++)
111     {
112       gunichar *ustr;
113       ustr = g_utf8_to_ucs4 (str, len, NULL, NULL, NULL);
114       g_free (ustr);
115     }
116   return 0;
117 }
118
119 static int
120 grind_utf8_to_ucs4_fast (const char *str, gsize len)
121 {
122   int i;
123   for (i = 0; i < NUM_ITERATIONS; i++)
124     {
125       gunichar *ustr;
126       ustr = g_utf8_to_ucs4_fast (str, -1, NULL);
127       g_free (ustr);
128     }
129   return 0;
130 }
131
132 static int
133 grind_utf8_to_ucs4_fast_sized (const char *str, gsize len)
134 {
135   int i;
136   for (i = 0; i < NUM_ITERATIONS; i++)
137     {
138       gunichar *ustr;
139       ustr = g_utf8_to_ucs4_fast (str, len, NULL);
140       g_free (ustr);
141     }
142   return 0;
143 }
144
145 static void
146 perform_for (GrindFunc grind_func, const char *str, const char *label)
147 {
148   gsize len;
149   gulong bytes_ground;
150   gdouble time_elapsed;
151   gdouble result;
152
153   len = strlen (str);
154   bytes_ground = (gulong) len * NUM_ITERATIONS;
155
156   g_test_timer_start ();
157
158   grind_func (str, len);
159
160   time_elapsed = g_test_timer_elapsed ();
161
162   result = ((gdouble) bytes_ground / time_elapsed) * 1.0e-6;
163
164   g_test_maximized_result (result, "%-9s %6.1f MB/s", label, result);
165 }
166
167 static void
168 perform (gconstpointer data)
169 {
170   GrindFunc grind_func = (GrindFunc) data;
171
172   if (!g_test_perf ())
173     return;
174
175   perform_for (grind_func, str_ascii, "ASCII:");
176   perform_for (grind_func, str_latin1, "Latin-1:");
177   perform_for (grind_func, str_cyrillic, "Cyrillic:");
178   perform_for (grind_func, str_chinese, "Chinese:");
179 }
180
181 int
182 main (int argc, char **argv)
183 {
184   g_test_init (&argc, &argv, NULL);
185
186   if (g_test_perf ())
187     {
188       g_test_add_data_func ("/utf8/perf/get_char", grind_get_char, perform);
189       g_test_add_data_func ("/utf8/perf/get_char-backwards", grind_get_char_backwards, perform);
190       g_test_add_data_func ("/utf8/perf/get_char_validated", grind_get_char_validated, perform);
191       g_test_add_data_func ("/utf8/perf/utf8_to_ucs4", grind_utf8_to_ucs4, perform);
192       g_test_add_data_func ("/utf8/perf/utf8_to_ucs4-sized", grind_utf8_to_ucs4_sized, perform);
193       g_test_add_data_func ("/utf8/perf/utf8_to_ucs4_fast", grind_utf8_to_ucs4_fast, perform);
194       g_test_add_data_func ("/utf8/perf/utf8_to_ucs4_fast-sized", grind_utf8_to_ucs4_fast_sized, perform);
195     }
196
197   return g_test_run ();
198 }