OSDN Git Service

* obj-c++.dg/defs.mm (abort): Make it extern "C".
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / obj-c++.dg / template-4.mm
1 /* Author:  Ziemowit Laski <zlaski@apple.com>.  */
2
3 /* { dg-do run } */
4 /* { dg-skip-if "" { *-*-darwin* } { "-m64" } { "" } } */
5
6 #include <objc/Object.h>
7 #include <stdarg.h>
8 #include <stdlib.h>
9
10 #ifdef __NEXT_RUNTIME__
11 /* The following ain't pretty, but does allow us to have just one copy
12    of next_mapping.h.  */
13 #include "../objc/execute/next_mapping.h"
14 #else
15 #include <objc/NXConstStr.h>
16 #endif
17
18 #define CHECK_IF(expr) if(!(expr)) abort()
19
20 template <class ARR, class TYPE> class TestT
21 {
22 public:
23   TYPE k;
24   int abc(ARR *array) {
25     return [array count] * k;
26   }
27   TestT(TYPE _k): k(_k) { }
28 };
29
30 template <class TYPE>
31 const char *getDesc(void) {
32   return [TYPE name];
33 }
34
35 @class Array;
36
37 template <class TYPE>
38 int abc(TYPE *xyz, Array *array) {
39   return [xyz count] + [array count];
40 }
41
42 @interface Array: Object {
43   id *arr;
44   int count;
45 }
46 + (id)arrayWithObjects:(id)first, ... ;
47 - (int)count;
48 @end
49
50 @implementation Array
51 + (id)arrayWithObjects:(id)first, ... {
52   Array *a = [Array new];
53   a->count = 0;
54   a->arr = (id *) calloc(8, sizeof(id));
55
56   va_list args;
57   va_start (args, first);
58   
59   a->arr[a->count++] = first;
60
61   for (id el; el = va_arg(args, id); a->count++)
62     a->arr[a->count] = el;
63
64   return a;
65 }
66 - (int)count {
67   return count;
68 }
69 @end
70
71 int main(void) {
72   CHECK_IF(!strcmp ([@"Object" cString], getDesc<Object>()));
73   CHECK_IF(!strcmp ([@"Array" cString], getDesc<Array>()));
74
75   Array* a1 = [Array arrayWithObjects:@"One", @"Two", @"Three", nil];
76   Array* a2 = [Array arrayWithObjects:@"Four", @"Five", nil];
77
78   TestT<Array, int> t(7);
79   CHECK_IF(t.abc(a1) + t.abc(a2) == 35);
80   CHECK_IF(abc(a1, a2) * t.k == 35);
81   return 0;
82 }