1995-04-03 20:49:14 +00:00
|
|
|
/*
|
|
|
|
Test NSValue, NSNumber, and related classes
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/NSValue.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSGeometry.h>
|
1997-10-16 23:56:27 +00:00
|
|
|
#include <Foundation/NSArray.h>
|
1998-10-03 05:11:05 +00:00
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
1995-04-03 20:49:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
NSPoint p;
|
2001-01-12 14:29:34 +00:00
|
|
|
NSRange range;
|
1995-04-03 20:49:14 +00:00
|
|
|
NSRect rect;
|
|
|
|
NSValue *v1, *v2;
|
2001-11-12 12:09:40 +00:00
|
|
|
NSNumber *nc, *ns, *n1, *n2, *n3, *n4, *n5, *n6, *n7;
|
1997-10-16 23:56:27 +00:00
|
|
|
NSArray *a1, *a2;
|
1998-10-03 05:11:05 +00:00
|
|
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
1995-04-03 20:49:14 +00:00
|
|
|
|
|
|
|
// Numbers
|
2001-11-12 12:09:40 +00:00
|
|
|
nc = [NSNumber numberWithChar: -100];
|
|
|
|
ns = [NSNumber numberWithShort: -100];
|
2000-08-07 22:00:31 +00:00
|
|
|
printf("try %d, %d", [nc charValue], [ns shortValue]);
|
2001-11-12 12:09:40 +00:00
|
|
|
printf("nc compare: ns is %d\n", [nc compare: ns]);
|
|
|
|
n1 = [NSNumber numberWithUnsignedShort: 30];
|
|
|
|
printf("n1 = %s\n", [[n1 description] cString]);
|
|
|
|
n2 = [NSNumber numberWithDouble: 2.7];
|
|
|
|
printf("n2 = %s\n", [[n2 description] cString]);
|
|
|
|
n3 = [NSNumber numberWithDouble: 30];
|
|
|
|
printf("n3 = %s\n", [[n3 description] cString]);
|
|
|
|
n4 = [NSNumber numberWithChar: 111];
|
|
|
|
printf("n4 = %s\n", [[n4 description] cString]);
|
|
|
|
n5 = [NSNumber numberWithChar: 111];
|
|
|
|
printf("n5 = %s\n", [[n5 description] cString]);
|
|
|
|
n6 = [NSNumber numberWithFloat: 1.5];
|
|
|
|
printf("n6 = %s\n", [[n6 description] cString]);
|
|
|
|
n7 = [NSNumber numberWithShort: 25];
|
|
|
|
printf("n7 = %s\n", [[n7 description] cString]);
|
1995-04-03 20:49:14 +00:00
|
|
|
printf("Number(n1) as int %d, as float %f\n",
|
|
|
|
[n1 intValue], [n1 floatValue]);
|
|
|
|
printf("n1 times n2=%f as int to get %d\n",
|
|
|
|
[n2 floatValue], [n1 intValue]*[n2 intValue]);
|
|
|
|
printf("n2 as string: %s\n", [[n2 stringValue] cString]);
|
2001-11-12 12:09:40 +00:00
|
|
|
printf("n2 compare: n1 is %d\n", [n2 compare: n1]);
|
|
|
|
printf("n1 compare: n2 is %d\n", [n1 compare: n2]);
|
|
|
|
printf("n1 isEqual: n3 is %d\n", [n1 isEqual: n3]);
|
|
|
|
printf("n4 isEqual: n5 is %d\n", [n4 isEqual: n5]);
|
1995-04-03 20:49:14 +00:00
|
|
|
|
2001-11-12 12:09:40 +00:00
|
|
|
a1 = [NSArray arrayWithObjects:
|
1997-10-16 23:56:27 +00:00
|
|
|
[NSNumber numberWithChar: 111],
|
|
|
|
[NSNumber numberWithUnsignedChar: 112],
|
|
|
|
[NSNumber numberWithShort: 121],
|
|
|
|
[NSNumber numberWithUnsignedShort: 122],
|
|
|
|
[NSNumber numberWithInt: 131],
|
|
|
|
[NSNumber numberWithUnsignedInt: 132],
|
|
|
|
[NSNumber numberWithInt: 141],
|
|
|
|
[NSNumber numberWithUnsignedInt: 142],
|
|
|
|
[NSNumber numberWithFloat: 151],
|
|
|
|
[NSNumber numberWithDouble: 152], nil];
|
|
|
|
|
2001-11-12 12:09:40 +00:00
|
|
|
a2 = [NSArray arrayWithObjects:
|
1997-10-16 23:56:27 +00:00
|
|
|
[NSNumber numberWithChar: 111],
|
|
|
|
[NSNumber numberWithUnsignedChar: 112],
|
|
|
|
[NSNumber numberWithShort: 121],
|
|
|
|
[NSNumber numberWithUnsignedShort: 122],
|
|
|
|
[NSNumber numberWithInt: 131],
|
|
|
|
[NSNumber numberWithUnsignedInt: 132],
|
|
|
|
[NSNumber numberWithInt: 141],
|
|
|
|
[NSNumber numberWithUnsignedInt: 142],
|
|
|
|
[NSNumber numberWithFloat: 151],
|
|
|
|
[NSNumber numberWithDouble: 152], nil];
|
|
|
|
|
2001-11-12 12:09:40 +00:00
|
|
|
printf("a1 isEqual: a2 is %d\n", [a1 isEqual: a2]);
|
1995-04-03 20:49:14 +00:00
|
|
|
|
|
|
|
// Test values, Geometry
|
|
|
|
rect = NSMakeRect(1.0, 103.3, 40.0, 843.);
|
|
|
|
rect = NSIntersectionRect(rect, NSMakeRect(20, 78., 89., 30));
|
2001-11-12 12:09:40 +00:00
|
|
|
v1 = [NSValue valueWithRect: rect];
|
1995-04-03 20:49:14 +00:00
|
|
|
printf("Encoding for rect is %s\n", [v1 objCType]);
|
|
|
|
rect = [v1 rectValue];
|
|
|
|
printf("Rect is %f %f %f %f\n", NSMinX(rect), NSMinY(rect), NSMaxX(rect),
|
|
|
|
NSMaxY(rect));
|
2001-11-12 12:09:40 +00:00
|
|
|
v2 = [NSValue valueWithPoint: NSMakePoint(3,4)];
|
|
|
|
v1 = [NSValue valueWithNonretainedObject: v2];
|
|
|
|
[[v1 nonretainedObjectValue] getValue: &p];
|
1995-04-03 20:49:14 +00:00
|
|
|
printf("point is %f %f\n", p.x, p.y);
|
2001-01-12 14:29:34 +00:00
|
|
|
range = NSMakeRange(1, 103);
|
|
|
|
range = NSIntersectionRange(range, NSMakeRange(2, 73));
|
2001-11-12 12:09:40 +00:00
|
|
|
v1 = [NSValue valueWithRange: range];
|
2001-01-12 14:29:34 +00:00
|
|
|
printf("Encoding for range is %s\n", [v1 objCType]);
|
|
|
|
range = [v1 rangeValue];
|
|
|
|
printf("Range is %u %u\n", range.location, range.length);
|
1995-04-03 20:49:14 +00:00
|
|
|
|
2001-11-12 12:09:40 +00:00
|
|
|
printf("Try getting a null NSValue, should get a NSLog error message: \n");
|
|
|
|
v2 = [NSValue value: NULL withObjCType: @encode(int)];
|
1998-10-03 05:11:05 +00:00
|
|
|
[arp release];
|
1995-04-03 20:49:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|