= #JxnSafePrimitiveParameterCast_Demo.jxn

ut = @JxnUnitTest();
ut_ok = @JxnUnitTest();


i4 = 3
di = 3.   ! integer
df = 3.5  ! fractional
i8in = JxnCast.toLong Integer.MAX_VALUE
i8ex = JxnCast.toLong Integer.MAX_VALUE + 1


#function $test( $1 )
JxnPrimitiveWrapper.setPluggable $1
JxnPrimitiveWrapper.getParameterCast()

ut.assertEquals( "int:3",           JxnParameterCheck.whatIs i4 )
ut.assertEquals( "double:3.0",      JxnParameterCheck.whatIs di )
ut.assertEquals( "double:3.5",      JxnParameterCheck.whatIs df )
ut.assertEquals( "long:2147483647", JxnParameterCheck.whatIs i8in )
ut.assertEquals( "long:2147483648", JxnParameterCheck.whatIs i8ex )
!
! upcast alway done (as in Java):
ut.assertEquals( "long:3",     JxnParameterCheck.testLong   i4 )
ut.assertEquals( "double:3.0", JxnParameterCheck.testDouble i4 )
!
! no implicit downcast from double to int:
$exp = "invokeMethod: JxnParameterCheck.testInt( double ) undefined, ...";
ut_ok.assertException( $exp, $this.test "JxnParameterCheck.testInt di" )
ut_ok.assertException( $exp, $this.test "JxnParameterCheck.testInt df" )
!
$exp = "invokeMethod: JxnParameterCheck.testInt( long ) undefined, ...";
#if $1.equals "Safe"
! Safe: controlled implicit downcast long to int
ut.assertEquals( "int:2147483647", JxnParameterCheck.testInt i8in )  ! i8in fits into int
#else  ! Java
! Java: no implicit downcast long to int
ut_ok.assertException( $exp, $this.test "JxnParameterCheck.testInt i8in" )
#endif
ut_ok.assertException( $exp, $this.test "JxnParameterCheck.testInt i8ex" )  ! i8ex does not fit into int
!
! explicit downcast:
ut.assertEquals(    "int:3", JxnParameterCheck.testInt di.intValue() )
ut_ok.assertEquals( "int:3", JxnParameterCheck.testInt df.intValue() )  ! => loss of precision
ut.assertEquals(    "int:2147483647",  JxnParameterCheck.testInt i8in.intValue() )
ut_ok.assertEquals( "int:-2147483648", JxnParameterCheck.testInt i8ex.intValue() )  ! => overflow
! or equivalent:
ut.assertEquals(    "int:3", JxnParameterCheck.testInt JxnCast.toInt di )
ut_ok.assertEquals( "int:3", JxnParameterCheck.testInt JxnCast.toInt df )  ! => loss of precision
ut.assertEquals(    "int:2147483647",  JxnParameterCheck.testInt JxnCast.toInt i8in )
ut_ok.assertEquals( "int:-2147483648", JxnParameterCheck.testInt JxnCast.toInt i8ex )  ! => overflow 
!
#return
#endfunction
$test.setDebug 1


$test "Safe"
$test "Java"
JxnPrimitiveWrapper.setPluggable ""


ut.summary()
ut_ok.summary()
JxnUnitTest.summaryAll( true )