Debian Codegraph @

libgomp/testsuite/libgomp.c++/imperfect-template-2.C

raw
1 // { dg-do run }
2 // Test that template class iterators and imperfectly-nested loops
3 // work together.
4 // This variant tests initialization by declaration.
5
6 typedef __PTRDIFF_TYPE__ ptrdiff_t;
7 extern "C" void abort ();
8
9 template <typename T>
10 class I
11 {
12 public:
13 typedef ptrdiff_t difference_type;
14 I ();
15 ~I ();
16 I (T *);
17 I (const I &);
18 T &operator * ();
19 T *operator -> ();
20 T &operator [] (const difference_type &) const;
21 I &operator = (const I &);
22 I &operator ++ ();
23 I operator ++ (int);
24 I &operator -- ();
25 I operator -- (int);
26 I &operator += (const difference_type &);
27 I &operator -= (const difference_type &);
28 I operator + (const difference_type &) const;
29 I operator - (const difference_type &) const;
30 template <typename S> friend bool operator == (I<S> &, I<S> &);
31 template <typename S> friend bool operator == (const I<S> &, const I<S> &);
32 template <typename S> friend bool operator < (I<S> &, I<S> &);
33 template <typename S> friend bool operator < (const I<S> &, const I<S> &);
34 template <typename S> friend bool operator <= (I<S> &, I<S> &);
35 template <typename S> friend bool operator <= (const I<S> &, const I<S> &);
36 template <typename S> friend bool operator > (I<S> &, I<S> &);
37 template <typename S> friend bool operator > (const I<S> &, const I<S> &);
38 template <typename S> friend bool operator >= (I<S> &, I<S> &);
39 template <typename S> friend bool operator >= (const I<S> &, const I<S> &);
40 template <typename S> friend typename I<S>::difference_type operator - (I<S> &, I<S> &);
41 template <typename S> friend typename I<S>::difference_type operator - (const I<S> &, const I<S> &);
42 template <typename S> friend I<S> operator + (typename I<S>::difference_type , const I<S> &);
43 private:
44 T *p;
45 };
46 template <typename T> I<T>::I () : p (0) {}
47 template <typename T> I<T>::~I () { p = (T *) 0; }
48 template <typename T> I<T>::I (T *x) : p (x) {}
49 template <typename T> I<T>::I (const I &x) : p (x.p) {}
50 template <typename T> T &I<T>::operator * () { return *p; }
51 template <typename T> T *I<T>::operator -> () { return p; }
52 template <typename T> T &I<T>::operator [] (const difference_type &x) const { return p[x]; }
53 template <typename T> I<T> &I<T>::operator = (const I &x) { p = x.p; return *this; }
54 template <typename T> I<T> &I<T>::operator ++ () { ++p; return *this; }
55 template <typename T> I<T> I<T>::operator ++ (int) { return I (p++); }
56 template <typename T> I<T> &I<T>::operator -- () { --p; return *this; }
57 template <typename T> I<T> I<T>::operator -- (int) { return I (p--); }
58 template <typename T> I<T> &I<T>::operator += (const difference_type &x) { p += x; return *this; }
59 template <typename T> I<T> &I<T>::operator -= (const difference_type &x) { p -= x; return *this; }
60 template <typename T> I<T> I<T>::operator + (const difference_type &x) const { return I (p + x); }
61 template <typename T> I<T> I<T>::operator - (const difference_type &x) const { return I (p - x); }
62 template <typename T> bool operator == (I<T> &x, I<T> &y) { return x.p == y.p; }
63 template <typename T> bool operator == (const I<T> &x, const I<T> &y) { return x.p == y.p; }
64 template <typename T> bool operator != (I<T> &x, I<T> &y) { return !(x == y); }
65 template <typename T> bool operator != (const I<T> &x, const I<T> &y) { return !(x == y); }
66 template <typename T> bool operator < (I<T> &x, I<T> &y) { return x.p < y.p; }
67 template <typename T> bool operator < (const I<T> &x, const I<T> &y) { return x.p < y.p; }
68 template <typename T> bool operator <= (I<T> &x, I<T> &y) { return x.p <= y.p; }
69 template <typename T> bool operator <= (const I<T> &x, const I<T> &y) { return x.p <= y.p; }
70 template <typename T> bool operator > (I<T> &x, I<T> &y) { return x.p > y.p; }
71 template <typename T> bool operator > (const I<T> &x, const I<T> &y) { return x.p > y.p; }
72 template <typename T> bool operator >= (I<T> &x, I<T> &y) { return x.p >= y.p; }
73 template <typename T> bool operator >= (const I<T> &x, const I<T> &y) { return x.p >= y.p; }
74 template <typename T> typename I<T>::difference_type operator - (I<T> &x, I<T> &y) { return x.p - y.p; }
75 template <typename T> typename I<T>::difference_type operator - (const I<T> &x, const I<T> &y) { return x.p - y.p; }
76 template <typename T> I<T> operator + (typename I<T>::difference_type x, const I<T> &y) { return I<T> (x + y.p); }
77
78 template <typename T>
79 class J
80 {
81 public:
82 J(const I<T> &x, const I<T> &y) : b (x), e (y) {}
83 const I<T> &begin ();
84 const I<T> &end ();
85 private:
86 I<T> b, e;
87 };
88
89 template <typename T> const I<T> &J<T>::begin () { return b; }
90 template <typename T> const I<T> &J<T>::end () { return e; }
91
92 static int f1count[3], f2count[3];
93
94 #ifndef __cplusplus
95 extern void abort (void);
96 #else
97 extern "C" void abort (void);
98 #endif
99
100 void f1 (int depth)
101 {
102 f1count[depth]++;
103 }
104
105 void f2 (int depth)
106 {
107 f2count[depth]++;
108 }
109
110 template <typename T>
111 void s1 (J<T> a1, J<T> a2, J<T> a3)
112 {
113 #pragma omp for collapse(3)
114 for (I<T> i = a1.begin (); i < a1.end (); i++)
115 {
116 f1 (0);
117 for (I<T> j = a2.begin (); j < a2.end (); j++)
118 {
119 f1 (1);
120 for (I<T> k = a3.begin (); k < a3.end (); k++)
121 {
122 f1 (2);
123 f2 (2);
124 }
125 f2 (1);
126 }
127 f2 (0);
128 }
129 }
130
131
132 int
133 main (void)
134 {
135
136 int index[] = {0, 1, 2, 3, 4, 5};
137
138 J<int> x (&index[0], &index[3]);
139 J<int> y (&index[0], &index[4]);
140 J<int> z (&index[0], &index[5]);
141
142 f1count[0] = 0;
143 f1count[1] = 0;
144 f1count[2] = 0;
145 f2count[0] = 0;
146 f2count[1] = 0;
147 f2count[2] = 0;
148
149 s1<int> (x, y, z);
150
151 /* All intervening code at the same depth must be executed the same
152 number of times. */
153 if (f1count[0] != f2count[0]) abort ();
154 if (f1count[1] != f2count[1]) abort ();
155 if (f1count[2] != f2count[2]) abort ();
156
157 /* Intervening code must be executed at least as many times as the loop
158 that encloses it. */
159 if (f1count[0] < 3) abort ();
160 if (f1count[1] < 3 * 4) abort ();
161
162 /* Intervening code must not be executed more times than the number
163 of logical iterations. */
164 if (f1count[0] > 3 * 4 * 5) abort ();
165 if (f1count[1] > 3 * 4 * 5) abort ();
166
167 /* Check that the innermost loop body is executed exactly the number
168 of logical iterations expected. */
169 if (f1count[2] != 3 * 4 * 5) abort ();
170 }