OSDN Git Service

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