~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/facet/src/test/org/apache/lucene/util/collections/IntToDoubleMapTest.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.util.collections;
2
 
 
3
 
import org.junit.Test;
4
 
 
5
 
import org.apache.lucene.util.LuceneTestCase;
6
 
import org.apache.lucene.util.collections.DoubleIterator;
7
 
import org.apache.lucene.util.collections.IntIterator;
8
 
import org.apache.lucene.util.collections.IntToDoubleMap;
9
 
 
10
 
import java.util.HashSet;
11
 
import java.util.Random;
12
 
 
13
 
/**
14
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
15
 
 * contributor license agreements.  See the NOTICE file distributed with
16
 
 * this work for additional information regarding copyright ownership.
17
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
18
 
 * (the "License"); you may not use this file except in compliance with
19
 
 * the License.  You may obtain a copy of the License at
20
 
 *
21
 
 *     http://www.apache.org/licenses/LICENSE-2.0
22
 
 *
23
 
 * Unless required by applicable law or agreed to in writing, software
24
 
 * distributed under the License is distributed on an "AS IS" BASIS,
25
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26
 
 * See the License for the specific language governing permissions and
27
 
 * limitations under the License.
28
 
 */
29
 
 
30
 
public class IntToDoubleMapTest extends LuceneTestCase {
31
 
  private static void assertGround(double value) {
32
 
    assertEquals(IntToDoubleMap.GROUND, value, Double.MAX_VALUE);
33
 
  }
34
 
  
35
 
  @Test
36
 
  public void test0() {
37
 
    IntToDoubleMap map = new IntToDoubleMap();
38
 
 
39
 
    assertGround(map.get(0));
40
 
    
41
 
    for (int i = 0; i < 100; ++i) {
42
 
      int value = 100 + i;
43
 
      assertFalse(map.containsValue(value));
44
 
      map.put(i, value);
45
 
      assertTrue(map.containsValue(value));
46
 
      assertNotNull(map.get(i));
47
 
    }
48
 
 
49
 
    assertEquals(100, map.size());
50
 
    for (int i = 0; i < 100; ++i) {
51
 
      assertTrue(map.containsKey(i));
52
 
      assertEquals(100 + i, map.get(i), Double.MAX_VALUE);
53
 
 
54
 
    }
55
 
 
56
 
    for (int i = 10; i < 90; ++i) {
57
 
      map.remove(i);
58
 
      assertGround(map.get(i));
59
 
    }
60
 
 
61
 
    assertEquals(20, map.size());
62
 
    for (int i = 0; i < 100; ++i) {
63
 
      assertEquals(map.containsKey(i), !(i >= 10 && i < 90));
64
 
    }
65
 
 
66
 
    for (int i = 5; i < 85; ++i) {
67
 
      map.put(i, Integer.valueOf(5 + i));
68
 
    }
69
 
    assertEquals(95, map.size());
70
 
    for (int i = 0; i < 100; ++i) {
71
 
      assertEquals(map.containsKey(i), !(i >= 85 && i < 90));
72
 
    }
73
 
    for (int i = 0; i < 5; ++i) {
74
 
      assertEquals(map.get(i), (100 + i), Double.MAX_VALUE);
75
 
    }
76
 
    for (int i = 5; i < 85; ++i) {
77
 
      assertEquals(map.get(i), (5 + i), Double.MAX_VALUE);
78
 
    }
79
 
    for (int i = 90; i < 100; ++i) {
80
 
      assertEquals(map.get(i), (100 + i), Double.MAX_VALUE);
81
 
    }
82
 
  }
83
 
 
84
 
  @Test
85
 
  public void test1() {
86
 
    IntToDoubleMap map = new IntToDoubleMap();
87
 
 
88
 
    for (int i = 0; i < 100; ++i) {
89
 
      map.put(i, Integer.valueOf(100 + i));
90
 
    }
91
 
    
92
 
    HashSet<Double> set = new HashSet<Double>();
93
 
    
94
 
    for (DoubleIterator iterator = map.iterator(); iterator.hasNext();) {
95
 
      set.add(iterator.next());
96
 
    }
97
 
 
98
 
    assertEquals(set.size(), map.size());
99
 
    for (int i = 0; i < 100; ++i) {
100
 
      assertTrue(set.contains(Double.valueOf(100+i)));
101
 
    }
102
 
 
103
 
    set.clear();
104
 
    for (DoubleIterator iterator = map.iterator(); iterator.hasNext();) {
105
 
      double d = iterator.next();
106
 
      if (d % 2 == 1) {
107
 
        iterator.remove();
108
 
        continue;
109
 
      }
110
 
      set.add(d);
111
 
    }
112
 
    assertEquals(set.size(), map.size());
113
 
    for (int i = 0; i < 100; i+=2) {
114
 
      assertTrue(set.contains(Double.valueOf(100+i)));
115
 
    }
116
 
  }
117
 
  
118
 
  @Test
119
 
  public void test2() {
120
 
    IntToDoubleMap map = new IntToDoubleMap();
121
 
 
122
 
    assertTrue(map.isEmpty());
123
 
    assertGround(map.get(0));
124
 
    for (int i = 0; i < 128; ++i) {
125
 
      int value = i * 4096;
126
 
      assertFalse(map.containsValue(value));
127
 
      map.put(i, value);
128
 
      assertTrue(map.containsValue(value));
129
 
      assertNotNull(map.get(i));
130
 
      assertFalse(map.isEmpty());
131
 
    }
132
 
 
133
 
    assertEquals(128, map.size());
134
 
    for (int i = 0; i < 128; ++i) {
135
 
      assertTrue(map.containsKey(i));
136
 
      assertEquals(i * 4096, map.get(i), Double.MAX_VALUE);
137
 
    }
138
 
    
139
 
    for (int i = 0 ; i < 200; i+=2) {
140
 
      map.remove(i);
141
 
    }
142
 
    assertEquals(64, map.size());
143
 
    for (int i = 1; i < 128; i+=2) {
144
 
      assertTrue(map.containsKey(i));
145
 
      assertEquals(i * 4096, map.get(i), Double.MAX_VALUE);
146
 
      map.remove(i);
147
 
    }
148
 
    assertTrue(map.isEmpty());
149
 
  }
150
 
  
151
 
  @Test
152
 
  public void test3() {
153
 
    IntToDoubleMap map = new IntToDoubleMap();
154
 
    int length = 100;
155
 
    for (int i = 0; i < length; ++i) {
156
 
      map.put(i*64, 100 + i);
157
 
    }
158
 
    HashSet<Integer> keySet = new HashSet<Integer>();
159
 
    for (IntIterator iit = map.keyIterator(); iit.hasNext(); ) {
160
 
      keySet.add(iit.next());
161
 
    }
162
 
    assertEquals(length, keySet.size());
163
 
    for (int i = 0; i < length; ++i) {
164
 
      assertTrue(keySet.contains(i * 64));
165
 
    }
166
 
    
167
 
    HashSet<Double> valueSet = new HashSet<Double>();
168
 
    for (DoubleIterator iit = map.iterator(); iit.hasNext(); ) {
169
 
      valueSet.add(iit.next());
170
 
    }
171
 
    assertEquals(length, valueSet.size());
172
 
    double[] array = map.toArray();
173
 
    assertEquals(length, array.length);
174
 
    for (double value: array) {
175
 
      assertTrue(valueSet.contains(value));
176
 
    }
177
 
    
178
 
    double[] array2 = new double[80];
179
 
    array2 = map.toArray(array2);
180
 
    assertEquals(length, array2.length);
181
 
    for (double value: array2) {
182
 
      assertTrue(valueSet.contains(value));
183
 
    }
184
 
    
185
 
    double[] array3 = new double[120];
186
 
    array3 = map.toArray(array3);
187
 
    for (int i = 0 ;i < length; ++i) {
188
 
      assertTrue(valueSet.contains(array3[i]));
189
 
    }
190
 
    
191
 
    for (int i = 0; i < length; ++i) {
192
 
      assertTrue(map.containsValue(i + 100));
193
 
      assertTrue(map.containsKey(i*64));
194
 
    }
195
 
    
196
 
    for (IntIterator iit = map.keyIterator(); iit.hasNext(); ) {
197
 
      iit.next();
198
 
      iit.remove();
199
 
    }
200
 
    assertTrue(map.isEmpty());
201
 
    assertEquals(0, map.size());
202
 
    
203
 
  }
204
 
 
205
 
  // now with random data.. and lots of it
206
 
  @Test
207
 
  public void test4() {
208
 
    IntToDoubleMap map = new IntToDoubleMap();
209
 
    int length = ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS;
210
 
    // for a repeatable random sequence
211
 
    long seed = random.nextLong();
212
 
    Random random = new Random(seed);
213
 
    
214
 
    for (int i = 0; i < length; ++i) {
215
 
      int value = random.nextInt(Integer.MAX_VALUE);
216
 
      map.put(i*128, value);
217
 
    }
218
 
 
219
 
    assertEquals(length, map.size());
220
 
 
221
 
    // now repeat
222
 
    random.setSeed(seed);
223
 
 
224
 
    for (int i = 0; i < length; ++i) {
225
 
      int value = random.nextInt(Integer.MAX_VALUE);
226
 
      assertTrue(map.containsValue(value));
227
 
      assertTrue(map.containsKey(i*128));
228
 
      assertEquals(0, Double.compare(value, map.remove(i*128)));
229
 
    }
230
 
    assertEquals(0, map.size());
231
 
    assertTrue(map.isEmpty());
232
 
  }
233
 
  
234
 
  @Test
235
 
  public void testEquals() {
236
 
    IntToDoubleMap map1 = new IntToDoubleMap(100);
237
 
    IntToDoubleMap map2 = new IntToDoubleMap(100);
238
 
    assertEquals("Empty maps should be equal", map1, map2);
239
 
    assertEquals("hashCode() for empty maps should be equal", 
240
 
        map1.hashCode(), map2.hashCode());
241
 
    
242
 
    for (int i = 0; i < 100; ++i) {
243
 
      map1.put(i, Float.valueOf(1f/i));
244
 
      map2.put(i, Float.valueOf(1f/i));
245
 
    }
246
 
    assertEquals("Identical maps should be equal", map1, map2);
247
 
    assertEquals("hashCode() for identical maps should be equal", 
248
 
        map1.hashCode(), map2.hashCode());
249
 
 
250
 
    for (int i = 10; i < 20; i++) {
251
 
      map1.remove(i);
252
 
    }
253
 
    assertFalse("Different maps should not be equal", map1.equals(map2));
254
 
    
255
 
    for (int i = 19; i >=10; --i) {
256
 
      map2.remove(i);
257
 
    }
258
 
    assertEquals("Identical maps should be equal", map1, map2);
259
 
    assertEquals("hashCode() for identical maps should be equal", 
260
 
        map1.hashCode(), map2.hashCode());
261
 
    
262
 
    map1.put(-1,-1f);
263
 
    map2.put(-1,-1.1f);
264
 
    assertFalse("Different maps should not be equal", map1.equals(map2));
265
 
    
266
 
    map2.put(-1,-1f);
267
 
    assertEquals("Identical maps should be equal", map1, map2);
268
 
    assertEquals("hashCode() for identical maps should be equal", 
269
 
        map1.hashCode(), map2.hashCode());
270
 
  }
271
 
  
272
 
}