Fix VASprintfTest.cpp for Darwin, add checks
Summary:
The EncodingError test ensures that trying to encode a multibyte wchar
with a given codepage fails. If setlocale() fails, the encoding is
performed using the current locale, which may or may not fail.
This patch asserts that both setlocale() operations are successful, as
well as falling back to a widely available unibyte encoding for
non-Windows systems.
<rdar://problem/33782806>
Reviewers: zturner, labath, lhames
Reviewed By: zturner
Subscribers: lldb-commits
Differential Revision: https://reviews.llvm.org/D36496
llvm-svn: 310499
diff --git a/lldb/unittests/Utility/VASprintfTest.cpp b/lldb/unittests/Utility/VASprintfTest.cpp
index 0b440942e..cb5b253 100644
--- a/lldb/unittests/Utility/VASprintfTest.cpp
+++ b/lldb/unittests/Utility/VASprintfTest.cpp
@@ -14,6 +14,12 @@
#include <locale.h>
+#if defined (_WIN32)
+#define TEST_ENCODING ".932" // On Windows, test codepage 932
+#else
+#define TEST_ENCODING "C" // ...otherwise, any widely available uni-byte LC
+#endif
+
using namespace lldb_private;
using namespace llvm;
@@ -46,7 +52,8 @@
// Save the current locale first.
std::string Current(::setlocale(LC_ALL, nullptr));
- setlocale(LC_ALL, ".932");
+ // Ensure tested locale is successfully set
+ ASSERT_TRUE(setlocale(LC_ALL, TEST_ENCODING));
wchar_t Invalid[2];
Invalid[0] = 0x100;
@@ -55,5 +62,6 @@
EXPECT_FALSE(Sprintf(Buffer, "%ls", Invalid));
EXPECT_EQ("<Encoding error>", Buffer);
- setlocale(LC_ALL, Current.c_str());
+ // Ensure we've restored the original locale once tested
+ ASSERT_TRUE(setlocale(LC_ALL, Current.c_str()));
}