001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.jjs.test;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: /**
021: * This test is intended to exercise as many code paths and node types as
022: * possible in the Java to JavaScript compiler. This test is not at all intended
023: * to execute correctly.
024: */
025: public class CoverageTest extends CoverageBase {
026:
027: /**
028: * TODO: document me.
029: */
030: public class Inner extends Super {
031:
032: public int x = 3;
033: public final int y = 4;
034:
035: public Inner() {
036: // ExplicitConstructorCall
037: this (4);
038: }
039:
040: public Inner(int i) {
041: // ExplicitConstructorCall
042: super (i);
043: }
044:
045: public void foo() {
046: final int z = this .y;
047:
048: new Inner() {
049: {
050: x = z;
051: this .x = z;
052: Inner.this .x = z;
053: next = CoverageTest.this .next;
054: next.foo();
055: CoverageTest.this .next.foo();
056: CoverageTest.this .x = z;
057: CoverageTest.super .x = z; // inexpressible in Java without name mangling
058: }
059:
060: public void foo() {
061: x = z;
062: this .x = z;
063: Inner.this .x = z;
064: next = CoverageTest.this .next;
065: next.foo();
066: CoverageTest.this .next.foo();
067: CoverageTest.this .x = z;
068: CoverageTest.super .x = z; // inexpressible in Java without name mangling
069: }
070: };
071:
072: class NamedLocal extends Inner {
073: public void foo() {
074: CoverageTest.this .getNext();
075: Inner.this .bar();
076: super .bar();
077: int x = z;
078: }
079:
080: // JDT bug? This works in 5.0 but not in 1.4
081: // TODO: will javac compile it?
082: class NamedLocalSub extends NamedLocal {
083: public void foo() {
084: Inner.this .bar();
085: NamedLocal.this .foo();
086: super .foo();
087: int x = z;
088: }
089: }
090: }
091: testEmptyStatement();
092:
093: new InnerSub().new InnerSubSub().fda();
094: new SecondMain().new FunkyInner();
095: new NamedLocal().new NamedLocalSub().foo();
096: }
097:
098: public void bar() {
099: }
100:
101: private void testAllocationExpression() {
102: // AllocationExpression
103: o = new Super();
104: assertEquals("3", o.toString());
105: o = new Super(42);
106: assertEquals("42", o.toString());
107: }
108:
109: private void testAndAndExpression() {
110: // AND_AND_Expression
111: z = i == 1 && betterNotEval();
112: assertFalse(z);
113: }
114:
115: private void testArrayAllocationExpression() {
116: // ArrayAllocationExpression
117: ia = new int[4];
118: assertEquals(4, ia.length);
119: iaa = new int[4][3];
120: assertEquals(4, iaa.length);
121: assertEquals(3, iaa[2].length);
122: iaaa = new int[4][3][];
123: assertEquals(4, iaaa.length);
124: assertEquals(3, iaaa[2].length);
125: assertNull(iaaa[2][2]);
126: }
127:
128: private void testArrayInitializer() {
129: // ArrayInitializer
130: ia = new int[] { i, j };
131: assertEquals(2, ia.length);
132: assertEquals(j, ia[1]);
133: iaa = new int[][] { { i, j } };
134: assertEquals(1, iaa.length);
135: assertEquals(2, iaa[0].length);
136: assertEquals(j, iaa[0][1]);
137: iaa = new int[][] { { i, j }, ia };
138: assertEquals(2, iaa.length);
139: assertEquals(2, iaa[0].length);
140: assertEquals(j, iaa[0][1]);
141: assertEquals(ia, iaa[1]);
142: }
143:
144: private void testArrayReference() {
145: // ArrayReference
146: i = ia[0];
147: assertEquals(ia[0], i);
148: ia[0] = i;
149: }
150:
151: /**
152: * TODO(later): implement asserts.
153: */
154: private void testAssertStatement() {
155: // i = 1;
156: // try {
157: // assert i == 2;
158: // fail();
159: // } catch (AssertionError e) {
160: // }
161: // try {
162: // assert i == 3 : "foo";
163: // fail();
164: // } catch (AssertionError e) {
165: // assertEquals("foo", e.getMessage());
166: // }
167: }
168:
169: private void testAssignment() {
170: // Assignment
171: i = j;
172: assertEquals(j, i);
173: }
174:
175: private void testBinaryExpression() {
176: // BinaryExpression
177: i = 4;
178: i = i + j;
179: assertEquals(6, i);
180: i = i - j;
181: assertEquals(4, i);
182: i = i * j;
183: assertEquals(8, i);
184: i = i / j;
185: assertEquals(4, i);
186: i = i % j;
187: assertEquals(0, i);
188: i = 7;
189: i = i & j;
190: assertEquals(2, i);
191: i = 0;
192: i = i | j;
193: assertEquals(2, i);
194: i = 7;
195: i = i ^ j;
196: assertEquals(5, i);
197: i = i << j;
198: assertEquals(20, i);
199: i = i >> j;
200: assertEquals(5, i);
201: i = i >>> j;
202: assertEquals(1, i);
203: }
204:
205: private void testBreakContinueLabelStatement() {
206: // BreakStatement, ContinueStatement
207: z = true;
208: i = 0;
209: x = 0;
210: outer: while (z) {
211: ++x;
212: inner: while (z) {
213: ++i;
214: if (i == 1)
215: continue;
216: if (i == 2)
217: continue outer;
218: if (i == 3)
219: break;
220: if (i == 4)
221: break outer;
222: }
223: }
224: assertEquals(4, i);
225: assertEquals(3, x);
226: }
227:
228: private void testCaseSwitchStatement() {
229: // CaseStatement, SwitchStatement
230: i = 6;
231: switch (j) {
232: case 1:
233: ++i;
234: case 2:
235: i += 2;
236: case 3:
237: i += 3;
238: case 4:
239: i += 4;
240: default:
241: i += 0;
242: }
243: assertEquals(15, i);
244: }
245:
246: private void testCastExpression() {
247: // CastExpression
248: o = (Super) o;
249: }
250:
251: private void testCharLiteral() {
252: // CharLiteral
253: i = 'c';
254: assertEquals("c", String.valueOf((char) i));
255: }
256:
257: private void testClassLiteralAccess() {
258: // ClassLiteralAccess
259: o = Super.class;
260: assertEquals(
261: "class com.google.gwt.dev.jjs.test.CoverageTest$Super",
262: o.toString());
263: }
264:
265: private void testCompoundAssignment() {
266: // CompoundAssignment
267: i = 4;
268: i += j;
269: assertEquals(6, i);
270: i -= j;
271: assertEquals(4, i);
272: i *= j;
273: assertEquals(8, i);
274: i /= j;
275: assertEquals(4, i);
276: i %= j;
277: assertEquals(0, i);
278: i = 7;
279: i &= j;
280: assertEquals(2, i);
281: i = 0;
282: i |= j;
283: assertEquals(2, i);
284: i = 7;
285: i ^= j;
286: assertEquals(5, i);
287: i <<= j;
288: assertEquals(20, i);
289: i >>= j;
290: assertEquals(5, i);
291: i >>>= j;
292: assertEquals(1, i);
293: }
294:
295: private void testConditionalExpression() {
296: // ConditionalExpression
297: z = false;
298: i = z ? 7 : j;
299: assertEquals(j, i);
300: }
301:
302: private void testDoStatement() {
303: // DoStatement
304: i = 3;
305: z = false;
306: do
307: i += j;
308: while (z);
309: assertEquals(5, i);
310: }
311:
312: private void testEmptyStatement() {
313: // EmptyStatement
314: ;
315: }
316:
317: private void testEqualExpression() {
318: // EqualExpression
319: i = 3;
320: assertFalse(i == j);
321: assertTrue(i != j);
322: assertFalse(i < j);
323: assertFalse(i <= j);
324: assertTrue(i > j);
325: assertTrue(i >= j);
326: }
327:
328: private void testForeachStatement() {
329: for (int q : ia) {
330: i = q;
331: }
332: }
333:
334: private void testForStatement() {
335: // ForStatement
336: i = 0;
337: for (int q = 0, v = 4; q < v; ++q)
338: i += q;
339: assertEquals(6, i);
340: for (i = 0; i < 4; ++i) {
341: }
342: assertEquals(4, i);
343: }
344:
345: private void testIfStatement() {
346: // IfStatement
347: z = false;
348: if (z)
349: fail();
350: if (z)
351: fail();
352: else
353: assertFalse(z);
354: if (!z)
355: assertFalse(z);
356: else
357: fail();
358: }
359:
360: private void testInstanceOfExpression() {
361: // InstanceOfExpression
362: Object o = CoverageTest.this ;
363: assertTrue(o instanceof CoverageBase);
364: }
365:
366: private void testLiterals() {
367: // DoubleLiteral
368: d = 3.141592653589793;
369: assertEquals(3, (int) d);
370:
371: // FalseLiteral
372: assertFalse(false);
373:
374: // FloatLiteral
375: f = 3.1415927f;
376: assertEquals(3, (int) f);
377:
378: // IntLiteral
379: i = 4;
380:
381: // IntLiteralMinValue
382: i = -2147483648;
383:
384: // LongLiteral
385: l = 4L;
386:
387: // LongLiteralMinValue
388: l = -9223372036854775808L;
389:
390: // NullLiteral
391: o = null;
392:
393: // StringLiteral
394: s = "f'oo\b\t\n\f\r\"\\";
395: assertEquals(s, "f" + '\'' + 'o' + 'o' + '\b' + '\t' + '\n'
396: + '\f' + '\r' + '"' + '\\');
397:
398: // TrueLiteral
399: assertTrue(true);
400: }
401:
402: private void testOrOrExpression() {
403: // OR_OR_Expression
404: i = 1;
405: assertTrue(i == 1 || betterNotEval());
406: }
407:
408: private void testPostfixExpression() {
409: // PostfixExpression
410: assertEquals(1, i++);
411: assertEquals(2, i--);
412: }
413:
414: private void testPrefixExpression() {
415: // PrefixExpression
416: assertEquals(2, ++i);
417: assertEquals(1, --i);
418: }
419:
420: private void testQualifiedAllocationExpression() {
421: // QualifiedAllocationExpression
422: o = new Inner();
423: o = CoverageTest.this .new Inner();
424: o = new CoverageTest().new Inner();
425: }
426:
427: private void testQualifiedNameReference() {
428: // QualifiedNameReference
429: // TODO: fields????
430: CoverageTest m = new CoverageTest();
431: ia = new int[2];
432: assertEquals("1", 2, ia.length);
433: assertEquals("2", 2, m.j);
434: assertEquals("3", 4, m.y);
435: assertEquals("4", 2, new CoverageTest().j);
436: assertEquals("5", 4, new CoverageTest().y);
437: assertEquals("6", 2, m.next.j);
438: assertEquals("7", 4, m.next.y);
439: assertEquals("8", 2, new CoverageTest().next.j);
440: assertEquals("9", 4, new CoverageTest().next.y);
441: assertEquals("A", 2, m.getNext().j);
442: assertEquals("B", 4, m.getNext().y);
443: assertEquals("C", 2, new CoverageTest().getNext().j);
444: assertEquals("D", 4, new CoverageTest().getNext().y);
445: }
446:
447: private void testReferenceCalls() {
448: // MessageSend, QualifiedSuperReference, QualifiedThisReference,
449: // SuperReference, ThisReference
450: Inner other = new Inner();
451: foo();
452: this .foo();
453: other.foo();
454: CoverageTest.this .foo();
455: super .foo();
456: Inner.super .foo();
457: CoverageTest.super .foo();
458:
459: sfoo();
460: this .sfoo();
461: CoverageTest.sfoo();
462: Inner.sfoo();
463: Super.sfoo();
464: other.sfoo();
465: CoverageTest.this .sfoo();
466: super .sfoo();
467: Inner.super .sfoo();
468: CoverageTest.super .sfoo();
469: }
470:
471: private Inner testReferences() {
472: // FieldReference, QualifiedSuperReference, QualifiedThisReference,
473: // SuperReference, ThisReference
474: Inner other = new Inner();
475: i = 3;
476: i = i + j + x + y;
477: assertEquals(12, i);
478: i = this .i + this .j + this .x + this .y;
479: assertEquals(21, i);
480: i = CoverageTest.i + CoverageTest.j;
481: assertEquals(8, i);
482: i = Inner.i + Inner.j;
483: assertEquals(10, i);
484: i = Super.i + Super.j;
485: assertEquals(12, i);
486: i = other.i + other.j + other.x + other.y;
487: assertEquals(21, i);
488: i = Inner.this .i + Inner.this .j + Inner.this .x
489: + Inner.this .y;
490: assertEquals(30, i);
491: i = CoverageTest.this .i + CoverageTest.this .j
492: + CoverageTest.this .x + CoverageTest.this .y;
493: assertEquals(15, i);
494: i = super .i + super .j + super .x + super .y;
495: assertEquals(25, i);
496: i = Inner.super .i + Inner.super .j + Inner.super .x
497: + Inner.super .y;
498: assertEquals(35, i);
499: i = CoverageTest.super .i + CoverageTest.super .j
500: + CoverageTest.super .x + CoverageTest.super .y;
501: assertEquals(10, i);
502: return other;
503: }
504:
505: private void testReturnStatement() {
506: // ReturnStatement
507: assertEquals("foo", doReturnFoo());
508: if (true)
509: return;
510: fail();
511: }
512:
513: private void testSynchronizedStatement() {
514: // SynchronizedStatement
515: synchronized (inner) {
516: inner.i = i;
517: }
518: }
519:
520: private void testTryCatchFinallyThrowStatement() {
521: // ThrowStatement, TryStatement
522: try {
523: i = 3;
524: if (true) {
525: throw new Exception();
526: }
527: fail();
528: } catch (Exception e) {
529: } finally {
530: i = 7;
531: }
532: assertEquals(7, i);
533:
534: try {
535: try {
536: i = 3;
537: } catch (Throwable t) {
538: fail();
539: }
540: } catch (Throwable t) {
541: fail();
542: } finally {
543: i = 7;
544: }
545: assertEquals(7, i);
546: }
547:
548: private void testUnaryExpression() {
549: // UnaryExpression
550: i = 4;
551: assertEquals(-4, -i);
552: assertEquals(-5, ~i);
553: z = true;
554: assertFalse(!z);
555: }
556:
557: private void testWhileStatement() {
558: // WhileStatement
559: z = false;
560: while (z)
561: fail();
562:
563: while (z) {
564: fail();
565: }
566: }
567: }
568:
569: /**
570: * TODO: document me.
571: */
572: public static class Super {
573: public static int i = 2;
574: public static final int j = 2;
575:
576: // Initializer
577: static {
578: Super.i = 1;
579: }
580:
581: // Initializer
582: static {
583: Super.i = 3;
584: }
585:
586: protected static void sfoo() {
587: }
588:
589: public int x = 2;
590: public final int y = 4;
591:
592: // Initializer
593: {
594: x = 1;
595: }
596:
597: // Initializer
598: {
599: x = 3;
600: }
601:
602: public Super() {
603: }
604:
605: public Super(int i) {
606: x = i;
607: }
608:
609: public void foo() {
610: }
611:
612: public String toString() {
613: return String.valueOf(x);
614: }
615: }
616:
617: private static class InnerSub extends Inner {
618: private class InnerSubSub extends InnerSub {
619: {
620: asdfasdfasdf = InnerSub.this .asdfasdfasdf;
621: InnerSub.this .asdfasdfasdf = asdfasdfasdf;
622: asdfasdfasdf = super .asdfasdfasdf;
623: super .asdfasdfasdf = asdfasdfasdf;
624: }
625:
626: void fda() {
627: asdfasdfasdf = InnerSub.this .asdfasdfasdf;
628: InnerSub.this .asdfasdfasdf = asdfasdfasdf;
629: asdfasdfasdf = super .asdfasdfasdf;
630: super .asdfasdfasdf = asdfasdfasdf;
631: }
632: }
633:
634: private int asdfasdfasdf = 3;
635:
636: InnerSub() {
637: new CoverageTest().super ();
638: }
639: }
640:
641: private static class SecondMain {
642: private class FunkyInner extends Inner {
643: FunkyInner() {
644: new CoverageTest().super ();
645: }
646: }
647: }
648:
649: public static double d;
650:
651: public static float f;
652:
653: public static int i = 1 + 2 + 3;
654:
655: public static int[] ia;
656:
657: public static int[][] iaa;
658:
659: public static int[][][] iaaa;
660:
661: public static final int j = 2;
662:
663: public static long l;
664:
665: public static Object o;
666:
667: public static String s = "foo";
668:
669: public static CoverageTest singleton;
670:
671: public static boolean z;
672:
673: public static boolean betterNotEval() {
674: fail();
675: return false;
676: }
677:
678: protected static void sfoo() {
679: }
680:
681: private static String doReturnFoo() {
682: if (true)
683: return "foo";
684: fail();
685: return "bar";
686: }
687:
688: public final Inner inner = new Inner();
689:
690: public CoverageTest next;
691:
692: public int x = 3;
693:
694: public final int y = 4;
695:
696: public CoverageTest() {
697: if (singleton == null) {
698: singleton = this ;
699: }
700: next = this ;
701: }
702:
703: public void foo() {
704: }
705:
706: public String getModuleName() {
707: return "com.google.gwt.dev.jjs.CompilerSuite";
708: }
709:
710: public CoverageTest getNext() {
711: return next;
712: }
713:
714: public void testAllocationExpression() {
715: inner.testAllocationExpression();
716: }
717:
718: public void testAndAndExpression() {
719: inner.testAndAndExpression();
720: }
721:
722: public void testArrayAllocationExpression() {
723: inner.testArrayAllocationExpression();
724: }
725:
726: public void testArrayInitializer() {
727: inner.testArrayInitializer();
728: }
729:
730: public void testArrayReference() {
731: inner.testArrayReference();
732: }
733:
734: public void testAssertStatement() {
735: inner.testAssertStatement();
736: }
737:
738: public void testAssignment() {
739: inner.testAssignment();
740: }
741:
742: public void testBinaryExpression() {
743: inner.testBinaryExpression();
744: }
745:
746: public void testBreakContinueLabelStatement() {
747: inner.testBreakContinueLabelStatement();
748: }
749:
750: public void testCaseSwitchStatement() {
751: inner.testCaseSwitchStatement();
752: }
753:
754: public void testCastExpression() {
755: inner.testCastExpression();
756: }
757:
758: public void testCharLiteral() {
759: inner.testCharLiteral();
760: }
761:
762: public void testClassLiteralAccess() {
763: inner.testClassLiteralAccess();
764: }
765:
766: public void testCompoundAssignment() {
767: inner.testCompoundAssignment();
768: }
769:
770: public void testConditionalExpression() {
771: inner.testConditionalExpression();
772: }
773:
774: public void testDoStatement() {
775: inner.testDoStatement();
776: }
777:
778: public void testEmptyStatement() {
779: inner.testEmptyStatement();
780: }
781:
782: public void testEqualExpression() {
783: inner.testEqualExpression();
784: }
785:
786: public void testForeachStatement() {
787: inner.testForeachStatement();
788: }
789:
790: public void testForStatement() {
791: inner.testForStatement();
792: }
793:
794: public void testIfStatement() {
795: inner.testIfStatement();
796: }
797:
798: public void testInstanceOfExpression() {
799: inner.testInstanceOfExpression();
800: }
801:
802: public void testLiterals() {
803: inner.testLiterals();
804: }
805:
806: public void testOrOrExpression() {
807: inner.testOrOrExpression();
808: }
809:
810: public void testPostfixExpression() {
811: inner.testPostfixExpression();
812: }
813:
814: public void testPrefixExpression() {
815: inner.testPrefixExpression();
816: }
817:
818: public void testQualifiedAllocationExpression() {
819: inner.testQualifiedAllocationExpression();
820: }
821:
822: public void testQualifiedNameReference() {
823: inner.testQualifiedNameReference();
824: }
825:
826: public void testReferenceCalls() {
827: inner.testReferenceCalls();
828: }
829:
830: public void testReferences() {
831: inner.testReferences();
832: }
833:
834: public void testReturnStatement() {
835: inner.testReturnStatement();
836: }
837:
838: public void testSynchronizedStatement() {
839: inner.testSynchronizedStatement();
840: }
841:
842: public void testTryCatchFinallyThrowStatement() {
843: inner.testTryCatchFinallyThrowStatement();
844: }
845:
846: public void testUnaryExpression() {
847: inner.testUnaryExpression();
848: }
849:
850: public void testWhileStatement() {
851: inner.testWhileStatement();
852: }
853:
854: }
855:
856: abstract class CoverageBase extends GWTTestCase {
857:
858: public static int i = 1;
859: public static final int j = 2;
860:
861: protected static void sfoo() {
862: }
863:
864: public int x = 3;
865: public final int y = 4;
866:
867: public void foo() {
868: }
869:
870: }
|