UnitTests addPrototype: #Numeric derivedFrom: {TestCase}. "100 factorial as determined by bc(1)" UnitTests Numeric addSlot: #bigInt valued: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000. UnitTests Numeric addSlot: #smallInt valued: 100. UnitTests Numeric addSlot: #complexZero valued: (Complex zero). UnitTests Numeric addSlot: #complexOne valued: (1 plusImaginary: 1). UnitTests Numeric addSlot: #complexNegativeOne valued: (-1 plusImaginary: -1). UnitTests Numeric addSlot: #complexFraction valued: ((1 / 2) plusImaginary: (1 / 3)). UnitTests Numeric addSlot: #complexFloat valued: (2.5 plusImaginary: 0.5). UnitTests Numeric addSlot: #complexTriangle valued: (4 plusImaginary: 3). "Tests involving Floats use numbers which can be represented as binary fractions to avoid precision issues. This isn't a problem since here we're testing coercions, not arithmetic." t@(UnitTests Numeric traits) testIsZero [ t assert: 0 isZero. t assert: 0.0 isZero. t assert: (0 / 1) isZero. t deny: 1 isZero. t deny: -1 isZero. t deny: t bigInt isZero. t deny: (1 / 2) isZero. t assert: t complexZero isZero. t deny: t complexOne isZero. t deny: t complexNegativeOne isZero. t deny: t complexTriangle isZero. ]. t@(UnitTests Numeric traits) testIsPositive [ t assert: t smallInt isPositive. t assert: t bigInt isPositive. t assert: (5 / 10) isPositive. t assert: (-5 / -10) isPositive. t assert: 0.1 isPositive. t deny: 0 isPositive. t deny: t smallInt negated isPositive. t deny: t bigInt negated isPositive. t deny: -0.1 isPositive. t deny: (-5 / 10) isPositive. t deny: (5 / -10) isPositive. "Is #isPositive meaningful for complex numbers?" ]. t@(UnitTests Numeric traits) testIsNegative [ t deny: t smallInt isNegative. t deny: t bigInt isNegative. t deny: (5 / 10) isNegative. t deny: (-5 / -10) isNegative. t deny: 0.1 isNegative. t deny: 0 isNegative. t assert: -0.1 isNegative. t assert: t smallInt negated isNegative. t assert: t bigInt negated isNegative. t assert: (-5 / 10) isNegative. "Is #isNegative meaningful for complex numbers?" ]. "Integer arithmetic tests." t@(UnitTests Numeric traits) testIntegerAddition "Test addition of Numbers to Integers" [ t assert: 2 + 2 = 4. t assert: 2 + (1 / 2) = (5 / 2). t assert: 2 + 2.1 = 4.1. t assert: 2 + t complexZero = 2. t assert: 2 + t complexOne = (3 plusImaginary: 1). t assert: 2 + t complexFraction = ((5 / 2) plusImaginary: (1 / 3)). t assert: 2 + t complexFloat = (4.5 plusImaginary: 0.5). ]. t@(UnitTests Numeric traits) testIntegerSubtraction "Test subtraction of Numbers from Integers" [ t assert: 4 - 2 = 2. t assert: 2 - (1 / 2) = (3 / 2). t assert: 2 - 2.5 = -0.5. t assert: 2 - t complexZero = (2 plusImaginary: 0). t assert: 2 - t complexOne = (1 plusImaginary: -1). t assert: 2 - t complexFraction = ((3 / 2) plusImaginary: (-1 / 3)). t assert: 2 - t complexFloat = (-0.5 plusImaginary: -0.5). ]. t@(UnitTests Numeric traits) testIntegerMultiplication [ t assert: 2 * 2 = 4. t assert: 2 * (3 / 4) = (3 / 2). t assert: 2 * 2.5 = 5.0. t assert: 2 * -2 = -4. t assert: 2 * (-3 / 4) = (-3 / 2). t assert: 2 * -2.5 = -5.0. t assert: -2 * 2 = -4. t assert: -2 * (3 / 4) = (-3 / 2). t assert: -2 * 2.5 = -5.0. t assert: -2 * -2 = 4. t assert: -2 * (-3 / 4) = (3 / 2). t assert: -2 * -2.5 = 5.0. t assert: 2 * 0.0 = 0.0. t assert: -2 * 0.0 = 0.0. t assert: 0 * 2 = 0. "TODO add complex numbers" ]. t@(UnitTests Numeric traits) testIntegerDivision [ t assert: 4 / 2 = 2. t assert: 4 / (1 / 2) = 8. t assert: 4 / 0.5 = 8.0. t assert: 4 / t complexOne = (2 plusImaginary: -2). t assert: 4 / t complexNegativeOne = (-2 plusImaginary: 2). t assert: 4 / t complexFraction = ((72 / 13) plusImaginary: (-48 / 13)). t assert: 4 / t complexFloat ~= 0. t assert: 4 / t complexTriangle = ((16 / 25) plusImaginary: (-12 / 25)). ]. t@(UnitTests Numeric traits) testIntegerEquality [ t assert: 2 = 2. t assert: 2 = 2.0. t assert: 2 = (4 / 2). ]. "Fraction arithmetic tests." t@(UnitTests Numeric traits) testFractionAddition "Test addition of Numbers to Fractions" [ t assert: (1 / 2) + 2 = (5 / 2) . t assert: (1 / 2) + (1 / 4) = (3 / 4). t assert: (1 / 2) + 2.1 = 2.6. ]. t@(UnitTests Numeric traits) testFractionSubtraction "Test subtraction of Numbers from Fractions" [ t assert: (1 / 2) - 2 = (-3 / 2) . t assert: (1 / 2) - (1 / 4) = (1 / 4). t assert: (1 / 4) - 2.5 = -2.25. ]. t@(UnitTests Numeric traits) testFractionMultiplication [ t assert: (3 / 4) * 3 = (9 / 4). t assert: (3 / 4) * -3 = (-9 / 4). t assert: (-3 / 4) * 3 = (-9 / 4). t assert: (3 / -4) * -3 = (9 / 4). t assert: (-3 / -4) * -3 = (-9 / 4). t assert: (3 / 4) * (1 / 2) = (3 / 8). t assert: (-3 / 4) * (1 / 2) = (-3 / 8). t assert: (3 / 4) * (-1 / 2) = (-3 / 8). t assert: (-3 / 4) * (-1 / 2) = (3 / 8). t assert: (-3 / -4) * (-1 / 2) = (-3 / 8). t assert: (1 / 2) * 3.5 = 1.75. t assert: (-1 / 2) * 3.5 = -1.75. t assert: (1 / 2) * -3.5 = -1.75. t assert: (-1 / 2) * -3.5 = 1.75. t assert: (-1 / -2) * -3.5 = -1.75. t assert: (3 / 4) * 0 = 0. t assert: (3 / 4) * 0.0 = 0.0. ]. t@(UnitTests Numeric traits) testFractionDivision [ t assert: (3 / 4) / 3 = (1 / 4). t assert: (3 / 4) / (1 / 2) = (6 / 4). t assert: (3 / 4) / 0.5 = 1.5. t assert: (3 / 4) / t complexTriangle = ((3 / 25) plusImaginary: (-9 / 100)). ]. "Float arithmetic tests." t@(UnitTests Numeric traits) testFloatAddition "Test addition of Numbers to Floats" [ t assert: 2.1 + 2 = 4.1. t assert: 2.1 + (1 / 2) = 2.6. t assert: 2.1 + 2.1 = 4.2. ]. t@(UnitTests Numeric traits) testFloatSubtraction "Test subtraction of Numbers from Floats" [ t assert: 2.5 - 2 = 0.5. t assert: 2.5 - (3 / 4) = 1.75. t assert: 2.5 - 1.75 = 0.75. t assert: 2.5 - 2.5 = 0.0. ]. t@(UnitTests Numeric traits) testFloatMultiplication [ t assert: 3.5 * 2 = 7.0. t assert: -3.5 * 2 = -7.0. t assert: 3.5 * -2 = -7.0. t assert: -3.5 * -2 = 7.0. t assert: 3.5 * (1 / 2) = 1.75. t assert: -3.5 * (1 / 2) = -1.75. t assert: 3.5 * (-1 / 2) = -1.75. t assert: -3.5 * (-1 / 2) = 1.75. t assert: 3.5 * 1.5 = 5.25. t assert: -3.5 * 1.5 = -5.25. t assert: 3.5 * -1.5 = -5.25. t assert: -3.5 * -1.5 = 5.25. t assert: 3.5 * 0 = 0. t assert: 3.5 * 0.0 = 0.0. ]. t@(UnitTests Numeric traits) testFloatDivision []. "Complex number arithmetic tests." t@(UnitTests Numeric traits) testComplexAddition []. t@(UnitTests Numeric traits) testComplexSubtraction []. t@(UnitTests Numeric traits) testComplexMultiplication []. t@(UnitTests Numeric traits) testComplexDivision []. "TODO:" "equality" "inequalities" "raisedTo:" t@(UnitTests Numeric traits) suite [| suite result | suite: TestSuite newEmpty. suite tests addAll: {t newForSelector: #testIsZero. t newForSelector: #testIsPositive. t newForSelector: #testIsNegative. t newForSelector: #testIntegerAddition. t newForSelector: #testIntegerSubtraction. t newForSelector: #testIntegerMultiplication. t newForSelector: #testIntegerDivision. t newForSelector: #testIntegerEquality. t newForSelector: #testFractionAddition. t newForSelector: #testFractionSubtraction. t newForSelector: #testFractionMultiplication. t newForSelector: #testFractionDivision. t newForSelector: #testFloatAddition. t newForSelector: #testFloatSubtraction. t newForSelector: #testFloatMultiplication. t newForSelector: #testFloatDivision. t newForSelector: #testComplexAddition. t newForSelector: #testComplexSubtraction. t newForSelector: #testComplexMultiplication. t newForSelector: #testComplexDivision}. suite ].