Skip to content

Commit

Permalink
fix: correct nearest current tick when current tick is below smallest (
Browse files Browse the repository at this point in the history
…#5)

* fix: correct nearest current tick when current tick is below smallest

* test: add ut for nearest current ticks

---------

Co-authored-by: Ha Nguyen <ha.nguyenk07@gmail.com>
  • Loading branch information
piavgh and hanguyenk committed Jun 7, 2023
1 parent e26430f commit 1372f9e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions entities/ticklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ func NextInitializedTickWithinOneWord(ticks []Tick, tick int, lte bool, tickSpac
}

func GetNearestCurrentTick(ticks []Tick, currentTick int) (int, error) {
isBelowSmallest, err := IsBelowSmallest(ticks, currentTick)
if err != nil {
return utils.MinTick, err
}

if isBelowSmallest {
return utils.MinTick, nil
}

tick, err := NextInitializedTick(ticks, currentTick, true)
if err != nil {
return TickIndexZero, err
Expand Down
34 changes: 34 additions & 0 deletions entities/ticklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,37 @@ func TestNextInitializedTickWithinOneWord(t *testing.T) {
}

}

func TestGetNearestCurrentTick(t *testing.T) {
testCases := []struct {
name string
ticks []Tick
currenTick int
expectedResult int
expectedError error
}{
{
name: "it should return minTicks with error when currentTick is min tick and ticks is empty",
ticks: []Tick{},
currenTick: utils.MinTick,
expectedResult: utils.MinTick,
expectedError: ErrEmptyTickList,
},
{
name: "it should return minTicks with no error when currentTick is min tick and ticks is not empty",
ticks: []Tick{lowTick, midTick, highTick},
currenTick: utils.MinTick,
expectedResult: utils.MinTick,
expectedError: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := GetNearestCurrentTick(tc.ticks, tc.currenTick)

assert.Equal(t, tc.expectedResult, result)
assert.ErrorIs(t, err, tc.expectedError)
})
}
}

0 comments on commit 1372f9e

Please sign in to comment.