From 1e6351cf759d0b0176c7256d62fb62b2c3e53535 Mon Sep 17 00:00:00 2001 From: Jonathan Trowbridge Date: Tue, 31 Jul 2018 19:43:07 -0400 Subject: [PATCH] master - fixed off-by-one in buffer state tail index. --- .../models/TorrentSessionBufferState.kt | 2 +- .../TorrentSessionBufferStateTest.kt | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/simpletorrentandroid/src/main/java/com/masterwok/simpletorrentandroid/models/TorrentSessionBufferState.kt b/simpletorrentandroid/src/main/java/com/masterwok/simpletorrentandroid/models/TorrentSessionBufferState.kt index 12e8adb..d156815 100644 --- a/simpletorrentandroid/src/main/java/com/masterwok/simpletorrentandroid/models/TorrentSessionBufferState.kt +++ b/simpletorrentandroid/src/main/java/com/masterwok/simpletorrentandroid/models/TorrentSessionBufferState.kt @@ -24,7 +24,7 @@ class TorrentSessionBufferState constructor( field = value } - var bufferTailIndex = startIndex + bufferSize + var bufferTailIndex = startIndex + bufferSize - 1 @Synchronized get() = field private set(value) { diff --git a/simpletorrentandroid/src/test/java/com/masterwok/simpletorrentandroid/TorrentSessionBufferStateTest.kt b/simpletorrentandroid/src/test/java/com/masterwok/simpletorrentandroid/TorrentSessionBufferStateTest.kt index 8663ef6..8f8e99f 100644 --- a/simpletorrentandroid/src/test/java/com/masterwok/simpletorrentandroid/TorrentSessionBufferStateTest.kt +++ b/simpletorrentandroid/src/test/java/com/masterwok/simpletorrentandroid/TorrentSessionBufferStateTest.kt @@ -22,7 +22,7 @@ class TorrentSessionBufferStateTest { bufferHeadIndex: Int , endIndex: Int , bufferSize: Int - ): Int = Math.min(bufferHeadIndex + bufferSize, endIndex) + ): Int = Math.min(bufferHeadIndex + bufferSize - 1, endIndex) private fun IntRange.random(): Int { return Random().nextInt((endInclusive + 1) - start) + start @@ -185,8 +185,14 @@ class TorrentSessionBufferStateTest { underTest.setPieceDownloaded(0) + val expectedTail = getExpectedTailIndex( + underTest.bufferHeadIndex + , underTest.endIndex + , bufferSize + ) + assertEquals(underTest.bufferHeadIndex, 30) - assertEquals(underTest.bufferTailIndex, Math.min(underTest.bufferHeadIndex + bufferSize, endIndex)) + assertEquals(underTest.bufferTailIndex, Math.min(expectedTail, endIndex)) } @Test