()
| 163 | |
| 164 | |
| 165 | @Test |
| 166 | public void testRandom() |
| 167 | { |
| 168 | System.out.println("testRandp,"); |
| 169 | FibHeap<Long> heap = new FibHeap<Long>(); |
| 170 | |
| 171 | SortedMap<Long, Double> map = new TreeMap<Long, Double>(); |
| 172 | Map<Long, FibHeap.FibNode<Long>> heapNodes = new HashMap<Long, FibHeap.FibNode<Long>>(); |
| 173 | |
| 174 | Random rand = RandomUtil.getRandom(); |
| 175 | |
| 176 | for(int trials = 0; trials < 10; trials++) |
| 177 | for(int maxSize = 1; maxSize < 2000; maxSize*=2) |
| 178 | { |
| 179 | while(map.size() < maxSize) |
| 180 | { |
| 181 | |
| 182 | if(map.size() > 0 && rand.nextDouble() < 0.1) |
| 183 | {//ocasionally remove the min eliment |
| 184 | long entry = map.firstKey(); |
| 185 | double value = map.get(entry); |
| 186 | for( Map.Entry<Long, Double> mapEntry : map.entrySet()) |
| 187 | if(mapEntry.getValue() < value) |
| 188 | { |
| 189 | entry = mapEntry.getKey(); |
| 190 | value = mapEntry.getValue(); |
| 191 | } |
| 192 | map.remove(entry); |
| 193 | |
| 194 | FibHeap.FibNode<Long> min = heap.removeMin(); |
| 195 | heapNodes.remove(entry); |
| 196 | |
| 197 | assertEquals(entry, min.value.longValue()); |
| 198 | assertEquals(value, min.key, 0.0); |
| 199 | |
| 200 | } |
| 201 | else if(map.size() > 0 && rand.nextDouble() < 0.4) |
| 202 | {//lest decrease a random key's value |
| 203 | long partitioner = rand.nextLong(); |
| 204 | SortedMap<Long, Double> subMap = map.tailMap(partitioner); |
| 205 | |
| 206 | long valToDecrease; |
| 207 | if(subMap.isEmpty()) |
| 208 | { |
| 209 | subMap = map.headMap(partitioner); |
| 210 | valToDecrease = map.lastKey(); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | valToDecrease = map.firstKey(); |
| 215 | } |
| 216 | |
| 217 | double newVal = map.get(valToDecrease)/2; |
| 218 | map.put(valToDecrease, newVal); |
| 219 | heap.decreaseKey(heapNodes.get(valToDecrease), newVal); |
| 220 | |
| 221 | } |
| 222 | //then add something |
nothing calls this directly
no test coverage detected