Fix incorrect use of setlocale() in cppformat CStr tests following 61db02790c

Reported By: elexis
Differential Revision: https://code.wildfiregames.com/D2013
This was SVN commit r22518.
This commit is contained in:
wraitii 2019-07-20 12:57:03 +00:00
parent cca7627d88
commit 09916ce6cb
3 changed files with 47 additions and 6 deletions

43
source/lib/scoped_locale.h Executable file
View file

@ -0,0 +1,43 @@
/* Copyright (C) 2019 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef INCLUDED_SCOPED_LOCALE
#define INCLUDED_SCOPED_LOCALE
class ScopedLocale
{
public:
ScopedLocale(int category, char* newLocale) : m_Category(category)
{
m_OldLocale = setlocale(category, nullptr);
TS_ASSERT(setlocale(m_Category, newLocale) != nullptr);
}
~ScopedLocale()
{
TS_ASSERT(setlocale(m_Category, m_OldLocale));
}
private:
int m_Category;
char* m_OldLocale;
};
#endif // #ifndef INCLUDED_SCOPED_LOCALE

View file

@ -16,6 +16,7 @@
*/
#include "lib/self_test.h"
#include "lib/scoped_locale.h"
#include "ps/CStr.h"
@ -113,7 +114,7 @@ public:
// because GTK+ can change the locale when we're running Atlas.
// (If the host system doesn't have the locale we're using for this test
// then it'll just stick with the default, which is fine)
char* old = setlocale(LC_NUMERIC, "fr_FR.UTF-8");
ScopedLocale(LC_NUMERIC, "en_US.UTF-8");
CStr8 str1("1.234");
TS_ASSERT_DELTA(str1.ToFloat(), 1.234f, 0.0001f);
@ -138,7 +139,5 @@ public:
TS_ASSERT_EQUALS(str3.ToUInt(), 3u);
TS_ASSERT_EQUALS(str3.ToLong(), 3);
TS_ASSERT_EQUALS(str3.ToULong(), 3u);
setlocale(LC_NUMERIC, old);
}
};

View file

@ -16,6 +16,7 @@
*/
#include "lib/self_test.h"
#include "lib/scoped_locale.h"
#include "third_party/cppformat/format.h"
@ -25,7 +26,7 @@ public:
void test_basic()
{
// Make test behave independent of current host locale
char* old = setlocale(LC_ALL, "en_US.UTF-8");
ScopedLocale(LC_ALL, "en_US.UTF-8");
TS_ASSERT_EQUALS(fmt::sprintf("abc"), "abc");
@ -57,7 +58,5 @@ public:
TS_ASSERT_EQUALS(fmt::sprintf("T%sT", (const char*)NULL), "T(null)T");
TS_ASSERT_EQUALS(fmt::sprintf("T%pT", (void*)0x1234), "T0x1234T");
setlocale(LC_ALL, old);
}
};