Skip to content

Commit

Permalink
Add CFString tests to transcodertests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-webb committed Aug 2, 2023
1 parent d3d5d49 commit 921db1f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/cpp/messagebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ CharMessageBuffer& CharMessageBuffer::operator<<(const CFStringRef& msg)
}
else
{
m_priv->buf.append(&tmp[0], tmp.size());
m_priv->buf.append(tmp);
}
return *this;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/mock-apple/CFString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ void CFStringGetCharacters(CFStringRef theString, CFRange range, UniChar *buffer
}
}
CFStringRef CFStringCreateWithCharacters(CFAllocatorRef alloc, const UniChar *chars, CFIndex numChars) {
return (CFStringRef)apr_palloc(getStringPool(), (numChars + 1) * sizeof(UniChar));
UniChar* result = (UniChar*)apr_palloc(getStringPool(), (numChars + 1) * sizeof(UniChar));
for (UniChar* p = result; *p++ = *chars++;)
;
return (CFStringRef)result;
}
CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding) {
UniChar* result = (UniChar*)apr_palloc(getStringPool(), (strlen(cStr) + 1) * sizeof(UniChar));
Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ endif()

get_filename_component(UNIT_TEST_WORKING_DIR ../resources ABSOLUTE)
if(LOG4CXX_CFSTRING)
set(CFSTR_TESTS filetestcase messagebuffertest leveltestcase streamtestcase)
set(CFSTR_TESTS filetestcase messagebuffertest leveltestcase streamtestcase transcodertestcase)
endif()
foreach(testName IN LISTS ALL_LOG4CXX_TESTS)
if (${testName} IN_LIST CFSTR_TESTS)
Expand Down
34 changes: 32 additions & 2 deletions src/test/cpp/helpers/transcodertestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "../insertwide.h"
#include "../logunit.h"

#if LOG4CXX_CFSTRING_API
#include <CoreFoundation/CFString.h>
#endif

using namespace log4cxx;
using namespace log4cxx::helpers;
Expand All @@ -34,6 +37,9 @@ LOGUNIT_CLASS(TranscoderTestCase)
LOGUNIT_TEST(decode3);
#if LOG4CXX_WCHAR_T_API
LOGUNIT_TEST(decode4);
#endif
#if LOG4CXX_CFSTRING_API
LOGUNIT_TEST(decode5);
#endif
LOGUNIT_TEST(decode7);
LOGUNIT_TEST(decode8);
Expand All @@ -50,6 +56,9 @@ LOGUNIT_CLASS(TranscoderTestCase)
LOGUNIT_TEST(encode5);
#endif
LOGUNIT_TEST(encode6);
#if LOG4CXX_CFSTRING_API
LOGUNIT_TEST(encode7);
#endif
LOGUNIT_TEST(testDecodeUTF8_1);
LOGUNIT_TEST(testDecodeUTF8_2);
LOGUNIT_TEST(testDecodeUTF8_3);
Expand Down Expand Up @@ -106,6 +115,16 @@ LOGUNIT_CLASS(TranscoderTestCase)
}
#endif

#if LOG4CXX_CFSTRING_API
void decode5()
{
LogString nothing;
LogString decoded(LOG4CXX_STR("foo\n"));
Transcoder::decode(nothing, decoded);
LOGUNIT_ASSERT_EQUAL(LOG4CXX_STR("foo\n"), decoded);
}
#endif


enum { BUFSIZE = 255 };

Expand Down Expand Up @@ -169,7 +188,7 @@ LOGUNIT_CLASS(TranscoderTestCase)
{
// Test invalid multibyte string
LogString greeting;
greeting.push_back( 0xff );
greeting.push_back( logchar(0xff) );
std::wstring encoded;
Transcoder::encode(greeting, encoded);

Expand Down Expand Up @@ -247,6 +266,17 @@ LOGUNIT_CLASS(TranscoderTestCase)
Transcoder::encode(decoded, encoded);
}

#if LOG4CXX_CFSTRING_API
void encode7()
{
const LogString greeting(LOG4CXX_STR("Hello, World"));
CFStringRef encoded = Transcoder::encode(greeting);
LogString decoded;
Transcoder::decode(encoded, decoded);
LOGUNIT_ASSERT_EQUAL(LOG4CXX_STR("Hello, World"), decoded);
}
#endif

void testDecodeUTF8_1()
{
std::string src("a");
Expand All @@ -257,7 +287,7 @@ LOGUNIT_CLASS(TranscoderTestCase)

void testDecodeUTF8_2()
{
std::string src(1, 0x80);
std::string src(1, char(0x80));
LogString out;
Transcoder::decodeUTF8(src, out);
LOGUNIT_ASSERT_EQUAL(LogString(1, Transcoder::LOSSCHAR), out);
Expand Down

0 comments on commit 921db1f

Please sign in to comment.