001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestSimpleTripleSorter.java,v 1.11 2008/01/02 12:08:56 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.query.test;
008:
009: import com.hp.hpl.jena.graph.test.*;
010: import com.hp.hpl.jena.graph.query.*;
011: import com.hp.hpl.jena.graph.*;
012:
013: import java.util.*;
014: import junit.framework.*;
015:
016: /**
017: @author kers
018: */
019: public class TestSimpleTripleSorter extends GraphTestBase {
020: public TestSimpleTripleSorter(String name) {
021: super (name);
022: }
023:
024: public static TestSuite suite() {
025: return new TestSuite(TestSimpleTripleSorter.class);
026: }
027:
028: private TripleSorter sorter = new SimpleTripleSorter();
029:
030: /**
031: Test that the empty triple array sorts into an empty triple array
032: */
033: public void testEmpty() {
034: Triple[] triples = new Triple[] {};
035: assertEquals(0, sorter.sort(triples).length);
036: }
037:
038: /**
039: Test that a singleton triple array sorts into that same singleton array
040: for various different search-styles of triples
041: */
042: public void testSingle() {
043: testSingle("S P O");
044: testSingle("S ?P O");
045: testSingle("S P ?O");
046: testSingle("?S ?P O");
047: testSingle("?S P ?O");
048: testSingle("S ?P ?O");
049: testSingle("?S ?P ?O");
050: testSingle("?? P O");
051: testSingle("S ?? O");
052: testSingle("S P ??O");
053: testSingle("?? ?? O");
054: testSingle("?? P ??");
055: testSingle("S ?? ??");
056: testSingle("?? ?? ??");
057: }
058:
059: public void testSingle(String ts) {
060: Triple t = Triple.create(ts);
061: assertEquals(Arrays.asList(new Triple[] { t }), Arrays
062: .asList(sorter.sort(new Triple[] { t })));
063: }
064:
065: /**
066: Test that concrete nodes get sorted to the beginning of the result
067: */
068: public void testConcreteFirst() {
069: testReordersTo("S P O; ?s ?p ?o", "S P O; ?s ?p ?o");
070: testReordersTo("S P O; ?s ?p ?o", "?s ?p ?o; S P O");
071: testReordersTo("S P O; ?s ?p ?o; ?a ?b ?c",
072: "?s ?p ?o; ?a ?b ?c; S P O");
073: testReordersTo("S P O; ?s ?p ?o; ?a ?b ?c",
074: "?s ?p ?o; S P O; ?a ?b ?c");
075: }
076:
077: /**
078: Test that bound variables get sorted nearer the beginning than unbound ones
079: */
080: public void testBoundFirst() {
081: testReordersTo("?s R a; ?s ?p ?o", "?s ?p ?o; ?s R a");
082: testReordersTo("?s R a; ?s ?p b;", "?s ?p b; ?s R a");
083: testReordersTo("?a P b; ?c Q d; ?a P ?c",
084: "?a P b; ?a P ?c; ?c Q d");
085: }
086:
087: /**
088: Test that ANY is heavier than one variable but lighter than two
089: */
090: public void testANY() {
091: testReordersTo("?? C d; ?a X ?b", "?a X ?b; ?? C d");
092: testReordersTo("?a B c; ?? D e", "?? D e; ?a B c");
093: }
094:
095: /**
096: Test that binding a variable makes it lighter than an unbound variable
097: */
098: public void testInteraction() {
099: testReordersTo("?a P b; ?a Q ?b; ?b R ?c",
100: "?b R ?c; ?a Q ?b; ?a P b");
101: }
102:
103: /**
104: Test that a triple that binds more things gets sorted earlier than a equally-light
105: triple that binds fewer things
106: */
107: public void testSortByMass() {
108: testReordersTo("?b c d; ?a b c; ?b ?c d; ?a ?b ?d",
109: "?a b c; ?b c d; ?b ?c d; ?a ?b ?d");
110: }
111:
112: /**
113: Utility: test that the triple array described by <code>original</code> gets reordered
114: to the triple array described by <code>desired</code>.
115: */
116: public void testReordersTo(String desired, String original) {
117: Triple[] o = tripleArray(original), d = tripleArray(desired);
118: assertEquals(Arrays.asList(d), Arrays.asList(sorter.sort(o)));
119: }
120: }
121:
122: /*
123: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
124: All rights reserved.
125:
126: Redistribution and use in source and binary forms, with or without
127: modification, are permitted provided that the following conditions
128: are met:
129:
130: 1. Redistributions of source code must retain the above copyright
131: notice, this list of conditions and the following disclaimer.
132:
133: 2. Redistributions in binary form must reproduce the above copyright
134: notice, this list of conditions and the following disclaimer in the
135: documentation and/or other materials provided with the distribution.
136:
137: 3. The name of the author may not be used to endorse or promote products
138: derived from this software without specific prior written permission.
139:
140: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
141: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
142: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
143: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
144: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
145: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
146: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
147: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
148: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
149: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
150: */
|