2011-02-16 08:21:17 +00:00
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import "Testing.h"
|
|
|
|
#import "ObjectTesting.h"
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2011-03-04 08:56:10 +00:00
|
|
|
START_SET("basic")
|
2011-02-16 08:21:17 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
NSNumberFormatter *fmt;
|
|
|
|
NSNumber *num;
|
2011-03-04 09:16:40 +00:00
|
|
|
NSString *error;
|
2011-03-04 08:56:10 +00:00
|
|
|
|
|
|
|
[NSNumberFormatter
|
|
|
|
setDefaultFormatterBehavior: NSNumberFormatterBehavior10_0];
|
2011-02-16 08:21:17 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
TEST_FOR_CLASS(@"NSNumberFormatter",[NSNumberFormatter alloc],
|
|
|
|
"+[NSNumberFormatter alloc] returns a NSNumberFormatter");
|
2011-02-16 08:21:17 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
fmt = [[[NSNumberFormatter alloc] init] autorelease];
|
|
|
|
num = [[[NSNumber alloc] initWithFloat: 1234.567] autorelease];
|
2011-02-16 08:21:17 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
PASS_EQUAL([fmt stringForObjectValue: num], @"1,234.57",
|
|
|
|
"default format same as Cocoa")
|
2011-02-16 08:21:17 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
num = [[[NSNumber alloc] initWithFloat: 1.01] autorelease];
|
|
|
|
PASS_EQUAL([fmt stringFromNumber: num], @"1.01",
|
|
|
|
"Handle leading zeroes in fractional part: 1.01")
|
2011-03-02 12:09:23 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
[fmt setAllowsFloats: NO];
|
|
|
|
num = [[[NSNumber alloc] initWithFloat: 1234.567] autorelease];
|
2011-03-04 09:16:40 +00:00
|
|
|
PASS_EQUAL([fmt stringForObjectValue: num], @"1,234.57",
|
|
|
|
"-setAllowsFloats: does not effect rounding")
|
2011-03-02 12:09:23 +00:00
|
|
|
|
2011-03-04 09:16:40 +00:00
|
|
|
PASS(NO == [fmt getObjectValue: &num forString: @"1234.567"
|
|
|
|
errorDescription: 0], "float input is disallowed")
|
2011-02-16 08:21:17 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
[fmt setFormat: @"__000000"];
|
|
|
|
PASS_EQUAL([fmt stringForObjectValue: num], @" 001234",
|
|
|
|
"numeric and space padding OK")
|
2011-02-16 08:21:17 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
num = [[[NSNumber alloc] initWithFloat: 1234.56] autorelease];
|
|
|
|
[fmt setAllowsFloats: YES];
|
|
|
|
[fmt setPositiveFormat: @"$####.##c"];
|
|
|
|
[fmt setNegativeFormat: @"-$(####.##)"];
|
|
|
|
PASS_EQUAL([fmt stringForObjectValue: num], @"$1234.56c",
|
|
|
|
"prefix and suffix used properly")
|
2011-02-16 08:21:17 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
num = [[[NSNumber alloc] initWithFloat: -1234.56] autorelease];
|
|
|
|
PASS_EQUAL([fmt stringForObjectValue: num], @"-$(1234.56)",
|
|
|
|
"negativeFormat used for -ve number")
|
2011-02-16 08:21:17 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
PASS_EQUAL([fmt stringForObjectValue: [NSDecimalNumber notANumber]],
|
|
|
|
@"NaN", "notANumber special case")
|
2011-02-16 08:21:17 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
[fmt setFormat: @"0"];
|
|
|
|
PASS_EQUAL([fmt stringForObjectValue: num], @"-1235",
|
|
|
|
"format string of length 1")
|
2011-02-16 08:21:17 +00:00
|
|
|
|
2011-03-04 08:56:10 +00:00
|
|
|
END_SET("basic")
|
2011-02-16 08:21:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|