Skip to content

Commit

Permalink
Warn rather than fail the test when the locale 'en_US.UTF-8' is not a…
Browse files Browse the repository at this point in the history
…vailable (#296)
  • Loading branch information
swebb2066 authored Nov 20, 2023
1 parent d9433fc commit 89b013d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
33 changes: 23 additions & 10 deletions src/test/cpp/helpers/charsetdecodertestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

#include <log4cxx/private/string_c11.h>
#include <log4cxx/helpers/charsetdecoder.h>
#include <log4cxx/helpers/transcoder.h>
#include "../logunit.h"
#include "../insertwide.h"
#include <log4cxx/helpers/bytebuffer.h>
#include <log4cxx/helpers/loglog.h>

using namespace log4cxx;
using namespace log4cxx::helpers;
Expand Down Expand Up @@ -122,16 +124,27 @@ LOGUNIT_CLASS(CharsetDecoderTestCase)
const logchar* greet = utf8_greet;
#endif

std::locale::global(std::locale("en_US.UTF-8"));
auto dec = CharsetDecoder::getDecoder(LOG4CXX_STR("locale"));

ByteBuffer in(utf8_greet, sizeof (utf8_greet));
LogString out;
log4cxx_status_t stat = dec->decode(in, out);
LOGUNIT_ASSERT_EQUAL(false, CharsetDecoder::isError(stat));
stat = dec->decode(in, out);
LOGUNIT_ASSERT_EQUAL(false, CharsetDecoder::isError(stat));
LOGUNIT_ASSERT(out == greet);
try
{
std::locale::global(std::locale("en_US.UTF-8"));
auto dec = CharsetDecoder::getDecoder(LOG4CXX_STR("locale"));

ByteBuffer in(utf8_greet, sizeof (utf8_greet));
LogString out;
log4cxx_status_t stat = dec->decode(in, out);
LOGUNIT_ASSERT_EQUAL(false, CharsetDecoder::isError(stat));
stat = dec->decode(in, out);
LOGUNIT_ASSERT_EQUAL(false, CharsetDecoder::isError(stat));
LOGUNIT_ASSERT(out == greet);
}
catch (std::runtime_error& ex)
{
LogString msg;
Transcoder::decode(ex.what(), msg);
msg.append(LOG4CXX_STR(": "));
msg.append(LOG4CXX_STR("en_US.UTF-8"));
LogLog::warn(msg);
}
}


Expand Down
47 changes: 30 additions & 17 deletions src/test/cpp/helpers/charsetencodertestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "../logunit.h"
#include "../insertwide.h"
#include <log4cxx/helpers/bytebuffer.h>
#include <log4cxx/helpers/transcoder.h>
#include <log4cxx/helpers/loglog.h>
#include <apr.h>
#include <apr_atomic.h>
#include <condition_variable>
Expand Down Expand Up @@ -200,28 +202,39 @@ LOGUNIT_CLASS(CharsetEncoderTestCase)
#endif
LogString greeting(greet);

std::locale::global(std::locale("en_US.UTF-8"));
auto enc = CharsetEncoder::getEncoder(LOG4CXX_STR("locale"));
try
{
std::locale::global(std::locale("en_US.UTF-8"));
auto enc = CharsetEncoder::getEncoder(LOG4CXX_STR("locale"));

char buf[BUFSIZE];
ByteBuffer out(buf, BUFSIZE);
LogString::const_iterator iter = greeting.begin();
log4cxx_status_t stat = enc->encode(greeting, iter, out);
LOGUNIT_ASSERT_EQUAL(false, CharsetEncoder::isError(stat));
stat = enc->encode(greeting, iter, out);
LOGUNIT_ASSERT_EQUAL(false, CharsetEncoder::isError(stat));
char buf[BUFSIZE];
ByteBuffer out(buf, BUFSIZE);
LogString::const_iterator iter = greeting.begin();
log4cxx_status_t stat = enc->encode(greeting, iter, out);
LOGUNIT_ASSERT_EQUAL(false, CharsetEncoder::isError(stat));
stat = enc->encode(greeting, iter, out);
LOGUNIT_ASSERT_EQUAL(false, CharsetEncoder::isError(stat));

out.flip();
LOGUNIT_ASSERT_EQUAL((size_t) 13, out.limit());
out.flip();
LOGUNIT_ASSERT_EQUAL((size_t) 13, out.limit());

for (size_t i = 0; i < out.limit(); i++)
for (size_t i = 0; i < out.limit(); i++)
{
unsigned expected = (unsigned)utf8_greet[i];
unsigned actual = (unsigned)out.data()[i];
LOGUNIT_ASSERT_EQUAL(expected, actual);
}

LOGUNIT_ASSERT(iter == greeting.end());
}
catch (std::runtime_error& ex)
{
unsigned expected = (unsigned)utf8_greet[i];
unsigned actual = (unsigned)out.data()[i];
LOGUNIT_ASSERT_EQUAL(expected, actual);
LogString msg;
Transcoder::decode(ex.what(), msg);
msg.append(LOG4CXX_STR(": "));
msg.append(LOG4CXX_STR("en_US.UTF-8"));
LogLog::warn(msg);
}

LOGUNIT_ASSERT(iter == greeting.end());
}

#if APR_HAS_THREADS
Expand Down

0 comments on commit 89b013d

Please sign in to comment.