summaryrefslogtreecommitdiff
path: root/gcc/testsuite/objc.dg/special/load-category-2.m
blob: 7dc74595206e81a7fd23ad12dd90c46930a6608d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Contributed by Nicola Pero <nicola.pero@meta-innovation.com>, December 2010.  */
/* { dg-do run } */
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */

#include <stdio.h>
#include <stdlib.h>
#include <objc/objc.h>

#include "load-category-2.h"

/* This test tests that +load is called in the correct order for
   classes and categories.  +load needs to be called in superclasses
   before subclasses, and in the main class before categories.  */

/* Compile the classes in random order to prevent the runtime from
   sending +load in the correct order just because the classes happen
   to have been compiled in that order.  */
@implementation TestClass2
+ load
{
  printf ("[TestClass2 +load]\n");
  /* Check superclasses/subclasses +load order.  */
  check_that_load_step_was_completed (0);
  check_that_load_step_was_not_completed (1);
  check_that_load_step_was_not_completed (2);

  /* Check that the corresponding category's +load was not done.  */
  check_that_load_step_was_not_completed (4);

  complete_load_step (1);
}
@end

@implementation TestClass3
+ load
{
  printf ("[TestClass3 +load]\n");

  /* Check superclasses/subclasses +load order.  */
  check_that_load_step_was_completed (0);
  check_that_load_step_was_completed (1);
  check_that_load_step_was_not_completed (2);

  /* Check that the corresponding category's +load was not done.  */
  check_that_load_step_was_not_completed (5);

  complete_load_step (2);
}
@end

@implementation TestClass1
+ initialize { return self; }
+ load
{
  printf ("[TestClass1 +load]\n");

  /* Check superclasses/subclasses +load order.  */
  check_that_load_step_was_not_completed (0);
  check_that_load_step_was_not_completed (1);
  check_that_load_step_was_not_completed (2);

  /* Check that the corresponding category's +load was not done.  */
  check_that_load_step_was_not_completed (3);

  complete_load_step (0);
}
@end


static BOOL load_step_completed[6] = { NO, NO, NO, NO, NO, NO };

void complete_load_step (int load_step)
{
  load_step_completed[load_step] = YES;
}

void check_that_load_step_was_completed (int load_step)
{
  if (load_step_completed[load_step] == NO)
    {
      printf ("Load step %d was not completed but should have been\n", load_step);
      abort ();
    }
}

void check_that_load_step_was_not_completed (int load_step)
{
  if (load_step_completed[load_step] == YES)
    {
      printf ("Load step %d was completed but shouldn't have been\n", load_step);
      abort ();
    }
}

int main (void)
{
  check_that_load_step_was_completed (0);
  check_that_load_step_was_completed (1);
  check_that_load_step_was_completed (2);
  check_that_load_step_was_completed (3);
  check_that_load_step_was_completed (4);
  check_that_load_step_was_completed (5);

  return 0;
}