fix ConcurrentScoreMap.set() calculation of totalCount()

+ test case
pull/72/head^2
reger 9 years ago
parent ebf818ad95
commit 39dd244693

@ -133,7 +133,10 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
if (obj == null) return;
// use atomic operations
this.map.putIfAbsent(obj, new AtomicLong(0));
final AtomicLong old = this.map.putIfAbsent(obj, new AtomicLong(0));
// adjust overall counter if value replaced
if (old != null) this.gcount -= old.longValue(); // must use old befor setting a new value (it's a object reference)
this.map.get(obj).set(newScore);
// increase overall counter

@ -0,0 +1,34 @@
package net.yacy.cora.sorting;
import java.util.Iterator;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class ConcurrentScoreMapTest {
/**
* Test of totalCount method, of class ConcurrentScoreMap.
*/
@Test
public void testTotalCount() {
final ConcurrentScoreMap<String> csm = new ConcurrentScoreMap<String>();
csm.set("first", 10);
csm.set("second", 5);
csm.set("third", 13);
csm.set("first", 100);
final Iterator<String> it = csm.keys(true);
long sum = 0;
while (it.hasNext()) {
String x = it.next();
long val = csm.get(x);
sum += val;
}
assertEquals(sum, csm.totalCount());
}
}
Loading…
Cancel
Save