Made the edits requested by Hiqua

pull/1713/head
Maxet1000 2 years ago
parent 393880a047
commit 6ba27f26d8

@ -65,25 +65,25 @@ class ScoreList {
* Returns the number of skips after the offset in the interval used to calculate
* the percentage of completed days.
*
* If skips are found in the interval, it is expanded until the interval has the size of the
* sum of the denominator and the number of skips within the interval.
* If skips are found in the interval, it expands the interval by the number of skips found
* and repeats this process for the expanded part until no skips are found in an expanded part.
*/
@Synchronized
fun getNumberOfSkipsByInterval(
values: IntArray,
firstIndexToCheck: Int,
lastIndexToCheck: Int
firstIndexCurrentInterval: Int,
lastIndexCurrentInterval: Int
): Int {
if (lastIndexToCheck < firstIndexToCheck) return 0
if (lastIndexCurrentInterval < firstIndexCurrentInterval) return 0
var nbOfSkips = 0
var nextLastIndex = lastIndexToCheck
for (i in firstIndexToCheck..lastIndexToCheck) {
var nextLastIndex = lastIndexCurrentInterval
for (i in firstIndexCurrentInterval..lastIndexCurrentInterval) {
if (values[i] == Entry.SKIP) {
nbOfSkips++
if (lastIndexToCheck + nbOfSkips < values.size) nextLastIndex++
if (lastIndexCurrentInterval + nbOfSkips < values.size) nextLastIndex++
}
}
return nbOfSkips + getNumberOfSkipsByInterval(values, lastIndexToCheck + 1, nextLastIndex)
return nbOfSkips + getNumberOfSkipsByInterval(values, lastIndexCurrentInterval + 1, nextLastIndex)
}
/**
@ -152,8 +152,9 @@ class ScoreList {
if (offset + denominator < values.size) {
val nbOfSkips =
getNumberOfSkipsByInterval(values, offset, offset + denominator)
if (offset + denominator + nbOfSkips < values.size) {
if (values[offset + denominator + nbOfSkips] == Entry.YES_MANUAL) {
val lastIndexForRollingSum = offset + denominator + nbOfSkips
if (lastIndexForRollingSum < values.size) {
if (values[lastIndexForRollingSum] == Entry.YES_MANUAL) {
if (values[offset] != Entry.SKIP) rollingSum -= 1.0
}
}

@ -24,6 +24,8 @@ import org.hamcrest.number.IsCloseTo
import org.hamcrest.number.OrderingComparison
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.models.Entry.Companion.SKIP
import org.isoron.uhabits.core.models.Entry.Companion.UNKNOWN
import org.isoron.uhabits.core.models.Entry.Companion.YES_MANUAL
import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday
import org.junit.Before
import org.junit.Test
@ -149,21 +151,24 @@ class YesNoScoreListTest : BaseScoreListTest() {
@Test
fun test_getNumberOfSkipsByInterval_NoSkips() {
val vars = intArrayOf(-1, -1, -1, -1, 3, 2, 2, -1, 2, 2, -1, 2, 2, -1, 3, 2)
val vars = intArrayOf(UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, SKIP, YES_MANUAL, YES_MANUAL,
UNKNOWN, YES_MANUAL, YES_MANUAL, UNKNOWN, YES_MANUAL, YES_MANUAL, UNKNOWN, SKIP, YES_MANUAL)
val nbOfSkips = habit.scores.getNumberOfSkipsByInterval(vars, 5, 13)
assertEquals(0, nbOfSkips)
}
@Test
fun test_getNumberOfSkipsByInterval_SkipsOnlyInInitialInterval() {
val vars = intArrayOf(-1, -1, -1, -1, 3, 2, 2, -1, 3, 3, -1, 2, 2, -1, 3, 2)
val vars = intArrayOf(UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, SKIP, YES_MANUAL, YES_MANUAL,
UNKNOWN, SKIP, SKIP, UNKNOWN, YES_MANUAL, YES_MANUAL, UNKNOWN, SKIP, YES_MANUAL)
val nbOfSkips = habit.scores.getNumberOfSkipsByInterval(vars, 4, 9)
assertEquals(3, nbOfSkips)
}
@Test
fun test_getNumberOfSkipsByInterval_SkipsInSubsequentIntervals() {
val vars = intArrayOf(-1, -1, -1, -1, 3, 2, 2, -1, 3, 3, -1, 2, 2, -1, 3, 2)
val vars = intArrayOf(UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, SKIP, YES_MANUAL, YES_MANUAL,
UNKNOWN, SKIP, SKIP, UNKNOWN, YES_MANUAL, YES_MANUAL, UNKNOWN, SKIP, YES_MANUAL)
val nbOfSkips = habit.scores.getNumberOfSkipsByInterval(vars, 4, 11)
assertEquals(4, nbOfSkips)
}

Loading…
Cancel
Save