Debian Codegraph @

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

raw
1 // { dg-do run }
2 // Test that class iterators and imperfectly-nested loops work together.
3 // This variant tests initialization by assignment.
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 I i, j, k;
111
112 #pragma omp for collapse(3)
113 for (i = a1.begin (); i < a1.end (); i++)
114 {
115 f1 (0);
116 for (j = a2.begin (); j < a2.end (); j++)
117 {
118 f1 (1);
119 for (k = a3.begin (); k < a3.end (); k++)
120 {
121 f1 (2);
122 f2 (2);
123 }
124 f2 (1);
125 }
126 f2 (0);
127 }
128 }
129
130
131 int
132 main (void)
133 {
134
135 int index[] = {0, 1, 2, 3, 4, 5};
136
137 J x (&index[0], &index[3]);
138 J y (&index[0], &index[4]);
139 J z (&index[0], &index[5]);
140
141 f1count[0] = 0;
142 f1count[1] = 0;
143 f1count[2] = 0;
144 f2count[0] = 0;
145 f2count[1] = 0;
146 f2count[2] = 0;
147
148 s1 (x, y, z);
149
150 /* All intervening code at the same depth must be executed the same
151 number of times. */
152 if (f1count[0] != f2count[0]) abort ();
153 if (f1count[1] != f2count[1]) abort ();
154 if (f1count[2] != f2count[2]) abort ();
155
156 /* Intervening code must be executed at least as many times as the loop
157 that encloses it. */
158 if (f1count[0] < 3) abort ();
159 if (f1count[1] < 3 * 4) abort ();
160
161 /* Intervening code must not be executed more times than the number
162 of logical iterations. */
163 if (f1count[0] > 3 * 4 * 5) abort ();
164 if (f1count[1] > 3 * 4 * 5) abort ();
165
166 /* Check that the innermost loop body is executed exactly the number
167 of logical iterations expected. */
168 if (f1count[2] != 3 * 4 * 5) abort ();
169 }