]> git.saurik.com Git - apple/objc4.git/blob - test/taggedNSPointers.m
objc4-680.tar.gz
[apple/objc4.git] / test / taggedNSPointers.m
1 // TEST_CFLAGS -framework Foundation
2
3 #include "test.h"
4 #include <objc/runtime.h>
5 #import <Foundation/Foundation.h>
6
7 #if OBJC_HAVE_TAGGED_POINTERS
8
9 void testTaggedNumber()
10 {
11 NSNumber *taggedNS = [NSNumber numberWithInt: 1234];
12 CFNumberRef taggedCF = (CFNumberRef)objc_unretainedPointer(taggedNS);
13 int result;
14
15 testassert( CFGetTypeID(taggedCF) == CFNumberGetTypeID() );
16 testassert(_objc_getClassForTag(OBJC_TAG_NSNumber) == [taggedNS class]);
17
18 CFNumberGetValue(taggedCF, kCFNumberIntType, &result);
19 testassert(result == 1234);
20
21 testassert(_objc_isTaggedPointer(taggedCF));
22 testassert(_objc_getTaggedPointerTag(taggedCF) == OBJC_TAG_NSNumber);
23 testassert(_objc_makeTaggedPointer(_objc_getTaggedPointerTag(taggedCF), _objc_getTaggedPointerValue(taggedCF)) == taggedCF);
24
25 // do some generic object-y things to the taggedPointer instance
26 CFRetain(taggedCF);
27 CFRelease(taggedCF);
28
29 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
30 [dict setObject: taggedNS forKey: @"fred"];
31 testassert(taggedNS == [dict objectForKey: @"fred"]);
32 [dict setObject: @"bob" forKey: taggedNS];
33 testassert([@"bob" isEqualToString: [dict objectForKey: taggedNS]]);
34
35 NSNumber *iM88 = [NSNumber numberWithInt:-88];
36 NSNumber *i12346 = [NSNumber numberWithInt: 12346];
37 NSNumber *i12347 = [NSNumber numberWithInt: 12347];
38
39 NSArray *anArray = [NSArray arrayWithObjects: iM88, i12346, i12347, nil];
40 testassert([anArray count] == 3);
41 testassert([anArray indexOfObject: i12346] == 1);
42
43 NSSet *aSet = [NSSet setWithObjects: iM88, i12346, i12347, nil];
44 testassert([aSet count] == 3);
45 testassert([aSet containsObject: i12346]);
46
47 [taggedNS performSelector: @selector(intValue)];
48 testassert(![taggedNS isProxy]);
49 testassert([taggedNS isKindOfClass: [NSNumber class]]);
50 testassert([taggedNS respondsToSelector: @selector(intValue)]);
51
52 (void)[taggedNS description];
53 }
54
55 int main()
56 {
57 PUSH_POOL {
58 testTaggedNumber(); // should be tested by CF... our tests are wrong, wrong, wrong.
59 } POP_POOL;
60
61 succeed(__FILE__);
62 }
63
64 // OBJC_HAVE_TAGGED_POINTERS
65 #else
66 // not OBJC_HAVE_TAGGED_POINTERS
67
68 // Tagged pointers not supported. Crash if an NSNumber actually
69 // is a tagged pointer (which means this test is out of date).
70
71 int main()
72 {
73 PUSH_POOL {
74 testassert(*(void **)objc_unretainedPointer([NSNumber numberWithInt:1234]));
75 } POP_POOL;
76
77 succeed(__FILE__);
78 }
79
80 #endif