= #JxnPortable/docs/programmer_examples/demos/JxnSafePrimitiveArithmetic_Demo_UnitTest.jxn

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

#import static JxnParameterCheck  ! for whatIs


JxnPrimitiveWrapper.setPluggable "Safe"  ! use jxn safe arithmetic
JxnPrimitiveWrapper.getArithmetic()
!
ut.assertEquals( "int:-2147483648", whatIs( Integer.MIN_VALUE ) )
ut.assertEquals( "int:2147483647",  whatIs( Integer.MAX_VALUE ) )
!
ut.assertEquals( "long:-2147483649", whatIs( Integer.MIN_VALUE - 1 ) )
ut.assertEquals( "long:2147483648",  whatIs( -Integer.MIN_VALUE ) )
ut.assertEquals( "long:2147483648",  whatIs( Integer.MAX_VALUE + 1 ) )
!
ut.assertEquals( "int:1073741824",  whatIs( 2^30 ) )
ut.assertEquals( "long:2147483648", whatIs( 2^31 ) )
ut.assertEquals( "long:4611686018427387904",    whatIs( 2^62 ) )
ut.assertEquals( "double:9.223372036854776E18", whatIs( 2^63 ) )
!
ut.assertEquals( "int:3",       whatIs( 12/4 ) )
ut.assertEquals( "double:3.75", whatIs( 15/4 ) )  ! type conversion for non integer result
!
ut.assertEquals( "boolean:false", whatIs( -true ) )
ut.assertEquals( "boolean:true",  whatIs( -false ) )
!
ut.assertEquals( "boolean:false", whatIs( false + false ) )
ut.assertEquals( "boolean:true",  whatIs( false + true ) )
ut.assertEquals( "boolean:true",  whatIs(  true + false ) )
ut.assertEquals( "boolean:true",  whatIs(  true + true ) )
!
ut.assertEquals( "boolean:false", whatIs( false * false ) )
ut.assertEquals( "boolean:false", whatIs( false * true ) )
ut.assertEquals( "boolean:false", whatIs(  true * false ) )
ut.assertEquals( "boolean:true",  whatIs(  true * true ) )
!
ut.assertEquals( "boolean:false", whatIs( false ^ false ) )
ut.assertEquals( "boolean:true",  whatIs( false ^ true ) )
ut.assertEquals( "boolean:true",  whatIs(  true ^ false ) )
ut.assertEquals( "boolean:false", whatIs(  true ^ true ) )


JxnPrimitiveWrapper.setPluggable "Java"  ! switch to java arithmetic (overflow possible)
JxnPrimitiveWrapper.getArithmetic()
!
ut.assertEquals( "int:-2147483648", whatIs( Integer.MIN_VALUE ) )
ut.assertEquals( "int:2147483647",  whatIs( Integer.MAX_VALUE ) )
!
ut_ok.assertEquals( "int:2147483647",  whatIs( Integer.MIN_VALUE - 1 ) )
ut_ok.assertEquals( "int:-2147483648", whatIs( -Integer.MIN_VALUE ) )
ut_ok.assertEquals( "int:-2147483648", whatIs( Integer.MAX_VALUE + 1 ) )
!
ut.assertEquals( "double:1.073741824E9", whatIs( 2^30 ) )
ut.assertEquals( "double:2.147483648E9", whatIs( 2^31 ) )
ut.assertEquals( "double:4.6116860184273879E18", whatIs( 2^62 ) )
ut.assertEquals( "double:9.223372036854776E18",  whatIs( 2^63 ) )
!
ut.assertEquals(    "int:3", whatIs( 12/4 ) )
ut_ok.assertEquals( "int:3", whatIs( 15/4 ) )  ! integer result with decimals truncated => loss of presision
!
$exp = "potenz: - boolean not supported";
ut_ok.assertException( $exp, $this.test "-true" )
ut_ok.assertException( $exp, $this.test "-false" )
ut_ok.assertException( "expression: boolean + boolean not supported", $this.test "true + false" )
ut_ok.assertException( "term: boolean * boolean not supported", $this.test "true * false" )
ut_ok.assertException( "JxnJavaPrimitiveArithmetic.pow: boolean ^ boolean not supported", $this.test "true ^ false" )


JxnPrimitiveWrapper.setPluggable ""  ! restore default (safe)

#remove static JxnParameterCheck

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