5 #include <malloc/malloc.h>
6 #include <objc/objc-runtime.h>
9 @interface SuperIvars {
14 @implementation SuperIvars @end
16 @interface SubIvars : SuperIvars {
20 @implementation SubIvars @end
23 @interface FourIvars {
29 @implementation FourIvars @end
32 @interface NoIvars { } @end
33 @implementation NoIvars @end
35 static int isNamed(Ivar iv, const char *name)
37 return (0 == strcmp(name, ivar_getName(iv)));
46 cls = objc_getClass("SubIvars");
50 ivars = class_copyIvarList(cls, &count);
52 testassert(count == 2);
53 testassert(isNamed(ivars[0], "ivar3"));
54 testassert(isNamed(ivars[1], "ivar4"));
55 // ivars[] should be null-terminated
56 testassert(ivars[2] == NULL);
59 cls = objc_getClass("SuperIvars");
63 ivars = class_copyIvarList(cls, &count);
65 testassert(count == 3);
66 testassert(isNamed(ivars[0], "isa"));
67 testassert(isNamed(ivars[1], "ivar1"));
68 testassert(isNamed(ivars[2], "ivar2"));
69 // ivars[] should be null-terminated
70 testassert(ivars[3] == NULL);
73 // Check null-termination - this ivar list block would be 16 bytes
74 // if it weren't for the terminator
75 cls = objc_getClass("FourIvars");
79 ivars = class_copyIvarList(cls, &count);
81 testassert(count == 4);
82 testassert(malloc_size(ivars) >= 5 * sizeof(Ivar));
83 testassert(ivars[3] != NULL);
84 testassert(ivars[4] == NULL);
87 // Check NULL count parameter
88 ivars = class_copyIvarList(cls, NULL);
90 testassert(ivars[4] == NULL);
91 testassert(ivars[3] != NULL);
94 // Check NULL class parameter
96 ivars = class_copyIvarList(NULL, &count);
98 testassert(count == 0);
100 // Check NULL class and count
101 ivars = class_copyIvarList(NULL, NULL);
104 // Check class with no ivars
105 cls = objc_getClass("NoIvars");
109 ivars = class_copyIvarList(cls, &count);
111 testassert(count == 0);