tests: regex: restructure setup of locales In order to make it easier adding more locale-related tests, add a generalized framework handling initial setup of languages as well as the cleanup of them afterwards.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
diff --git a/tests/core/posix.c b/tests/core/posix.c
index 40eab0a..8155538 100644
--- a/tests/core/posix.c
+++ b/tests/core/posix.c
@@ -15,8 +15,12 @@
#include "posix.h"
#include "userdiff.h"
+static const char *old_locales[LC_ALL];
+
void test_core_posix__initialize(void)
{
+ memset(&old_locales, 0, sizeof(old_locales));
+
#ifdef GIT_WIN32
/* on win32, the WSA context needs to be initialized
* before any socket calls can be performed */
@@ -29,6 +33,13 @@ void test_core_posix__initialize(void)
void test_core_posix__cleanup(void)
{
+ int i;
+
+ for (i = 0; i < LC_ALL; i++) {
+ if (old_locales[i])
+ setlocale(i, old_locales[i]);
+ }
+
p_unlink("fallocate_test");
}
@@ -152,54 +163,38 @@ void test_core_posix__utimes(void)
p_unlink("foo");
}
-void test_core_posix__p_regcomp_ignores_global_locale_ctype(void)
+static void try_set_locale(int category)
{
- p_regex_t preg;
- int error = 0;
+ old_locales[category] = setlocale(category, NULL);
- const char* oldlocale = setlocale(LC_CTYPE, NULL);
-
- if (!setlocale(LC_CTYPE, "UTF-8") &&
- !setlocale(LC_CTYPE, "c.utf8") &&
- !setlocale(LC_CTYPE, "en_US.UTF-8"))
+ if (!setlocale(category, "UTF-8") &&
+ !setlocale(category, "c.utf8") &&
+ !setlocale(category, "en_US.UTF-8"))
cl_skip();
- if (MB_CUR_MAX == 1) {
- setlocale(LC_CTYPE, oldlocale);
+ if (MB_CUR_MAX == 1)
cl_fail("Expected locale to be switched to multibyte");
- }
-
- error = p_regcomp(&preg, "[\xc0-\xff][\x80-\xbf]", P_REG_EXTENDED);
- p_regfree(&preg);
-
- setlocale(LC_CTYPE, oldlocale);
-
- cl_must_pass(error);
}
-void test_core_posix__p_regcomp_ignores_global_locale_collate(void)
+void test_core_posix__p_regcomp_ignores_global_locale_ctype(void)
{
- regex_t preg;
- int error = 0;
+ p_regex_t preg;
- const char* oldlocale = setlocale(LC_COLLATE, NULL);
+ try_set_locale(LC_CTYPE);
- if (!setlocale(LC_COLLATE, "UTF-8") &&
- !setlocale(LC_COLLATE, "c.utf8") &&
- !setlocale(LC_COLLATE, "en_US.UTF-8"))
- cl_skip();
+ cl_must_pass(p_regcomp(&preg, "[\xc0-\xff][\x80-\xbf]", P_REG_EXTENDED));
- if (MB_CUR_MAX == 1) {
- setlocale(LC_COLLATE, oldlocale);
- cl_fail("Expected locale to be switched to multibyte");
- }
+ p_regfree(&preg);
+}
- error = p_regcomp(&preg, "[\xc0-\xff][\x80-\xbf]", REG_EXTENDED);
- regfree(&preg);
+void test_core_posix__p_regcomp_ignores_global_locale_collate(void)
+{
+ p_regex_t preg;
- setlocale(LC_COLLATE, oldlocale);
+ try_set_locale(LC_COLLATE);
+ cl_must_pass(p_regcomp(&preg, "[\xc0-\xff][\x80-\xbf]", P_REG_EXTENDED));
- cl_must_pass(error);
+ p_regfree(&preg);
}
void test_core_posix__p_regcomp_compile_userdiff_regexps(void)