From 9f1a8fa7a2fc563de9dc6e5058f6f340954fc1a4 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 4 Aug 2012 23:04:40 +0800 Subject: [PATCH 1/3] enable '.003' or '-.003' style number in CPDecimalMakeWithString function and add new test cases --- Foundation/CPDecimal.j | 3 ++- Tests/Foundation/CPDecimalNumberTest.j | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Foundation/CPDecimal.j b/Foundation/CPDecimal.j index ec06deea7..92382d1ce 100644 --- a/Foundation/CPDecimal.j +++ b/Foundation/CPDecimal.j @@ -115,7 +115,8 @@ function CPDecimalMakeWithString(string, locale) // Note: this doesn't accept .01 for example, should it? // If yes simply add '?' after integer part group, i.e. ([+\-]?)((?:0|[1-9]\d*)?) - var matches = string.match(/^([+\-]?)((?:0|[1-9]\d*))(?:\.(\d*))?(?:[eE]([+\-]?)(\d+))?$/); + // Note: now it accept .01 style. + var matches = string.match(/^([+\-]?)((?:0|[1-9]\d*)?)(?:\.(\d*))?(?:[eE]([+\-]?)(\d+))?$/); if (!matches) return CPDecimalMakeNaN(); diff --git a/Tests/Foundation/CPDecimalNumberTest.j b/Tests/Foundation/CPDecimalNumberTest.j index e74788065..471ed91dd 100644 --- a/Tests/Foundation/CPDecimalNumberTest.j +++ b/Tests/Foundation/CPDecimalNumberTest.j @@ -522,6 +522,10 @@ [self assert:"123456789123456789123456789000000000000000" equals:[dcmn stringValue] message:"stringValue: - large number"]; dcmn = [CPDecimalNumber decimalNumberWithString:@"82346.2341144"]; [self assert:"82346.2341144" equals:[dcmn descriptionWithLocale:nil] message:"descriptionWithLocale: - large number"]; + dcmn = [CPDecimalNumber decimalNumberWithString:@".125"]; + [self assert:"0.125" equals:[dcmn descriptionWithLocale:nil] message:"stringValue: - no integral part number"]; + dcmn = [CPDecimalNumber decimalNumberWithString:@"-.003"]; + [self assert:"-0.003" equals:[dcmn descriptionWithLocale:nil] message:"stringValue: - no integral part negative number"]; } - (void)testEncoding From 175c70d99e4ce8f7e30035f7eff5195adc9420bf Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 5 Aug 2012 08:37:44 +0800 Subject: [PATCH 2/3] fix a new bug in CPDecimalMakeWithString and remove some test cases --- Foundation/CPDecimal.j | 3 +++ Tests/Foundation/CPDecimalNumberTest.j | 12 +++--------- Tests/Foundation/CPDecimalTest.j | 4 ---- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/Foundation/CPDecimal.j b/Foundation/CPDecimal.j index 92382d1ce..8824a6218 100644 --- a/Foundation/CPDecimal.j +++ b/Foundation/CPDecimal.j @@ -142,6 +142,9 @@ function CPDecimalMakeWithString(string, locale) { // input is too long, increase exponent and truncate exponent += inputlength - CPDecimalMaxDigits; + } else if (inputlength === 0) + { + return CPDecimalMakeNaN(); } if (exponent > CPDecimalMaxExponent || exponent < CPDecimalMinExponent) diff --git a/Tests/Foundation/CPDecimalNumberTest.j b/Tests/Foundation/CPDecimalNumberTest.j index 471ed91dd..7e1ea479c 100644 --- a/Tests/Foundation/CPDecimalNumberTest.j +++ b/Tests/Foundation/CPDecimalNumberTest.j @@ -145,20 +145,14 @@ dcmn = [[CPDecimalNumber alloc] initWithString:@"foo"]; [self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 2 overflow should return NaN"]; - dcmn = [[CPDecimalNumber alloc] initWithString:@".123"]; + dcmn = [[CPDecimalNumber alloc] initWithString:@"0123"]; [self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 3 overflow should return NaN"]; - dcmn = [[CPDecimalNumber alloc] initWithString:@"-.123"]; + dcmn = [[CPDecimalNumber alloc] initWithString:@"1e200"]; [self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 4 overflow should return NaN"]; - dcmn = [[CPDecimalNumber alloc] initWithString:@"0123"]; - [self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 5 overflow should return NaN"]; - - dcmn = [[CPDecimalNumber alloc] initWithString:@"1e200"]; - [self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 6 overflow should return NaN"]; - dcmn = [[CPDecimalNumber alloc] initWithString:@"12312e-23421"]; - [self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 7 overflow should return NaN"]; + [self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 5 overflow should return NaN"]; } - (void)testAdd diff --git a/Tests/Foundation/CPDecimalTest.j b/Tests/Foundation/CPDecimalTest.j index 9acdfbdbb..1a8411a09 100644 --- a/Tests/Foundation/CPDecimalTest.j +++ b/Tests/Foundation/CPDecimalTest.j @@ -99,10 +99,6 @@ [self assertTrue:dcm._isNaN message:"CPDecimalMakeWithString() Ti9: catch of invalid number string. Should return NaN"]; dcm = CPDecimalMakeWithString(@"-0001"); [self assertTrue:dcm._isNaN message:"CPDecimalMakeWithString() Ti10: catch of invalid number string. Should return NaN"]; - dcm = CPDecimalMakeWithString(@".123"); - [self assertTrue:dcm._isNaN message:"CPDecimalMakeWithString() Ti11: catch of invalid number string. Should return NaN"]; - dcm = CPDecimalMakeWithString(@"-.1"); - [self assertTrue:dcm._isNaN message:"CPDecimalMakeWithString() Ti12: catch of invalid number string. Should return NaN"]; //test make with parts dcm = CPDecimalMakeWithParts(10127658,2); From def8016f446031cc4b2598cb401719f0c9828f1c Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 5 Aug 2012 09:35:44 +0800 Subject: [PATCH 3/3] code style change --- Foundation/CPDecimal.j | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Foundation/CPDecimal.j b/Foundation/CPDecimal.j index 8824a6218..4b2f68697 100644 --- a/Foundation/CPDecimal.j +++ b/Foundation/CPDecimal.j @@ -142,7 +142,8 @@ function CPDecimalMakeWithString(string, locale) { // input is too long, increase exponent and truncate exponent += inputlength - CPDecimalMaxDigits; - } else if (inputlength === 0) + } + else if (inputlength === 0) { return CPDecimalMakeNaN(); }