001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestHashedTripleBunch.java,v 1.7 2008/02/01 11:28:50 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.mem.test;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.mem.*;
011:
012: public class TestHashedTripleBunch extends TestTripleBunch {
013: public TestHashedTripleBunch(String name) {
014: super (name);
015: }
016:
017: protected static class HTB extends HashedTripleBunch {
018: public HTB(TripleBunch b) {
019: super (b);
020: }
021:
022: protected int improveHashCode(int hashCode) {
023: return hashCode;
024: }
025: }
026:
027: public TripleBunch getBunch() {
028: return new HashedTripleBunch(emptyBunch);
029: }
030:
031: HashedTripleBunch htb = new HTB(emptyBunch);
032:
033: static class TripleWithHash extends Triple {
034: final int hash;
035:
036: TripleWithHash(int hash, Node s, Node p, Node o) {
037: super (s, p, o);
038: this .hash = hash;
039: }
040:
041: public static TripleWithHash create(int n, String s) {
042: Triple t = triple(s);
043: return new TripleWithHash(n, t.getSubject(), t
044: .getPredicate(), t.getObject());
045: }
046:
047: public int hashCode() {
048: return hash;
049: }
050: }
051:
052: public void testHashcodeUsedAsIndex() {
053: HashedTripleBunch htb = new HTB(emptyBunch);
054: int limit = htb.currentCapacity();
055: for (int i = 0; i < limit; i += 1) {
056: TripleWithHash t = TripleWithHash.create(i, "s p o");
057: htb.add(t);
058: assertSame(t, htb.getItemForTestingAt(i));
059: }
060: }
061:
062: public void testRemovePerformsShiftFromTop() {
063: int capacity = htb.currentCapacity();
064: testRemovePerformsShift(capacity - 1, capacity);
065: }
066:
067: public void testRemovePerformsShiftFromMiddle() {
068: int capacity = htb.currentCapacity();
069: testRemovePerformsShift(capacity - 3, capacity);
070: }
071:
072: public void testRemovePerformsShiftWrappingLowestTwo() {
073: int capacity = htb.currentCapacity();
074: testRemovePerformsShift(0, capacity);
075: }
076:
077: public void testRemovePerformsShiftWrappingLowest() {
078: int capacity = htb.currentCapacity();
079: testRemovePerformsShift(1, capacity);
080: }
081:
082: private void testRemovePerformsShift(int most, int capacity) {
083: int next = most - 1;
084: if (next < 0)
085: next += capacity;
086: int least = most - 2;
087: if (least < 0)
088: least += capacity;
089: TripleWithHash t1 = TripleWithHash.create(most, "s p o");
090: TripleWithHash t2 = TripleWithHash.create(next, "a b c");
091: TripleWithHash t3 = TripleWithHash.create(most, "x y z");
092: htb.add(t1);
093: htb.add(t2);
094: htb.add(t3);
095: assertSame(t1, htb.getItemForTestingAt(most));
096: assertSame(t2, htb.getItemForTestingAt(next));
097: assertSame(t3, htb.getItemForTestingAt(least));
098: //
099: htb.remove(t1);
100: assertSame(t3, htb.getItemForTestingAt(most));
101: assertSame(t2, htb.getItemForTestingAt(next));
102: assertSame(null, htb.getItemForTestingAt(least));
103: }
104:
105: public void testIteratorRemovePerformsShiftAndDeliversElementFromTop() {
106: int capacity = htb.currentCapacity();
107: testIteratorRemovePerformsShiftAndDeliversElement(capacity - 1,
108: capacity);
109: }
110:
111: public void testIteratorRemovePerformsShiftAndDeliversElementFromMiddle() {
112: int capacity = htb.currentCapacity();
113: testIteratorRemovePerformsShiftAndDeliversElement(capacity - 3,
114: capacity);
115: }
116:
117: // public void testIteratorRemovePerformsShiftAndDeliversElementWrappingLowest()
118: // {
119: // int capacity = htb.currentCapacity();
120: // testIteratorRemovePerformsShiftAndDeliversElement( 1, capacity );
121: // }
122: //
123: // public void testIteratorRemovePerformsShiftAndDeliversElementWrappingLowestTwo()
124: // {
125: // int capacity = htb.currentCapacity();
126: // testIteratorRemovePerformsShiftAndDeliversElement( 0, capacity );
127: // }
128:
129: private void testIteratorRemovePerformsShiftAndDeliversElement(
130: int most, int capacity) {
131: // int next = most - 1; if (next < 0) next += capacity;
132: // int least = most - 2; if (least < 0) least += capacity;
133: // TripleWithHash t1 = TripleWithHash.create( most, "s p o" );
134: // TripleWithHash t2 = TripleWithHash.create( next, "a b c" );
135: // TripleWithHash t3 = TripleWithHash.create( most, "x y z" );
136: // htb.add( t1 );
137: // htb.add( t2 );
138: // htb.add( t3 );
139: // ExtendedIterator it = htb.iterator();
140: // assertSame( t1, it.next() );
141: // it.remove();
142: // assertSame( t3, it.next() );
143: // assertSame( t2, it.next() );
144: }
145: }
146:
147: /*
148: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
149: * All rights reserved.
150: *
151: * Redistribution and use in source and binary forms, with or without
152: * modification, are permitted provided that the following conditions
153: * are met:
154: * 1. Redistributions of source code must retain the above copyright
155: * notice, this list of conditions and the following disclaimer.
156: * 2. Redistributions in binary form must reproduce the above copyright
157: * notice, this list of conditions and the following disclaimer in the
158: * documentation and/or other materials provided with the distribution.
159: * 3. The name of the author may not be used to endorse or promote products
160: * derived from this software without specific prior written permission.
161: *
162: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
163: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
164: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
165: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
166: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
167: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
168: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
169: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
170: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
171: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
172: */
|