ItEr58S04ValidacionEProbasFuncionaisItEr57S04: [Bug #470] Fix bug.

If shares are zero, the remainder can be bigger than the number of shares
This commit is contained in:
Óscar González Fernández 2010-05-25 23:05:47 +02:00
parent 1461d5d01d
commit 9bcc65b0ef
2 changed files with 11 additions and 1 deletions

View file

@ -117,7 +117,8 @@ public class ProportionalDistributor {
.transform(difference(currentProportions));
Collections.sort(transform, Collections.reverseOrder());
for (int i = 0; i < remaining; i++) {
ProportionWithPosition proportionWithPosition = transform.get(i);
ProportionWithPosition proportionWithPosition = transform.get(i
% currentProportions.length);
result[proportionWithPosition.position] = result[proportionWithPosition.position] + 1;
}
}

View file

@ -63,6 +63,15 @@ public class ProportionalDistributorTest {
assertThat(distributor.distribute(100), equalToDistribution(50, 50, 0));
}
@Test
public void ifNoSharesProvidedItDistributesEqually() {
ProportionalDistributor distributor = ProportionalDistributor.create(0,
0, 0, 0);
assertThat(distributor.distribute(4), equalToDistribution(1, 1, 1, 1));
assertThat(distributor.distribute(5), equalToDistribution(2, 1, 1, 1));
assertThat(distributor.distribute(6), equalToDistribution(2, 2, 1, 1));
}
@Test
public void disputedPartGoesToFirstIfEqualWeight() {
ProportionalDistributor distributor = ProportionalDistributor.create(