Commit e207b2a2ff021d297bfce8d0ad20ec32359a7daa

Patrick Steinhardt 2018-03-02T15:09:20

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.

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)