Debian Codegraph @

gcc/testsuite/objc.dg/special/load-category-2.m

raw
1 /* Contributed by Nicola Pero <[email protected]>, December 2010. */
2 /* { dg-do run } */
3 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
4 /* { dg-additional-options "-Wno-objc-root-class" } */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <objc/objc.h>
9
10 #include "load-category-2.h"
11
12 /* This test tests that +load is called in the correct order for
13 classes and categories. +load needs to be called in superclasses
14 before subclasses, and in the main class before categories. */
15
16 /* Compile the classes in random order to prevent the runtime from
17 sending +load in the correct order just because the classes happen
18 to have been compiled in that order. */
19 @implementation TestClass2
20 + load
21 {
22 printf ("[TestClass2 +load]\n");
23 /* Check superclasses/subclasses +load order. */
24 check_that_load_step_was_completed (0);
25 check_that_load_step_was_not_completed (1);
26 check_that_load_step_was_not_completed (2);
27
28 /* Check that the corresponding category's +load was not done. */
29 check_that_load_step_was_not_completed (4);
30
31 complete_load_step (1);
32 }
33 @end
34
35 @implementation TestClass3
36 + load
37 {
38 printf ("[TestClass3 +load]\n");
39
40 /* Check superclasses/subclasses +load order. */
41 check_that_load_step_was_completed (0);
42 check_that_load_step_was_completed (1);
43 check_that_load_step_was_not_completed (2);
44
45 /* Check that the corresponding category's +load was not done. */
46 check_that_load_step_was_not_completed (5);
47
48 complete_load_step (2);
49 }
50 @end
51
52 @implementation TestClass1
53 + initialize { return self; }
54 + load
55 {
56 printf ("[TestClass1 +load]\n");
57
58 /* Check superclasses/subclasses +load order. */
59 check_that_load_step_was_not_completed (0);
60 check_that_load_step_was_not_completed (1);
61 check_that_load_step_was_not_completed (2);
62
63 /* Check that the corresponding category's +load was not done. */
64 check_that_load_step_was_not_completed (3);
65
66 complete_load_step (0);
67 }
68 @end
69
70
71 static BOOL load_step_completed[6] = { NO, NO, NO, NO, NO, NO };
72
73 void complete_load_step (int load_step)
74 {
75 load_step_completed[load_step] = YES;
76 }
77
78 void check_that_load_step_was_completed (int load_step)
79 {
80 if (load_step_completed[load_step] == NO)
81 {
82 printf ("Load step %d was not completed but should have been\n", load_step);
83 abort ();
84 }
85 }
86
87 void check_that_load_step_was_not_completed (int load_step)
88 {
89 if (load_step_completed[load_step] == YES)
90 {
91 printf ("Load step %d was completed but shouldn't have been\n", load_step);
92 abort ();
93 }
94 }
95
96 int main (void)
97 {
98 check_that_load_step_was_completed (0);
99 check_that_load_step_was_completed (1);
100 check_that_load_step_was_completed (2);
101 check_that_load_step_was_completed (3);
102 check_that_load_step_was_completed (4);
103 check_that_load_step_was_completed (5);
104
105 return 0;
106 }