[libcxx-commits] [libcxx] 77fd54d - [libcxx] [test] Fix the collate compare test for Glibc, Windows and FreeBSD

Martin Storsjö via libcxx-commits libcxx-commits at lists.llvm.org
Thu Mar 3 00:20:03 PST 2022


Author: Martin Storsjö
Date: 2022-03-03T10:09:24+02:00
New Revision: 77fd54d2eb7322838d50495b54b3d0b3c6c07a3d

URL: https://github.com/llvm/llvm-project/commit/77fd54d2eb7322838d50495b54b3d0b3c6c07a3d
DIFF: https://github.com/llvm/llvm-project/commit/77fd54d2eb7322838d50495b54b3d0b3c6c07a3d.diff

LOG: [libcxx] [test] Fix the collate compare test for Glibc, Windows and FreeBSD

The old expected behaviour was specific to Apple platforms,
while Glibc, Windows and FreeBSD collate differently (ignoring
case). Make the old tested behaviour a special case for Apple
platforms, and make the default case the one used by the other
three.

In clang-cl/DLL configurations, the test is hit by
https://llvm.org/PR41018 (making the test fail to link).

Differential Revision: https://reviews.llvm.org/D120797

Added: 
    

Modified: 
    libcxx/test/std/localization/locale.categories/category.collate/locale.collate.byname/compare.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/localization/locale.categories/category.collate/locale.collate.byname/compare.pass.cpp b/libcxx/test/std/localization/locale.categories/category.collate/locale.collate.byname/compare.pass.cpp
index 5a8305608e1e1..a917bde709543 100644
--- a/libcxx/test/std/localization/locale.categories/category.collate/locale.collate.byname/compare.pass.cpp
+++ b/libcxx/test/std/localization/locale.categories/category.collate/locale.collate.byname/compare.pass.cpp
@@ -19,11 +19,8 @@
 //     has any 
diff erence from "C" collation.  But I do believe I'm picking
 //     up the OS's collation files.
 
-// TODO investigation needed.
-// Glibc seems to collate files 
diff erently from the way Apple's C library does it.
-// XFAIL: target={{.*}}-linux-gnu{{.*}}
-
-// XFAIL: LIBCXX-WINDOWS-FIXME
+// https://llvm.org/PR41018
+// XFAIL: windows-dll && msvc
 
 // XFAIL: LIBCXX-AIX-FIXME
 
@@ -36,24 +33,49 @@
 #include "test_macros.h"
 #include "platform_support.h" // locale name macros
 
+#define ASSERT_COMPARE(type, str1, str2, expected) \
+    do { \
+        type s1(str1); \
+        type s2(str2); \
+        assert(f.compare(s1.data(), s1.data() + s1.size(), \
+                         s2.data(), s2.data() + s2.size()) == (expected)); \
+    } while (0)
+
 int main(int, char**)
 {
     {
         std::locale l(LOCALE_en_US_UTF_8);
         {
             const std::collate<char>& f = std::use_facet<std::collate<char> >(l);
-            std::string s2("aaaaaaA");
-            std::string s3("BaaaaaA");
-            assert(f.compare(s2.data(), s2.data() + s2.size(),
-                             s3.data(), s3.data() + s3.size()) == 1);
+
+            ASSERT_COMPARE(std::string, "aaa", "bbb", -1);
+            ASSERT_COMPARE(std::string, "AAA", "BBB", -1);
+            ASSERT_COMPARE(std::string, "bbb", "aaa", 1);
+            ASSERT_COMPARE(std::string, "ccc", "ccc", 0);
+
+#if defined(__APPLE__)
+            // Apple's default collation is case-sensitive
+            ASSERT_COMPARE(std::string, "aaaaaaA", "BaaaaaA", 1);
+#else
+            // Glibc, Windows, and FreeBSD's default collation is case-insensitive
+            ASSERT_COMPARE(std::string, "aaaaaaA", "BaaaaaA", -1);
+#endif
         }
 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
         {
             const std::collate<wchar_t>& f = std::use_facet<std::collate<wchar_t> >(l);
-            std::wstring s2(L"aaaaaaA");
-            std::wstring s3(L"BaaaaaA");
-            assert(f.compare(s2.data(), s2.data() + s2.size(),
-                             s3.data(), s3.data() + s3.size()) == 1);
+
+            ASSERT_COMPARE(std::wstring, L"aaa", L"bbb", -1);
+            ASSERT_COMPARE(std::wstring, L"AAA", L"BBB", -1);
+            ASSERT_COMPARE(std::wstring, L"bbb", L"aaa", 1);
+            ASSERT_COMPARE(std::wstring, L"ccc", L"ccc", 0);
+#if defined(__APPLE__)
+            // Apple's default collation is case-sensitive
+            ASSERT_COMPARE(std::wstring, L"aaaaaaA", L"BaaaaaA", 1);
+#else
+            // Glibc, Windows, and FreeBSD's default collation is case-insensitive
+            ASSERT_COMPARE(std::wstring, L"aaaaaaA", L"BaaaaaA", -1);
+#endif
         }
 #endif
     }
@@ -61,18 +83,25 @@ int main(int, char**)
         std::locale l("C");
         {
             const std::collate<char>& f = std::use_facet<std::collate<char> >(l);
-            std::string s2("aaaaaaA");
-            std::string s3("BaaaaaA");
-            assert(f.compare(s2.data(), s2.data() + s2.size(),
-                             s3.data(), s3.data() + s3.size()) == 1);
+            ASSERT_COMPARE(std::string, "aaa", "bbb", -1);
+            ASSERT_COMPARE(std::string, "AAA", "BBB", -1);
+            ASSERT_COMPARE(std::string, "bbb", "aaa", 1);
+            ASSERT_COMPARE(std::string, "ccc", "ccc", 0);
+
+            // In the C locale, these are collated lexicographically.
+            ASSERT_COMPARE(std::string, "aaaaaaA", "BaaaaaA", 1);
         }
 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
         {
             const std::collate<wchar_t>& f = std::use_facet<std::collate<wchar_t> >(l);
-            std::wstring s2(L"aaaaaaA");
-            std::wstring s3(L"BaaaaaA");
-            assert(f.compare(s2.data(), s2.data() + s2.size(),
-                             s3.data(), s3.data() + s3.size()) == 1);
+
+            ASSERT_COMPARE(std::wstring, L"aaa", L"bbb", -1);
+            ASSERT_COMPARE(std::wstring, L"AAA", L"BBB", -1);
+            ASSERT_COMPARE(std::wstring, L"bbb", L"aaa", 1);
+            ASSERT_COMPARE(std::wstring, L"ccc", L"ccc", 0);
+
+            // In the C locale, these are collated lexicographically.
+            ASSERT_COMPARE(std::wstring, L"aaaaaaA", L"BaaaaaA", 1);
         }
 #endif
     }


        


More information about the libcxx-commits mailing list