aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/obj-c++.dg/cxx-ivars-2.mm
blob: 22ab118df822baa67599dc26f19239a7d03faf0f (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
// Check if the '- .cxx_construct' and '-.cxx_destruct' methods get called
// and if they perform their desired function.

// { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } }
// { dg-do run { target *-*-darwin* } }
// { dg-skip-if "" { *-*-* } { "-fgnu-runtime" } { "" } }
// { dg-options "-fobjc-call-cxx-cdtors" }

#include "../objc-obj-c++-shared/TestsuiteObject.m"
#include <stdlib.h>
#define CHECK_IF(expr) if(!(expr)) abort()

static int ctor1_called, ctor2_called, dtor1_called;

struct bar {
  int a, b;
  bar(void) {
    a = 5; b = 6;
    ctor1_called++;
  }
  ~bar(void) {
    a = b = 99;
    dtor1_called++;
  }
};

struct boo: bar {
  int c;
  boo(int _c = 9): c(_c) {
    ctor2_called++;
  }
};

@interface Baz: TestsuiteObject {
@public
  bar aa;
}
@end

@implementation Baz
@end

@interface Foo: Baz {
@public
  int a;
  boo bb;
  bar b;
  float c;
  bar d;
}
@end

@implementation Foo
@end

int main (void)
{
  CHECK_IF(!ctor1_called && !ctor2_called && !dtor1_called); /* are we sane? */

  Baz *baz = [Baz new];
  CHECK_IF(ctor1_called && !ctor2_called && !dtor1_called);
  CHECK_IF(baz->aa.a == 5 && baz->aa.b == 6);
  ctor1_called = 0;  /* reset */
  
  [baz free];
  CHECK_IF(!ctor1_called && !ctor2_called && dtor1_called);
  dtor1_called = 0;  /* reset */

  Foo *foo = [Foo new];
  CHECK_IF(ctor1_called && ctor2_called && !dtor1_called);
  CHECK_IF(foo->bb.a == 5 && foo->bb.b == 6 && foo->bb.c == 9);
  CHECK_IF(foo->b.a == 5 && foo->b.b == 6);
  CHECK_IF(foo->d.a == 5 && foo->d.b == 6);
  ctor1_called = ctor2_called = 0;  /* reset */
  
  [foo free];
  CHECK_IF(!ctor1_called && !ctor2_called && dtor1_called);
}