1
0
mirror of https://https.git.savannah.gnu.org/git/gnulib.git synced 2026-06-15 23:35:50 +00:00
Files
gnulib/tests/bench-mbswidth.c
Paul Eggert d60036ddca bench-tests: work with wchar-single
* modules/mbiter-bench-tests, modules/mbiterf-bench-tests:
* modules/mbswidth-bench-tests, modules/mbuiter-bench-tests:
* modules/mbuiterf-bench-tests, modules/mcel-bench-tests:
(Depends-on): Add streq.
* tests/bench-mbiter.c, tests/bench-mbiterf.c, tests/bench-mbswidth.c:
* tests/bench-mbuiter.c, tests/bench-mbuiterf.c, tests/bench-mcel.c:
If GNULIB_WCHAR_SINGLE_LOCALE, don’t call setlocale with different
locales.
2026-05-07 11:27:03 -07:00

134 lines
3.7 KiB
C

/* Benchmarks for mbswidth().
Copyright (C) 2023-2026 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <uchar.h>
#include "bench.h"
#include "bench-multibyte.h"
#include "mbswidth.h"
#ifndef GNULIB_WCHAR_SINGLE_LOCALE
# define GNULIB_WCHAR_SINGLE_LOCALE 0
#endif
static void
do_test (char test, int repeat, const char *locale_name, const char *text)
{
printf ("Test %c\n", test);
static char const *current_locale_name;
if (GNULIB_WCHAR_SINGLE_LOCALE && current_locale_name
&& !streq (locale_name, current_locale_name))
printf ("Skipping test: earlier locale %s != test locale %s\n",
current_locale_name, locale_name);
else if (!setlocale (LC_ALL, locale_name))
printf ("Skipping test: locale %s not installed.\n", locale_name);
else
{
current_locale_name = locale_name;
struct timings_state ts;
timing_start (&ts);
for (int count = 0; count < repeat; count++)
{
mbswidth (text, 0);
}
timing_end (&ts);
timing_output (&ts);
}
printf ("\n");
}
/* Performs some or all of the following tests:
a - ASCII text, C locale
b - ASCII text, UTF-8 locale
c - French text, C locale
d - French text, ISO-8859-1 locale
e - French text, UTF-8 locale
f - Greek text, C locale
g - Greek text, ISO-8859-7 locale
h - Greek text, UTF-8 locale
i - Chinese text, UTF-8 locale
j - Chinese text, GB18030 locale
Pass the tests to be performed as first argument. */
int
main (int argc, char *argv[])
{
if (argc != 3)
{
fprintf (stderr, "Usage: %s TESTS REPETITIONS\n", argv[0]);
fprintf (stderr, "Example: %s abcdefghij 100000\n", argv[0]);
exit (1);
}
const char *tests = argv[1];
int repeat = atoi (argv[2]);
text_init ();
/* Execute each test. */
for (size_t i = 0; i < strlen (tests); i++)
{
char test = tests[i];
switch (test)
{
case 'a':
do_test (test, repeat, "C", text_latin_ascii);
break;
case 'b':
do_test (test, repeat, "en_US.UTF-8", text_latin_ascii);
break;
case 'c':
do_test (test, repeat, "C", text_french_iso8859);
break;
case 'd':
do_test (test, repeat, "fr_FR.ISO-8859-1", text_french_iso8859);
break;
case 'e':
do_test (test, repeat, "en_US.UTF-8", text_french_utf8);
break;
case 'f':
do_test (test, repeat, "C", text_greek_iso8859);
break;
case 'g':
do_test (test, repeat, "el_GR.ISO-8859-7", text_greek_iso8859);
break;
case 'h':
do_test (test, repeat, "en_US.UTF-8", text_greek_utf8);
break;
case 'i':
do_test (test, repeat, "en_US.UTF-8", text_chinese_utf8);
break;
case 'j':
do_test (test, repeat, "zh_CN.GB18030", text_chinese_gb18030);
break;
default:
/* Ignore. */
;
}
}
return 0;
}