Skip to content

Commit

Permalink
tests: added testUTFAndWCharPreallocDest and renamed testLongSimpleTe…
Browse files Browse the repository at this point in the history
…xt -> testUTFAndWCharNullDest

There were no tests calling WCharToUTF or UTFToWChar with non-NULL dst buffers
(not even implicit calls generated by fbc). That's a big gap, because the
UTF-to-WChar functions all have completely separate implementations for those
two cases.

One of the asserts in the new testcase fails.

Likewise these are no non-NULL dst tests for Char <-> WChar and UTF <-> Char,
although some cases would be implicitly generated by fbc.
  • Loading branch information
rversteegen committed Apr 30, 2016
1 parent 954b41a commit b87d569
Showing 1 changed file with 79 additions and 2 deletions.
81 changes: 79 additions & 2 deletions tests/wstring/utf_conv.bas
Expand Up @@ -74,11 +74,12 @@ sub testZstringToNullDest cdecl( )
deallocate( utf32 )
end sub

'' Tests WCharToUTF and UTFToWChar with NULL dest.
'' The rtlib has UTF encoding routines that can allocate memory for us. If using
'' that version of them, they pre-allocate some chars (currently typically 8) at
'' a time. To ensure this works correctly, we have to test with strings longer
'' than that.
sub testLongSimpleText cdecl( )
sub testUTFAndWCharNullDest cdecl( )
const TEXT = "testing with a long, but simple 7-bit ASCII string"
dim src as wstring * 100 = wstr( TEXT )

Expand Down Expand Up @@ -143,13 +144,89 @@ sub testLongSimpleText cdecl( )
end scope
end sub

'' Tests WCharToUTF and UTFToWChar with a preallocated destination buffer.
'' These do not test the cases where the destination buffer is not long enough
'' to hold the output string (which should work but result in no null
'' terminators).
sub testUTFAndWCharPreallocDest cdecl( )
const TEXT = "testing with a long, but simple 7-bit ASCII string"
dim src as wstring * 100 = wstr( TEXT )

'' Use buffers shorter than the input buffer, but longer than the input string
dim as integer buflen = (len( src ) + 2) * 4
dim as ubyte ptr utfbuf = callocate( buflen )
dim as ubyte ptr wbuf = callocate( buflen )

scope
'' Convert to UTF8, including null terminator
dim as integer utf8bytes
dim as ubyte ptr utf8 = WCharToUTF( UTF_ENCOD_UTF8, src, len( src ) + 1, utfbuf, @utf8bytes )
CU_ASSERT( utf8 = utfbuf )
CU_ASSERT( utf8bytes = (len( TEXT ) + 1) )
CU_ASSERT( *cptr( zstring ptr, utf8 ) = TEXT )

'' Convert back to wstring
dim as integer chars = buflen '' Input buffer length in characters
dim as wstring ptr w = UTFToWChar( UTF_ENCOD_UTF8, utf8, wbuf, @chars )
CU_ASSERT( w = wbuf )
CU_ASSERT( chars = len( TEXT ) )
CU_ASSERT( *w = src )
end scope

scope
'' Convert to UTF16, including null terminator
dim as integer utf16bytes
memset( utfbuf, 0, buflen )
dim as ushort ptr utf16 = WCharToUTF( UTF_ENCOD_UTF16, src, len( src ) + 1, utfbuf, @utf16bytes )
CU_ASSERT( utf16 = cptr( ushort ptr, utfbuf ) )
CU_ASSERT( (utf16bytes mod 2) = 0 )
CU_ASSERT( (utf16bytes \ 2) = (len( TEXT ) + 1) )
for i as integer = 0 to (utf16bytes \ 2) - 1
CU_ASSERT( utf16[i] = src[i] )
next

'' Convert back to wstring
dim as integer chars = buflen \ 2 '' Input buffer length in characters
memset( wbuf, 0, buflen )
dim as wstring ptr w = UTFToWChar( UTF_ENCOD_UTF16, utf16, wbuf, @chars )
CU_ASSERT( w = wbuf )
CU_ASSERT( chars = len( TEXT ) )
CU_ASSERT( *w = src )
end scope

scope
'' Convert to UTF32, including null terminator
dim as integer utf32bytes
memset( utfbuf, 0, buflen )
dim as ulong ptr utf32 = WCharToUTF( UTF_ENCOD_UTF32, src, len( src ) + 1, utfbuf, @utf32bytes )
CU_ASSERT( utf32 = cptr( ulong ptr, utfbuf ) )
CU_ASSERT( (utf32bytes mod 4) = 0 )
CU_ASSERT( (utf32bytes \ 4) = (len( TEXT ) + 1) )
for i as integer = 0 to (utf32bytes \ 4) - 1
CU_ASSERT( utf32[i] = src[i] )
next

'' Convert back to wstring
dim as integer chars = buflen \ 4 '' Input buffer length in characters
memset( wbuf, 0, buflen )
dim as wstring ptr w = UTFToWChar( UTF_ENCOD_UTF32, utf32, wbuf, @chars )
CU_ASSERT( w = wbuf )
CU_ASSERT( chars = len( TEXT ) )
CU_ASSERT( *w = src )
end scope

deallocate( utfbuf )
deallocate( wbuf )
end sub

sub ctor( ) constructor
fbcu.add_suite( "tests/wstrings/utf_conv" )
fbcu.add_test( "test_z", @test_z )
fbcu.add_test( "test_w", @test_w )
fbcu.add_test( "testWstringToNullDest", @testWstringToNullDest )
fbcu.add_test( "testZstringToNullDest", @testZstringToNullDest )
fbcu.add_test( "testLongSimpleText", @testLongSimpleText )
fbcu.add_test( "testUTFAndWCharNullDest", @testUTFAndWCharNullDest )
fbcu.add_test( "testUTFAndWCharPreallocDest", @testUTFAndWCharPreallocDest )
end sub

end namespace

0 comments on commit b87d569

Please sign in to comment.