Commit c8f15d72 authored by Matija Obreza's avatar Matija Obreza

Fix: PDCIStatistics use Number instead of Float/Double

- unit test added
parent ac8085cc
......@@ -94,37 +94,30 @@ public class PDCIStatistics implements Serializable {
public void makeHistogram(List<Object[]> hist) {
for (Object[] h : hist) {
// Determining JPA data types
// System.err.println("Hist " + h[0].getClass() + " " +
// h[1].getClass());
int index = (int) Math.round(((Number) h[0]).doubleValue() * 2);
long count = ((Number) h[1]).longValue();
int index = (int) (((Float) h[0]) * 2);
long count = (Long) h[1];
// System.err.println("Index for " + h[0] + " = " + index +
// " count=" + count);
histogram[index] = count;
histogram[index] += count;
}
}
public void updateMin(Float updateMin) {
public void updateMin(Number updateMin) {
if (updateMin == null)
return;
this.min = this.min == null || this.min.floatValue() > updateMin ? updateMin : this.min;
this.min = this.min == null || this.min > updateMin.doubleValue() ? updateMin.doubleValue() : this.min;
}
public void updateMax(Float updateMax) {
public void updateMax(Number updateMax) {
if (updateMax == null)
return;
this.max = this.max == null || this.max.floatValue() < updateMax ? updateMax : this.max;
this.max = this.max == null || this.max < updateMax.doubleValue() ? updateMax.doubleValue() : this.max;
}
public void updateCountAndAvg(Long count, Number avg) {
if (count == null || count == 0 || avg == null)
public void updateCountAndAvg(Number count, Number avg) {
if (count == null || count.longValue() == 0 || avg == null)
return;
this.avg = this.avg == null || this.count == null || this.count == 0 ? avg.doubleValue() : (this.avg * this.count + avg.doubleValue() * count) / (this.count + count);
this.count = this.count==null ? count : this.count + count;
this.avg = this.avg == null || this.count == null || this.count == 0 ? avg.doubleValue() : (this.avg * this.count + avg.doubleValue() * count.doubleValue()) / (this.count + count.doubleValue());
this.count = this.count==null ? count.longValue() : this.count + count.longValue();
}
/**
......
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed 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.genesys.test.simpletest;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.genesys2.server.model.genesys.PDCIStatistics;
import org.junit.Test;
import com.google.common.collect.Lists;
public class PDCIStatisticsTest {
@Test
public void testSetAvg() {
PDCIStatistics stats = new PDCIStatistics();
stats.setAvg(1f);
assertThat(stats.getAvg(), is(1d));
stats.setAvg(1);
assertThat(stats.getAvg(), is(1d));
stats.setAvg(1d);
assertThat(stats.getAvg(), is(1d));
}
@Test
public void testSetMin() {
PDCIStatistics stats = new PDCIStatistics();
stats.setMin(1f);
assertThat(stats.getMin(), is(1d));
stats.setMin(1);
assertThat(stats.getMin(), is(1d));
stats.setMin(1d);
assertThat(stats.getMin(), is(1d));
}
@Test
public void testSetMax() {
PDCIStatistics stats = new PDCIStatistics();
stats.setMax(1f);
assertThat(stats.getMax(), is(1d));
stats.setMax(1);
assertThat(stats.getMax(), is(1d));
stats.setMax(1d);
assertThat(stats.getMax(), is(1d));
}
@Test
public void testUpdateMax() {
PDCIStatistics stats = new PDCIStatistics();
stats.updateMax(null);
assertThat(stats.getMax(), nullValue());
stats.updateMax(1f);
assertThat(stats.getMax(), is(1d));
stats.updateMax(null);
assertThat(stats.getMax(), is(1d));
stats.updateMax(1);
assertThat(stats.getMax(), is(1d));
stats.updateMax(1d);
assertThat(stats.getMax(), is(1d));
}
@Test
public void testUpdateMin() {
PDCIStatistics stats = new PDCIStatistics();
stats.updateMin(null);
assertThat(stats.getMin(), nullValue());
stats.updateMin(1f);
assertThat(stats.getMin(), is(1d));
stats.updateMin(null);
assertThat(stats.getMin(), is(1d));
stats.updateMin(1);
assertThat(stats.getMin(), is(1d));
stats.updateMin(1d);
assertThat(stats.getMin(), is(1d));
}
@Test
public void testUpdateCountAndAvg() {
PDCIStatistics stats = new PDCIStatistics();
stats.updateCountAndAvg(1f, 2f);
assertThat(stats.getCount(), is(1l));
assertThat(stats.getAvg(), is(2d));
stats.updateCountAndAvg(1f, 2f);
assertThat(stats.getCount(), is(2l));
assertThat(stats.getAvg(), is(2d));
stats.updateCountAndAvg(2, 4);
assertThat(stats.getCount(), is(4l));
assertThat(stats.getAvg(), is(3d));
}
/**
* Test histogram: it's from 0 to 10, but uses .5 splits between integers which makes it 21 elements long.
*
* <pre>
* hist[0] = number of values with score 0 - .5
* hist[1] = ... .5 - 1
* hist[2] = ... 1 - 1.5
* hist[3] = ... 1.5 - 2
* ...
* hist[21] = 9.5 - 10
* </pre>
*
*/
@Test
public void testHistogram() {
PDCIStatistics stats = new PDCIStatistics();
/*@formatter:off*/
List<Object[]> hist = Lists.newArrayList(
new Object[] { .2, 1d }, new Object[] { .5, 5d },
new Object[] { 1, 10 }, new Object[] { 2, 20 },
new Object[] { 3f, 30 }, new Object[] { 4d, 40d },
new Object[] { 5d, 50 }, new Object[] { 6f, 60f },
new Object[] { 7, 70f }, new Object[] { 8d, 80f },
new Object[] { 9f, 40d }, new Object[] { 10, 50 },
// extras
new Object[] { 9.1f, 50d }, new Object[] { 9.9, 50 }
);
/*@formatter:on*/
stats.makeHistogram(hist);
assertThat(stats.getHistogram().length, is(21));
assertThat(stats.getHistogram()[9*2], is(90l));
assertThat(stats.getHistogram()[(int) (2*9.5)], is(0l));
assertThat(stats.getHistogram()[10*2], is(100l));
assertThat(Arrays.toString(stats.getHistogram()), is("[1, 5, 10, 0, 20, 0, 30, 0, 40, 0, 50, 0, 60, 0, 70, 0, 80, 0, 90, 0, 100]"));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment