aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/objc.dg/gnu-api-2-sel.m
blob: e710083232d25ff4b74a888532b541b2d6c74adb (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
/* Test the Modern GNU Objective-C Runtime API.

  This is test 'sel', covering all functions starting with 'sel'.  */

/* { dg-do run } */
/* { dg-skip-if "" { *-*-* } { "-fnext-runtime" } { "" } } */

/* To get the modern GNU Objective-C Runtime API, you include
   objc/runtime.h.  */
#include <objc/runtime.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

@interface MyRootClass
{ Class isa; }
+ alloc;
- init;
@end

@implementation MyRootClass
+ alloc { return class_createInstance (self, 0); }
- init  { return self; }
@end

@protocol MyProtocol
- (id) variable;
@end

@protocol MySecondProtocol
- (id) setVariable: (id)value;
@end

@interface MySubClass : MyRootClass <MyProtocol>
{ id variable_ivar; }
- (void) setVariable: (id)value;
- (id) variable;
@end

@implementation MySubClass
- (void) setVariable: (id)value { variable_ivar = value; }
- (id) variable { return variable_ivar; }
@end


int main(int argc, void **args)
{
  /* Functions are tested in alphabetical order.  */

  printf ("Testing sel_getName () ...\n");
  {
    if (strcmp (sel_getName (@selector (variable)), "variable") != 0)
      abort ();

    if (strcmp (sel_getName (NULL), "<null selector>") != 0)
      abort ();
  }

  printf ("Testing sel_getType () ...\n");
  {
    /* Get a selector from a real class, so it has interesting
       types.  */
    Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
					     @selector (variable));
    
    if (strcmp (sel_getType (method_getName (method)), method_getTypeEncoding (method)) != 0)
      abort ();
  }

  printf ("Testing sel_getUid () ...\n");
  {
    if (strcmp (sel_getName (sel_getUid ("myMethod")), "myMethod") != 0)
      abort ();
  }

  printf ("Testing sel_isEqual () ...\n");
  {
    if (! sel_isEqual (@selector (setVariable:), @selector (setVariable:)))
      abort ();
  }
  
  printf ("Testing sel_registerName () ...\n");
  {
    if (strcmp (sel_getName (sel_registerName ("myMethod")), "myMethod") != 0)
      abort ();
  }

  printf ("Testing set_registerTypedName () ...\n");
  {
    const char *types = method_getTypeEncoding (class_getInstanceMethod 
						(objc_getClass ("MySubClass"),
						 @selector (variable)));
    SEL selector = sel_registerTypedName ("aMethod", types);
    
    if (strcmp (sel_getName (selector), "aMethod") != 0)
      abort ();

    if (strcmp (sel_getType (selector), types) != 0)
      abort ();
  }

  return 0;
}