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

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

Loading…
Cancel
Save