Debian Codegraph @

libgomp/testsuite/libgomp.c++/imperfect-class-3.C

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