mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-12-06 20:55:32 +00:00
CPCoding support for CPDecimalNumber and test
This commit is contained in:
1 parent
5a0cfc1595
commit
6a6256bdb4
2 files changed
+58
No files matched your search
@@ -754,3 +754,47 @@ var CPDefaultDcmHandler = [CPDecimalNumberHandler decimalNumberHandlerWithRoundi
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
// CPCoding category
|
||||
var CPDecimalNumberDecimalExponent = @"CPDecimalNumberDecimalExponent",
|
||||
CPDecimalNumberDecimalIsNegative = @"CPDecimalNumberDecimalIsNegative",
|
||||
CPDecimalNumberDecimalIsCompact = @"CPDecimalNumberDecimalIsCompact",
|
||||
CPDecimalNumberDecimalIsNaN = @"CPDecimalNumberDecimalIsNaN",
|
||||
CPDecimalNumberDecimalMantissa = @"CPDecimalNumberDecimalMantissa";
|
||||
|
||||
@implementation CPDecimalNumber (CPCoding)
|
||||
|
||||
/*!
|
||||
Called by CPCoder's \e decodeObject: to initialise the object with an archived one.
|
||||
@param aCoder a \c CPCoder instance
|
||||
*/
|
||||
- (id)initWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
if (self)
|
||||
{
|
||||
var dcm = CPDecimalMakeZero();
|
||||
dcm._exponent = [aCoder decodeIntForKey:CPDecimalNumberDecimalExponent];
|
||||
dcm._isNegative = [aCoder decodeBoolForKey:CPDecimalNumberDecimalIsNegative];
|
||||
dcm._isCompact = [aCoder decodeBoolForKey:CPDecimalNumberDecimalIsCompact];
|
||||
dcm._isNaN = [aCoder decodeBoolForKey:CPDecimalNumberDecimalIsNaN];
|
||||
dcm._mantissa = [aCoder decodeObjectForKey:CPDecimalNumberDecimalMantissa];
|
||||
[self initWithDecimal:dcm];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/*!
|
||||
Called by CPCoder's \e encodeObject: to archive the object instance.
|
||||
@param aCoder a \c CPCoder instance
|
||||
*/
|
||||
- (void)encodeWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
[aCoder encodeInt:_data._exponent forKey:CPDecimalNumberDecimalExponent];
|
||||
[aCoder encodeBool:_data._isNegative forKey:CPDecimalNumberDecimalIsNegative];
|
||||
[aCoder encodeBool:_data._isCompact forKey:CPDecimalNumberDecimalIsCompact];
|
||||
[aCoder encodeBool:_data._isNaN forKey:CPDecimalNumberDecimalIsNaN];
|
||||
[aCoder encodeObject:_data._mantissa forKey:CPDecimalNumberDecimalMantissa];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -532,4 +532,18 @@
|
||||
[self assert:"82346.2341144" equals:[dcmn descriptionWithLocale:nil] message:"descriptionWithLocale: - large number"];
|
||||
}
|
||||
|
||||
|
||||
- (void)testEncoding
|
||||
{
|
||||
var number = [CPDecimalNumber decimalNumberWithString:@"-1.233e24"],
|
||||
encoded = [CPKeyedArchiver archivedDataWithRootObject:number],
|
||||
decoded = [CPKeyedUnarchiver unarchiveObjectWithData:encoded];
|
||||
|
||||
[self assert:21 equals:decoded._data._exponent message:"exponent not unarchived correctly"];
|
||||
[self assert:[1,2,3,3] equals:decoded._data._mantissa message:"mantissa not unarchived correctly"];
|
||||
[self assert:YES equals:decoded._data._isNegative message:"sign not unarchived correctly"];
|
||||
[self assert:NO equals:decoded._data._isNaN message:"isNaN not unarchived correctly"];
|
||||
[self assert:YES equals:decoded._data._isCompact message:"isCompact not unarchived correctly"];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user