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:
parent
0abaf16da5
commit
e4b83d91b5
@ -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
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user