'From Squeak3.7beta of ''1 April 2004'' [latest update: #5923] on 18 September 2004 at 8:13:31 pm'! "Change Set: NumberReadFromTests-dtl Date: 18 September 2004 Author: David T. Lewis Unit tests for NumberReadFromFixes. Includes new tests for Integers, Floats, and ScaledDecimals. Prior to applying NumberReadFromFixes, IntegerTest should show two failing tests, FloatTest should show zero failing tests, and ScaleDecimalTest should show four failing tests. After applying NumberReadFromFixes, all tests should pass. "! ClassTestCase subclass: #ScaledDecimalTest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Kernel-Numbers-Tests'! !ScaledDecimalTest commentStamp: '' prior: 0! I provide a test suite for ScaledDecimal values. Examine my tests to see how SmallIntegers should behave, and see how to use them.! !FloatTest methodsFor: 'testing - conversion' stamp: 'dtl 9/18/2004 12:40'! testStringAsNumber "This covers parsing in Number>>readFrom:" | aFloat | aFloat _ '10r-12.3456' asNumber. self assert: -12.3456 = aFloat. aFloat _ '10r-12.3456e2' asNumber. self assert: -1234.56 = aFloat. aFloat _ '10r-12.3456d2' asNumber. self assert: -1234.56 = aFloat. aFloat _ '10r-12.3456q2' asNumber. self assert: -1234.56 = aFloat. aFloat _ '-12.3456q2' asNumber. self assert: -1234.56 = aFloat. aFloat _ '12.3456q2' asNumber. self assert: 1234.56 = aFloat. ! ! !IntegerTest methodsFor: 'testing - instance creation' stamp: 'dtl 9/18/2004 17:14'! testReadFrom "Ensure remaining characters in a stream are not lost when parsing an integer." | rs i s | rs _ ReadStream on: '123s could be confused with a ScaledDecimal'. i _ Number readFrom: rs. self assert: i == 123. s _ rs upToEnd. self assert: 's could be confused with a ScaledDecimal' = s. rs _ ReadStream on: '123.s could be confused with a ScaledDecimal'. i _ Number readFrom: rs. self assert: i == 123. s _ rs upToEnd. self assert: '.s could be confused with a ScaledDecimal' = s ! ! !IntegerTest methodsFor: 'testing - instance creation' stamp: 'dtl 9/18/2004 17:07'! testStringAsNumber "This covers parsing in Number>>readFrom: Trailing decimal points should be ignored." self assert: ('123' asNumber == 123). self assert: ('-123' asNumber == -123). self assert: ('123.' asNumber == 123). self assert: ('-123.' asNumber == -123). self assert: ('123This is not to be read' asNumber == 123). self assert: ('123s could be confused with a ScaledDecimal' asNumber == 123). self assert: ('123e could be confused with a Float' asNumber == 123). ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 12:22'! testAsNumber "Ensure no loss of precision" | sd | sd _ '1.40s2' asNumber. self assert: ScaledDecimal == sd class. self assert: sd scale == 2. self assert: '1.40s2' = sd printString. ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 15:51'! testAsNumberNegatedWithoutDecimalPoint | sd | sd _ '-123s0' asNumber. self assert: ScaledDecimal == sd class. self assert: sd scale == 0. self assert: '-123s0' = sd printString. ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 15:51'! testAsNumberNegatedWithoutDecimalPoint2 | sd | sd _ '-123s2' asNumber. self assert: ScaledDecimal == sd class. self assert: sd scale == 2. self assert: '-123.00s2' = sd printString. ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 12:21'! testAsNumberWithExtendedScale | sd | sd _ '123s2' asNumber. self assert: ScaledDecimal == sd class. self assert: sd scale == 2. self assert: '123.00s2' = sd printString. ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 12:28'! testAsNumberWithRadix | sd | sd _ '10r-22.2s5' asNumber. self assert: ScaledDecimal == sd class. self assert: sd scale == 5. self assert: '-22.20000s5' = sd printString. ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 12:20'! testAsNumberWithSuperfluousDecimalPoint | sd | sd _ '123.s2' asNumber. self assert: ScaledDecimal == sd class. self assert: sd scale == 2. self assert: '123.00s2' = sd printString. ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 15:49'! testAsNumberWithoutDecimalPoint | sd | sd _ '123s0' asNumber. self assert: ScaledDecimal == sd class. self assert: sd scale == 0. self assert: '123s0' = sd printString. ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 15:51'! testAsNumberWithoutDecimalPoint2 | sd | sd _ '123s2' asNumber. self assert: ScaledDecimal == sd class. self assert: sd scale == 2. self assert: '123.00s2' = sd printString. ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 12:04'! testConvertFromFloat | aFloat sd f2 diff | aFloat _ 11/13 asFloat. sd _ aFloat asScaledDecimal: 2. self assert: 2 == sd scale. self assert: '0.84s2' = sd printString. f2 _ sd asFloat. diff _ f2 - aFloat. self assert: diff < 1.0e-9. "actually, f = f2, but this is not a requirement" ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 12:24'! testConvertFromFraction | sd | sd _ (13 / 11) asScaledDecimal: 6. self assert: ScaledDecimal == sd class. self assert: ('1.181818s6' = sd printString). self assert: 6 == sd scale ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 11:06'! testConvertFromInteger "Converting an Integer to a ScaledDecimal yields a ScaledDecimal with scale 0, regardless of the scale specified in the #asScaledDecimal: message." | sd | sd _ 13 asScaledDecimal: 6. self assert: 0 = sd scale. self assert: ('13s0' = sd printString). sd _ -13 asScaledDecimal: 6. self assert: 0 = sd scale. self assert: ('-13s0' = sd printString). sd _ 130000000013 asScaledDecimal: 6. self assert: 0 = sd scale. self assert: ('130000000013s0' = sd printString). sd _ -130000000013 asScaledDecimal: 6. self assert: 0 = sd scale. self assert: ('-130000000013s0' = sd printString) ! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 13:38'! testLiteral | sd | sd _ 1.40s2. self assert: ScaledDecimal == sd class. self assert: sd scale == 2. self assert: '1.40s2' = sd printString! ! !ScaledDecimalTest methodsFor: 'testing' stamp: 'dtl 9/18/2004 11:51'! testPrintString "The printed representation of a ScaledDecimal is truncated, not rounded. Not sure if this is right, so this test describes the current Squeak implementation. If someone knows a reason that rounding would be preferable, then update this test." | sd | sd _ (13 / 11) asScaledDecimal: 6. self assert: ('1.181818s6' = sd printString). sd _ (13 / 11) asScaledDecimal: 5. self deny: ('1.18182s5' = sd printString). sd _ (13 / 11) asScaledDecimal: 5. self assert: ('1.18181s5' = sd printString) ! ! !ScaledDecimalTest reorganize! ('testing' testAsNumber testAsNumberNegatedWithoutDecimalPoint testAsNumberNegatedWithoutDecimalPoint2 testAsNumberWithExtendedScale testAsNumberWithRadix testAsNumberWithSuperfluousDecimalPoint testAsNumberWithoutDecimalPoint testAsNumberWithoutDecimalPoint2 testConvertFromFloat testConvertFromFraction testConvertFromInteger testLiteral testPrintString) ! !FloatTest reorganize! ('testing - arithmetic' testDivide testIsZero) ('testing - conversion' testStringAsNumber) !