1
1
mirror of https://github.com/walles/moar.git synced 2024-11-09 17:40:09 +03:00

Fix another off-by-one error

This commit is contained in:
Johan Walles 2019-06-14 06:59:19 +02:00
parent 0abaf16da5
commit e4b83d91b5
2 changed files with 23 additions and 1 deletions

View File

@ -76,7 +76,7 @@ func (r *_Reader) GetLines(firstLineOneBased int, wantedLineCount int) *Lines {
firstLineZeroBased := firstLineOneBased - 1
lastLineZeroBased := firstLineZeroBased + wantedLineCount - 1
if lastLineZeroBased > len(r.lines) {
if lastLineZeroBased >= len(r.lines) {
lastLineZeroBased = len(r.lines) - 1
}

View File

@ -31,6 +31,28 @@ func _TestGetLines(t *testing.T, reader *_Reader) {
t.Errorf("Expected first line to be %d, was %d", lineCount - 9, lines.firstLineOneBased)
return
}
startOfLastSection := lines.firstLineOneBased
lines = reader.GetLines(startOfLastSection, 10)
if lines.firstLineOneBased != startOfLastSection {
t.Errorf("Expected start line %d when asking for the last 10 lines, got %d",
startOfLastSection, lines.firstLineOneBased)
return
}
lines = reader.GetLines(startOfLastSection + 1, 10)
if lines.firstLineOneBased != startOfLastSection {
t.Errorf("Expected start line %d when asking for the last+1 10 lines, got %d",
startOfLastSection, lines.firstLineOneBased)
return
}
lines = reader.GetLines(startOfLastSection - 1, 10)
if lines.firstLineOneBased != startOfLastSection - 1 {
t.Errorf("Expected start line %d when asking for the last-1 10 lines, got %d",
startOfLastSection, lines.firstLineOneBased)
return
}
}
func _GetTestFiles() []string {