1// TEST_CONFIG
2
3#include "test.h"
4#include "testroot.i"
5
6int state = 0;
7int catstate = 0;
8int deallocstate = 0;
9
10@interface Deallocator : TestRoot @end
11@implementation Deallocator
12-(id)init {
13    self = [super init];
14    if (objc_collectingEnabled()) {
15        deallocstate = 1;
16    }
17    return self;
18}
19-(void)dealloc {
20    deallocstate = 1;
21    SUPER_DEALLOC();
22}
23@end
24
25
26@interface Super : TestRoot @end
27@implementation Super
28+(void)initialize { 
29    if (self == [Super class]) {
30        testprintf("in +[Super initialize]\n");
31        testassert(state == 2);
32        state = 3;
33    } else { 
34        testprintf("in +[Super initialize] on behalf of Sub\n");
35        testassert(state == 3);
36        state = 4;
37    }
38} 
39-(void)load { fail("-[Super load] called!"); }
40+(void)load { 
41    testprintf("in +[Super load]\n");
42    testassert(state == 0); 
43    state = 1;
44} 
45@end
46
47@interface Sub : Super { }  @end
48@implementation Sub
49+(void)load { 
50    testprintf("in +[Sub load]\n");
51    testassert(state == 1); 
52    state = 2;
53} 
54-(void)load { fail("-[Sub load] called!"); } 
55@end
56
57@interface SubNoLoad : Super { } @end
58@implementation SubNoLoad @end
59
60@interface Super (Category) @end
61@implementation Super (Category) 
62-(void)load { fail("-[Super(Category) load called!"); }
63+(void)load {
64    testprintf("in +[Super(Category) load]\n");
65    testassert(state >= 1); 
66    catstate++;
67}
68@end
69
70
71@interface Sub (Category) @end
72@implementation Sub (Category) 
73-(void)load { fail("-[Sub(Category) load called!"); }
74+(void)load {
75    testprintf("in +[Sub(Category) load]\n");
76    testassert(state >= 2); 
77    catstate++;
78
79    // test autorelease pool
80    __autoreleasing id x;
81    x = AUTORELEASE([Deallocator new]);
82}
83@end
84
85
86@interface SubNoLoad (Category) @end
87@implementation SubNoLoad (Category) 
88-(void)load { fail("-[SubNoLoad(Category) load called!"); }
89+(void)load {
90    testprintf("in +[SubNoLoad(Category) load]\n");
91    testassert(state >= 1); 
92    catstate++;
93}
94@end
95
96int main()
97{
98    testassert(state == 2);
99    testassert(catstate == 3);
100    testassert(deallocstate == 1);
101    [Sub class];
102    testassert(state == 4);
103    testassert(catstate == 3);
104
105    succeed(__FILE__);
106}
107