Skip to content

Commit

Permalink
udt-wstring: IIF function will accept UDT as Z|WSTRING
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrm committed Jun 16, 2019
1 parent d9a09d7 commit ff66fc1
Show file tree
Hide file tree
Showing 5 changed files with 511 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Version 1.07.0
- STR/WSTR function will accept UDT as Z|WSTRING to return a Z|WSTRING
- 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

[fixed]
- sf.net #881: C backend: support for varadic function parameters in gcc using __builtin_va_list type and related macros
Expand Down
29 changes: 29 additions & 0 deletions src/compiler/ast-node-iif.bas
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,35 @@ function astNewIIF _
dtype = FB_DATATYPE_INVALID
subtype = NULL

'' Maybe UDT extends Z|WSTRING? Check for string conversions...
if( truexpr->dtype <> falsexpr->dtype ) then
if( truexpr->dtype = FB_DATATYPE_STRUCT ) then
if( symbGetUdtIsZstring( truexpr->subtype ) ) then
if( falsexpr->dtype = FB_DATATYPE_CHAR ) then
astTryOvlStringCONV( truexpr )
truexpr->dtype = astGetDataType( truexpr )
end if
elseif( symbGetUdtIsWstring( truexpr->subtype ) ) then
if( falsexpr->dtype = FB_DATATYPE_WCHAR ) then
astTryOvlStringCONV( truexpr )
truexpr->dtype = astGetDataType( truexpr )
end if
end if
elseif( falsexpr->dtype = FB_DATATYPE_STRUCT ) then
if( symbGetUdtIsZstring( falsexpr->subtype ) ) then
if( truexpr->dtype = FB_DATATYPE_CHAR ) then
astTryOvlStringCONV( falsexpr )
falsexpr->dtype = astGetDataType( falsexpr )
end if
elseif( symbGetUdtIsWstring( falsexpr->subtype ) ) then
if( truexpr->dtype = FB_DATATYPE_WCHAR ) then
astTryOvlStringCONV( falsexpr )
falsexpr->dtype = astGetDataType( falsexpr )
end if
end if
end if
end if

'' check types & find the iif() result type
if( hCheckTypes( truexpr->dtype, truexpr->subtype, _
falsexpr->dtype, falsexpr->subtype, _
Expand Down
215 changes: 215 additions & 0 deletions tests/udt-wstring/iif.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#include "fbcunit.bi"
#include once "uwstring-fixed.bi"
#include once "chk-wstring.bi"

#define ustring UWSTRING_FIXED

SUITE( fbc_tests.udt_wstring_.iif_ )

#macro check_wstring( expr, true_expr, false_expr )
scope
dim t1 as wstring * 50 = true_expr
dim t2 as wstring * 50 = false_expr

dim u1 as ustring = true_expr
dim u2 as ustring = false_expr

'' WSTRING = iif( expr, LITERAL1, LITERAL2 )
scope
dim a as wstring * 50 = iif( expr, true_expr, false_expr )
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( a, t1 )
else
CU_ASSERT_WSTRING_EQUAL( a, t2 )
endif
end scope

'' WSTRING = iif( expr, WSTRING1, LITERAL2 )
scope
dim a as wstring * 50 = iif( expr, t1, false_expr )
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( a, t1 )
else
CU_ASSERT_WSTRING_EQUAL( a, t2 )
endif
end scope

'' WSTRING = iif( expr, LITERAL1, WSTRING2 )
scope
dim a as wstring * 50 = iif( expr, true_expr, t2 )
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( a, t1 )
else
CU_ASSERT_WSTRING_EQUAL( a, t2 )
endif
end scope

'' WSTRING = iif( expr, WSTRING1, WSTRING2 )
scope
dim a as wstring * 50 = iif( expr, t1, t2 )
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( a, t1 )
else
CU_ASSERT_WSTRING_EQUAL( a, t2 )
endif
end scope

'' WSTRING = iif( expr, USTRING1, LITERAL2 )
scope
dim a as wstring * 50 = iif( expr, u1, false_expr )
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( a, t1 )
else
CU_ASSERT_WSTRING_EQUAL( a, t2 )
endif
end scope

'' WSTRING = iif( expr, LITERAL1, USTRING2 )
scope
dim a as wstring * 50 = iif( expr, true_expr, u2 )
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( a, t1 )
else
CU_ASSERT_WSTRING_EQUAL( a, t2 )
endif
end scope

'' WSTRING = iif( expr, USTRING1, USTRING2 )
scope
dim a as wstring * 50 = iif( expr, u1, u2 )
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( a, t1 )
else
CU_ASSERT_WSTRING_EQUAL( a, t2 )
endif
end scope
end scope
#endmacro

#macro check_ustring( expr, true_expr, false_expr )
scope
dim t1 as wstring * 50 = true_expr
dim t2 as wstring * 50 = false_expr

dim u1 as ustring = true_expr
dim u2 as ustring = false_expr

'' USTRING = iif( expr, LITERAL1, LITERAL2 )
scope
dim a as ustring = iif( expr, true_expr, false_expr )
dim r as wstring * 50 = a
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( r, t1 )
else
CU_ASSERT_WSTRING_EQUAL( r, t2 )
endif
end scope

'' USTRING = iif( expr, WSTRING1, LITERAL2 )
scope
dim a as ustring = iif( expr, t1, false_expr )
dim r as wstring * 50 = a
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( r, t1 )
else
CU_ASSERT_WSTRING_EQUAL( r, t2 )
endif
end scope

'' USTRING = iif( expr, LITERAL1, WSTRING2 )
scope
dim a as ustring = iif( expr, true_expr, t2 )
dim r as wstring * 50 = a
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( r, t1 )
else
CU_ASSERT_WSTRING_EQUAL( r, t2 )
endif
end scope

'' USTRING = iif( expr, WSTRING1, WSTRING2 )
scope
dim a as ustring = iif( expr, t1, t2 )
dim r as wstring * 50 = a
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( r, t1 )
else
CU_ASSERT_WSTRING_EQUAL( r, t2 )
endif
end scope

'' USTRING = iif( expr, USTRING1, LITERAL2 )
scope
dim a as ustring = iif( expr, u1, false_expr )
dim r as wstring * 50 = a
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( r, t1 )
else
CU_ASSERT_WSTRING_EQUAL( r, t2 )
endif
end scope

'' USTRING = iif( expr, LITERAL1, USTRING2 )
scope
dim a as ustring = iif( expr, true_expr, u2 )
dim r as wstring * 50 = a
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( r, t1 )
else
CU_ASSERT_WSTRING_EQUAL( r, t2 )
endif
end scope

'' USTRING = iif( expr, USTRING1, USTRING2 )
scope
dim a as ustring = iif( expr, u1, u2 )
dim r as wstring * 50 = a
if( expr ) then
CU_ASSERT_WSTRING_EQUAL( r, t1 )
else
CU_ASSERT_WSTRING_EQUAL( r, t2 )
endif
end scope

end scope
#endmacro

TEST( wstring_iif )

check_wstring( 0, "", "a" )
check_wstring( -1, "", "a" )

check_wstring( 0, "a", "xyz" )
check_wstring( -1, "a", "xyz" )

check_wstring( 0, !"\u3041\u3043\u3045", "a" )
check_wstring( -1, !"\u3041\u3043\u3045", "a" )

check_wstring( 0, "a", !"\u3041\u3043\u3045" )
check_wstring( -1, "a", !"\u3041\u3043\u3045" )

check_wstring( 0, !"\u3047", !"\u3041\u3043\u3045" )
check_wstring( -1, !"\u3047", !"\u3041\u3043\u3045" )

END_TEST

TEST( ustring_iif )

check_ustring( 0, "", "a" )
check_ustring( -1, "", "a" )

check_ustring( 0, "a", "xyz" )
check_ustring( -1, "a", "xyz" )

check_ustring( 0, !"\u3041\u3043\u3045", "a" )
check_ustring( -1, !"\u3041\u3043\u3045", "a" )

check_ustring( 0, "a", !"\u3041\u3043\u3045" )
check_ustring( -1, "a", !"\u3041\u3043\u3045" )

check_ustring( 0, !"\u3047", !"\u3041\u3043\u3045" )
check_ustring( -1, !"\u3047", !"\u3041\u3043\u3045" )

END_TEST

END_SUITE
Loading

0 comments on commit ff66fc1

Please sign in to comment.