blob: cd976b245876d234946b0c4c66d28e5e4f05b242 [file] [log] [blame]
Aart Bikd141f452015-07-16 17:40:44 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//
18// Test on (in)variant static and instance field and array references in loops.
19//
20public class Main {
21
22 private static Object anObject = new Object();
23 private static Object anotherObject = new Object();
24
25 //
26 // Static fields.
27 //
28
29 private static boolean sZ;
30 private static byte sB;
31 private static char sC;
32 private static short sS;
33 private static int sI;
34 private static long sJ;
35 private static float sF;
36 private static double sD;
37 private static Object sL;
38
39 //
40 // Static arrays.
41 //
42
43 private static boolean[] sArrZ;
44 private static byte[] sArrB;
45 private static char[] sArrC;
46 private static short[] sArrS;
47 private static int[] sArrI;
48 private static long[] sArrJ;
49 private static float[] sArrF;
50 private static double[] sArrD;
51 private static Object[] sArrL;
52
53 //
54 // Instance fields.
55 //
56
57 private boolean mZ;
58 private byte mB;
59 private char mC;
60 private short mS;
61 private int mI;
62 private long mJ;
63 private float mF;
64 private double mD;
65 private Object mL;
66
67 //
68 // Instance arrays.
69 //
70
71 private boolean[] mArrZ;
72 private byte[] mArrB;
73 private char[] mArrC;
74 private short[] mArrS;
75 private int[] mArrI;
76 private long[] mArrJ;
77 private float[] mArrF;
78 private double[] mArrD;
79 private Object[] mArrL;
80
81 //
82 // Loops on static arrays with invariant static field references.
83 //
84
85 private static void SInvLoopZ() {
86 for (int i = 0; i < sArrZ.length; i++) {
87 sArrZ[i] = sZ;
88 }
89 }
90
91 private static void SInvLoopB() {
92 for (int i = 0; i < sArrB.length; i++) {
93 sArrB[i] = sB;
94 }
95 }
96
97 private static void SInvLoopC() {
98 for (int i = 0; i < sArrC.length; i++) {
99 sArrC[i] = sC;
100 }
101 }
102
103 private static void SInvLoopS() {
104 for (int i = 0; i < sArrS.length; i++) {
105 sArrS[i] = sS;
106 }
107 }
108
109 private static void SInvLoopI() {
110 for (int i = 0; i < sArrI.length; i++) {
111 sArrI[i] = sI;
112 }
113 }
114
115 private static void SInvLoopJ() {
116 for (int i = 0; i < sArrJ.length; i++) {
117 sArrJ[i] = sJ;
118 }
119 }
120
121 private static void SInvLoopF() {
122 for (int i = 0; i < sArrF.length; i++) {
123 sArrF[i] = sF;
124 }
125 }
126
127 private static void SInvLoopD() {
128 for (int i = 0; i < sArrD.length; i++) {
129 sArrD[i] = sD;
130 }
131 }
132
133 private static void SInvLoopL() {
134 for (int i = 0; i < sArrL.length; i++) {
135 sArrL[i] = sL;
136 }
137 }
138
139 //
140 // Loops on static arrays with variant static field references.
141 //
142
143 private static void SVarLoopZ() {
144 for (int i = 0; i < sArrZ.length; i++) {
145 sArrZ[i] = sZ;
146 if (i == 10)
147 sZ = !sZ;
148 }
149 }
150
151 private static void SVarLoopB() {
152 for (int i = 0; i < sArrB.length; i++) {
153 sArrB[i] = sB;
154 if (i == 10)
155 sB++;
156 }
157 }
158
159 private static void SVarLoopC() {
160 for (int i = 0; i < sArrC.length; i++) {
161 sArrC[i] = sC;
162 if (i == 10)
163 sC++;
164 }
165 }
166
167 private static void SVarLoopS() {
168 for (int i = 0; i < sArrS.length; i++) {
169 sArrS[i] = sS;
170 if (i == 10)
171 sS++;
172 }
173 }
174
175 private static void SVarLoopI() {
176 for (int i = 0; i < sArrI.length; i++) {
177 sArrI[i] = sI;
178 if (i == 10)
179 sI++;
180 }
181 }
182
183 private static void SVarLoopJ() {
184 for (int i = 0; i < sArrJ.length; i++) {
185 sArrJ[i] = sJ;
186 if (i == 10)
187 sJ++;
188 }
189 }
190
191 private static void SVarLoopF() {
192 for (int i = 0; i < sArrF.length; i++) {
193 sArrF[i] = sF;
194 if (i == 10)
195 sF++;
196 }
197 }
198
199 private static void SVarLoopD() {
200 for (int i = 0; i < sArrD.length; i++) {
201 sArrD[i] = sD;
202 if (i == 10)
203 sD++;
204 }
205 }
206
207 private static void SVarLoopL() {
208 for (int i = 0; i < sArrL.length; i++) {
209 sArrL[i] = sL;
210 if (i == 10)
211 sL = anotherObject;
212 }
213 }
214
215 //
216 // Loops on instance arrays with invariant instance field references.
217 //
218
219 private void InvLoopZ() {
220 for (int i = 0; i < mArrZ.length; i++) {
221 mArrZ[i] = mZ;
222 }
223 }
224
225 private void InvLoopB() {
226 for (int i = 0; i < mArrB.length; i++) {
227 mArrB[i] = mB;
228 }
229 }
230
231 private void InvLoopC() {
232 for (int i = 0; i < mArrC.length; i++) {
233 mArrC[i] = mC;
234 }
235 }
236
237 private void InvLoopS() {
238 for (int i = 0; i < mArrS.length; i++) {
239 mArrS[i] = mS;
240 }
241 }
242
243 private void InvLoopI() {
244 for (int i = 0; i < mArrI.length; i++) {
245 mArrI[i] = mI;
246 }
247 }
248
249 private void InvLoopJ() {
250 for (int i = 0; i < mArrJ.length; i++) {
251 mArrJ[i] = mJ;
252 }
253 }
254
255 private void InvLoopF() {
256 for (int i = 0; i < mArrF.length; i++) {
257 mArrF[i] = mF;
258 }
259 }
260
261 private void InvLoopD() {
262 for (int i = 0; i < mArrD.length; i++) {
263 mArrD[i] = mD;
264 }
265 }
266
267 private void InvLoopL() {
268 for (int i = 0; i < mArrL.length; i++) {
269 mArrL[i] = mL;
270 }
271 }
272
273 //
274 // Loops on instance arrays with variant instance field references.
275 //
276
277 private void VarLoopZ() {
278 for (int i = 0; i < mArrZ.length; i++) {
279 mArrZ[i] = mZ;
280 if (i == 10)
281 mZ = !mZ;
282 }
283 }
284
285 private void VarLoopB() {
286 for (int i = 0; i < mArrB.length; i++) {
287 mArrB[i] = mB;
288 if (i == 10)
289 mB++;
290 }
291 }
292
293 private void VarLoopC() {
294 for (int i = 0; i < mArrC.length; i++) {
295 mArrC[i] = mC;
296 if (i == 10)
297 mC++;
298 }
299 }
300
301 private void VarLoopS() {
302 for (int i = 0; i < mArrS.length; i++) {
303 mArrS[i] = mS;
304 if (i == 10)
305 mS++;
306 }
307 }
308
309 private void VarLoopI() {
310 for (int i = 0; i < mArrI.length; i++) {
311 mArrI[i] = mI;
312 if (i == 10)
313 mI++;
314 }
315 }
316
317 private void VarLoopJ() {
318 for (int i = 0; i < mArrJ.length; i++) {
319 mArrJ[i] = mJ;
320 if (i == 10)
321 mJ++;
322 }
323 }
324
325 private void VarLoopF() {
326 for (int i = 0; i < mArrF.length; i++) {
327 mArrF[i] = mF;
328 if (i == 10)
329 mF++;
330 }
331 }
332
333 private void VarLoopD() {
334 for (int i = 0; i < mArrD.length; i++) {
335 mArrD[i] = mD;
336 if (i == 10)
337 mD++;
338 }
339 }
340
341 private void VarLoopL() {
342 for (int i = 0; i < mArrL.length; i++) {
343 mArrL[i] = mL;
344 if (i == 10)
345 mL = anotherObject;
346 }
347 }
348 //
349 // Driver and testers.
350 //
351
352 public static void main(String[] args) {
353 DoStaticTests();
354 new Main().DoInstanceTests();
355 }
356
357 private static void DoStaticTests() {
358 // Type Z.
359 sZ = true;
360 sArrZ = new boolean[100];
361 SInvLoopZ();
362 for (int i = 0; i < sArrZ.length; i++) {
363 expectEquals(true, sArrZ[i]);
364 }
365 SVarLoopZ();
366 for (int i = 0; i < sArrZ.length; i++) {
367 expectEquals(i <= 10, sArrZ[i]);
368 }
369 // Type B.
370 sB = 1;
371 sArrB = new byte[100];
372 SInvLoopB();
373 for (int i = 0; i < sArrB.length; i++) {
374 expectEquals(1, sArrB[i]);
375 }
376 SVarLoopB();
377 for (int i = 0; i < sArrB.length; i++) {
378 expectEquals(i <= 10 ? 1 : 2, sArrB[i]);
379 }
380 // Type C.
381 sC = 2;
382 sArrC = new char[100];
383 SInvLoopC();
384 for (int i = 0; i < sArrC.length; i++) {
385 expectEquals(2, sArrC[i]);
386 }
387 SVarLoopC();
388 for (int i = 0; i < sArrC.length; i++) {
389 expectEquals(i <= 10 ? 2 : 3, sArrC[i]);
390 }
391 // Type S.
392 sS = 3;
393 sArrS = new short[100];
394 SInvLoopS();
395 for (int i = 0; i < sArrS.length; i++) {
396 expectEquals(3, sArrS[i]);
397 }
398 SVarLoopS();
399 for (int i = 0; i < sArrS.length; i++) {
400 expectEquals(i <= 10 ? 3 : 4, sArrS[i]);
401 }
402 // Type I.
403 sI = 4;
404 sArrI = new int[100];
405 SInvLoopI();
406 for (int i = 0; i < sArrI.length; i++) {
407 expectEquals(4, sArrI[i]);
408 }
409 SVarLoopI();
410 for (int i = 0; i < sArrI.length; i++) {
411 expectEquals(i <= 10 ? 4 : 5, sArrI[i]);
412 }
413 // Type J.
414 sJ = 5;
415 sArrJ = new long[100];
416 SInvLoopJ();
417 for (int i = 0; i < sArrJ.length; i++) {
418 expectEquals(5, sArrJ[i]);
419 }
420 SVarLoopJ();
421 for (int i = 0; i < sArrJ.length; i++) {
422 expectEquals(i <= 10 ? 5 : 6, sArrJ[i]);
423 }
424 // Type F.
425 sF = 6.0f;
426 sArrF = new float[100];
427 SInvLoopF();
428 for (int i = 0; i < sArrF.length; i++) {
429 expectEquals(6, sArrF[i]);
430 }
431 SVarLoopF();
432 for (int i = 0; i < sArrF.length; i++) {
433 expectEquals(i <= 10 ? 6 : 7, sArrF[i]);
434 }
435 // Type D.
436 sD = 7.0;
437 sArrD = new double[100];
438 SInvLoopD();
439 for (int i = 0; i < sArrD.length; i++) {
440 expectEquals(7.0, sArrD[i]);
441 }
442 SVarLoopD();
443 for (int i = 0; i < sArrD.length; i++) {
444 expectEquals(i <= 10 ? 7 : 8, sArrD[i]);
445 }
446 // Type L.
447 sL = anObject;
448 sArrL = new Object[100];
449 SInvLoopL();
450 for (int i = 0; i < sArrL.length; i++) {
451 expectEquals(anObject, sArrL[i]);
452 }
453 SVarLoopL();
454 for (int i = 0; i < sArrL.length; i++) {
455 expectEquals(i <= 10 ? anObject : anotherObject, sArrL[i]);
456 }
457 }
458
459 private void DoInstanceTests() {
460 // Type Z.
461 mZ = true;
462 mArrZ = new boolean[100];
463 InvLoopZ();
464 for (int i = 0; i < mArrZ.length; i++) {
465 expectEquals(true, mArrZ[i]);
466 }
467 VarLoopZ();
468 for (int i = 0; i < mArrZ.length; i++) {
469 expectEquals(i <= 10, mArrZ[i]);
470 }
471 // Type B.
472 mB = 1;
473 mArrB = new byte[100];
474 InvLoopB();
475 for (int i = 0; i < mArrB.length; i++) {
476 expectEquals(1, mArrB[i]);
477 }
478 VarLoopB();
479 for (int i = 0; i < mArrB.length; i++) {
480 expectEquals(i <= 10 ? 1 : 2, mArrB[i]);
481 }
482 // Type C.
483 mC = 2;
484 mArrC = new char[100];
485 InvLoopC();
486 for (int i = 0; i < mArrC.length; i++) {
487 expectEquals(2, mArrC[i]);
488 }
489 VarLoopC();
490 for (int i = 0; i < mArrC.length; i++) {
491 expectEquals(i <= 10 ? 2 : 3, mArrC[i]);
492 }
493 // Type S.
494 mS = 3;
495 mArrS = new short[100];
496 InvLoopS();
497 for (int i = 0; i < mArrS.length; i++) {
498 expectEquals(3, mArrS[i]);
499 }
500 VarLoopS();
501 for (int i = 0; i < mArrS.length; i++) {
502 expectEquals(i <= 10 ? 3 : 4, mArrS[i]);
503 }
504 // Type I.
505 mI = 4;
506 mArrI = new int[100];
507 InvLoopI();
508 for (int i = 0; i < mArrI.length; i++) {
509 expectEquals(4, mArrI[i]);
510 }
511 VarLoopI();
512 for (int i = 0; i < mArrI.length; i++) {
513 expectEquals(i <= 10 ? 4 : 5, mArrI[i]);
514 }
515 // Type J.
516 mJ = 5;
517 mArrJ = new long[100];
518 InvLoopJ();
519 for (int i = 0; i < mArrJ.length; i++) {
520 expectEquals(5, mArrJ[i]);
521 }
522 VarLoopJ();
523 for (int i = 0; i < mArrJ.length; i++) {
524 expectEquals(i <= 10 ? 5 : 6, mArrJ[i]);
525 }
526 // Type F.
527 mF = 6.0f;
528 mArrF = new float[100];
529 InvLoopF();
530 for (int i = 0; i < mArrF.length; i++) {
531 expectEquals(6, mArrF[i]);
532 }
533 VarLoopF();
534 for (int i = 0; i < mArrF.length; i++) {
535 expectEquals(i <= 10 ? 6 : 7, mArrF[i]);
536 }
537 // Type D.
538 mD = 7.0;
539 mArrD = new double[100];
540 InvLoopD();
541 for (int i = 0; i < mArrD.length; i++) {
542 expectEquals(7.0, mArrD[i]);
543 }
544 VarLoopD();
545 for (int i = 0; i < mArrD.length; i++) {
546 expectEquals(i <= 10 ? 7 : 8, mArrD[i]);
547 }
548 // Type L.
549 mL = anObject;
550 mArrL = new Object[100];
551 InvLoopL();
552 for (int i = 0; i < mArrL.length; i++) {
553 expectEquals(anObject, mArrL[i]);
554 }
555 VarLoopL();
556 for (int i = 0; i < mArrL.length; i++) {
557 expectEquals(i <= 10 ? anObject : anotherObject, mArrL[i]);
558 }
559 }
560
561 private static void expectEquals(boolean expected, boolean result) {
562 if (expected != result) {
563 throw new Error("Expected: " + expected + ", found: " + result);
564 }
565 }
566
567 private static void expectEquals(byte expected, byte result) {
568 if (expected != result) {
569 throw new Error("Expected: " + expected + ", found: " + result);
570 }
571 }
572
573 private static void expectEquals(char expected, char result) {
574 if (expected != result) {
575 throw new Error("Expected: " + expected + ", found: " + result);
576 }
577 }
578
579 private static void expectEquals(short expected, short result) {
580 if (expected != result) {
581 throw new Error("Expected: " + expected + ", found: " + result);
582 }
583 }
584
585 private static void expectEquals(int expected, int result) {
586 if (expected != result) {
587 throw new Error("Expected: " + expected + ", found: " + result);
588 }
589 }
590
591 private static void expectEquals(long expected, long result) {
592 if (expected != result) {
593 throw new Error("Expected: " + expected + ", found: " + result);
594 }
595 }
596
597 private static void expectEquals(float expected, float result) {
598 if (expected != result) {
599 throw new Error("Expected: " + expected + ", found: " + result);
600 }
601 }
602
603 private static void expectEquals(double expected, double result) {
604 if (expected != result) {
605 throw new Error("Expected: " + expected + ", found: " + result);
606 }
607 }
608
609 private static void expectEquals(Object expected, Object result) {
610 if (expected != result) {
611 throw new Error("Expected: " + expected + ", found: " + result);
612 }
613 }
614}