Skip to content

Commit 3368c2f

Browse files
committed
udt-wstring: PRINT/LPRINT/WRITE will accept UDT as Z|WSTRING
1 parent 5dcb9e2 commit 3368c2f

File tree

7 files changed

+1096
-0
lines changed

7 files changed

+1096
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Version 1.07.0
2323
- SELECT statement will accept UDT as Z|WSTRING to return a Z|WSTRING
2424
- SWAP statement will accept UDT as Z|WSTRING
2525
- IIF function will accept UDT as Z|WSTRING
26+
- PRINT/LPRINT/WRITE will accept UDT as Z|WSTRING
2627

2728
[fixed]
2829
- sf.net #881: C backend: support for varadic function parameters in gcc using __builtin_va_list type and related macros

src/compiler/rtl-print.bas

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@ function rtlPrint _
690690
f = PROCLOOKUP( PRINTVOID )
691691
end if
692692
else
693+
astTryOvlStringCONV( expr )
694+
693695
'' UDT? try to convert to string with type casting op overloading
694696
select case typeGet( astGetDataType( expr ) )
695697
case FB_DATATYPE_STRUCT, FB_DATATYPE_ENUM
@@ -901,6 +903,8 @@ function rtlWrite _
901903
if( expr = NULL ) then
902904
f = PROCLOOKUP( WRITEVOID )
903905
else
906+
astTryOvlStringCONV( expr )
907+
904908
'' UDT? try to convert to string with type casting op overloading
905909
select case astGetDataType( expr )
906910
case FB_DATATYPE_STRUCT, FB_DATATYPE_ENUM
@@ -1071,6 +1075,8 @@ function rtlPrintUsing _
10711075
exit function
10721076
end if
10731077

1078+
astTryOvlStringCONV( expr )
1079+
10741080
'' UDT? try to convert to double with type casting op overloading
10751081
select case astGetDataType( expr )
10761082
case FB_DATATYPE_STRUCT, FB_DATATYPE_ENUM

tests/udt-wstring/print.bas

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include "fbcunit.bi"
2+
#include once "uwstring-fixed.bi"
3+
#include once "chk-wstring.bi"
4+
5+
#define ustring UWSTRING_FIXED
6+
7+
#define PRINT_IF_UNEQUAL
8+
9+
#define BUFFER_SIZE 50
10+
11+
#define QT(s) wstr("""" & wstr(s) & """")
12+
13+
#define WPRINT( value, cmp ) _
14+
print #(1), value & ", " & QT(cmp)
15+
16+
#define WPRINT_STR( value, cmp ) _
17+
print #(1), QT(value) & ", " & QT(cmp)
18+
19+
#macro OPEN_FILE( fn )
20+
const TESTFILE = ("./udt-wstring/" & fn)
21+
22+
open TESTFILE for output encoding "utf-16" as #1
23+
#endmacro
24+
25+
#macro CLOSE_AND_TEST_FILE()
26+
close #1
27+
28+
dim as wstring * BUFFER_SIZE sResult, sExpected
29+
dim as integer errcount = 0
30+
open TESTFILE for input encoding "utf-16" as #1
31+
do until eof(1)
32+
input #1, sResult, sExpected
33+
if( sResult <> sExpected ) then
34+
#ifdef PRINT_IF_UNEQUAL
35+
print QT(sResult) & " <> " & QT(sExpected)
36+
errcount += 1
37+
#endif
38+
end if
39+
CU_ASSERT_EQUAL( sResult, sExpected )
40+
loop
41+
close #1
42+
43+
if errcount = 0 then
44+
kill TESTFILE
45+
end if
46+
#endmacro
47+
48+
49+
SUITE( fbc_tests.udt_wstring_.print_ )
50+
51+
TEST( numbers )
52+
53+
#macro check( dtype, value )
54+
scope
55+
dim x as ustring = wstr(value)
56+
dim cmp as wstring * BUFFER_SIZE = wstr( value )
57+
WPRINT( x, cmp )
58+
end scope
59+
#endmacro
60+
61+
OPEN_FILE( "print_numbers.txt" )
62+
63+
check( boolean, false )
64+
check( boolean, true )
65+
66+
check( byte, -128 )
67+
check( byte, -0 )
68+
check( byte, 127 )
69+
70+
check( ubyte, 0 )
71+
check( ubyte, 128 )
72+
check( ubyte, 255 )
73+
74+
check( short, -32768 )
75+
check( short, 0 )
76+
check( short, 32767 )
77+
78+
check( ushort, 0 )
79+
check( ushort, 32768 )
80+
check( ushort, 65535 )
81+
82+
check( long, -2147483648ll )
83+
check( long, 0 )
84+
check( long, 2147483647ull )
85+
86+
check( ulong, 0 )
87+
check( ulong, 2147483648ull )
88+
check( ulong, 4294967295ull )
89+
90+
check( longint, (-9223372036854775807ll-1ll) )
91+
check( longint, 0 )
92+
check( longint, 9223372036854775807ull )
93+
94+
check( ulongint, 0 )
95+
check( ulongint, 9223372036854775808ull )
96+
check( ulongint, 18446744073709551615ull )
97+
98+
check( single, -1.5 )
99+
check( single, -1.0 )
100+
check( single, -1.0 )
101+
check( single, -0.5 )
102+
check( single, 0.0 )
103+
check( single, 0.5 )
104+
check( single, 1.0)
105+
check( single, 1.5 )
106+
check( single, 2.0 )
107+
check( single, 2.5 )
108+
109+
check( double, -1.5 )
110+
check( double, -1.0 )
111+
check( double, -1.0 )
112+
check( double, -0.5 )
113+
check( double, 0.0 )
114+
check( double, 0.5 )
115+
check( double, 1.0)
116+
check( double, 1.5 )
117+
check( double, 2.0 )
118+
check( double, 2.5 )
119+
120+
CLOSE_AND_TEST_FILE()
121+
122+
END_TEST
123+
124+
TEST( strings )
125+
126+
#macro check( literal )
127+
scope
128+
dim x as ustring = literal
129+
WPRINT_STR( x, literal )
130+
end scope
131+
#endmacro
132+
133+
OPEN_FILE( "print_strings.txt" )
134+
135+
check( "" )
136+
check( " " )
137+
check( "0123456789" )
138+
check( "ABCDEFGHIJKLMNOP" )
139+
check( !"\u3041\u3043\u3045\u3047\u3049" )
140+
141+
CLOSE_AND_TEST_FILE()
142+
143+
END_TEST
144+
145+
END_SUITE

tests/udt-zstring/print.bas

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#include "fbcunit.bi"
2+
#include once "uzstring-fixed.bi"
3+
#include once "chk-zstring.bi"
4+
5+
#define ustring UZSTRING_FIXED
6+
7+
#define PRINT_IF_UNEQUAL
8+
9+
#define BUFFER_SIZE 50
10+
11+
#define QT(s) str("""" & str(s) & """")
12+
13+
#define ZPRINT( value, cmp ) _
14+
print #(1), value & ", " & QT(cmp)
15+
16+
#define ZPRINT_STR( value, cmp ) _
17+
print #(1), QT(value) & ", " & QT(cmp)
18+
19+
#macro OPEN_FILE( fn )
20+
const TESTFILE = ("./udt-zstring/" & fn)
21+
22+
open TESTFILE for output encoding "utf-16" as #1
23+
#endmacro
24+
25+
#macro CLOSE_AND_TEST_FILE()
26+
close #1
27+
28+
dim as zstring * BUFFER_SIZE sResult, sExpected
29+
dim as integer errcount = 0
30+
open TESTFILE for input encoding "utf-16" as #1
31+
do until eof(1)
32+
input #1, sResult, sExpected
33+
if( sResult <> sExpected ) then
34+
#ifdef PRINT_IF_UNEQUAL
35+
print QT(sResult) & " <> " & QT(sExpected)
36+
errcount += 1
37+
#endif
38+
end if
39+
CU_ASSERT_EQUAL( sResult, sExpected )
40+
loop
41+
close #1
42+
43+
if errcount = 0 then
44+
kill TESTFILE
45+
end if
46+
#endmacro
47+
48+
49+
SUITE( fbc_tests.udt_zstring_.print_ )
50+
51+
TEST( numbers )
52+
53+
#macro check( dtype, value )
54+
scope
55+
dim x as ustring = str(value)
56+
dim cmp as zstring * BUFFER_SIZE = str( value )
57+
ZPRINT( x, cmp )
58+
end scope
59+
#endmacro
60+
61+
OPEN_FILE( "print_numbers.txt" )
62+
63+
check( boolean, false )
64+
check( boolean, true )
65+
66+
check( byte, -128 )
67+
check( byte, -0 )
68+
check( byte, 127 )
69+
70+
check( ubyte, 0 )
71+
check( ubyte, 128 )
72+
check( ubyte, 255 )
73+
74+
check( short, -32768 )
75+
check( short, 0 )
76+
check( short, 32767 )
77+
78+
check( ushort, 0 )
79+
check( ushort, 32768 )
80+
check( ushort, 65535 )
81+
82+
check( long, -2147483648ll )
83+
check( long, 0 )
84+
check( long, 2147483647ull )
85+
86+
check( ulong, 0 )
87+
check( ulong, 2147483648ull )
88+
check( ulong, 4294967295ull )
89+
90+
check( longint, (-9223372036854775807ll-1ll) )
91+
check( longint, 0 )
92+
check( longint, 9223372036854775807ull )
93+
94+
check( ulongint, 0 )
95+
check( ulongint, 9223372036854775808ull )
96+
check( ulongint, 18446744073709551615ull )
97+
98+
check( single, -1.5 )
99+
check( single, -1.0 )
100+
check( single, -1.0 )
101+
check( single, -0.5 )
102+
check( single, 0.0 )
103+
check( single, 0.5 )
104+
check( single, 1.0)
105+
check( single, 1.5 )
106+
check( single, 2.0 )
107+
check( single, 2.5 )
108+
109+
check( double, -1.5 )
110+
check( double, -1.0 )
111+
check( double, -1.0 )
112+
check( double, -0.5 )
113+
check( double, 0.0 )
114+
check( double, 0.5 )
115+
check( double, 1.0)
116+
check( double, 1.5 )
117+
check( double, 2.0 )
118+
check( double, 2.5 )
119+
120+
CLOSE_AND_TEST_FILE()
121+
122+
END_TEST
123+
124+
TEST( strings )
125+
126+
#macro check( literal )
127+
scope
128+
dim x as ustring = literal
129+
ZPRINT_STR( x, literal )
130+
end scope
131+
#endmacro
132+
133+
OPEN_FILE( "print_strings.txt" )
134+
135+
check( "" )
136+
check( " " )
137+
check( "0123456789" )
138+
check( "ABCDEFGHIJKLMNOP" )
139+
140+
CLOSE_AND_TEST_FILE()
141+
142+
END_TEST
143+
144+
END_SUITE

0 commit comments

Comments
 (0)