iobplugin tests
This commit is contained in:
parent
d198de94c0
commit
4133bc9ad1
|
@ -110,35 +110,7 @@ public class IobCobCalculatorPlugin extends PluginBase {
|
|||
return bucketed_data;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public List<BgReading> getBucketedData(long fromTime) {
|
||||
//log.debug("Locking getBucketedData");
|
||||
synchronized (dataLock) {
|
||||
if (bucketed_data == null) {
|
||||
if (L.isEnabled(L.AUTOSENS))
|
||||
log.debug("No bucketed data available");
|
||||
return null;
|
||||
}
|
||||
int index = indexNewerThan(fromTime);
|
||||
if (index > -1) {
|
||||
List<BgReading> part = bucketed_data.subList(0, index);
|
||||
if (L.isEnabled(L.AUTOSENS))
|
||||
log.debug("Bucketed data striped off: " + part.size() + "/" + bucketed_data.size());
|
||||
return part;
|
||||
}
|
||||
}
|
||||
//log.debug("Releasing getBucketedData");
|
||||
return null;
|
||||
}
|
||||
|
||||
private int indexNewerThan(long time) {
|
||||
for (int index = 0; index < bucketed_data.size(); index++) {
|
||||
if (bucketed_data.get(index).date < time)
|
||||
return index - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// roundup to whole minute
|
||||
public static long roundUpTime(long time) {
|
||||
if (time % 60000 == 0)
|
||||
return time;
|
||||
|
@ -187,24 +159,26 @@ public class IobCobCalculatorPlugin extends PluginBase {
|
|||
}
|
||||
|
||||
@Nullable
|
||||
private BgReading findNewer(long time) {
|
||||
public BgReading findNewer(long time) {
|
||||
BgReading lastFound = bgReadings.get(0);
|
||||
if (lastFound.date < time) return null;
|
||||
for (int i = 1; i < bgReadings.size(); ++i) {
|
||||
if (bgReadings.get(i).date == time) return bgReadings.get(i);
|
||||
if (bgReadings.get(i).date > time) continue;
|
||||
lastFound = bgReadings.get(i);
|
||||
lastFound = bgReadings.get(i-1);
|
||||
if (bgReadings.get(i).date < time) break;
|
||||
}
|
||||
return lastFound;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private BgReading findOlder(long time) {
|
||||
public BgReading findOlder(long time) {
|
||||
BgReading lastFound = bgReadings.get(bgReadings.size() - 1);
|
||||
if (lastFound.date > time) return null;
|
||||
for (int i = bgReadings.size() - 2; i >= 0; --i) {
|
||||
if (bgReadings.get(i).date == time) return bgReadings.get(i);
|
||||
if (bgReadings.get(i).date < time) continue;
|
||||
lastFound = bgReadings.get(i);
|
||||
lastFound = bgReadings.get(i + 1);
|
||||
if (bgReadings.get(i).date > time) break;
|
||||
}
|
||||
return lastFound;
|
||||
|
@ -217,7 +191,7 @@ public class IobCobCalculatorPlugin extends PluginBase {
|
|||
}
|
||||
|
||||
bucketed_data = new ArrayList<>();
|
||||
long currentTime = bgReadings.get(0).date + 5 * 60 * 1000 - bgReadings.get(0).date % (5 * 60 * 1000) - 5 * 60 * 1000L;
|
||||
long currentTime = bgReadings.get(0).date - bgReadings.get(0).date % T.mins(5).msecs();
|
||||
//log.debug("First reading: " + new Date(currentTime).toLocaleString());
|
||||
|
||||
while (true) {
|
||||
|
@ -227,16 +201,20 @@ public class IobCobCalculatorPlugin extends PluginBase {
|
|||
if (newer == null || older == null)
|
||||
break;
|
||||
|
||||
double bgDelta = newer.value - older.value;
|
||||
long timeDiffToNew = newer.date - currentTime;
|
||||
if (older.date == newer.date) { // direct hit
|
||||
bucketed_data.add(newer);
|
||||
} else {
|
||||
double bgDelta = newer.value - older.value;
|
||||
long timeDiffToNew = newer.date - currentTime;
|
||||
|
||||
double currentBg = newer.value - (double) timeDiffToNew / (newer.date - older.date) * bgDelta;
|
||||
BgReading newBgreading = new BgReading();
|
||||
newBgreading.date = currentTime;
|
||||
newBgreading.value = Math.round(currentBg);
|
||||
bucketed_data.add(newBgreading);
|
||||
//log.debug("BG: " + newBgreading.value + " (" + new Date(newBgreading.date).toLocaleString() + ") Prev: " + older.value + " (" + new Date(older.date).toLocaleString() + ") Newer: " + newer.value + " (" + new Date(newer.date).toLocaleString() + ")");
|
||||
currentTime -= 5 * 60 * 1000L;
|
||||
double currentBg = newer.value - (double) timeDiffToNew / (newer.date - older.date) * bgDelta;
|
||||
BgReading newBgreading = new BgReading();
|
||||
newBgreading.date = currentTime;
|
||||
newBgreading.value = Math.round(currentBg);
|
||||
bucketed_data.add(newBgreading);
|
||||
//log.debug("BG: " + newBgreading.value + " (" + new Date(newBgreading.date).toLocaleString() + ") Prev: " + older.value + " (" + new Date(older.date).toLocaleString() + ") Newer: " + newer.value + " (" + new Date(newer.date).toLocaleString() + ")");
|
||||
}
|
||||
currentTime -= T.mins(5).msecs();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,14 +15,14 @@ import java.util.List;
|
|||
import info.AAPSMocker;
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.db.BgReading;
|
||||
import info.nightscout.androidaps.db.DatabaseHelper;
|
||||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
||||
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
|
||||
import info.nightscout.utils.SP;
|
||||
import info.nightscout.utils.T;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, SP.class, Context.class})
|
||||
@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, SP.class, Context.class, L.class})
|
||||
public class IobCobCalculatorPluginTest {
|
||||
|
||||
IobCobCalculatorPlugin iobCobCalculatorPlugin = IobCobCalculatorPlugin.getPlugin();
|
||||
|
@ -105,10 +105,140 @@ public class IobCobCalculatorPluginTest {
|
|||
Assert.assertEquals(bgReadingList.get(3).date, iobCobCalculatorPlugin.getBucketedData().get(3).date);
|
||||
Assert.assertEquals(bgReadingList.size(), iobCobCalculatorPlugin.getBucketedData().size());
|
||||
|
||||
// Missing value should be replaced
|
||||
bgReadingList.clear();
|
||||
bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(10).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(5).msecs()).value(100));
|
||||
|
||||
iobCobCalculatorPlugin.setBgReadings(bgReadingList);
|
||||
iobCobCalculatorPlugin.createBucketedData();
|
||||
|
||||
Assert.assertEquals(true, iobCobCalculatorPlugin.isAbout5minData());
|
||||
Assert.assertEquals(bgReadingList.get(0).date, iobCobCalculatorPlugin.getBucketedData().get(0).date);
|
||||
Assert.assertEquals(bgReadingList.get(2).date, iobCobCalculatorPlugin.getBucketedData().get(3).date);
|
||||
Assert.assertEquals(bgReadingList.size() + 1, iobCobCalculatorPlugin.getBucketedData().size());
|
||||
|
||||
// drift should be cleared
|
||||
bgReadingList.clear();
|
||||
bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(15).msecs() + T.secs(10).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(10).msecs() + T.secs(10).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(5).msecs() - T.secs(10).msecs()).value(100));
|
||||
|
||||
iobCobCalculatorPlugin.setBgReadings(bgReadingList);
|
||||
iobCobCalculatorPlugin.createBucketedData();
|
||||
|
||||
Assert.assertEquals(false, iobCobCalculatorPlugin.isAbout5minData()); // too much drift
|
||||
Assert.assertEquals(T.mins(20).msecs(), iobCobCalculatorPlugin.getBucketedData().get(0).date);
|
||||
Assert.assertEquals(T.mins(15).msecs(), iobCobCalculatorPlugin.getBucketedData().get(1).date);
|
||||
Assert.assertEquals(T.mins(10).msecs(), iobCobCalculatorPlugin.getBucketedData().get(2).date);
|
||||
Assert.assertEquals(T.mins(5).msecs(), iobCobCalculatorPlugin.getBucketedData().get(3).date);
|
||||
Assert.assertEquals(bgReadingList.size(), iobCobCalculatorPlugin.getBucketedData().size());
|
||||
|
||||
// bucketed data should return null if not enough bg data
|
||||
bgReadingList.clear();
|
||||
bgReadingList.add(new BgReading().date(T.mins(30).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(5).msecs()).value(100));
|
||||
|
||||
iobCobCalculatorPlugin.setBgReadings(bgReadingList);
|
||||
iobCobCalculatorPlugin.createBucketedData();
|
||||
|
||||
Assert.assertEquals(true, iobCobCalculatorPlugin.isAbout5minData());
|
||||
Assert.assertEquals(null, iobCobCalculatorPlugin.getBucketedData());
|
||||
|
||||
// data should be reconstructed
|
||||
bgReadingList.clear();
|
||||
bgReadingList.add(new BgReading().date(T.mins(50).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(45).msecs()).value(90));
|
||||
bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(40));
|
||||
|
||||
iobCobCalculatorPlugin.setBgReadings(bgReadingList);
|
||||
iobCobCalculatorPlugin.createBucketedData();
|
||||
|
||||
Assert.assertEquals(true, iobCobCalculatorPlugin.isAbout5minData());
|
||||
Assert.assertEquals(T.mins(50).msecs(), iobCobCalculatorPlugin.getBucketedData().get(0).date);
|
||||
Assert.assertEquals(T.mins(20).msecs(), iobCobCalculatorPlugin.getBucketedData().get(6).date);
|
||||
Assert.assertEquals(7, iobCobCalculatorPlugin.getBucketedData().size());
|
||||
|
||||
Assert.assertEquals(100, iobCobCalculatorPlugin.getBucketedData().get(0).value, 1);
|
||||
Assert.assertEquals(90, iobCobCalculatorPlugin.getBucketedData().get(1).value, 1);
|
||||
Assert.assertEquals(50, iobCobCalculatorPlugin.getBucketedData().get(5).value, 1);
|
||||
Assert.assertEquals(40, iobCobCalculatorPlugin.getBucketedData().get(6).value, 1);
|
||||
|
||||
// non 5min data should be reconstructed
|
||||
bgReadingList.clear();
|
||||
bgReadingList.add(new BgReading().date(T.mins(50).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(48).msecs()).value(96));
|
||||
bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(40));
|
||||
|
||||
iobCobCalculatorPlugin.setBgReadings(bgReadingList);
|
||||
iobCobCalculatorPlugin.createBucketedData();
|
||||
|
||||
Assert.assertEquals(false, iobCobCalculatorPlugin.isAbout5minData());
|
||||
Assert.assertEquals(T.mins(50).msecs(), iobCobCalculatorPlugin.getBucketedData().get(0).date);
|
||||
Assert.assertEquals(T.mins(20).msecs(), iobCobCalculatorPlugin.getBucketedData().get(6).date);
|
||||
Assert.assertEquals(7, iobCobCalculatorPlugin.getBucketedData().size());
|
||||
|
||||
Assert.assertEquals(100, iobCobCalculatorPlugin.getBucketedData().get(0).value, 1);
|
||||
Assert.assertEquals(90, iobCobCalculatorPlugin.getBucketedData().get(1).value, 1);
|
||||
Assert.assertEquals(50, iobCobCalculatorPlugin.getBucketedData().get(5).value, 1);
|
||||
Assert.assertEquals(40, iobCobCalculatorPlugin.getBucketedData().get(6).value, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBgReadingsTest() {
|
||||
List<BgReading> bgReadingList = new ArrayList<>();
|
||||
|
||||
iobCobCalculatorPlugin.setBgReadings(bgReadingList);
|
||||
|
||||
Assert.assertEquals(bgReadingList, iobCobCalculatorPlugin.getBgReadings());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void roundUpTimeTest() {
|
||||
Assert.assertEquals(T.mins(3).msecs(), iobCobCalculatorPlugin.roundUpTime(T.secs(155).msecs()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findNewerTest() {
|
||||
List<BgReading> bgReadingList = new ArrayList<>();
|
||||
|
||||
bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(15).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(10).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(5).msecs()).value(100));
|
||||
|
||||
iobCobCalculatorPlugin.setBgReadings(bgReadingList);
|
||||
|
||||
Assert.assertEquals(T.mins(10).msecs(), iobCobCalculatorPlugin.findNewer(T.mins(8).msecs()).date);
|
||||
Assert.assertEquals(T.mins(5).msecs(), iobCobCalculatorPlugin.findNewer(T.mins(5).msecs()).date);
|
||||
Assert.assertEquals(T.mins(10).msecs(), iobCobCalculatorPlugin.findNewer(T.mins(10).msecs()).date);
|
||||
Assert.assertEquals(T.mins(20).msecs(), iobCobCalculatorPlugin.findNewer(T.mins(20).msecs()).date);
|
||||
Assert.assertEquals(null, iobCobCalculatorPlugin.findNewer(T.mins(22).msecs()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findOlderTest() {
|
||||
List<BgReading> bgReadingList = new ArrayList<>();
|
||||
|
||||
bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(15).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(10).msecs()).value(100));
|
||||
bgReadingList.add(new BgReading().date(T.mins(5).msecs()).value(100));
|
||||
|
||||
iobCobCalculatorPlugin.setBgReadings(bgReadingList);
|
||||
|
||||
Assert.assertEquals(T.mins(5).msecs(), iobCobCalculatorPlugin.findOlder(T.mins(8).msecs()).date);
|
||||
Assert.assertEquals(T.mins(5).msecs(), iobCobCalculatorPlugin.findOlder(T.mins(5).msecs()).date);
|
||||
Assert.assertEquals(T.mins(10).msecs(), iobCobCalculatorPlugin.findOlder(T.mins(10).msecs()).date);
|
||||
Assert.assertEquals(T.mins(20).msecs(), iobCobCalculatorPlugin.findOlder(T.mins(20).msecs()).date);
|
||||
Assert.assertEquals(null, iobCobCalculatorPlugin.findOlder(T.mins(4).msecs()));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void doMock() {
|
||||
AAPSMocker.mockMainApp();
|
||||
AAPSMocker.mockL();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue