Microsoft Small Basic

Program Listing: GTZ663
' Math.Abs() Test
' Copyright © 2017 Nonki Takahashi. The MIT License.

Init()
Test()

Sub Init
LF = Text.GetCharacter(10)
testcase[1] = "number=-1.7976931348623157E+308;abs=1.7976931348623157E+308;"
testcase[2] = "number=-8E28;abs=8E28;"
testcase[3] = "number=-79228162514264337593543950336;abs=79228162514264337593543950336;"
'Unhandled Exception: System.OverflowException: Value was either too large or too
' small for a Decimal.
' at System.Decimal..ctor(Double value)
' at Microsoft.SmallBasic.Library.Math.DoubleToDecimal(Double number)
' at _SmallBasicProgram.test()
' at _SmallBasicProgram._Main()
'testcase[4] = "number=-79228162514264337593543950335;abs=79228162514264337593543950335;"
'testcase[5] = "number=-79228162514264333195497438208;abs=79228162514264333195497438208;"
testcase[6] = "number=-79228162514264333195497438207;abs=79228162514264333195497438207;"
testcase[7] = "number=-79228162514264333100000000000;abs=79228162514264333100000000000;"
testcase[8] = "number=-79228162514264300000000000001;abs=79228162514264300000000000001;"
testcase[9] = "number=-79228162514264300000000000000;abs=79228162514264300000000000000;"
testcase[10] = "number=-7E28;abs=7E28;"
testcase[11] = "number=-1.2;abs=1.2;"
testcase[12] = "number=-1E-28;abs=1E-28;"
testcase[13] = "number=-1E-29;abs=1E-29;"
testcase[14] = "number=0;abs=0;"
testcase[15] = "number=1E-29;abs=1E-29;"
testcase[16] = "number=1E-28;abs=1E-28;"
testcase[17] = "number=1.2;abs=1.2;"
testcase[18] = "number=7E28;abs=7E28;"
testcase[19] = "number=79228162514264300000000000000;abs=79228162514264300000000000000;"
testcase[20] = "number=79228162514264300000000000001;abs=79228162514264300000000000001;"
testcase[21] = "number=79228162514264333100000000000;abs=79228162514264333100000000000;"
testcase[22] = "number=79228162514264333195497438207;abs=79228162514264333195497438207;"
'Unhandled Exception: System.OverflowException: Value was either too large or too
' small for a Decimal.
' at System.Decimal..ctor(Double value)
' at Microsoft.SmallBasic.Library.Math.DoubleToDecimal(Double number)
' at _SmallBasicProgram.test()
' at _SmallBasicProgram._Main()
'testcase[23] = "number=79228162514264333195497438208;abs=79228162514264333195497438208;"
'testcase[24] = "number=79228162514264337593543950335;abs=79228162514264337593543950335;"
testcase[25] = "number=79228162514264337593543950336;abs=79228162514264337593543950336;"
testcase[26] = "number=8E28;abs=8E28;"
testcase[27] = "number=1.7976931348623157E+308;abs=1.7976931348623157E+308;"
nTest = Array.GetItemCount(testcase)
index = Array.GetAllIndices(testcase)
EndSub

Sub Test
TextWindow.WriteLine("Math.Abs() Test")
TextWindow.WriteLine("exp:expected, res:result" + LF)
For i = 1 To nTest
If index[i] < 10 Then
TextWindow.Write(" ")
EndIf
TextWindow.Write(index[i])
case = testcase[index[i]]
abs = Math.Abs(case["number"])
If abs = case["abs"] Then
TextWindow.ForegroundColor = "Green"
TextWindow.Write(" PASS ")
nPass = nPass + 1
Else
TextWindow.ForegroundColor = "Red"
TextWindow.Write(" FAIL ")
EndIf
TextWindow.ForegroundColor = "Gray"
TextWindow.Write("|" + case["number"] + "| ")
TextWindow.Write("exp=" + case["abs"] + " ")
TextWindow.Write("res=" + abs)
TextWindow.WriteLine("")
EndFor
TextWindow.WriteLine(LF + "Pass: " + nPass + "/" + nTest)
EndSub