MyFractionAlgebra - UnitTest
the following script shows how class
JxnUnitTest
is used to test class
MyFractionAlgebra
= #JxnPortable/docs/programmer_examples/MyFractionAlgebra_UnitTest.jxn
ut = @JxnUnitTest();
!
a = @MyFractionAlgebra( 3, 4 )
= 3 / 4 = 0.75 (MyFractionAlgebra)
b = @MyFractionAlgebra( 2, 5 )
= 2 / 5 = 0.4 (MyFractionAlgebra)
!
!
! assertEquals( expected, actual ):
!
ut.assertEquals( 0.75, a.doubleValue() )
= ok (java.lang.String)
ut.assertEquals( 0.4, b.doubleValue() )
= ok (java.lang.String)
!
!
! fraction arithmetic:
!
c = a + b
= 23 / 20 = 1.15 (MyFractionAlgebra)
ut.assertEquals( 23, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 20, c.getDenominator() )
= ok (java.lang.String)
!
c = a - b
= 7 / 20 = 0.35 (MyFractionAlgebra)
ut.assertEquals( 7, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 20, c.getDenominator() )
= ok (java.lang.String)
!
c = a b
= 3 / 10 = 0.3 (MyFractionAlgebra)
ut.assertEquals( 3, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 10, c.getDenominator() )
= ok (java.lang.String)
!
c = a / b
= 15 / 8 = 1.875 (MyFractionAlgebra)
ut.assertEquals( 15, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 8, c.getDenominator() )
= ok (java.lang.String)
!
!
! mixed arithmetic with int or double:
!
c = a + 1
= 7 / 4 = 1.75 (MyFractionAlgebra)
ut.assertEquals( 7, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 4, c.getDenominator() )
= ok (java.lang.String)
!
c = 1. - b
= 3 / 5 = 0.6 (MyFractionAlgebra)
ut.assertEquals( 3, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 5, c.getDenominator() )
= ok (java.lang.String)
!
c = 2 a
= 3 / 2 = 1.5 (MyFractionAlgebra)
ut.assertEquals( 3, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 2, c.getDenominator() )
= ok (java.lang.String)
!
c = a 2.
= 3 / 2 = 1.5 (MyFractionAlgebra)
ut.assertEquals( 3, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 2, c.getDenominator() )
= ok (java.lang.String)
!
c = 2.5 / b
= 25 / 4 = 6.25 (MyFractionAlgebra)
ut.assertEquals( 25, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 4, c.getDenominator() )
= ok (java.lang.String)
!
c = b / 2
= 1 / 5 = 0.2 (MyFractionAlgebra)
ut.assertEquals( 1, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 5, c.getDenominator() )
= ok (java.lang.String)
!
!
! power of fraction:
!
ut.assertEquals( 1., ( a^0 ).doubleValue() )
= ok (java.lang.String)
!
ut.assertEquals( a, a^1 )
= ok (java.lang.String)
!
c = a^3
= 27 / 64 = 0.421875 (MyFractionAlgebra)
ut.assertEquals( 27, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 64, c.getDenominator() )
= ok (java.lang.String)
!
c = b^-2
= 25 / 4 = 6.25 (MyFractionAlgebra)
ut.assertEquals( 25, c.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 4, c.getDenominator() )
= ok (java.lang.String)
!
! ut.assertEquals( a^2, a^2. ) ! automatic cast to int no more supported
expected = "potenz: MyFractionAlgebra^double not supported";
ut.assertException( expected, $this.test "a^2." )
: a^2.
? ^
? potenz: MyFractionAlgebra^double not supported
= ok (expected exception) (java.lang.String)
ut.assertException( expected, $this.test "a^2.5" )
: a^2.5
? ^
? potenz: MyFractionAlgebra^double not supported
= ok (expected exception) (java.lang.String)
!
! expected = "MyFractionAlgebra.pow( int ): KmgFormelException: integer overflow ...";
! ut.assertException( expected, $this.test "a^20" )
c = a^20 ! the result is an approximation, as it can not be represented as an exact fraction with int numerator and denominator
= 0.0031712119389339932 ~ 3305159 / 1042238445 (MyFractionAlgebra)
ut.assertEquals( a.doubleValue()^20, c.doubleValue() ) ! nevertheless it is equal to the expected double value
= ok (java.lang.String)
!
!
! determine fraction for a double:
!
pi = @MyFractionAlgebra( PI ) ! approximation as fraction
= 3.141592653589793 ~ 817696623 / 260280919 (MyFractionAlgebra)
ut.assertEquals( PI, pi.doubleValue() )
= ok (java.lang.String)
!
#function $test( n )
sqrt_n = @MyFractionAlgebra sqrt n ! approximation as fraction
ut.assertEquals( @MyFractionAlgebra n, sqrt_n * sqrt_n )
#endfunction
!
JxnLoop.repeat( $this, "$n", 10000, "$test $n;" )
= ok (java.lang.String)
!
!
! mixed array arithmectic:
!
ia = { 2, 3, 5 }
= { 2, 3, 5 } (int[3])
!
ca = ia - b
= { 8 / 5 = 1.6, 13 / 5 = 2.6, 23 / 5 = 4.6 } (MyFractionAlgebra[3])
ut.assertEquals( { 8, 13, 23 }, ca.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 5, @JxnUnroll ca.getDenominator() )
= { ok, ok, ok } (java.lang.String[3])
!
ca = a / ia
= { 3 / 8 = 0.375, 1 / 4 = 0.25, 3 / 20 = 0.15 } (MyFractionAlgebra[3])
ut.assertEquals( { 3, 1, 3 }, ca.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( { 8, 4, 20 }, ca.getDenominator() )
= ok (java.lang.String)
!
!
! check whether inputs a and b have been modified:
!
ut.assertEquals( 3, a.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 4, a.getDenominator() )
= ok (java.lang.String)
ut.assertEquals( 2, b.getNumerator() )
= ok (java.lang.String)
ut.assertEquals( 5, b.getDenominator() )
= ok (java.lang.String)
!
ut.summary()
= ok (10043 tests passed) (java.lang.String)
JxnUnitTestFailureExpected.summaryAll( true )
= ok (10043 tests in 1 instance(s) passed) (java.lang.String)