1 /* EasyTAG - tag editor for audio files
2  * Copyright (C) 2014 David King <amigadave@amigadave.com>
3  * Copyright (C) 2014 Abhinav Jangda <abhijangda@hotmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "scan.h"
21 
22 /* TODO: Add more test strings. */
23 
24 static const gsize PERF_ITERATIONS = 500000;
25 
26 static void
check_string(const gchar * cases,const gchar * result)27 check_string (const gchar *cases, const gchar *result)
28 {
29     gchar *string1, *string2;
30 
31     string1 = g_utf8_normalize (cases, -1, G_NORMALIZE_ALL);
32     string2 = g_utf8_normalize (result, -1, G_NORMALIZE_ALL);
33 
34     g_assert_cmpstr (string1, ==, string2);
35 
36     g_free (string1);
37     g_free (string2);
38 }
39 
40 static void
scan_underscore_to_space(void)41 scan_underscore_to_space (void)
42 {
43     gsize i;
44     const gchar * const cases[] = { " ်0STRING ်0_A_B" };
45     const gchar * const results[] = { " ်0STRING ်0 A B" };
46 
47     for (i = 0; i < G_N_ELEMENTS (cases); i++)
48     {
49         gchar *string;
50 
51         string = g_strdup (cases[i]);
52         Scan_Convert_Underscore_Into_Space (string);
53         check_string (string, results[i]);
54 
55         g_free (string);
56     }
57 }
58 
59 static void
scan_remove_space(void)60 scan_remove_space (void)
61 {
62     gsize i;
63     const gchar * const cases[] = { " STR ING A   B " };
64     const gchar * const results[] = { "STRINGAB" };
65 
66     for (i = 0; i < G_N_ELEMENTS (cases); i++)
67     {
68         gchar *string;
69 
70         string = g_strdup (cases[i]);
71         Scan_Process_Fields_Remove_Space (string);
72         check_string (string, results[i]);
73 
74         g_free (string);
75     }
76 }
77 
78 static void
scan_p20_to_space(void)79 scan_p20_to_space (void)
80 {
81     gsize i;
82     const gchar * const cases[] = { "S%20T%20R%20", "%20ă b  %20c",
83                                     "STЂR%20ING%20A%20B" };
84     const gchar * const results[] = { "S T R ", " ă b   c", "STЂR ING A B" };
85 
86     for (i = 0; i < G_N_ELEMENTS (cases); i++)
87     {
88         gchar *string;
89 
90         string = g_strdup (cases[i]);
91         Scan_Convert_P20_Into_Space (string);
92         check_string (string, results[i]);
93 
94         g_free (string);
95     }
96 }
97 
98 static void
scan_insert_space(void)99 scan_insert_space (void)
100 {
101     gsize i;
102     const gchar * const cases[] = { "STRINGAB", "StRiNgAb", "tRßiNgAb", "AՄՆ",
103                                     "bՄԵ", "cՄԻ", "dՎՆ", "eՄԽ", "fꜲ" };
104     const gchar * const results[] = { "S T R I N G A B", "St Ri Ng Ab",
105                                       "t Rßi Ng Ab", "A Մ Ն", "b Մ Ե", "c Մ Ի",
106                                       "d Վ Ն", "e Մ Խ", "f Ꜳ" };
107 
108     for (i = 0; i < G_N_ELEMENTS (cases); i++)
109     {
110         gchar *string;
111 
112         string = Scan_Process_Fields_Insert_Space (cases[i]);
113         check_string (string, results[i]);
114 
115         g_free (string);
116     }
117 }
118 
119 static void
scan_all_uppercase(void)120 scan_all_uppercase (void)
121 {
122     gsize i;
123     const gchar * const cases[] = { "stringab", "tRßiNgAb", "aʼnbcd", "lowΐer",
124                                     "uppΰer", "sTRINGև", "ᾖᾀ", "pᾖp",
125                                     "sAfflAs" };
126     const gchar * const results[] = { "STRINGAB", "TRSSINGAB", "AʼNBCD",
127                                       "LOWΪ́ER", "UPPΫ́ER", "STRINGԵՒ", "ἮΙἈΙ",
128                                       "PἮΙP", "SAFFLAS" };
129 
130     for (i = 0; i < G_N_ELEMENTS (cases); i++)
131     {
132         gchar *string;
133 
134         string = Scan_Process_Fields_All_Uppercase (cases[i]);
135         check_string (string, results[i]);
136 
137         g_free (string);
138     }
139 }
140 
141 static void
scan_all_lowercase(void)142 scan_all_lowercase (void)
143 {
144     gsize i;
145     const gchar * const cases[] = { "STRINGAB", "tRßiNgAb", "SMALLß",
146                                     "AAAԵՒBB", "ʼN", "PΪ́E", "ἮΙ", "Ϋ́E" };
147     const gchar * const results[] = { "stringab", "trßingab", "smallß",
148                                       "aaaեւbb", "ʼn", "pΐe", "ἦι", "ΰe" };
149 
150     for (i = 0; i < G_N_ELEMENTS (cases); i++)
151     {
152         gchar *string;
153 
154         string = Scan_Process_Fields_All_Downcase (cases[i]);
155         check_string (string, results[i]);
156 
157         g_free (string);
158     }
159 }
160 
161 static void
scan_letter_uppercase(void)162 scan_letter_uppercase (void)
163 {
164     gsize i;
165     const gchar * const cases[] = { "st ri ng in ab", "tr ßi ng ab",
166                                     "ßr ßi ng ab", "ßr i ng ab", "ßr mi ng ab",
167                                     "I I ng ab", "ß I ng ab", "ßi ng ab" };
168     const gchar * const results[] = { "St ri ng in ab", "Tr ßi ng ab",
169                                       "SSr ßi ng ab", "SSr I ng ab",
170                                       "SSr mi ng ab", "I I ng ab",
171                                       "SS I ng ab", "SSi ng ab" };
172 
173     for (i = 0; i < G_N_ELEMENTS (cases); i++)
174     {
175         gchar *string;
176 
177         string = Scan_Process_Fields_Letter_Uppercase (cases[i]);
178         check_string (string, results[i]);
179 
180         g_free (string);
181     }
182 }
183 
184 static void
scan_letters_uppercase(void)185 scan_letters_uppercase (void)
186 {
187     gsize i;
188     const gchar * const cases[] = { "Foo Bar The Baz", "The", "The The",
189                              "The The The", "Vibrate (single version)",
190                              "MCMXC", "Foo Bar The III (single version)",
191                              "01 02 Caps" };
192     const gchar * const results[] = { "Foo Bar the Baz", "The", "The The",
193                                       "The the The",
194                                       "Vibrate (Single Version)", "Mcmxc",
195                                       "Foo Bar the Iii (Single Version)",
196                                       "01 02 Caps" };
197     const gchar * const results_roman[] = { "Foo Bar the Baz", "The",
198                                             "The The", "The the The",
199                                             "Vibrate (Single Version)",
200                                             "MCMXC",
201                                             "Foo Bar the III (Single Version)",
202                                             "01 02 Caps" };
203     const gchar * const results_preps[] = { "Foo Bar The Baz", "The",
204                                             "The The", "The The The",
205                                             "Vibrate (Single Version)",
206                                             "Mcmxc",
207                                             "Foo Bar The Iii (Single Version)",
208                                             "01 02 Caps" };
209     const gchar * const results_preps_roman[] = { "Foo Bar The Baz", "The",
210                                                   "The The", "The The The",
211                                                   "Vibrate (Single Version)",
212                                                   "MCMXC",
213                                                   "Foo Bar The III (Single Version)",
214                                                   "01 02 Caps" };
215 
216     for (i = 0; i < G_N_ELEMENTS (cases); i++)
217     {
218         gchar *string;
219 
220         /* Lower-case exempted words, do not handle Roman numerals. */
221         string = g_strdup (cases[i]);
222         Scan_Process_Fields_First_Letters_Uppercase (&string, FALSE, FALSE);
223         check_string (string, results[i]);
224         g_free (string);
225 
226         /* Lower-case exempted words, handle Roman numerals. */
227         string = g_strdup (cases[i]);
228         Scan_Process_Fields_First_Letters_Uppercase (&string, FALSE, TRUE);
229         check_string (string, results_roman[i]);
230         g_free (string);
231 
232         /* Upper-case all words, do not handle Roman numerals. */
233         string = g_strdup (cases[i]);
234         Scan_Process_Fields_First_Letters_Uppercase (&string, TRUE, FALSE);
235         check_string (string, results_preps[i]);
236         g_free (string);
237 
238         /* Upper-case all words, handle Roman numerals. */
239         string = g_strdup (cases[i]);
240         Scan_Process_Fields_First_Letters_Uppercase (&string, TRUE, TRUE);
241         check_string (string, results_preps_roman[i]);
242         g_free (string);
243     }
244 }
245 
246 static void
scan_perf(gconstpointer user_data)247 scan_perf (gconstpointer user_data)
248 {
249     gsize i;
250     gdouble time;
251 
252     g_test_timer_start ();
253 
254     for (i = 0; i < PERF_ITERATIONS; i++)
255     {
256         ((GTestFunc)user_data) ();
257     }
258 
259     time = g_test_timer_elapsed ();
260 
261     g_test_minimized_result (time, "%6.1f seconds", time);
262 }
263 
264 int
main(int argc,char ** argv)265 main (int argc, char** argv)
266 {
267     g_test_init (&argc, &argv, NULL);
268 
269     g_test_add_func ("/scan/underscore-to-space", scan_underscore_to_space);
270     g_test_add_func ("/scan/remove-space", scan_remove_space);
271     g_test_add_func ("/scan/P20-to-space", scan_p20_to_space);
272     g_test_add_func ("/scan/insert-space", scan_insert_space);
273     g_test_add_func ("/scan/all-uppercase", scan_all_uppercase);
274     g_test_add_func ("/scan/all-lowercase", scan_all_lowercase);
275     g_test_add_func ("/scan/letter-uppercase", scan_letter_uppercase);
276     g_test_add_func ("/scan/letters-uppercase", scan_letters_uppercase);
277 
278     if (g_test_perf ())
279     {
280         g_test_add_data_func ("/scan/perf/underscore-to-space",
281                               scan_underscore_to_space, scan_perf);
282         g_test_add_data_func ("/scan/perf/remove-space", scan_remove_space,
283                               scan_perf);
284         g_test_add_data_func ("/scan/perf/P20-to-space", scan_p20_to_space,
285                               scan_perf);
286         g_test_add_data_func ("/scan/perf/insert-space", scan_insert_space,
287                               scan_perf);
288         g_test_add_data_func ("/scan/perf/all-uppercase", scan_all_uppercase,
289                               scan_perf);
290         g_test_add_data_func ("/scan/perf/all-lowercase", scan_all_lowercase,
291                               scan_perf);
292         g_test_add_data_func ("/scan/perf/letter-uppercase",
293                               scan_letter_uppercase, scan_perf);
294         g_test_add_data_func ("/scan/perf/letters-uppercase",
295                               scan_letters_uppercase, scan_perf);
296     }
297 
298     return g_test_run ();
299 }
300