add kline window tail test

This commit is contained in:
c9s 2020-08-18 13:14:52 +08:00
parent 163036c745
commit 86769d4535
2 changed files with 29 additions and 2 deletions

View File

@ -320,9 +320,14 @@ func (k KLineWindow) Take(size int) KLineWindow {
func (k KLineWindow) Tail(size int) KLineWindow {
if len(k) <= size {
return k[:]
win := make(KLineWindow, len(k))
copy(win, k)
return win
}
return k[len(k)-size:]
win := make(KLineWindow, size)
copy(win, k[len(k)-size:])
return win
}
func (k *KLineWindow) Truncate(size int) {

22
bbgo/types/kline_test.go Normal file
View File

@ -0,0 +1,22 @@
package types
import ("testing"
"github.com/stretchr/testify/assert"
)
func TestKLineWindow_Tail(t *testing.T) {
var win = KLineWindow{
{ Open: "11600.0", Close: "11600.0", High: "11600.0", Low: "11600.0"},
{ Open: "11600.0", Close: "11600.0", High: "11600.0", Low: "11600.0"},
}
var win2 = win.Tail(1)
assert.Len(t, win2, 1)
var win3 = win.Tail(2)
assert.Len(t, win3, 2)
var win4 = win.Tail(3)
assert.Len(t, win4, 2)
}