001: /*
002: This source file is part of Smyle, a database library.
003: For up-to-date information, see http://www.drjava.de/smyle
004: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: For full license text, see doc/license/lgpl.txt in this distribution
021: */
022:
023: package drjava.smyle.tests;
024:
025: import junit.framework.*;
026: import drjava.smyle.core.indexing.*;
027: import drjava.smyle.meta.*;
028: import drjava.smyle.testtypes.*;
029:
030: public class QueryOptimizerTest extends TestCase {
031: public QueryOptimizerTest(String name) {
032: super (name);
033: }
034:
035: static final Function[] nil = new Function[0];
036: static final IndexProfile profile_nil_nil = new IndexProfile(nil,
037: nil), profile_l_nil = new IndexProfile(
038: new Function[] { DataTypes.f_l }, nil),
039: profile_nil_l = new IndexProfile(nil,
040: new Function[] { DataTypes.f_l }),
041: profile_l_ll = new IndexProfile(
042: new Function[] { DataTypes.f_l },
043: new Function[] { DataTypes.f_ll }),
044: profile_b$s_l$ll = new IndexProfile(new Function[] {
045: DataTypes.f_b, DataTypes.f_s }, new Function[] {
046: DataTypes.f_l, DataTypes.f_ll }),
047: profile_l$ll_nil = new IndexProfile(new Function[] {
048: DataTypes.f_l, DataTypes.f_ll }, nil);
049:
050: static final IndexProfile[] someProfiles = { profile_nil_nil,
051: profile_l_nil, profile_nil_l, profile_b$s_l$ll };
052:
053: static final Function[][] someFieldLists = { nil,
054: { DataTypes.f_l }, { DataTypes.f_l, DataTypes.f_s } };
055:
056: public void testCreateProfile() {
057: assertEquals(new IndexProfile(nil, nil), QueryOptimizer
058: .createProfile(new Filter()));
059: assertEquals(profile_l_nil, QueryOptimizer
060: .createProfile(new DataTypes_filter().lEquals(0)));
061: assertEquals(profile_nil_l, QueryOptimizer
062: .createProfile(new DataTypes_filter().orderByL()));
063: assertEquals(profile_b$s_l$ll, QueryOptimizer
064: .createProfile(new DataTypes_filter().bEquals(true)
065: .sEquals("test").orderByL().orderByLl()));
066: }
067:
068: public void testIndexProfileEquality() {
069: assertEquals(profile_nil_nil, profile_nil_nil);
070: assertTrue(!profile_nil_nil.equals(new IndexProfile(
071: new Function[] { new Field(0) }, nil)));
072: assertTrue(!profile_nil_nil.equals(new IndexProfile(nil,
073: new Function[] { new Field(0) })));
074: }
075:
076: public void testIndexProfileHashCode() {
077: assertEquals(profile_nil_nil.hashCode(), new IndexProfile(nil,
078: nil).hashCode());
079: assertEquals(profile_nil_l.hashCode(), new IndexProfile(nil,
080: new Function[] { DataTypes.f_l }).hashCode());
081: }
082:
083: public void testIndexProfileToString() {
084: assertEquals("perm=[] ord=[]", profile_nil_nil.toString());
085: assertEquals(
086: "perm=[Field #4 Field #1] ord=[Field #3 Field #2]",
087: new IndexProfile(new Function[] { DataTypes.f_strct,
088: DataTypes.f_l }, new Function[] {
089: DataTypes.f_s, DataTypes.f_ll }).toString());
090: }
091:
092: public void testIndexScore() {
093: // nil profile always gives score 0
094: for (int i = 0; i < someFieldLists.length; i++)
095: assertEquals(0, QueryOptimizer.indexScore(profile_nil_nil,
096: someFieldLists[i]));
097:
098: // empty field list always gives score 0
099: for (int i = 0; i < someProfiles.length; i++)
100: assertEquals(0, QueryOptimizer.indexScore(someProfiles[i],
101: nil));
102:
103: assertEquals(1, QueryOptimizer.indexScore(profile_l_nil,
104: new Function[] { DataTypes.f_l }));
105: assertEquals(1, QueryOptimizer.indexScore(profile_l_nil,
106: new Function[] { DataTypes.f_l, DataTypes.f_ll }));
107:
108: // index can't be used because first field isn't in profile
109: assertEquals(0, QueryOptimizer.indexScore(profile_l_nil,
110: new Function[] { DataTypes.f_ll, DataTypes.f_l }));
111:
112: assertEquals(2, QueryOptimizer.indexScore(profile_l_ll,
113: new Function[] { DataTypes.f_l, DataTypes.f_ll }));
114:
115: assertEquals(2, QueryOptimizer.indexScore(profile_l$ll_nil,
116: new Function[] { DataTypes.f_l, DataTypes.f_ll,
117: DataTypes.f_strct }));
118: assertEquals(2, QueryOptimizer.indexScore(profile_l$ll_nil,
119: new Function[] { DataTypes.f_ll, DataTypes.f_l,
120: DataTypes.f_strct }));
121:
122: // wrong order in ord part => only 2 points
123: assertEquals(2, QueryOptimizer.indexScore(profile_b$s_l$ll,
124: new Function[] { DataTypes.f_b, DataTypes.f_s,
125: DataTypes.f_ll }));
126: }
127: }
|