mirror of
https://github.com/bloeys/nterm.git
synced 2025-12-29 06:28:20 +00:00
Fix bug in ViewsFromTo where from might clip first value
This commit is contained in:
@ -154,7 +154,7 @@ func (b *Buffer[T]) ViewsFromTo(fromIndex, toIndex uint64) (v1, v2 []T) {
|
||||
return
|
||||
}
|
||||
|
||||
fromIndex -= v1Len - 1
|
||||
fromIndex -= v1Len
|
||||
toIndex -= v1Len
|
||||
if toIndex >= v2Len {
|
||||
toIndex = v2Len
|
||||
@ -187,7 +187,9 @@ type Iterator[T any] struct {
|
||||
V1 []T
|
||||
V2 []T
|
||||
|
||||
// Curr is the index of the element that will be returned on Next()
|
||||
// Curr is the index of the element that will be returned on Next(),
|
||||
// which means it is an index into V1 or V2 and so is relative to Buffer.Start value at the time
|
||||
// of creating this iterator instance
|
||||
Curr int64
|
||||
InV1 bool
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package ring_test
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/bloeys/nterm/ring"
|
||||
@ -135,7 +136,8 @@ func TestRing(t *testing.T) {
|
||||
|
||||
v11, v22 = b2.ViewsFromTo(3, 40)
|
||||
Check(t, 0, len(v11))
|
||||
Check(t, 0, len(v22))
|
||||
Check(t, 1, len(v22))
|
||||
CheckArr(t, []int{6}, v22)
|
||||
|
||||
v11, v22 = b2.ViewsFromTo(1, 2)
|
||||
Check(t, 1, len(v11))
|
||||
@ -159,6 +161,11 @@ func TestRing(t *testing.T) {
|
||||
Check(t, 2, len(v22))
|
||||
CheckArr(t, []int{3, 4}, v11)
|
||||
CheckArr(t, []int{5, 6}, v22)
|
||||
|
||||
v11, v22 = b2.ViewsFromTo(2, 3)
|
||||
Check(t, 0, len(v11))
|
||||
Check(t, 2, len(v22))
|
||||
CheckArr(t, []int{5, 6}, v22)
|
||||
}
|
||||
|
||||
func TestIterator(t *testing.T) {
|
||||
@ -291,21 +298,23 @@ func TestIterator(t *testing.T) {
|
||||
|
||||
func Check[T comparable](t *testing.T, expected, got T) {
|
||||
if got != expected {
|
||||
t.Fatalf("Expected %v but got %v\n", expected, got)
|
||||
_, _, line, _ := runtime.Caller(1)
|
||||
t.Fatalf("Expected %v but got %v by test at line %d\n", expected, got, line)
|
||||
}
|
||||
}
|
||||
|
||||
func CheckArr[T comparable](t *testing.T, expected, got []T) {
|
||||
|
||||
_, _, line, _ := runtime.Caller(1)
|
||||
if len(expected) != len(got) {
|
||||
t.Fatalf("Expected %v but got %v\n", expected, got)
|
||||
t.Fatalf("Expected %v but got %v by test at line %d\n", expected, got, line)
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < len(expected); i++ {
|
||||
|
||||
if expected[i] != got[i] {
|
||||
t.Fatalf("Expected %v but got %v\n", expected, got)
|
||||
t.Fatalf("Expected %v but got %v by test at line %d\n", expected, got, line)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user