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 java.util.*;
026: import junit.framework.*;
027: import org.artsProject.mcop.*;
028: import drjava.smyle.core.*;
029:
030: public class FastIntVectorTest extends TestCase {
031: public FastIntVectorTest(String name) {
032: super (name);
033: }
034:
035: final int blockSize = 4;
036:
037: FastIntVector v;
038: ArrayList<Integer> vRef;
039: //ChunkManager cm;
040: BitSet whiteList;
041: Random random;
042:
043: public void setUp() throws Exception {
044: //cm = new DefaultChunkManager(new MemoryDisk(), DiskStore.VERSION);
045: v = new FastIntVector();
046: v.setBlockSize(blockSize);
047: vRef = new ArrayList<Integer>();
048: whiteList = new BitSet();
049: random = new Random(0);
050: }
051:
052: public void testAddAndGet() throws Exception {
053: for (int i = 0; i < 20; i++)
054: add(i * 2);
055: for (int i = 0; i < 20; i++)
056: assertEquals(i * 2, v.get(i));
057: assertContentsAndStructure();
058: }
059:
060: public void testRandomAdd() throws Exception {
061: for (int i = 0; i < 16; i++) {
062: add((random.nextInt() & 0x7fffffff) % (vRef.size() + 1),
063: i * 1000);
064: assertContentsAndStructure();
065: }
066: }
067:
068: public void testRemoveElement() throws Exception {
069: for (int i = 0; i < 32; i++)
070: add(i * 2);
071: assertTrue(!removeElement(-123));
072: for (int i = 0; i < 32; i++) {
073: assertTrue(removeElement((i ^ 5) * 2));
074: assertContentsAndStructure();
075: }
076: }
077:
078: public void testRemoveZeroBug() throws Exception {
079: add(1);
080: assertTrue(!v.removeElement(0));
081: }
082:
083: public void testRemove() throws Exception {
084: for (int i = 0; i < 32; i++)
085: add(i * 2);
086: for (int i = 0; i < 32; i++) {
087: remove((random.nextInt() & 0x7fffffff) % vRef.size());
088: assertContentsAndStructure();
089: }
090: }
091:
092: public void testSet() throws Exception {
093: for (int i = 0; i < 32; i++)
094: add(i * 2);
095: for (int i = 31; i >= 0; i--) {
096: set(i, i * 3);
097: assertContentsAndStructure();
098: }
099: }
100:
101: public void testIterator() throws Exception {
102: assertTrue(!v.iterator().hasNext());
103:
104: for (int i = 0; i < 32; i++)
105: add(i * 2);
106:
107: Iterator<Integer> iRef = vRef.iterator();
108: for (IntIterator i = v.iterator(); i.hasNext();)
109: assertEquals(iRef.next().intValue(), i.next());
110: assertTrue(!iRef.hasNext());
111: }
112:
113: void add(int i) {
114: v.add(i);
115: vRef.add(new Integer(i));
116: }
117:
118: void add(int index, int value) {
119: v.add(index, value);
120: vRef.add(index, new Integer(value));
121: }
122:
123: void set(int index, int value) {
124: v.set(index, value);
125: vRef.set(index, new Integer(value));
126: }
127:
128: boolean removeElement(int i) {
129: vRef.remove(new Integer(i));
130: return v.removeElement(i);
131: }
132:
133: void remove(int index) {
134: v.remove(index);
135: vRef.remove(index);
136: }
137:
138: void rotate() throws Exception {
139: /*ChunkRef master = vector.flush();
140: BitSet oldWhiteList = (BitSet) whiteList.clone();
141: vector.collectChunks(whiteList);
142: cm.deleteEverythingBut(whiteList);
143: whiteList = oldWhiteList;
144: vector = new FastIntVector<HString>(cm, new HStringMarDemar(), master);*/
145: }
146:
147: void assertContentsAndStructure() {
148: assertContentsAndStructure(blockSize / 2);
149: }
150:
151: void assertContentsAndStructure(int minBlockSize) {
152: assertEquals(vRef.size(), v.size());
153: assertEquals(vRef.isEmpty(), v.isEmpty());
154: int n = 0;
155: for (int b = 0; b < v.numBlocks(); b++) {
156: int[] data = v.getBlockData(b);
157: if (!(vRef.size() <= blockSize && data.length == vRef.size()))
158: assert("Data length doesn't fit block size "+blockSize+": "+data.length,
159: data.length >= minBlockSize && data.length <= blockSize);
160: for (int i = 0; i < data.length; i++)
161: assertEquals(vRef.get(n++).intValue(), data[i]);;
162: }
163: assertEquals("Total entries", vRef.size(), n);
164: }
165:
166: public void testChunkRefSeqCompatibility() {
167: // load from buffer
168: Buffer b = new Buffer();
169: ArrayList<ChunkRef> vCR = new ArrayList<ChunkRef>();
170: for (int i = 0; i < 16; i++) {
171: vCR.add(new ChunkRef(i + 9));
172: vRef.add(new Integer(i + 9));
173: }
174: MCOP.writeSeq(b, vCR);
175: String data1 = b.toString();
176: v.read(b);
177: assertContentsAndStructure(blockSize);
178:
179: // save to buffer
180: b = new Buffer();
181: v.marshal(b);
182: assertEquals(data1, b.toString());
183: }
184:
185: public void testClear() throws Exception {
186: v.add(55);
187: v.clear();
188: assertContentsAndStructure();
189: }
190:
191: public void testIndexOf() throws Exception {
192: for (int i = 0; i < 32; i++)
193: v.add(i * 3);
194: assertEquals(0, v.indexOf(0));
195: assertEquals(16, v.indexOf(16 * 3));
196: assertEquals(31, v.indexOf(31 * 3));
197: assertEquals(-1, v.indexOf(32 * 3));
198: }
199: }
|