0001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
0002:
0003: This file is part of the db4o open source object database.
0004:
0005: db4o is free software; you can redistribute it and/or modify it under
0006: the terms of version 2 of the GNU General Public License as published
0007: by the Free Software Foundation and as clarified by db4objects' GPL
0008: interpretation policy, available at
0009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
0010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
0011: Suite 350, San Mateo, CA 94403, USA.
0012:
0013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
0014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
0015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0016: for more details.
0017:
0018: You should have received a copy of the GNU General Public License along
0019: with this program; if not, write to the Free Software Foundation, Inc.,
0020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
0021: package com.db4o.test.nativequery.analysis;
0022:
0023: import java.util.Date;
0024:
0025: import EDU.purdue.cs.bloat.cfg.*;
0026: import EDU.purdue.cs.bloat.file.*;
0027: import EDU.purdue.cs.bloat.tree.*;
0028:
0029: import com.db4o.instrumentation.core.*;
0030: import com.db4o.nativequery.*;
0031: import com.db4o.nativequery.analysis.*;
0032: import com.db4o.nativequery.expr.*;
0033: import com.db4o.nativequery.expr.cmp.*;
0034: import com.db4o.nativequery.expr.cmp.operand.*;
0035:
0036: import db4ounit.*;
0037:
0038: public class BloatExprBuilderVisitorTestCase implements TestCase,
0039: TestLifeCycle {
0040: private static final String INT_WRAPPED_FIELDNAME = "idWrap";
0041: private static final String BOOLEAN_FIELDNAME = "bool";
0042: private static final String BOOLEAN_WRAPPED_FIELDNAME = "boolWrapper";
0043: private static final String DATE_FIELDNAME = "date";
0044: private static final String INT_FIELDNAME = "id";
0045: private static final String FLOAT_FIELDNAME = "value";
0046: private static final String OTHER_FLOAT_FIELDNAME = "otherValue";
0047: private static final String DATA_FIELDNAME = "next";
0048: private static final String STRING_FIELDNAME = "name";
0049: private final static boolean BOOLEAN_CMPVAL = false;
0050: private final static int INT_CMPVAL = 42;
0051: private final static float FLOAT_CMPVAL = 12.3f;
0052: private final static String STRING_CMPVAL = "Test";
0053: private final static Integer INT_WRAPPER_CMPVAL = new Integer(
0054: INT_CMPVAL);
0055: private final static Boolean BOOLEAN_WRAPPER_CMPVAL = Boolean.TRUE;
0056: private final static Date DATE_WRAPPER_CMPVAL = new Date();
0057: private final Integer intWrapperCmpVal = new Integer(INT_CMPVAL);
0058:
0059: private boolean boolMember = false;
0060: private String stringMember = "foo";
0061: private int intMember = 43;
0062: private float floatMember = 47.11f;
0063: private int[] intArrayMember = {};
0064: private Data[] objArrayMember = {};
0065: private Date dateMember;
0066:
0067: private ClassFileLoader loader;
0068: private BloatLoaderContext bloatUtil;
0069:
0070: private int intMemberPlusOne() {
0071: return intMember + 1;
0072: }
0073:
0074: private int sum(int a, int b) {
0075: return a + b;
0076: }
0077:
0078: public void setUp() throws Exception {
0079: ClassSource classSource = new Db4oClassSource(
0080: new ClassLoaderNativeClassFactory(Data.class
0081: .getClassLoader()));
0082: loader = new ClassFileLoader(classSource);
0083: bloatUtil = new BloatLoaderContext(loader);
0084: }
0085:
0086: // unconditional
0087:
0088: boolean sampleTrue(Data data) {
0089: return true;
0090: }
0091:
0092: public void testTrue() throws Exception {
0093: Assert.areEqual(BoolConstExpression.TRUE,
0094: expression("sampleTrue"));
0095: }
0096:
0097: boolean sampleFalse(Data data) {
0098: return false;
0099: }
0100:
0101: public void testFalse() throws Exception {
0102: Assert.areEqual(BoolConstExpression.FALSE,
0103: expression("sampleFalse"));
0104: }
0105:
0106: // primitive identity
0107:
0108: // boolean
0109:
0110: boolean sampleFieldBooleanComp(Data data) {
0111: return data.bool;
0112: }
0113:
0114: public void testFieldBooleanComp() throws Exception {
0115: assertComparison("sampleFieldBooleanComp", BOOLEAN_FIELDNAME,
0116: Boolean.TRUE, ComparisonOperator.EQUALS, false);
0117: }
0118:
0119: boolean sampleFieldBooleanConstantEqualsComp(Data data) {
0120: return data.bool == true;
0121: }
0122:
0123: public void testFieldBooleanConstantEqualsComp() throws Exception {
0124: assertComparison("sampleFieldBooleanConstantEqualsComp",
0125: BOOLEAN_FIELDNAME, Boolean.TRUE,
0126: ComparisonOperator.EQUALS, false);
0127: }
0128:
0129: boolean sampleUnnecessarilyComplicatedFieldBooleanConstantEqualsComp(
0130: Data data) {
0131: if (data.bool) {
0132: return true;
0133: }
0134: return false;
0135: }
0136:
0137: public void testUnnecessarilyComplicatedFieldBooleanConstantEqualsComp()
0138: throws Exception {
0139: assertComparison(
0140: "sampleUnnecessarilyComplicatedFieldBooleanConstantEqualsComp",
0141: BOOLEAN_FIELDNAME, Boolean.TRUE,
0142: ComparisonOperator.EQUALS, false);
0143: }
0144:
0145: boolean sampleFieldBooleanNotComp(Data data) {
0146: return !data.bool;
0147: }
0148:
0149: public void testFieldBooleanNotComp() throws Exception {
0150: assertComparison("sampleFieldBooleanNotComp",
0151: BOOLEAN_FIELDNAME, Boolean.FALSE,
0152: ComparisonOperator.EQUALS, false);
0153: }
0154:
0155: boolean sampleFieldBooleanEqualsComp(Data data) {
0156: return data.bool == boolMember;
0157: }
0158:
0159: public void testFieldBooleanEqualsComp() throws Exception {
0160: assertComparison("sampleFieldBooleanEqualsComp",
0161: BOOLEAN_FIELDNAME,
0162: new FieldValue(PredicateFieldRoot.INSTANCE,
0163: "boolMember", "Z"), ComparisonOperator.EQUALS,
0164: false);
0165: }
0166:
0167: boolean sampleBooleanFieldEqualsComp(Data data) {
0168: return boolMember == data.bool;
0169: }
0170:
0171: public void testBooleanFieldEqualsComp() throws Exception {
0172: assertComparison("sampleBooleanFieldEqualsComp",
0173: BOOLEAN_FIELDNAME,
0174: new FieldValue(PredicateFieldRoot.INSTANCE,
0175: "boolMember", "Z"), ComparisonOperator.EQUALS,
0176: false);
0177: }
0178:
0179: boolean sampleFieldBooleanNotEqualsComp(Data data) {
0180: return data.bool != boolMember;
0181: }
0182:
0183: public void testFieldBooleanNotEqualsComp() throws Exception {
0184: assertComparison("sampleFieldBooleanNotEqualsComp",
0185: BOOLEAN_FIELDNAME,
0186: new FieldValue(PredicateFieldRoot.INSTANCE,
0187: "boolMember", "Z"), ComparisonOperator.EQUALS,
0188: true);
0189: }
0190:
0191: boolean sampleBooleanFieldNotEqualsComp(Data data) {
0192: return boolMember != data.bool;
0193: }
0194:
0195: public void testBooleanFieldNotEqualsComp() throws Exception {
0196: assertComparison("sampleBooleanFieldNotEqualsComp",
0197: BOOLEAN_FIELDNAME,
0198: new FieldValue(PredicateFieldRoot.INSTANCE,
0199: "boolMember", "Z"), ComparisonOperator.EQUALS,
0200: true);
0201: }
0202:
0203: // int
0204:
0205: boolean sampleFieldIntZeroEqualsComp(Data data) {
0206: return data.id == 0;
0207: }
0208:
0209: public void testFieldIntZeroEqualsComp() throws Exception {
0210: assertComparison("sampleFieldIntZeroEqualsComp", INT_FIELDNAME,
0211: new Integer(0), ComparisonOperator.EQUALS, false);
0212: }
0213:
0214: boolean sampleFieldIntEqualsComp(Data data) {
0215: return data.id == INT_CMPVAL;
0216: }
0217:
0218: public void testFieldIntEqualsComp() throws Exception {
0219: assertComparison("sampleFieldIntEqualsComp", INT_FIELDNAME,
0220: new Integer(INT_CMPVAL), ComparisonOperator.EQUALS,
0221: false);
0222: }
0223:
0224: boolean sampleFieldIntNotEqualsComp(Data data) {
0225: return data.id != INT_CMPVAL;
0226: }
0227:
0228: public void testFieldIntNotEqualsComp() throws Exception {
0229: assertComparison("sampleFieldIntNotEqualsComp", INT_FIELDNAME,
0230: new Integer(INT_CMPVAL), ComparisonOperator.EQUALS,
0231: true);
0232: }
0233:
0234: boolean sampleIntFieldEqualsComp(Data data) {
0235: return INT_CMPVAL == data.id;
0236: }
0237:
0238: public void testIntFieldEqualsComp() throws Exception {
0239: assertComparison("sampleIntFieldEqualsComp", INT_FIELDNAME,
0240: new Integer(INT_CMPVAL), ComparisonOperator.EQUALS,
0241: false);
0242: }
0243:
0244: boolean sampleIntFieldNotEqualsComp(Data data) {
0245: return INT_CMPVAL != data.id;
0246: }
0247:
0248: public void testIntFieldNotEqualsComp() throws Exception {
0249: assertComparison("sampleIntFieldNotEqualsComp", INT_FIELDNAME,
0250: new Integer(INT_CMPVAL), ComparisonOperator.EQUALS,
0251: true);
0252: }
0253:
0254: // float
0255:
0256: boolean sampleFieldFloatZeroEqualsComp(Data data) {
0257: return data.value == 0.0f;
0258: }
0259:
0260: public void testFieldFloatZeroEqualsComp() throws Exception {
0261: assertComparison("sampleFieldFloatZeroEqualsComp",
0262: FLOAT_FIELDNAME, new Float(0.0f),
0263: ComparisonOperator.EQUALS, false);
0264: }
0265:
0266: boolean sampleFieldFloatZeroIntEqualsComp(Data data) {
0267: return data.value == 0;
0268: }
0269:
0270: public void testFieldFloatZeroIntEqualsComp() throws Exception {
0271: assertComparison("sampleFieldFloatZeroIntEqualsComp",
0272: FLOAT_FIELDNAME, new Float(0.0f),
0273: ComparisonOperator.EQUALS, false);
0274: }
0275:
0276: boolean sampleFieldFloatEqualsComp(Data data) {
0277: return data.value == FLOAT_CMPVAL;
0278: }
0279:
0280: public void testFieldFloatEqualsComp() throws Exception {
0281: assertComparison("sampleFieldFloatEqualsComp", FLOAT_FIELDNAME,
0282: new Float(FLOAT_CMPVAL), ComparisonOperator.EQUALS,
0283: false);
0284: }
0285:
0286: boolean sampleFieldFloatNotEqualsComp(Data data) {
0287: return data.value != FLOAT_CMPVAL;
0288: }
0289:
0290: public void testFieldFloatNotEqualsComp() throws Exception {
0291: assertComparison("sampleFieldFloatNotEqualsComp",
0292: FLOAT_FIELDNAME, new Float(FLOAT_CMPVAL),
0293: ComparisonOperator.EQUALS, true);
0294: }
0295:
0296: boolean sampleFloatFieldEqualsComp(Data data) {
0297: return FLOAT_CMPVAL == data.value;
0298: }
0299:
0300: public void testFloatFieldEqualsComp() throws Exception {
0301: assertComparison("sampleFloatFieldEqualsComp", FLOAT_FIELDNAME,
0302: new Float(FLOAT_CMPVAL), ComparisonOperator.EQUALS,
0303: false);
0304: }
0305:
0306: boolean sampleFloatFieldNotEqualsComp(Data data) {
0307: return FLOAT_CMPVAL != data.value;
0308: }
0309:
0310: public void testFloatFieldNotEqualsComp() throws Exception {
0311: assertComparison("sampleFloatFieldNotEqualsComp",
0312: FLOAT_FIELDNAME, new Float(FLOAT_CMPVAL),
0313: ComparisonOperator.EQUALS, true);
0314: }
0315:
0316: // object identity
0317:
0318: // TODO: nonsense, but need single roots for method calls
0319: boolean sampleCandidateIdentity(Data data) {
0320: return data == null;
0321: }
0322:
0323: public void _testCandidateIdentity() throws Exception {
0324: assertComparison("sampleCandidateIdentity", new String[] {},
0325: null, ComparisonOperator.EQUALS, false);
0326: }
0327:
0328: boolean sampleIdentityNullComp(Data data) {
0329: return data.next == null;
0330: }
0331:
0332: public void testIdentityNullComp() throws Exception {
0333: assertComparison("sampleIdentityNullComp", DATA_FIELDNAME,
0334: null, ComparisonOperator.EQUALS, false);
0335: }
0336:
0337: boolean sampleNotIdentityNullComp(Data data) {
0338: return data.next != null;
0339: }
0340:
0341: public void testNotIdentityNullComp() throws Exception {
0342: assertComparison("sampleNotIdentityNullComp", DATA_FIELDNAME,
0343: null, ComparisonOperator.EQUALS, true);
0344: }
0345:
0346: // primitive unequal comparison
0347:
0348: // int
0349:
0350: boolean sampleFieldIntSmallerComp(Data data) {
0351: return data.id < INT_CMPVAL;
0352: }
0353:
0354: public void testFieldIntSmallerComp() throws Exception {
0355: assertComparison("sampleFieldIntSmallerComp", INT_FIELDNAME,
0356: new Integer(INT_CMPVAL), ComparisonOperator.SMALLER,
0357: false);
0358: }
0359:
0360: boolean sampleFieldIntGreaterComp(Data data) {
0361: return data.id > INT_CMPVAL;
0362: }
0363:
0364: public void testFieldIntGreaterComp() throws Exception {
0365: assertComparison("sampleFieldIntGreaterComp", INT_FIELDNAME,
0366: new Integer(INT_CMPVAL), ComparisonOperator.GREATER,
0367: false);
0368: }
0369:
0370: boolean sampleFieldIntSmallerEqualsComp(Data data) {
0371: return data.id <= INT_CMPVAL;
0372: }
0373:
0374: public void testFieldIntSmallerEqualsComp() throws Exception {
0375: assertComparison("sampleFieldIntSmallerEqualsComp",
0376: INT_FIELDNAME, new Integer(INT_CMPVAL),
0377: ComparisonOperator.GREATER, true);
0378: }
0379:
0380: boolean sampleFieldIntGreaterEqualsComp(Data data) {
0381: return data.id >= INT_CMPVAL;
0382: }
0383:
0384: public void testFieldIntGreaterEqualsComp() throws Exception {
0385: assertComparison("sampleFieldIntGreaterEqualsComp",
0386: INT_FIELDNAME, new Integer(INT_CMPVAL),
0387: ComparisonOperator.SMALLER, true);
0388: }
0389:
0390: boolean sampleIntFieldSmallerComp(Data data) {
0391: return INT_CMPVAL < data.id;
0392: }
0393:
0394: public void testIntFieldSmallerComp() throws Exception {
0395: assertComparison("sampleIntFieldSmallerComp", INT_FIELDNAME,
0396: new Integer(INT_CMPVAL), ComparisonOperator.GREATER,
0397: false);
0398: }
0399:
0400: boolean sampleIntFieldGreaterComp(Data data) {
0401: return INT_CMPVAL > data.id;
0402: }
0403:
0404: public void testIntFieldGreaterComp() throws Exception {
0405: assertComparison("sampleIntFieldGreaterComp", INT_FIELDNAME,
0406: new Integer(INT_CMPVAL), ComparisonOperator.SMALLER,
0407: false);
0408: }
0409:
0410: boolean sampleIntFieldSmallerEqualsComp(Data data) {
0411: return INT_CMPVAL <= data.id;
0412: }
0413:
0414: public void testIntFieldSmallerEqualsComp() throws Exception {
0415: assertComparison("sampleIntFieldSmallerEqualsComp",
0416: INT_FIELDNAME, new Integer(INT_CMPVAL),
0417: ComparisonOperator.SMALLER, true);
0418: }
0419:
0420: boolean sampleIntFieldGreaterEqualsComp(Data data) {
0421: return INT_CMPVAL >= data.id;
0422: }
0423:
0424: public void testIntFieldGreaterEqualsComp() throws Exception {
0425: assertComparison("sampleIntFieldGreaterEqualsComp",
0426: INT_FIELDNAME, new Integer(INT_CMPVAL),
0427: ComparisonOperator.GREATER, true);
0428: }
0429:
0430: boolean sampleFieldFloatSmallerComp(Data data) {
0431: return data.value < FLOAT_CMPVAL;
0432: }
0433:
0434: public void testFieldFloatSmallerComp() throws Exception {
0435: assertComparison("sampleFieldFloatSmallerComp",
0436: FLOAT_FIELDNAME, new Float(FLOAT_CMPVAL),
0437: ComparisonOperator.SMALLER, false);
0438: }
0439:
0440: boolean sampleFieldFloatGreaterComp(Data data) {
0441: return data.value > FLOAT_CMPVAL;
0442: }
0443:
0444: public void testFieldFloatGreaterComp() throws Exception {
0445: assertComparison("sampleFieldFloatGreaterComp",
0446: FLOAT_FIELDNAME, new Float(FLOAT_CMPVAL),
0447: ComparisonOperator.GREATER, false);
0448: }
0449:
0450: boolean sampleFieldFloatSmallerEqualsComp(Data data) {
0451: return data.value <= FLOAT_CMPVAL;
0452: }
0453:
0454: public void testFieldFloatSmallerEqualsComp() throws Exception {
0455: assertComparison("sampleFieldFloatSmallerEqualsComp",
0456: FLOAT_FIELDNAME, new Float(FLOAT_CMPVAL),
0457: ComparisonOperator.GREATER, true);
0458: }
0459:
0460: boolean sampleMemberIntSmallerEqualsComp(Data data) {
0461: return intMember <= data.id;
0462: }
0463:
0464: public void testMemberIntSmallerEqualsComp() throws Exception {
0465: assertComparison("sampleMemberIntSmallerEqualsComp",
0466: INT_FIELDNAME, new FieldValue(
0467: PredicateFieldRoot.INSTANCE, "intMember", "I"),
0468: ComparisonOperator.SMALLER, true);
0469: }
0470:
0471: boolean sampleMemberFloatSmallerEqualsComp(Data data) {
0472: return floatMember <= data.value;
0473: }
0474:
0475: public void testMemberFloatSmallerEqualsComp() throws Exception {
0476: assertComparison("sampleMemberFloatSmallerEqualsComp",
0477: FLOAT_FIELDNAME,
0478: new FieldValue(PredicateFieldRoot.INSTANCE,
0479: "floatMember", "F"),
0480: ComparisonOperator.SMALLER, true);
0481: }
0482:
0483: // string equality
0484:
0485: boolean sampleFieldStringEqualsComp(Data data) {
0486: return data.name.equals(STRING_CMPVAL);
0487: }
0488:
0489: public void testFieldStringEqualsComp() throws Exception {
0490: assertComparison("sampleFieldStringEqualsComp",
0491: STRING_FIELDNAME, STRING_CMPVAL,
0492: ComparisonOperator.EQUALS, false);
0493: }
0494:
0495: boolean sampleStringFieldEqualsComp(Data data) {
0496: return STRING_CMPVAL.equals(data.name);
0497: }
0498:
0499: public void testStringFieldEqualsComp() throws Exception {
0500: assertComparison("sampleStringFieldEqualsComp",
0501: STRING_FIELDNAME, STRING_CMPVAL,
0502: ComparisonOperator.EQUALS, false);
0503: }
0504:
0505: // string-specific comparisons
0506:
0507: boolean sampleFieldStringContains(Data data) {
0508: return data.name.contains(STRING_CMPVAL);
0509: }
0510:
0511: public void testFieldStringContains() throws Exception {
0512: assertComparison("sampleFieldStringContains", STRING_FIELDNAME,
0513: STRING_CMPVAL, ComparisonOperator.CONTAINS, false);
0514: }
0515:
0516: boolean sampleFieldStringContainsWrongWay(Data data) {
0517: return STRING_CMPVAL.contains(data.name);
0518: }
0519:
0520: public void testFieldStringContainsWrongWay() throws Exception {
0521: assertInvalid("sampleFieldStringContainsWrongWay");
0522: }
0523:
0524: boolean sampleFieldStringStartsWith(Data data) {
0525: return data.name.startsWith(STRING_CMPVAL);
0526: }
0527:
0528: public void testFieldStringStartsWith() throws Exception {
0529: assertComparison("sampleFieldStringStartsWith",
0530: STRING_FIELDNAME, STRING_CMPVAL,
0531: ComparisonOperator.STARTSWITH, false);
0532: }
0533:
0534: boolean sampleFieldStringStartsWithWrongWay(Data data) {
0535: return STRING_CMPVAL.startsWith(data.name);
0536: }
0537:
0538: public void testFieldStringStartsWithWrongWay() throws Exception {
0539: assertInvalid("sampleFieldStringStartsWithWrongWay");
0540: }
0541:
0542: boolean sampleFieldStringEndsWith(Data data) {
0543: return data.name.endsWith(STRING_CMPVAL);
0544: }
0545:
0546: public void testFieldStringEndsWith() throws Exception {
0547: assertComparison("sampleFieldStringEndsWith", STRING_FIELDNAME,
0548: STRING_CMPVAL, ComparisonOperator.ENDSWITH, false);
0549: }
0550:
0551: boolean sampleFieldStringEndsWithWrongWay(Data data) {
0552: return STRING_CMPVAL.endsWith(data.name);
0553: }
0554:
0555: public void testFieldStringEndsWithWrongWay() throws Exception {
0556: assertInvalid("sampleFieldStringEndsWithWrongWay");
0557: }
0558:
0559: boolean sampleFieldStringToLowerCaseStartsWith(Data data)
0560: throws Exception {
0561: return data.getName().toLowerCase().startsWith(STRING_CMPVAL);
0562: }
0563:
0564: public void testFieldStringToLowerCaseStartsWith() throws Exception {
0565: assertInvalid("sampleFieldStringToLowerCaseStartsWith");
0566: }
0567:
0568: // primitive wrapper equality
0569:
0570: boolean sampleFieldDateEqualsComp(Data data) {
0571: return data.getDate().equals(dateMember);
0572: }
0573:
0574: public void testFieldDateEqualsComp() throws Exception {
0575: assertComparison("sampleFieldDateEqualsComp", DATE_FIELDNAME,
0576: new FieldValue(PredicateFieldRoot.INSTANCE,
0577: "dateMember", "java.util.Date"),
0578: ComparisonOperator.EQUALS, false);
0579: }
0580:
0581: boolean sampleFieldIntWrapperEqualsComp(Data data) {
0582: return data.getIdWrapped().equals(intWrapperCmpVal);
0583: }
0584:
0585: public void testFieldIntWrapperEqualsComp() throws Exception {
0586: assertComparison("sampleFieldIntWrapperEqualsComp",
0587: INT_WRAPPED_FIELDNAME, new FieldValue(
0588: PredicateFieldRoot.INSTANCE,
0589: "intWrapperCmpVal", "java.lang.Integer"),
0590: ComparisonOperator.EQUALS, false);
0591: }
0592:
0593: boolean sampleIntWrapperFieldEqualsComp(Data data) {
0594: return intWrapperCmpVal.equals(data.getIdWrapped());
0595: }
0596:
0597: public void testIntWrapperFieldEqualsComp() throws Exception {
0598: assertComparison("sampleIntWrapperFieldEqualsComp",
0599: INT_WRAPPED_FIELDNAME, new FieldValue(
0600: PredicateFieldRoot.INSTANCE,
0601: "intWrapperCmpVal", "java.lang.Integer"),
0602: ComparisonOperator.EQUALS, false);
0603: }
0604:
0605: // date equality
0606:
0607: boolean sampleFieldBooleanWrapperEqualsComp(Data data) {
0608: return data.boolWrapper.booleanValue();
0609: }
0610:
0611: public void testFieldBooleanWrapperEqualsComp() throws Exception {
0612: assertComparison("sampleFieldBooleanWrapperEqualsComp",
0613: BOOLEAN_WRAPPED_FIELDNAME, Boolean.TRUE,
0614: ComparisonOperator.EQUALS, false);
0615: }
0616:
0617: // descend into primitive wrapper
0618:
0619: boolean sampleWrapperFieldValueIntSameComp(Data data) {
0620: return data.getIdWrapped().intValue() == INT_CMPVAL;
0621: }
0622:
0623: public void testWrapperFieldValueIntSameComp() throws Exception {
0624: assertComparison("sampleWrapperFieldValueIntSameComp",
0625: INT_WRAPPED_FIELDNAME, new Integer(INT_CMPVAL),
0626: ComparisonOperator.EQUALS, false);
0627: }
0628:
0629: boolean sampleNotValueBoolWrapperFieldSameComp(Data data) {
0630: return data.boolWrapper.booleanValue();
0631: }
0632:
0633: public void testNotValueBoolWrapperFieldSameComp() throws Exception {
0634: assertComparison("sampleNotValueBoolWrapperFieldSameComp",
0635: BOOLEAN_WRAPPED_FIELDNAME, Boolean.TRUE,
0636: ComparisonOperator.EQUALS, false);
0637: }
0638:
0639: // primitive field against wrapper
0640:
0641: boolean sampleFieldWrapperIntSameComp(Data data) {
0642: return data.getId() == INT_WRAPPER_CMPVAL.intValue();
0643: }
0644:
0645: public void testFieldWrapperIntSameComp() throws Exception {
0646: assertComparison("sampleFieldWrapperIntSameComp",
0647: INT_FIELDNAME, new MethodCallValue(new FieldValue(
0648: new StaticFieldRoot(
0649: BloatExprBuilderVisitorTestCase.class
0650: .getName()),
0651: "INT_WRAPPER_CMPVAL", Integer.class.getName()),
0652: "intValue", new Class[0],
0653: new ComparisonOperand[0]),
0654: ComparisonOperator.EQUALS, false);
0655: }
0656:
0657: boolean sampleBoolWrapperFieldSameComp(Data data) {
0658: return data.bool == BOOLEAN_WRAPPER_CMPVAL.booleanValue();
0659: }
0660:
0661: public void testBoolWrapperFieldSameComp() throws Exception {
0662: assertComparison("sampleBoolWrapperFieldSameComp",
0663: BOOLEAN_FIELDNAME, new MethodCallValue(new FieldValue(
0664: new StaticFieldRoot(
0665: BloatExprBuilderVisitorTestCase.class
0666: .getName()),
0667: "BOOLEAN_WRAPPER_CMPVAL", Boolean.class
0668: .getName()), "booleanValue",
0669: new Class[0], new ComparisonOperand[0]),
0670: ComparisonOperator.EQUALS, false);
0671: }
0672:
0673: // wrapper comparison
0674:
0675: boolean sampleFieldWrapperIntCompToEquals(Data data) {
0676: return data.getIdWrapped().compareTo(INT_WRAPPER_CMPVAL) == 0;
0677: }
0678:
0679: public void testFieldWrapperIntCompToEquals() throws Exception {
0680: assertComparison("sampleFieldWrapperIntCompToEquals",
0681: INT_WRAPPED_FIELDNAME, new FieldValue(
0682: new StaticFieldRoot(
0683: BloatExprBuilderVisitorTestCase.class
0684: .getName()),
0685: "INT_WRAPPER_CMPVAL", Integer.class.getName()),
0686: ComparisonOperator.EQUALS, false);
0687: }
0688:
0689: boolean sampleFieldWrapperIntCompToNotEquals(Data data) {
0690: return data.getIdWrapped().compareTo(INT_WRAPPER_CMPVAL) != 0;
0691: }
0692:
0693: public void testFieldWrapperIntCompToNotEquals() throws Exception {
0694: assertComparison("sampleFieldWrapperIntCompToNotEquals",
0695: INT_WRAPPED_FIELDNAME, new FieldValue(
0696: new StaticFieldRoot(
0697: BloatExprBuilderVisitorTestCase.class
0698: .getName()),
0699: "INT_WRAPPER_CMPVAL", Integer.class.getName()),
0700: ComparisonOperator.EQUALS, true);
0701: }
0702:
0703: boolean sampleFieldWrapperIntCompToGreater(Data data) {
0704: return data.getIdWrapped().compareTo(INT_WRAPPER_CMPVAL) > 0;
0705: }
0706:
0707: public void testFieldWrapperIntCompToGreater() throws Exception {
0708: assertComparison("sampleFieldWrapperIntCompToGreater",
0709: INT_WRAPPED_FIELDNAME, new FieldValue(
0710: new StaticFieldRoot(
0711: BloatExprBuilderVisitorTestCase.class
0712: .getName()),
0713: "INT_WRAPPER_CMPVAL", Integer.class.getName()),
0714: ComparisonOperator.GREATER, false);
0715: }
0716:
0717: boolean sampleFieldWrapperIntCompToLE(Data data) {
0718: return data.getIdWrapped().compareTo(INT_WRAPPER_CMPVAL) <= 0;
0719: }
0720:
0721: public void testFieldWrapperIntCompToLE() throws Exception {
0722: assertComparison("sampleFieldWrapperIntCompToLE",
0723: INT_WRAPPED_FIELDNAME, new FieldValue(
0724: new StaticFieldRoot(
0725: BloatExprBuilderVisitorTestCase.class
0726: .getName()),
0727: "INT_WRAPPER_CMPVAL", Integer.class.getName()),
0728: ComparisonOperator.GREATER, true);
0729: }
0730:
0731: //static member comparison
0732:
0733: boolean sampleStaticFieldIntWrapperEqualsComp(Data data) {
0734: return data.getIdWrapped().equals(INT_WRAPPER_CMPVAL);
0735: }
0736:
0737: public void testStaticFieldIntWrapperEqualsComp() throws Exception {
0738: //assertInvalid("sampleStaticFieldIntWrapperEqualsComp");
0739: assertComparison("sampleStaticFieldIntWrapperEqualsComp",
0740: INT_WRAPPED_FIELDNAME, new FieldValue(
0741: new StaticFieldRoot(getClass().getName()),
0742: "INT_WRAPPER_CMPVAL", "java.lang.Integer"),
0743: ComparisonOperator.EQUALS, false);
0744: }
0745:
0746: boolean sampleStaticIntWrapperFieldEqualsComp(Data data) {
0747: return INT_WRAPPER_CMPVAL.equals(data.getIdWrapped());
0748: }
0749:
0750: public void testStaticIntWrapperFieldEqualsComp() throws Exception {
0751: //assertInvalid("sampleStaticIntWrapperFieldEqualsComp");
0752: assertComparison("sampleStaticIntWrapperFieldEqualsComp",
0753: INT_WRAPPED_FIELDNAME, new FieldValue(
0754: new StaticFieldRoot(getClass().getName()),
0755: "INT_WRAPPER_CMPVAL", "java.lang.Integer"),
0756: ComparisonOperator.EQUALS, false);
0757: }
0758:
0759: // getter
0760:
0761: boolean sampleGetterBoolComp(Data data) {
0762: return data.getBool();
0763: }
0764:
0765: public void testGetterBoolComp() throws Exception {
0766: assertComparison("sampleGetterBoolComp", BOOLEAN_FIELDNAME,
0767: Boolean.TRUE, ComparisonOperator.EQUALS, false);
0768: }
0769:
0770: // TODO fails when run via Ant?!?
0771: boolean sampleBoolGetterNotEqualsComp(Data data) {
0772: return BOOLEAN_CMPVAL != data.getBool();
0773: }
0774:
0775: public void _testBoolGetterNotEqualsComp() throws Exception {
0776: assertComparison("sampleBoolGetterNotEqualsComp",
0777: BOOLEAN_FIELDNAME, Boolean.valueOf(!BOOLEAN_CMPVAL),
0778: ComparisonOperator.EQUALS, false);
0779: }
0780:
0781: boolean sampleGetterIntEqualsComp(Data data) {
0782: return data.getId() == INT_CMPVAL;
0783: }
0784:
0785: public void testGetterIntEqualsComp() throws Exception {
0786: assertComparison("sampleGetterIntEqualsComp", INT_FIELDNAME,
0787: new Integer(INT_CMPVAL), ComparisonOperator.EQUALS,
0788: false);
0789: }
0790:
0791: boolean sampleGetterStringEqualsComp(Data data) {
0792: return data.getName().equals(STRING_CMPVAL);
0793: }
0794:
0795: public void testGetterStringEqualsComp() throws Exception {
0796: assertComparison("sampleGetterStringEqualsComp",
0797: STRING_FIELDNAME, STRING_CMPVAL,
0798: ComparisonOperator.EQUALS, false);
0799: }
0800:
0801: boolean sampleGetterFloatSmallerComp(Data data) {
0802: return data.getValue() < FLOAT_CMPVAL;
0803: }
0804:
0805: public void testGetterFloatSmallerComp() throws Exception {
0806: assertComparison("sampleGetterFloatSmallerComp",
0807: FLOAT_FIELDNAME, new Float(FLOAT_CMPVAL),
0808: ComparisonOperator.SMALLER, false);
0809: }
0810:
0811: // field cascade
0812:
0813: boolean sampleCascadeFieldStringEqualsComp(Data data) {
0814: return data.next.name.equals(STRING_CMPVAL);
0815: }
0816:
0817: public void testCascadeFieldStringEqualsComp() throws Exception {
0818: assertComparison("sampleCascadeFieldStringEqualsComp",
0819: new String[] { DATA_FIELDNAME, STRING_FIELDNAME },
0820: STRING_CMPVAL, ComparisonOperator.EQUALS, false);
0821: }
0822:
0823: boolean sampleGetterCascadeIntFieldEqualsComp(Data data) {
0824: return INT_CMPVAL == data.getNext().getId();
0825: }
0826:
0827: public void testGetterCascadeIntFieldEqualsComp() throws Exception {
0828: assertComparison("sampleGetterCascadeIntFieldEqualsComp",
0829: new String[] { DATA_FIELDNAME, INT_FIELDNAME },
0830: new Integer(INT_CMPVAL), ComparisonOperator.EQUALS,
0831: false);
0832: }
0833:
0834: boolean sampleCascadeStringFieldEqualsComp(Data data) {
0835: return STRING_CMPVAL.equals(data.next.name);
0836: }
0837:
0838: public void testCascadeStringFieldEqualsComp() throws Exception {
0839: assertComparison("sampleCascadeStringFieldEqualsComp",
0840: new String[] { DATA_FIELDNAME, STRING_FIELDNAME },
0841: STRING_CMPVAL, ComparisonOperator.EQUALS, false);
0842: }
0843:
0844: boolean sampleGetterCascadeStringFieldEqualsComp(Data data) {
0845: return STRING_CMPVAL.equals(data.getNext().getName());
0846: }
0847:
0848: public void testGetterCascadeStringFieldEqualsComp()
0849: throws Exception {
0850: assertComparison("sampleGetterCascadeStringFieldEqualsComp",
0851: new String[] { DATA_FIELDNAME, STRING_FIELDNAME },
0852: STRING_CMPVAL, ComparisonOperator.EQUALS, false);
0853: }
0854:
0855: boolean sampleGetterCascadeFloatFieldGreaterEqualsComp(Data data) {
0856: return FLOAT_CMPVAL >= data.getNext().getValue();
0857: }
0858:
0859: public void testGetterCascadeFloatFieldGreaterEqualsComp()
0860: throws Exception {
0861: assertComparison(
0862: "sampleGetterCascadeFloatFieldGreaterEqualsComp",
0863: new String[] { DATA_FIELDNAME, FLOAT_FIELDNAME },
0864: new Float(FLOAT_CMPVAL), ComparisonOperator.GREATER,
0865: true);
0866: }
0867:
0868: // member field comparison
0869:
0870: boolean sampleFieldIntMemberEqualsComp(Data data) {
0871: return data.getId() == intMember;
0872: }
0873:
0874: public void testFieldIntMemberEqualsComp() throws Exception {
0875: assertComparison("sampleFieldIntMemberEqualsComp",
0876: new String[] { INT_FIELDNAME }, new FieldValue(
0877: PredicateFieldRoot.INSTANCE, "intMember", "I"),
0878: ComparisonOperator.EQUALS, false);
0879: }
0880:
0881: boolean sampleIntMemberFieldGreaterEqualsComp(Data data) {
0882: return intMember >= data.getId();
0883: }
0884:
0885: public void testIntMemberFieldGreaterEqualsComp() throws Exception {
0886: assertComparison("sampleIntMemberFieldGreaterEqualsComp",
0887: new String[] { INT_FIELDNAME }, new FieldValue(
0888: PredicateFieldRoot.INSTANCE, "intMember", "I"),
0889: ComparisonOperator.GREATER, true);
0890: }
0891:
0892: boolean sampleFieldStringMemberEqualsComp(Data data) {
0893: return data.getName().equals(stringMember);
0894: }
0895:
0896: public void testFieldStringMemberEqualsComp() throws Exception {
0897: assertComparison("sampleFieldStringMemberEqualsComp",
0898: new String[] { STRING_FIELDNAME }, new FieldValue(
0899: PredicateFieldRoot.INSTANCE, "stringMember",
0900: "java.lang.String"), ComparisonOperator.EQUALS,
0901: false);
0902: }
0903:
0904: boolean sampleFieldFloatMemberNotEqualsComp(Data data) {
0905: return data.getValue() != floatMember;
0906: }
0907:
0908: public void testFieldFloatMemberNotEqualsComp() throws Exception {
0909: assertComparison("sampleFieldFloatMemberNotEqualsComp",
0910: new String[] { FLOAT_FIELDNAME },
0911: new FieldValue(PredicateFieldRoot.INSTANCE,
0912: "floatMember", "F"), ComparisonOperator.EQUALS,
0913: true);
0914: }
0915:
0916: boolean sampleFloatMemberFieldNotEqualsComp(Data data) {
0917: return floatMember != data.getValue();
0918: }
0919:
0920: public void testFloatMemberFieldNotEqualsComp() throws Exception {
0921: assertComparison("sampleFloatMemberFieldNotEqualsComp",
0922: new String[] { FLOAT_FIELDNAME },
0923: new FieldValue(PredicateFieldRoot.INSTANCE,
0924: "floatMember", "F"), ComparisonOperator.EQUALS,
0925: true);
0926: }
0927:
0928: // negations
0929:
0930: boolean sampleStringNot(Data data) {
0931: return !STRING_CMPVAL.equals(data.name);
0932: }
0933:
0934: public void testStringNot() throws Exception {
0935: assertComparison("sampleStringNot", STRING_FIELDNAME,
0936: STRING_CMPVAL, ComparisonOperator.EQUALS, true);
0937: }
0938:
0939: boolean sampleIntEqualsNot(Data data) {
0940: return !(data.id == INT_CMPVAL);
0941: }
0942:
0943: public void testIntEqualsNot() throws Exception {
0944: assertComparison("sampleIntEqualsNot", INT_FIELDNAME,
0945: new Integer(INT_CMPVAL), ComparisonOperator.EQUALS,
0946: true);
0947: }
0948:
0949: boolean sampleIntNotEqualsNot(Data data) {
0950: return !(data.id != INT_CMPVAL);
0951: }
0952:
0953: public void testIntNotEqualsNot() throws Exception {
0954: assertComparison("sampleIntNotEqualsNot", INT_FIELDNAME,
0955: new Integer(INT_CMPVAL), ComparisonOperator.EQUALS,
0956: false);
0957: }
0958:
0959: boolean sampleIntGreaterNot(Data data) {
0960: return !(data.id > INT_CMPVAL);
0961: }
0962:
0963: public void testIntGreaterNot() throws Exception {
0964: assertComparison("sampleIntGreaterNot", INT_FIELDNAME,
0965: new Integer(INT_CMPVAL), ComparisonOperator.GREATER,
0966: true);
0967: }
0968:
0969: boolean sampleIntSmallerEqualsNot(Data data) {
0970: return !(data.id <= INT_CMPVAL);
0971: }
0972:
0973: public void testIntSmallerEqualsNot() throws Exception {
0974: assertComparison("sampleIntSmallerEqualsNot", INT_FIELDNAME,
0975: new Integer(INT_CMPVAL), ComparisonOperator.GREATER,
0976: false);
0977: }
0978:
0979: boolean sampleIntNotNot(Data data) {
0980: return !(!(data.id < INT_CMPVAL));
0981: }
0982:
0983: public void testIntNotNot() throws Exception {
0984: assertComparison("sampleIntNotNot", INT_FIELDNAME, new Integer(
0985: INT_CMPVAL), ComparisonOperator.SMALLER, false);
0986: }
0987:
0988: // conjunctions
0989:
0990: boolean sampleBoolBoolAnd(Data data) {
0991: return !data.getBool() && data.getBool();
0992: }
0993:
0994: public void testBoolBoolAnd() throws Exception {
0995: AndExpression expr = (AndExpression) expression("sampleBoolBoolAnd");
0996: NQOptimizationAssertUtil.assertComparison(expr.left(),
0997: new String[] { BOOLEAN_FIELDNAME }, Boolean.FALSE,
0998: ComparisonOperator.EQUALS, false);
0999: NQOptimizationAssertUtil.assertComparison(expr.right(),
1000: new String[] { BOOLEAN_FIELDNAME }, Boolean.TRUE,
1001: ComparisonOperator.EQUALS, false);
1002: }
1003:
1004: boolean sampleIntIntAnd(Data data) {
1005: return (data.id > 42) && (data.id < 100);
1006: }
1007:
1008: public void testIntIntAnd() throws Exception {
1009: AndExpression expr = (AndExpression) expression("sampleIntIntAnd");
1010: NQOptimizationAssertUtil.assertComparison(expr.left(),
1011: new String[] { "id" }, new Integer(42),
1012: ComparisonOperator.GREATER, false);
1013: NQOptimizationAssertUtil.assertComparison(expr.right(),
1014: new String[] { "id" }, new Integer(100),
1015: ComparisonOperator.SMALLER, false);
1016: }
1017:
1018: boolean sampleStringIntOr(Data data) {
1019: return (data.name.equals("Foo")) || (data.id == 42);
1020: }
1021:
1022: public void testStringIntOr() throws Exception {
1023: OrExpression expr = (OrExpression) expression("sampleStringIntOr");
1024: NQOptimizationAssertUtil.assertComparison(expr.left(),
1025: new String[] { "name" }, "Foo",
1026: ComparisonOperator.EQUALS, false);
1027: ComparisonExpression right = (ComparisonExpression) expr
1028: .right();
1029: NQOptimizationAssertUtil.assertComparison(right,
1030: new String[] { "id" }, new Integer(42),
1031: ComparisonOperator.EQUALS, false);
1032: }
1033:
1034: boolean sampleIntStringNotOr(Data data) {
1035: return !((data.id == 42) || (data.name.equals("Foo")));
1036: }
1037:
1038: public void testIntStringNotOr() throws Exception {
1039: AndExpression expr = (AndExpression) expression("sampleIntStringNotOr");
1040: NQOptimizationAssertUtil.assertComparison(expr.left(),
1041: new String[] { "id" }, new Integer(42),
1042: ComparisonOperator.EQUALS, true);
1043: NQOptimizationAssertUtil.assertComparison(expr.right(),
1044: new String[] { "name" }, "Foo",
1045: ComparisonOperator.EQUALS, true);
1046: }
1047:
1048: boolean sampleOuterOrInnerAnd(Data data) {
1049: return (data.id == 42) && (data.getName().equals("Bar"))
1050: || (data.name.equals("Foo"));
1051: }
1052:
1053: public void testOuterOrInnerAnd() throws Exception {
1054: OrExpression expr = (OrExpression) expression("sampleOuterOrInnerAnd");
1055: NQOptimizationAssertUtil.assertComparison(expr.left(),
1056: new String[] { "name" }, "Foo",
1057: ComparisonOperator.EQUALS, false);
1058: AndExpression andExpr = (AndExpression) expr.right();
1059: NQOptimizationAssertUtil.assertComparison(andExpr.left(),
1060: new String[] { "id" }, new Integer(42),
1061: ComparisonOperator.EQUALS, false);
1062: NQOptimizationAssertUtil.assertComparison(andExpr.right(),
1063: new String[] { "name" }, "Bar",
1064: ComparisonOperator.EQUALS, false);
1065: }
1066:
1067: boolean sampleOuterAndInnerOr(Data data) {
1068: return ((data.id < 42) || (data.getName().equals("Bar")))
1069: && (data.getId() > 10);
1070: }
1071:
1072: public void testOuterAndInnerOr() throws Exception {
1073: AndExpression expr = (AndExpression) expression("sampleOuterAndInnerOr");
1074: NQOptimizationAssertUtil.assertComparison(expr.left(),
1075: new String[] { "id" }, new Integer(10),
1076: ComparisonOperator.GREATER, false);
1077: OrExpression orExpr = (OrExpression) expr.right();
1078: NQOptimizationAssertUtil.assertComparison(orExpr.left(),
1079: new String[] { "id" }, new Integer(42),
1080: ComparisonOperator.SMALLER, false);
1081: NQOptimizationAssertUtil.assertComparison(orExpr.right(),
1082: new String[] { "name" }, "Bar",
1083: ComparisonOperator.EQUALS, false);
1084: }
1085:
1086: // arithmetic
1087:
1088: boolean sampleSanityIntAdd(Data data) {
1089: return data.id < INT_CMPVAL + INT_CMPVAL; // compile time constant!
1090: }
1091:
1092: public void testSanityIntAdd() throws Exception {
1093: assertComparison("sampleSanityIntAdd", INT_FIELDNAME,
1094: new Integer(2 * INT_CMPVAL),
1095: ComparisonOperator.SMALLER, false);
1096: }
1097:
1098: boolean sampleSanityIntMultiply(Data data) {
1099: return data.id < 2 * INT_CMPVAL; // compile time constant!
1100: }
1101:
1102: public void testSanityIntMultiply() throws Exception {
1103: assertComparison("sampleSanityIntMultiply", INT_FIELDNAME,
1104: new Integer(2 * INT_CMPVAL),
1105: ComparisonOperator.SMALLER, false);
1106: }
1107:
1108: boolean sampleMemberIntMultiply(Data data) {
1109: return data.id < 2 * intMember;
1110: }
1111:
1112: public void testMemberIntMultiply() throws Exception {
1113: assertComparison("sampleMemberIntMultiply", INT_FIELDNAME,
1114: new ArithmeticExpression(
1115: new ConstValue(new Integer(2)), new FieldValue(
1116: PredicateFieldRoot.INSTANCE,
1117: "intMember", "I"),
1118: ArithmeticOperator.MULTIPLY),
1119: ComparisonOperator.SMALLER, false);
1120: }
1121:
1122: boolean sampleIntMemberDivide(Data data) {
1123: return data.id > intMember / 2;
1124: }
1125:
1126: public void testIntMemberDivide() throws Exception {
1127: assertComparison("sampleIntMemberDivide", INT_FIELDNAME,
1128: new ArithmeticExpression(new FieldValue(
1129: PredicateFieldRoot.INSTANCE, "intMember", "I"),
1130: new ConstValue(new Integer(2)),
1131: ArithmeticOperator.DIVIDE),
1132: ComparisonOperator.GREATER, false);
1133: }
1134:
1135: boolean sampleIntMemberMemberAdd(Data data) {
1136: return data.id == intMember + intMember;
1137: }
1138:
1139: public void testIntMemberMemberAdd() throws Exception {
1140: assertComparison("sampleIntMemberMemberAdd", INT_FIELDNAME,
1141: new ArithmeticExpression(new FieldValue(
1142: PredicateFieldRoot.INSTANCE, "intMember", "I"),
1143: new FieldValue(PredicateFieldRoot.INSTANCE,
1144: "intMember", "I"),
1145: ArithmeticOperator.ADD),
1146: ComparisonOperator.EQUALS, false);
1147: }
1148:
1149: // array access
1150:
1151: boolean sampleIntArrayAccess(Data data) {
1152: return data.id == intArrayMember[0];
1153: }
1154:
1155: public void testIntArrayAccess() throws Exception {
1156: assertComparison("sampleIntArrayAccess", "id",
1157: new ArrayAccessValue(new FieldValue(
1158: PredicateFieldRoot.INSTANCE, "intArrayMember",
1159: "[I"), new ConstValue(new Integer(0))),
1160: ComparisonOperator.EQUALS, false);
1161: }
1162:
1163: boolean sampleObjectArrayAccess(Data data) {
1164: return data.next.id == objArrayMember[0].id;
1165: }
1166:
1167: public void testObjectArrayAccess() throws Exception {
1168: assertComparison("sampleObjectArrayAccess", new String[] {
1169: "next", "id" }, new FieldValue(
1170: new ArrayAccessValue(new FieldValue(
1171: PredicateFieldRoot.INSTANCE, "objArrayMember",
1172: "[L" + Data.class.getName() + ";"),
1173: new ConstValue(new Integer(0))), "id", "I"),
1174: ComparisonOperator.EQUALS, false);
1175: }
1176:
1177: // non-candidate method calls
1178:
1179: boolean sampleIntAddInPredicateMethod(Data data) {
1180: return data.getId() == intMemberPlusOne();
1181: }
1182:
1183: public void testIntAddInPredicateMethod() throws Exception {
1184: assertComparison("sampleIntAddInPredicateMethod",
1185: INT_FIELDNAME, new MethodCallValue(
1186: PredicateFieldRoot.INSTANCE,
1187: "intMemberPlusOne", new Class[] {},
1188: new ComparisonOperand[] {}),
1189: ComparisonOperator.EQUALS, false);
1190: }
1191:
1192: boolean sampleStaticMethodCall(Data data) {
1193: return data.id == Integer.parseInt(stringMember);
1194: }
1195:
1196: public void testStaticMethodCall() throws Exception {
1197: assertComparison("sampleStaticMethodCall", INT_FIELDNAME,
1198: new MethodCallValue(new StaticFieldRoot(Integer.class
1199: .getName()), "parseInt",
1200: new Class[] { String.class },
1201: new ComparisonOperand[] { new FieldValue(
1202: PredicateFieldRoot.INSTANCE,
1203: "stringMember", "java.lang.String") }),
1204: ComparisonOperator.EQUALS, false);
1205: }
1206:
1207: boolean sampleTwoParamMethodCall(Data data) {
1208: return data.id == sum(intMember, 0);
1209: }
1210:
1211: public void testTwoParamMethodCall() throws Exception {
1212: assertComparison("sampleTwoParamMethodCall", INT_FIELDNAME,
1213: new MethodCallValue(PredicateFieldRoot.INSTANCE, "sum",
1214: new Class[] { Integer.TYPE, Integer.TYPE },
1215: new ComparisonOperand[] {
1216: new FieldValue(
1217: PredicateFieldRoot.INSTANCE,
1218: "intMember", "I"),
1219: new ConstValue(new Integer(0)) }),
1220: ComparisonOperator.EQUALS, false);
1221: }
1222:
1223: // multiple methods with the same name
1224:
1225: boolean sampleTimesValueMethodEqualsComp(Data data) {
1226: return data.getValue(INT_CMPVAL) == floatMember;
1227: }
1228:
1229: public void testTimesValueMethodEqualsComp() throws Exception {
1230: assertComparison("sampleTimesValueMethodEqualsComp",
1231: new String[] { OTHER_FLOAT_FIELDNAME },
1232: new FieldValue(PredicateFieldRoot.INSTANCE,
1233: "floatMember", "F"), ComparisonOperator.EQUALS,
1234: false);
1235: }
1236:
1237: // not applicable
1238:
1239: // TODO: definitely applicable - fix!
1240: boolean sampleInvalidOtherMemberEqualsComp(Data data) {
1241: return stringMember.equals(STRING_CMPVAL);
1242: }
1243:
1244: public void testInvalidOtherMemberEqualsComp() throws Exception {
1245: assertInvalid("sampleInvalidOtherMemberEqualsComp");
1246: }
1247:
1248: boolean sampleInvalidLocalVarComp(Data data) {
1249: Data next = data.next;
1250: return next.bool;
1251: }
1252:
1253: public void testInvalidLocalVarComp() throws Exception {
1254: assertInvalid("sampleInvalidLocalVarComp");
1255: }
1256:
1257: boolean sampleInvalidLocalVarCombinedComp(Data data) {
1258: Data next = data.next;
1259: return next.bool && data.bool;
1260: }
1261:
1262: public void testInvalidLocalVarCombinedComp() throws Exception {
1263: assertInvalid("sampleInvalidLocalVarCombinedComp");
1264: }
1265:
1266: boolean sampleInvalidNotOptimizableMethodCallCombined(Data data) {
1267: return data.getName().indexOf(STRING_CMPVAL) >= 0 && data.bool;
1268: }
1269:
1270: public void testInvalidNotOptimizableMethodCallCombined()
1271: throws Exception {
1272: assertInvalid("sampleInvalidNotOptimizableMethodCallCombined");
1273: }
1274:
1275: boolean sampleInvalidOtherMemberSameComp(Data data) {
1276: return stringMember == STRING_CMPVAL;
1277: }
1278:
1279: public void testInvalidOtherMemberSameComp() throws Exception {
1280: assertInvalid("sampleInvalidOtherMemberSameComp");
1281: }
1282:
1283: boolean sampleInvalidCandidateMemberArithmetic(Data data) {
1284: return data.id - 1 == INT_CMPVAL;
1285: }
1286:
1287: public void testInvalidCandidateMemberArithmetic() throws Exception {
1288: assertInvalid("sampleInvalidCandidateMemberArithmetic");
1289: }
1290:
1291: boolean sampleInvalidTemporaryStorage(Data data) {
1292: int val = INT_CMPVAL - 1;
1293: return data.id == val;
1294: }
1295:
1296: public void testInvalidTemporaryStorage() throws Exception {
1297: assertInvalid("sampleInvalidTemporaryStorage");
1298: }
1299:
1300: boolean sampleInvalidMethodCall(Data data) {
1301: data.someMethod();
1302: return true;
1303: }
1304:
1305: public void testInvalidMethodCall() throws Exception {
1306: assertInvalid("sampleInvalidMethodCall");
1307: }
1308:
1309: boolean sampleInvalidConstructorCall(Data data) {
1310: return data.next == new Data().getNext();
1311: }
1312:
1313: public void testInvalidConstructorCall() throws Exception {
1314: assertInvalid("sampleInvalidConstructorCall");
1315: }
1316:
1317: boolean sampleSimpleObjectComparison(Data data) {
1318: return data.equals(new Data());
1319: }
1320:
1321: public void testSimpleObjectComparison() throws Exception {
1322: assertInvalid("sampleSimpleObjectComparison");
1323: }
1324:
1325: boolean sampleSimpleFieldObjectComparison(Data data) {
1326: return data.next.equals(new Data());
1327: }
1328:
1329: public void testSimpleFieldObjectComparison() throws Exception {
1330: assertInvalid("sampleSimpleFieldObjectComparison");
1331: }
1332:
1333: boolean sampleSimpleFieldObjectIdentityComparison(Data data) {
1334: return data.next.equals(data.next);
1335: }
1336:
1337: public void testSimpleFieldObjectIdentityComparison()
1338: throws Exception {
1339: assertInvalid("sampleSimpleFieldObjectIdentityComparison");
1340: }
1341:
1342: boolean sampleCandEqualsNullComparison(Data data) {
1343: return data.equals(null);
1344: }
1345:
1346: public void testCandEqualsNullComparison() throws Exception {
1347: assertInvalid("sampleCandEqualsNullComparison");
1348: }
1349:
1350: boolean sampleCandIdentityObjectComparison(Data data) {
1351: return data.equals(data);
1352: }
1353:
1354: public void testCandIdentityObjectComparison() throws Exception {
1355: assertInvalid("sampleCandIdentityObjectComparison");
1356: }
1357:
1358: boolean sampleRecursiveCall(Data data) {
1359: return sampleRecursiveCall(data);
1360: }
1361:
1362: public void testRecursiveCall() throws Exception {
1363: assertInvalid("sampleRecursiveCall");
1364: }
1365:
1366: boolean sampleCandidateIntArrayAccess(Data data) {
1367: return data.intArray[0] == 0;
1368: }
1369:
1370: public void testCandidateIntArrayAccess() throws Exception {
1371: assertInvalid("sampleCandidateIntArrayAccess");
1372: }
1373:
1374: boolean sampleCandidateObjectArrayAccess(Data data) {
1375: return data.objArray[0].id == 0;
1376: }
1377:
1378: public void testCandidateObjectArrayAccess() throws Exception {
1379: assertInvalid("sampleCandidateObjectArrayAccess");
1380: }
1381:
1382: boolean sampleCandidateParamMethodCall(Data data) {
1383: return data.id == sum(data.id, 0);
1384: }
1385:
1386: public void testCandidateParamMethodCall() throws Exception {
1387: assertInvalid("sampleCandidateParamMethodCall");
1388: }
1389:
1390: boolean sampleCandidateParamStaticMethodCall(Data data) {
1391: return data.id == Integer.parseInt(data.name);
1392: }
1393:
1394: public void testCandidateParamStaticMethodCall() throws Exception {
1395: assertInvalid("sampleCandidateParamStaticMethodCall");
1396: }
1397:
1398: boolean sampleSwitch(Data data) {
1399: switch (data.id) {
1400: case 0:
1401: case 1:
1402: case 2:
1403: case 4:
1404: return true;
1405: default:
1406: return false;
1407: }
1408: }
1409:
1410: public void testSwitch() throws Exception {
1411: assertInvalid("sampleSwitch");
1412: }
1413:
1414: boolean sampleStringAppend(Data data) {
1415: return data.name.equals(stringMember + "X");
1416: }
1417:
1418: public void testStringAppend() throws Exception {
1419: assertInvalid("sampleStringAppend");
1420: }
1421:
1422: boolean sampleExternalWrapperComp(Data data) {
1423: return INT_WRAPPER_CMPVAL.compareTo(INT_WRAPPER_CMPVAL) == 0;
1424: }
1425:
1426: public void testExternalWrapperComp() throws Exception {
1427: assertInvalid("sampleExternalWrapperComp");
1428: }
1429:
1430: boolean sampleNotApplicableIfCondition(Data data) {
1431: if (stringMember.equals("XXX")) {
1432: return data.getName().equals(STRING_CMPVAL);
1433: } else {
1434: return false;
1435: }
1436: }
1437:
1438: public void testNotApplicableIfCondition() throws Exception {
1439: assertInvalid("sampleNotApplicableIfCondition");
1440: }
1441:
1442: boolean sampleNotApplicableIfStringAppendCondition(Data data) {
1443: if (stringMember.equals(stringMember + "X")) {
1444: return data.getName().equals(STRING_CMPVAL);
1445: } else {
1446: return false;
1447: }
1448: }
1449:
1450: public void testNotApplicableIfStringAppendCondition()
1451: throws Exception {
1452: assertInvalid("sampleNotApplicableIfStringAppendCondition");
1453: }
1454:
1455: boolean sampleNotApplicableSideEffectIfThenBranch(Data data) {
1456: if (data.getName().equals("foo")) {
1457: intMember += data.getId();
1458: return true;
1459: } else {
1460: return false;
1461: }
1462: }
1463:
1464: public void testNotApplicableSideEffectIfThenBranch()
1465: throws Exception {
1466: assertInvalid("sampleNotApplicableSideEffectIfThenBranch");
1467: }
1468:
1469: // internal
1470:
1471: private void assertComparison(String methodName, String fieldName,
1472: Object value, ComparisonOperator op, boolean negated) {
1473: assertComparison(methodName, new String[] { fieldName }, value,
1474: op, negated);
1475: }
1476:
1477: private void assertComparison(String methodName,
1478: String[] fieldNames, Object value, ComparisonOperator op,
1479: boolean negated) {
1480: try {
1481: Expression expr = expression(methodName);
1482: NQOptimizationAssertUtil.assertComparison(expr, fieldNames,
1483: value, op, negated);
1484: } catch (ClassNotFoundException e) {
1485: Assert.fail(e.getMessage());
1486: }
1487: }
1488:
1489: private void assertInvalid(String methodName)
1490: throws ClassNotFoundException {
1491: Expression expression = expression(methodName);
1492: if (expression != null) {
1493: System.err.println(expression);
1494: }
1495: Assert.isNull(expression);
1496: }
1497:
1498: private Expression expression(String methodName)
1499: throws ClassNotFoundException {
1500: BloatExprBuilderVisitor visitor = new BloatExprBuilderVisitor(
1501: bloatUtil);
1502: FlowGraph flowGraph = bloatUtil.flowGraph(getClass().getName(),
1503: methodName);
1504: if (NQDebug.LOG) {
1505: flowGraph.visit(new PrintVisitor());
1506: // flowGraph.visit(new TreeStructureVisitor());
1507: }
1508: flowGraph.visit(visitor);
1509: Expression expr = visitor.expression();
1510: if (NQDebug.LOG) {
1511: System.out.println(expr);
1512: }
1513: return expr;
1514: }
1515:
1516: public void tearDown() throws Exception {
1517: }
1518:
1519: public static void main(String[] args) throws Exception {
1520: // java.lang.reflect.Method method=BloatExprBuilderVisitorTestCase.class.getMethod("testFieldWrapperIntCompToEquals",new Class[]{});
1521: // Test[] tests={
1522: // new TestMethod(new BloatExprBuilderVisitorTestCase(),method)
1523: // };
1524: // TestSuite suite=new TestSuite(tests);
1525: // new TestRunner(suite).run();
1526: new TestRunner(BloatExprBuilderVisitorTestCase.class).run();
1527: }
1528: }
|