]> git.saurik.com Git - apple/objc4.git/blob - test/runtime.m
objc4-493.9.tar.gz
[apple/objc4.git] / test / runtime.m
1 /*
2 TEST_RUN_OUTPUT
3 objc\[\d+\]: class `DoesNotExist\' not linked into application
4 OK: runtime.m
5 END
6 */
7
8
9 #include "test.h"
10
11 #include <string.h>
12 #include <dlfcn.h>
13 #include <mach-o/ldsyms.h>
14 #include <objc/objc-runtime.h>
15
16 @interface Super { id isa; } @end
17 @implementation Super
18 +(void)initialize { }
19 +class { return self; }
20 @end
21
22 @interface Sub : Super { } @end
23 @implementation Sub @end
24
25
26 int main()
27 {
28 Class list[100];
29 Class *list2;
30 unsigned int count, count0, count2;
31 unsigned int i;
32 int foundSuper;
33 int foundSub;
34 const char **names;
35 Dl_info info;
36
37 [Super class];
38
39 // This shouldn't touch any classes.
40 dladdr(&_mh_execute_header, &info);
41 names = objc_copyClassNamesForImage(info.dli_fname, &count);
42 testassert(names);
43 testassert(count == 2);
44 testassert(names[count] == NULL);
45 foundSuper = 0;
46 foundSub = 0;
47 for (i = 0; i < count; i++) {
48 if (0 == strcmp(names[i], "Super")) foundSuper++;
49 if (0 == strcmp(names[i], "Sub")) foundSub++;
50 }
51 testassert(foundSuper == 1);
52 testassert(foundSub == 1);
53
54
55 // class Sub hasn't been touched - make sure it's in the class list too
56 count0 = objc_getClassList(NULL, 0);
57 testassert(count0 >= 2 && count0 < 100);
58
59 list[count0-1] = NULL;
60 count = objc_getClassList(list, count0-1);
61 testassert(list[count0-1] == NULL);
62 testassert(count == count0);
63
64 count = objc_getClassList(list, count0);
65 testassert(count == count0);
66 foundSuper = 0;
67 foundSub = 0;
68 for (i = 0; i < count; i++) {
69 if (0 == strcmp(class_getName(list[i]), "Super")) foundSuper++;
70 if (0 == strcmp(class_getName(list[i]), "Sub")) foundSub++;
71 // list should be non-meta classes only
72 testassert(!class_isMetaClass(list[i]));
73 }
74 testassert(foundSuper == 1);
75 testassert(foundSub == 1);
76
77 // fixme check class handler
78 testassert(objc_getClass("Super") == [Super class]);
79 testassert(objc_getClass("DoesNotExist") == nil);
80 testassert(objc_getClass(NULL) == nil);
81
82 testassert(objc_getMetaClass("Super") == [Super class]->isa);
83 testassert(objc_getMetaClass("DoesNotExist") == nil);
84 testassert(objc_getMetaClass(NULL) == nil);
85
86 // fixme check class no handler
87 testassert(objc_lookUpClass("Super") == [Super class]);
88 testassert(objc_lookUpClass("DoesNotExist") == nil);
89 testassert(objc_lookUpClass(NULL) == nil);
90
91 list2 = objc_copyClassList(&count2);
92 testassert(count2 == count);
93 testassert(list2);
94 testassert(malloc_size(list2) >= (1+count2) * sizeof(Class));
95 for (i = 0; i < count; i++) {
96 testassert(list[i] == list2[i]);
97 }
98 testassert(list2[count] == NULL);
99 free(list2);
100 free(objc_copyClassList(NULL));
101
102 succeed(__FILE__);
103 }