5 #include <malloc/malloc.h>
6 #include <objc/objc-runtime.h>
8 @interface SuperIvars {
13 @implementation SuperIvars @end
15 @interface SubIvars : SuperIvars {
19 @implementation SubIvars @end
22 @interface FourIvars {
28 @implementation FourIvars @end
30 @interface NoIvars { } @end
31 @implementation NoIvars @end
33 static int isNamed(Ivar iv, const char *name)
35 return (0 == strcmp(name, ivar_getName(iv)));
44 cls = objc_getClass("SubIvars");
48 ivars = class_copyIvarList(cls, &count);
50 testassert(count == 2);
51 testassert(isNamed(ivars[0], "ivar3"));
52 testassert(isNamed(ivars[1], "ivar4"));
53 // ivars[] should be null-terminated
54 testassert(ivars[2] == NULL);
57 cls = objc_getClass("SuperIvars");
61 ivars = class_copyIvarList(cls, &count);
63 testassert(count == 3);
64 testassert(isNamed(ivars[0], "isa"));
65 testassert(isNamed(ivars[1], "ivar1"));
66 testassert(isNamed(ivars[2], "ivar2"));
67 // ivars[] should be null-terminated
68 testassert(ivars[3] == NULL);
71 // Check null-termination - this ivar list block would be 16 bytes
72 // if it weren't for the terminator
73 cls = objc_getClass("FourIvars");
77 ivars = class_copyIvarList(cls, &count);
79 testassert(count == 4);
80 testassert(malloc_size(ivars) >= 5 * sizeof(Ivar));
81 testassert(ivars[3] != NULL);
82 testassert(ivars[4] == NULL);
85 // Check NULL count parameter
86 ivars = class_copyIvarList(cls, NULL);
88 testassert(ivars[4] == NULL);
89 testassert(ivars[3] != NULL);
92 // Check NULL class parameter
94 ivars = class_copyIvarList(NULL, &count);
96 testassert(count == 0);
98 // Check NULL class and count
99 ivars = class_copyIvarList(NULL, NULL);
102 // Check class with no ivars
103 cls = objc_getClass("NoIvars");
107 ivars = class_copyIvarList(cls, &count);
109 testassert(count == 0);