Skip to content

Commit

Permalink
udt-wstring: PRINT/LPRINT/WRITE will accept UDT as Z|WSTRING
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrm committed Jun 17, 2019
1 parent 5dcb9e2 commit 3368c2f
Show file tree
Hide file tree
Showing 7 changed files with 1,096 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Expand Up @@ -23,6 +23,7 @@ Version 1.07.0
- SELECT statement will accept UDT as Z|WSTRING to return a Z|WSTRING
- SWAP statement will accept UDT as Z|WSTRING
- IIF function will accept UDT as Z|WSTRING
- PRINT/LPRINT/WRITE will accept UDT as Z|WSTRING

[fixed]
- sf.net #881: C backend: support for varadic function parameters in gcc using __builtin_va_list type and related macros
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/rtl-print.bas
Expand Up @@ -690,6 +690,8 @@ function rtlPrint _
f = PROCLOOKUP( PRINTVOID )
end if
else
astTryOvlStringCONV( expr )

'' UDT? try to convert to string with type casting op overloading
select case typeGet( astGetDataType( expr ) )
case FB_DATATYPE_STRUCT, FB_DATATYPE_ENUM
Expand Down Expand Up @@ -901,6 +903,8 @@ function rtlWrite _
if( expr = NULL ) then
f = PROCLOOKUP( WRITEVOID )
else
astTryOvlStringCONV( expr )

'' UDT? try to convert to string with type casting op overloading
select case astGetDataType( expr )
case FB_DATATYPE_STRUCT, FB_DATATYPE_ENUM
Expand Down Expand Up @@ -1071,6 +1075,8 @@ function rtlPrintUsing _
exit function
end if

astTryOvlStringCONV( expr )

'' UDT? try to convert to double with type casting op overloading
select case astGetDataType( expr )
case FB_DATATYPE_STRUCT, FB_DATATYPE_ENUM
Expand Down
145 changes: 145 additions & 0 deletions tests/udt-wstring/print.bas
@@ -0,0 +1,145 @@
#include "fbcunit.bi"
#include once "uwstring-fixed.bi"
#include once "chk-wstring.bi"

#define ustring UWSTRING_FIXED

#define PRINT_IF_UNEQUAL

#define BUFFER_SIZE 50

#define QT(s) wstr("""" & wstr(s) & """")

#define WPRINT( value, cmp ) _
print #(1), value & ", " & QT(cmp)

#define WPRINT_STR( value, cmp ) _
print #(1), QT(value) & ", " & QT(cmp)

#macro OPEN_FILE( fn )
const TESTFILE = ("./udt-wstring/" & fn)

open TESTFILE for output encoding "utf-16" as #1
#endmacro

#macro CLOSE_AND_TEST_FILE()
close #1

dim as wstring * BUFFER_SIZE sResult, sExpected
dim as integer errcount = 0
open TESTFILE for input encoding "utf-16" as #1
do until eof(1)
input #1, sResult, sExpected
if( sResult <> sExpected ) then
#ifdef PRINT_IF_UNEQUAL
print QT(sResult) & " <> " & QT(sExpected)
errcount += 1
#endif
end if
CU_ASSERT_EQUAL( sResult, sExpected )
loop
close #1

if errcount = 0 then
kill TESTFILE
end if
#endmacro


SUITE( fbc_tests.udt_wstring_.print_ )

TEST( numbers )

#macro check( dtype, value )
scope
dim x as ustring = wstr(value)
dim cmp as wstring * BUFFER_SIZE = wstr( value )
WPRINT( x, cmp )
end scope
#endmacro

OPEN_FILE( "print_numbers.txt" )

check( boolean, false )
check( boolean, true )

check( byte, -128 )
check( byte, -0 )
check( byte, 127 )

check( ubyte, 0 )
check( ubyte, 128 )
check( ubyte, 255 )

check( short, -32768 )
check( short, 0 )
check( short, 32767 )

check( ushort, 0 )
check( ushort, 32768 )
check( ushort, 65535 )

check( long, -2147483648ll )
check( long, 0 )
check( long, 2147483647ull )

check( ulong, 0 )
check( ulong, 2147483648ull )
check( ulong, 4294967295ull )

check( longint, (-9223372036854775807ll-1ll) )
check( longint, 0 )
check( longint, 9223372036854775807ull )

check( ulongint, 0 )
check( ulongint, 9223372036854775808ull )
check( ulongint, 18446744073709551615ull )

check( single, -1.5 )
check( single, -1.0 )
check( single, -1.0 )
check( single, -0.5 )
check( single, 0.0 )
check( single, 0.5 )
check( single, 1.0)
check( single, 1.5 )
check( single, 2.0 )
check( single, 2.5 )

check( double, -1.5 )
check( double, -1.0 )
check( double, -1.0 )
check( double, -0.5 )
check( double, 0.0 )
check( double, 0.5 )
check( double, 1.0)
check( double, 1.5 )
check( double, 2.0 )
check( double, 2.5 )

CLOSE_AND_TEST_FILE()

END_TEST

TEST( strings )

#macro check( literal )
scope
dim x as ustring = literal
WPRINT_STR( x, literal )
end scope
#endmacro

OPEN_FILE( "print_strings.txt" )

check( "" )
check( " " )
check( "0123456789" )
check( "ABCDEFGHIJKLMNOP" )
check( !"\u3041\u3043\u3045\u3047\u3049" )

CLOSE_AND_TEST_FILE()

END_TEST

END_SUITE
144 changes: 144 additions & 0 deletions tests/udt-zstring/print.bas
@@ -0,0 +1,144 @@
#include "fbcunit.bi"
#include once "uzstring-fixed.bi"
#include once "chk-zstring.bi"

#define ustring UZSTRING_FIXED

#define PRINT_IF_UNEQUAL

#define BUFFER_SIZE 50

#define QT(s) str("""" & str(s) & """")

#define ZPRINT( value, cmp ) _
print #(1), value & ", " & QT(cmp)

#define ZPRINT_STR( value, cmp ) _
print #(1), QT(value) & ", " & QT(cmp)

#macro OPEN_FILE( fn )
const TESTFILE = ("./udt-zstring/" & fn)

open TESTFILE for output encoding "utf-16" as #1
#endmacro

#macro CLOSE_AND_TEST_FILE()
close #1

dim as zstring * BUFFER_SIZE sResult, sExpected
dim as integer errcount = 0
open TESTFILE for input encoding "utf-16" as #1
do until eof(1)
input #1, sResult, sExpected
if( sResult <> sExpected ) then
#ifdef PRINT_IF_UNEQUAL
print QT(sResult) & " <> " & QT(sExpected)
errcount += 1
#endif
end if
CU_ASSERT_EQUAL( sResult, sExpected )
loop
close #1

if errcount = 0 then
kill TESTFILE
end if
#endmacro


SUITE( fbc_tests.udt_zstring_.print_ )

TEST( numbers )

#macro check( dtype, value )
scope
dim x as ustring = str(value)
dim cmp as zstring * BUFFER_SIZE = str( value )
ZPRINT( x, cmp )
end scope
#endmacro

OPEN_FILE( "print_numbers.txt" )

check( boolean, false )
check( boolean, true )

check( byte, -128 )
check( byte, -0 )
check( byte, 127 )

check( ubyte, 0 )
check( ubyte, 128 )
check( ubyte, 255 )

check( short, -32768 )
check( short, 0 )
check( short, 32767 )

check( ushort, 0 )
check( ushort, 32768 )
check( ushort, 65535 )

check( long, -2147483648ll )
check( long, 0 )
check( long, 2147483647ull )

check( ulong, 0 )
check( ulong, 2147483648ull )
check( ulong, 4294967295ull )

check( longint, (-9223372036854775807ll-1ll) )
check( longint, 0 )
check( longint, 9223372036854775807ull )

check( ulongint, 0 )
check( ulongint, 9223372036854775808ull )
check( ulongint, 18446744073709551615ull )

check( single, -1.5 )
check( single, -1.0 )
check( single, -1.0 )
check( single, -0.5 )
check( single, 0.0 )
check( single, 0.5 )
check( single, 1.0)
check( single, 1.5 )
check( single, 2.0 )
check( single, 2.5 )

check( double, -1.5 )
check( double, -1.0 )
check( double, -1.0 )
check( double, -0.5 )
check( double, 0.0 )
check( double, 0.5 )
check( double, 1.0)
check( double, 1.5 )
check( double, 2.0 )
check( double, 2.5 )

CLOSE_AND_TEST_FILE()

END_TEST

TEST( strings )

#macro check( literal )
scope
dim x as ustring = literal
ZPRINT_STR( x, literal )
end scope
#endmacro

OPEN_FILE( "print_strings.txt" )

check( "" )
check( " " )
check( "0123456789" )
check( "ABCDEFGHIJKLMNOP" )

CLOSE_AND_TEST_FILE()

END_TEST

END_SUITE

0 comments on commit 3368c2f

Please sign in to comment.