001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved.
004: $Id: TestHashCommon.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 java.util.*;
010:
011: import com.hp.hpl.jena.mem.HashCommon;
012: import com.hp.hpl.jena.rdf.model.test.ModelTestBase;
013:
014: public class TestHashCommon extends ModelTestBase {
015: protected static final Item item2X = new Item(2, "X");
016: protected static final Item item1Y = new Item(1, "Y");
017: protected static final Item item2Z = new Item(2, "Z");
018:
019: public TestHashCommon(String name) {
020: super (name);
021: }
022:
023: static class ProbeHashCommon extends HashCommon {
024: protected ProbeHashCommon(int initialCapacity) {
025: super (initialCapacity);
026: }
027:
028: protected void set(int index, Item object) {
029: keys[index] = object;
030: }
031:
032: public Object removeFrom(int here) {
033: return super .removeFrom(here);
034: }
035:
036: public int top() {
037: return capacity - 1;
038: }
039:
040: public int capacity() {
041: return capacity;
042: }
043:
044: /*
045: Leaving the hashcode alone makes testing simpler.
046: */
047: protected int improveHashCode(int hashCode) {
048: return hashCode;
049: }
050: }
051:
052: static class Item {
053: protected final int n;
054: protected final String s;
055:
056: public Item(int n, String s) {
057: this .n = n;
058: this .s = s;
059: }
060:
061: public int hashCode() {
062: return n;
063: }
064:
065: public boolean equals(Object other) {
066: return other instanceof Item && s.equals(((Item) other).s);
067: }
068:
069: public String toString() {
070: return s + "#" + n;
071: }
072: }
073:
074: public void testSanityCheckTestDataConstruction() {
075: ProbeHashCommon h = probeWith("1:2:x 4:7:y -1:5:z");
076: assertEquals(new Item(2, "x"), h.getItemForTestingAt(1));
077: assertEquals(new Item(7, "y"), h.getItemForTestingAt(4));
078: assertEquals(new Item(5, "z"), h.getItemForTestingAt(h.top()));
079: }
080:
081: public void testHashcodeUsedAsIndex() {
082: ProbeHashCommon htb = new ProbeHashCommon(10);
083: int limit = htb.capacity();
084: for (int i = 0; i < limit; i += 1) {
085: Item t = new Item(i, "s p o");
086: // assertEquals( i, htb.)
087: // assertSame( t, htb.getItemForTestingAt( i ) );
088: }
089: }
090:
091: public void testRemoveNoMove() {
092: ProbeHashCommon h = probeWith("1:1:Y 2:2:Z");
093: Item moved = (Item) h.removeFrom(2);
094: assertSame(null, moved);
095: assertAlike(probeWith("1:1:Y"), h);
096: }
097:
098: public void testRemoveSimpleMove() {
099: ProbeHashCommon h = probeWith("0:2:X 1:1:Y 2:2:Z");
100: assertSame(null, (Item) h.removeFrom(1));
101: assertAlike(probeWith("1:2:X 2:2:Z"), h);
102: }
103:
104: public void testRemoveCircularMove() {
105: ProbeHashCommon h = probeWith("0:0:X 1:2:Y -1:2:Z");
106: Item moved = (Item) h.removeFrom(1);
107: assertAlike(probeWith("0:0:X 1:2:Z"), h);
108: assertEquals(new Item(2, "Z"), moved);
109: }
110:
111: public void testKeyIterator() {
112: ProbeHashCommon h = probeWith("0:0:X");
113: Set elements = h.keyIterator().toSet();
114: assertEquals(itemSet("0:X"), elements);
115: }
116:
117: /**
118: Assert that the two probe HashCommon's are "alike", that is, that they
119: have key arrays of equal size and are element-by-element equal.
120: Otherwise, fail (preferably with an appropriate message).
121: */
122: private void assertAlike(ProbeHashCommon desired,
123: ProbeHashCommon got) {
124: assertEquals("capacities must be equal", desired.capacity(),
125: got.capacity());
126: for (int i = 0; i < desired.capacity(); i += 1)
127: assertEquals(desired.getItemForTestingAt(i), got
128: .getItemForTestingAt(i));
129: }
130:
131: /**
132: Answer a probe with the specified items. <code>items</code> is a
133: space-separated string of item descriptions. Each description is a
134: colon-separated sequence <code>index:hash:label</code>: the
135: item <code>(hash, label)</code> will be placed at <code>index</code>.
136: Negative index values are interpreted as indexs from the <i>end</code>
137: of the key array, by adding the probe's capacity to them.
138: */
139: protected ProbeHashCommon probeWith(String items) {
140: ProbeHashCommon result = new ProbeHashCommon(10);
141: StringTokenizer st = new StringTokenizer(items);
142: while (st.hasMoreTokens()) {
143: String item = st.nextToken();
144: StringTokenizer itemElements = new StringTokenizer(item,
145: ":");
146: int index = Integer.parseInt(itemElements.nextToken());
147: int hash = Integer.parseInt(itemElements.nextToken());
148: String w = itemElements.nextToken();
149: result.set((index < 0 ? index + result.capacity() : index),
150: new Item(hash, w));
151: }
152: return result;
153: }
154:
155: protected Set itemSet(String items) {
156: Set result = new HashSet();
157: StringTokenizer st = new StringTokenizer(items);
158: while (st.hasMoreTokens())
159: addItem(result, st.nextToken());
160: return result;
161: }
162:
163: private void addItem(Set result, String item) {
164: StringTokenizer itemElements = new StringTokenizer(item, ":");
165: int hash = Integer.parseInt(itemElements.nextToken());
166: String w = itemElements.nextToken();
167: result.add(new Item(hash, w));
168: }
169: }
170:
171: /*
172: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
173: * All rights reserved.
174: *
175: * Redistribution and use in source and binary forms, with or without
176: * modification, are permitted provided that the following conditions
177: * are met:
178: * 1. Redistributions of source code must retain the above copyright
179: * notice, this list of conditions and the following disclaimer.
180: * 2. Redistributions in binary form must reproduce the above copyright
181: * notice, this list of conditions and the following disclaimer in the
182: * documentation and/or other materials provided with the distribution.
183: * 3. The name of the author may not be used to endorse or promote products
184: * derived from this software without specific prior written permission.
185: *
186: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
187: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
188: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
189: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
190: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
192: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
193: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
194: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
195: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
196: */
|