KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > ozoneDB > core > storage > gammaStore > LocTest


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2004 Leo Mekenkamp. All rights reserved.
5
//
6
// $Id$
7

8 package test.ozoneDB.core.storage.gammaStore;
9
10 import java.util.HashSet JavaDoc;
11 import java.util.Random JavaDoc;
12 import java.util.Set JavaDoc;
13 import java.util.TreeSet JavaDoc;
14 import junit.framework.Test;
15 import junit.framework.TestResult;
16 import junit.framework.TestSuite;
17 import junit.textui.TestRunner;
18 import org.ozoneDB.core.storage.gammaStore.Loc;
19 import test.OzoneTestCase;
20 import test.OzoneTestRunner;
21
22 /**
23  * @author leo
24  */

25 public class LocTest extends OzoneTestCase {
26     
27     private int capacity;
28     private long range;
29     private int slack;
30     
31     private Loc loc;
32     private Set JavaDoc mirror;
33     private Random JavaDoc random = new Random JavaDoc(0);
34     
35     public LocTest() {
36         super("testLoc");
37     }
38     
39     public static Test suite() {
40         TestSuite suite = new TestSuite();
41
42         suite.addTest(new LocTest());
43         return suite;
44     }
45
46     public void testLoc() {
47         capacity = 4;
48         slack = 0;
49         range = capacity * 2;
50         execute();
51         slack = 1;
52         execute();
53         slack = 3;
54         execute();
55         
56         capacity = 13;
57         slack = 0;
58         range = capacity * 2;
59         execute();
60         slack = 5;
61         execute();
62         slack = 8;
63         execute();
64
65         capacity = 100;
66         slack = 0;
67         range = capacity + 10;
68         execute();
69         slack = 2000;
70         execute();
71     }
72     
73     private void execute() {
74     
75         loc = new Loc(capacity, slack);
76         mirror = new TreeSet JavaDoc();
77         
78         checkEquality();
79
80         randomFill(capacity);
81
82         checkEquality();
83
84         checkEquality();
85         
86         randomRemove(capacity / 2);
87         
88         checkEquality();
89             
90         randomFill(capacity);
91         
92         checkEquality();
93
94         randomRemove(0);
95
96         checkEquality();
97
98         randomFill(capacity / 2 );
99
100         checkEquality();
101
102         randomRemove(capacity / 4);
103         
104         checkEquality();
105
106         randomRemove(0);
107
108         checkEquality();
109     }
110     
111     private void randomFill(int targetSize) {
112         while (mirror.size() < targetSize) {
113             assertEquals(loc.size(), mirror.size());
114             Long JavaDoc value = new Long JavaDoc(random.nextLong() % range);
115             boolean reallyAdded = mirror.add(value);
116             assertTrue(reallyAdded != (loc.getKeyPos(value.longValue()) >= 0));
117             loc.putKey(value.longValue());
118         }
119     }
120
121     private void randomRemove(int targetSize) {
122         while (loc.size() > targetSize) {
123             assertTrue("loc size: " + loc.size() + "; mirror size: " + mirror.size()
124                     +"\nloc:\n" + loc + "\nmirror:\n" + mirror, loc.size() == mirror.size());
125             Long JavaDoc value = new Long JavaDoc(random.nextLong() % range);
126             boolean reallyRemoved = mirror.remove(value);
127             assertTrue(reallyRemoved == (loc.removeKey(value.longValue()) >= 0));
128             if (!reallyRemoved) {
129                 value = (Long JavaDoc) mirror.iterator().next();
130                 mirror.remove(value);
131                 assertTrue(loc.removeKey(value.longValue()) >= 0);
132             }
133         }
134     }
135
136     private void checkEquality() {
137         checkFull(mirror.size() == capacity);
138         for (int i = 0; i < capacity * 3; i++) {
139             Long JavaDoc value = new Long JavaDoc(random.nextLong() % range);
140             int pos = loc.getKeyPos(value.longValue());
141             if (pos < 0) {
142                 assertFalse("value: " + value + "loc:\n " + loc + "\nmirror:\n" + mirror, mirror.contains(value));
143             } else {
144                 assertTrue("value: " + value + "loc:\n " + loc + "\nmirror:\n" + mirror, mirror.contains(value));
145             }
146         }
147     }
148     
149     private void checkFull(boolean isFull) {
150         try {
151             // RANGE + 1 is never put into the Loc
152
loc.putKey(range + 1);
153             assertFalse(isFull);
154             loc.removeKey(range + 1);
155         } catch (IndexOutOfBoundsException JavaDoc e) {
156             assertTrue("should not throw IndexOutOfBoundsException, because not full\n" + loc, isFull);
157         }
158     }
159
160         
161     
162     public static void main(String JavaDoc[] args) {
163 // LocTest locTest = new LocTest();
164
// locTest.run(new TestResult());
165
TestRunner testRunner = new TestRunner();
166         testRunner.run(LocTest.class);
167     }
168
169 }
170
Popular Tags