Repository navigation
Expand file tree
/
Copy pathHTreeSetTest.java
More file actions
160 lines (130 loc) · 5.4 KB
/
Copy pathHTreeSetTest.java
File metadata and controls
160 lines (130 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapdb;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/*
* Tests for HashSet which comes with JDBM. Original code comes from Apache Harmony,
* Modified by Jan Kotek for use in JDBM
*/
@SuppressWarnings({"unchecked","rawtypes"})
public class HTreeSetTest{
Store engine;
Set hs;
static Object[] objArray;
static {
objArray = new Object[1000];
for (int i = 0; i < objArray.length; i++)
objArray[i] = i;
}
@Before public void init(){
engine = new StoreDirect(null);
engine.init();
Engine[] engines = HTreeMap.fillEngineArray(engine);
hs = new HTreeMap(engines,
false, null, 0,HTreeMap.preallocateSegments(engines),Serializer.BASIC,null,0,0,0,0,0,null,null,null,null, 0L, false, null).keySet();
Collections.addAll(hs, objArray);
}
@Test public void test_Constructor() {
// Test for method java.util.HashSet()
Engine[] engines = HTreeMap.fillEngineArray(engine);
Set hs2 = new HTreeMap(engines,
false, null,0,HTreeMap.preallocateSegments(engines),Serializer.BASIC,null,0,0,0,0,0,null,null,null,null,0L, false, null).keySet();
assertEquals("Created incorrect HashSet", 0, hs2.size());
}
@After
public void close(){
engine.close();
}
@Test public void test_addLjava_lang_Object() {
// Test for method boolean java.util.HashSet.add(java.lang.Object)
int size = hs.size();
hs.add(8);
assertTrue("Added element already contained by set", hs.size() == size);
hs.add(-9);
assertTrue("Failed to increment set size after add",
hs.size() == size + 1);
assertTrue("Failed to add element to set", hs.contains(new Integer(-9)));
}
@Test public void test_clear() {
// Test for method void java.util.HashSet.clear()
Set orgSet = new java.util.HashSet(hs);
hs.clear();
Iterator i = orgSet.iterator();
assertEquals("Returned non-zero size after clear", 0, hs.size());
while (i.hasNext())
assertTrue("Failed to clear set", !hs.contains(i.next()));
}
@Test public void test_containsLjava_lang_Object() {
// Test for method boolean java.util.HashSet.contains(java.lang.Object)
assertTrue("Returned false for valid object", hs.contains(objArray[90]));
assertTrue("Returned true for invalid Object", !hs
.contains(-111111));
}
@Test public void test_isEmpty() {
// Test for method boolean java.util.HashSet.isEmpty()
Engine[] engines = HTreeMap.fillEngineArray(engine);
assertTrue("Empty set returned false", new HTreeMap(engines,
false, null,0,HTreeMap.preallocateSegments(engines),Serializer.BASIC,null,0,0,0,0,0,null,null,null,null,0L, false,null).keySet().isEmpty());
assertTrue("Non-empty set returned true", !hs.isEmpty());
}
@Test public void test_iterator() {
// Test for method java.util.Iterator java.util.HashSet.iterator()
Iterator i = hs.iterator();
int x = 0;
while (i.hasNext()) {
assertTrue("Failed to iterate over all elements", hs.contains(i
.next()));
++x;
}
assertTrue("Returned iteration of incorrect size", hs.size() == x);
}
@Test public void test_removeLjava_lang_Object() {
// Test for method boolean java.util.HashSet.remove(java.lang.Object)
int size = hs.size();
hs.remove(new Integer(98));
assertTrue("Failed to remove element", !hs.contains(new Integer(98)));
assertTrue("Failed to decrement set size", hs.size() == size - 1);
}
@Test public void test_size() {
// Test for method int java.util.HashSet.size()
assertTrue("Returned incorrect size", hs.size() == (objArray.length));
hs.clear();
assertEquals("Cleared set returned non-zero size", 0, hs.size());
}
@Test public void issue116_isEmpty(){
Set s = DBMaker.fileDB(TT.tempDbFile())
.transactionDisable()
.make()
.hashSet("name");
assertTrue(s.isEmpty());
assertEquals(0,s.size());
s.add("aa");
assertEquals(1,s.size());
assertFalse(s.isEmpty());
s.remove("aa");
assertTrue(s.isEmpty());
assertEquals(0,s.size());
}
}