Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
Fix locale test data for GLIBC 2.27 and newer.
Browse files Browse the repository at this point in the history
GLIBC 2.27 changed the locale data for fr_FR and ru_RU. In particular
they change the decimal and thousands separators used. This patch
makes the locale tests tolerate the updated locales.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@329143 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
EricWF committed Apr 4, 2018
1 parent 0b26184 commit b21b317
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/locale.cpp
Expand Up @@ -4240,6 +4240,7 @@ static bool checked_string_to_char_convert(char& dest,
// FIXME: Work around specific multibyte sequences that we can reasonable
// translate into a different single byte.
switch (wout) {
case L'\u202F': // narrow non-breaking space
case L'\u00A0': // non-breaking space
dest = ' ';
return true;
Expand Down
Expand Up @@ -25,6 +25,7 @@
#include "test_iterators.h"

#include "platform_support.h" // locale name macros
#include "test_macros.h"

typedef std::money_get<char, input_iterator<const char*> > Fn;

Expand All @@ -46,6 +47,30 @@ class my_facetw
: Fw(refs) {}
};


// GLIBC 2.27 and newer use U2027 (narrow non-breaking space) as a thousands sep.
// this function converts the spaces in string inputs to that character if need
// be.
static std::wstring convert_thousands_sep(std::wstring const& in) {
#if !defined(TEST_HAS_GLIBC) || !TEST_GLIBC_PREREQ(2,27)
return in;
#else
std::wstring out;
unsigned I = 0;
bool seen_decimal = false;
for (; I < in.size(); ++I) {
if (seen_decimal || in[I] != L' ') {
seen_decimal |= in[I] == L',';
out.push_back(in[I]);
continue;
}
assert(in[I] == L' ');
out.push_back(L'\u202F');
}
return out;
#endif
}

int main()
{
std::ios ios(0);
Expand Down Expand Up @@ -417,7 +442,7 @@ int main()
assert(ex == -1);
}
{ // positive
std::wstring v = L"1 234 567,89 ";
std::wstring v = convert_thousands_sep(L"1 234 567,89 ");
typedef input_iterator<const wchar_t*> I;
long double ex;
std::ios_base::iostate err = std::ios_base::goodbit;
Expand All @@ -428,7 +453,7 @@ int main()
assert(ex == 123456789);
}
{ // negative
std::wstring v = L"-1 234 567,89";
std::wstring v = convert_thousands_sep(L"-1 234 567,89");
typedef input_iterator<const wchar_t*> I;
long double ex;
std::ios_base::iostate err = std::ios_base::goodbit;
Expand Down Expand Up @@ -497,7 +522,7 @@ int main()
assert(ex == -1);
}
{ // positive, showbase
std::wstring v = L"1 234 567,89 \u20ac"; // EURO SIGN
std::wstring v = convert_thousands_sep(L"1 234 567,89 \u20ac"); // EURO SIGN
typedef input_iterator<const wchar_t*> I;
long double ex;
std::ios_base::iostate err = std::ios_base::goodbit;
Expand All @@ -508,7 +533,7 @@ int main()
assert(ex == 123456789);
}
{ // positive, showbase
std::wstring v = L"1 234 567,89 \u20ac"; // EURO SIGN
std::wstring v = convert_thousands_sep(L"1 234 567,89 \u20ac"); // EURO SIGN
showbase(ios);
typedef input_iterator<const wchar_t*> I;
long double ex;
Expand All @@ -521,7 +546,7 @@ int main()
noshowbase(ios);
}
{ // negative, showbase
std::wstring v = L"-1 234 567,89 \u20ac"; // EURO SIGN
std::wstring v = convert_thousands_sep(L"-1 234 567,89 \u20ac"); // EURO SIGN
showbase(ios);
typedef input_iterator<const wchar_t*> I;
long double ex;
Expand All @@ -534,7 +559,7 @@ int main()
noshowbase(ios);
}
{ // negative, showbase
std::wstring v = L"1 234 567,89 EUR -";
std::wstring v = convert_thousands_sep(L"1 234 567,89 EUR -");
showbase(ios);
typedef input_iterator<const wchar_t*> I;
long double ex;
Expand All @@ -546,7 +571,7 @@ int main()
noshowbase(ios);
}
{ // negative, showbase
std::wstring v = L"1 234 567,89 EUR -";
std::wstring v = convert_thousands_sep(L"1 234 567,89 EUR -");
typedef input_iterator<const wchar_t*> I;
long double ex;
std::ios_base::iostate err = std::ios_base::goodbit;
Expand Down Expand Up @@ -583,7 +608,7 @@ int main()
assert(ex == -1);
}
{ // positive
std::wstring v = L"1 234 567,89 ";
std::wstring v = convert_thousands_sep(L"1 234 567,89 ");
typedef input_iterator<const wchar_t*> I;
long double ex;
std::ios_base::iostate err = std::ios_base::goodbit;
Expand All @@ -594,7 +619,7 @@ int main()
assert(ex == 123456789);
}
{ // negative
std::wstring v = L"-1 234 567,89";
std::wstring v = convert_thousands_sep(L"-1 234 567,89");
typedef input_iterator<const wchar_t*> I;
long double ex;
std::ios_base::iostate err = std::ios_base::goodbit;
Expand Down Expand Up @@ -663,7 +688,7 @@ int main()
assert(ex == -1);
}
{ // positive, showbase
std::wstring v = L"1 234 567,89 EUR";
std::wstring v = convert_thousands_sep(L"1 234 567,89 EUR");
typedef input_iterator<const wchar_t*> I;
long double ex;
std::ios_base::iostate err = std::ios_base::goodbit;
Expand All @@ -674,7 +699,7 @@ int main()
assert(ex == 123456789);
}
{ // positive, showbase
std::wstring v = L"1 234 567,89 EUR";
std::wstring v = convert_thousands_sep(L"1 234 567,89 EUR");
showbase(ios);
typedef input_iterator<const wchar_t*> I;
long double ex;
Expand All @@ -687,7 +712,7 @@ int main()
noshowbase(ios);
}
{ // negative, showbase
std::wstring v = L"-1 234 567,89 EUR";
std::wstring v = convert_thousands_sep(L"-1 234 567,89 EUR");
showbase(ios);
typedef input_iterator<const wchar_t*> I;
long double ex;
Expand All @@ -700,7 +725,7 @@ int main()
noshowbase(ios);
}
{ // negative, showbase
std::wstring v = L"1 234 567,89 Eu-";
std::wstring v = convert_thousands_sep(L"1 234 567,89 Eu-");
showbase(ios);
typedef input_iterator<const wchar_t*> I;
long double ex;
Expand All @@ -712,7 +737,7 @@ int main()
noshowbase(ios);
}
{ // negative, showbase
std::wstring v = L"1 234 567,89 Eu-";
std::wstring v = convert_thousands_sep(L"1 234 567,89 Eu-");
typedef input_iterator<const wchar_t*> I;
long double ex;
std::ios_base::iostate err = std::ios_base::goodbit;
Expand Down
Expand Up @@ -25,6 +25,7 @@
#include "test_iterators.h"

#include "platform_support.h" // locale name macros
#include "test_macros.h"

typedef std::money_put<char, output_iterator<char*> > Fn;

Expand All @@ -46,6 +47,32 @@ class my_facetw
: Fw(refs) {}
};


// GLIBC 2.27 and newer use U2027 (narrow non-breaking space) as a thousands sep.
// this function converts the spaces in string inputs to that character if need
// be.
static std::wstring convert_thousands_sep(std::wstring const& in) {
#if !defined(TEST_HAS_GLIBC) || !TEST_GLIBC_PREREQ(2,27)
return in;
#else
std::wstring out;
unsigned I = 0;
bool seen_num_start = false;
bool seen_decimal = false;
for (; I < in.size(); ++I) {
seen_decimal |= in[I] == L',';
seen_num_start |= in[I] == '-' || std::iswdigit(in[I]);
if (seen_decimal || !seen_num_start || in[I] != L' ') {
out.push_back(in[I]);
continue;
}
assert(in[I] == L' ');
out.push_back(L'\u202F');
}
return out;
#endif
}

int main()
{
std::ios ios(0);
Expand Down Expand Up @@ -301,15 +328,15 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
false, ios, '*', v);
std::wstring ex(str, iter.base());
assert(ex == L"1 234 567,89");
assert(ex == convert_thousands_sep(L"1 234 567,89"));
}
{ // negative
long double v = -123456789;
wchar_t str[100];
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
false, ios, '*', v);
std::wstring ex(str, iter.base());
assert(ex == L"-1 234 567,89");
assert(ex == convert_thousands_sep(L"-1 234 567,89"));
}
{ // zero, showbase
long double v = 0;
Expand All @@ -336,7 +363,7 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
false, ios, '*', v);
std::wstring ex(str, iter.base());
assert(ex == L"1 234 567,89 \u20ac");
assert(ex == convert_thousands_sep(L"1 234 567,89 \u20ac"));
}
{ // negative, showbase
long double v = -123456789;
Expand All @@ -345,7 +372,7 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
false, ios, '*', v);
std::wstring ex(str, iter.base());
assert(ex == L"-1 234 567,89 \u20ac");
assert(ex == convert_thousands_sep(L"-1 234 567,89 \u20ac"));
}
{ // negative, showbase, left
long double v = -123456789;
Expand All @@ -356,7 +383,7 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
false, ios, ' ', v);
std::wstring ex(str, iter.base());
assert(ex == L"-1 234 567,89 \u20ac ");
assert(ex == convert_thousands_sep(L"-1 234 567,89 \u20ac "));
assert(ios.width() == 0);
}
{ // negative, showbase, internal
Expand All @@ -368,7 +395,7 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
false, ios, ' ', v);
std::wstring ex(str, iter.base());
assert(ex == L"-1 234 567,89 \u20ac");
assert(ex == convert_thousands_sep(L"-1 234 567,89 \u20ac"));
assert(ios.width() == 0);
}
{ // negative, showbase, right
Expand All @@ -380,7 +407,7 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
false, ios, ' ', v);
std::wstring ex(str, iter.base());
assert(ex == L" -1 234 567,89 \u20ac");
assert(ex == convert_thousands_sep(L" -1 234 567,89 \u20ac"));
assert(ios.width() == 0);
}

Expand Down Expand Up @@ -409,15 +436,15 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
true, ios, '*', v);
std::wstring ex(str, iter.base());
assert(ex == L"1 234 567,89");
assert(ex == convert_thousands_sep(L"1 234 567,89"));
}
{ // negative
long double v = -123456789;
wchar_t str[100];
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
true, ios, '*', v);
std::wstring ex(str, iter.base());
assert(ex == L"-1 234 567,89");
assert(ex == convert_thousands_sep(L"-1 234 567,89"));
}
{ // zero, showbase
long double v = 0;
Expand All @@ -444,7 +471,7 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
true, ios, '*', v);
std::wstring ex(str, iter.base());
assert(ex == L"1 234 567,89 EUR");
assert(ex == convert_thousands_sep(L"1 234 567,89 EUR"));
}
{ // negative, showbase
long double v = -123456789;
Expand All @@ -453,7 +480,7 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
true, ios, '*', v);
std::wstring ex(str, iter.base());
assert(ex == L"-1 234 567,89 EUR");
assert(ex == convert_thousands_sep(L"-1 234 567,89 EUR"));
}
{ // negative, showbase, left
long double v = -123456789;
Expand All @@ -464,7 +491,7 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
true, ios, ' ', v);
std::wstring ex(str, iter.base());
assert(ex == L"-1 234 567,89 EUR ");
assert(ex == convert_thousands_sep(L"-1 234 567,89 EUR "));
assert(ios.width() == 0);
}
{ // negative, showbase, internal
Expand All @@ -476,7 +503,7 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
true, ios, ' ', v);
std::wstring ex(str, iter.base());
assert(ex == L"-1 234 567,89 EUR");
assert(ex == convert_thousands_sep(L"-1 234 567,89 EUR"));
assert(ios.width() == 0);
}
{ // negative, showbase, right
Expand All @@ -488,7 +515,7 @@ int main()
output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
true, ios, ' ', v);
std::wstring ex(str, iter.base());
assert(ex == L" -1 234 567,89 EUR");
assert(ex == convert_thousands_sep(L" -1 234 567,89 EUR"));
assert(ios.width() == 0);
}
}
Expand Down
Expand Up @@ -21,7 +21,6 @@
#include <locale>
#include <limits>
#include <cassert>
#include <iostream> // FIXME: for debugging purposes only

#include "test_macros.h"
#include "platform_support.h" // locale name macros
Expand Down Expand Up @@ -111,7 +110,8 @@ int main()
assert(f.decimal_point() == L',');
}
// GLIBC 2.23 uses '.' as the decimal point while other C libraries use ','
#ifndef TEST_HAS_GLIBC
// GLIBC 2.27 corrects this.
#if !defined(TEST_HAS_GLIBC) || TEST_GLIBC_PREREQ(2, 27)
const char sep = ',';
const wchar_t wsep = L',';
#else
Expand All @@ -120,11 +120,6 @@ int main()
#endif
{
Fnf f(LOCALE_ru_RU_UTF_8, 1);
if (f.decimal_point() != sep) {
std::cout << "f.decimal_point() = '" << f.decimal_point() << "'\n";
std::cout << "sep = '" << sep << "'\n";
std::cout << std::endl;
}
assert(f.decimal_point() == sep);
}
{
Expand Down

0 comments on commit b21b317

Please sign in to comment.