AdGuardHome/internal/aghalg/ringbuffer_test.go

170 lines
3.2 KiB
Go
Raw Normal View History

Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
package aghalg_test
import (
"slices"
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
"testing"
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
"github.com/stretchr/testify/assert"
)
// elements is a helper function that returns n elements of the buffer.
func elements(b *aghalg.RingBuffer[int], n uint, reverse bool) (es []int) {
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
fn := b.Range
if reverse {
fn = b.ReverseRange
}
var i uint
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
fn(func(e int) (cont bool) {
if i >= n {
return false
}
es = append(es, e)
i++
return true
})
return es
}
func TestNewRingBuffer(t *testing.T) {
t.Run("success_and_clear", func(t *testing.T) {
b := aghalg.NewRingBuffer[int](5)
for i := range 10 {
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
b.Append(i)
}
assert.Equal(t, []int{5, 6, 7, 8, 9}, elements(b, b.Len(), false))
b.Clear()
assert.Zero(t, b.Len())
})
t.Run("zero", func(t *testing.T) {
b := aghalg.NewRingBuffer[int](0)
for i := range 10 {
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
b.Append(i)
bufLen := b.Len()
assert.EqualValues(t, 0, bufLen)
assert.Empty(t, elements(b, bufLen, false))
assert.Empty(t, elements(b, bufLen, true))
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
}
})
t.Run("single", func(t *testing.T) {
b := aghalg.NewRingBuffer[int](1)
for i := range 10 {
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
b.Append(i)
bufLen := b.Len()
assert.EqualValues(t, 1, bufLen)
assert.Equal(t, []int{i}, elements(b, bufLen, false))
assert.Equal(t, []int{i}, elements(b, bufLen, true))
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
}
})
}
func TestRingBuffer_Range(t *testing.T) {
const size = 5
b := aghalg.NewRingBuffer[int](size)
testCases := []struct {
name string
want []int
count int
length uint
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
}{{
name: "three",
count: 3,
length: 3,
want: []int{0, 1, 2},
}, {
name: "ten",
count: 10,
length: size,
want: []int{5, 6, 7, 8, 9},
}, {
name: "hundred",
count: 100,
length: size,
want: []int{95, 96, 97, 98, 99},
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i := range tc.count {
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
b.Append(i)
}
bufLen := b.Len()
assert.Equal(t, tc.length, bufLen)
want := tc.want
assert.Equal(t, want, elements(b, bufLen, false))
assert.Equal(t, want[:len(want)-1], elements(b, bufLen-1, false))
assert.Equal(t, want[:len(want)/2], elements(b, bufLen/2, false))
want = want[:cap(want)]
slices.Reverse(want)
assert.Equal(t, want, elements(b, bufLen, true))
assert.Equal(t, want[:len(want)-1], elements(b, bufLen-1, true))
assert.Equal(t, want[:len(want)/2], elements(b, bufLen/2, true))
})
}
}
func TestRingBuffer_Range_increment(t *testing.T) {
const size = 5
b := aghalg.NewRingBuffer[int](size)
testCases := []struct {
name string
want []int
}{{
name: "one",
want: []int{0},
}, {
name: "two",
want: []int{0, 1},
}, {
name: "three",
want: []int{0, 1, 2},
}, {
name: "four",
want: []int{0, 1, 2, 3},
}, {
name: "five",
want: []int{0, 1, 2, 3, 4},
}, {
name: "six",
want: []int{1, 2, 3, 4, 5},
}, {
name: "seven",
want: []int{2, 3, 4, 5, 6},
}, {
name: "eight",
want: []int{3, 4, 5, 6, 7},
}, {
name: "nine",
want: []int{4, 5, 6, 7, 8},
}, {
name: "ten",
want: []int{5, 6, 7, 8, 9},
}}
for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b.Append(i)
bufLen := b.Len()
assert.Equal(t, tc.want, elements(b, bufLen, false))
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
slices.Reverse(tc.want)
assert.Equal(t, tc.want, elements(b, bufLen, true))
Pull request 2020: AG-26236-ring-buffer Squashed commit of the following: commit 4b9cc9ddf52739fc5f918babedc99ac7ac0e2415 Merge: a6259ed57 39aeaf891 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:39:58 2023 +0300 Merge branch 'master' into AG-26236-ring-buffer commit a6259ed5758156e4110ee3ea6a49760d2880ade3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:30:20 2023 +0300 querylog: imp code commit 40f9f7cd5a1cff22bcb858020f2cfa9be8399671 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:11:49 2023 +0300 querylog: fix typo commit 1aabbadcb5fcbe6a95945c5bd1455b956b85a8d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 20:05:22 2023 +0300 querylog: imp err msg commit 02913d35b43e190e42765823ccfcdd332839e984 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:37:47 2023 +0300 aghalg: imp tests commit 98a7909088d2a65b78afa9fa3113545c94429e65 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 19:23:10 2023 +0300 all: imp tests commit e147804eeafe89e5020cd917784eef3ff2b310d0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 18:49:49 2023 +0300 all: imp code commit 5f21f2f63b7bce89f2bc79c97c7350cd693c956b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 3 14:39:43 2023 +0300 all: add tests commit 35a45c7dc5d5961f6987da7c69249c56e54e97f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 2 17:43:09 2023 +0300 all: imp code commit 21e51fcbe411258eaf830825df9d05b7ddcc187a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Sep 29 18:21:00 2023 +0300 all: add ring buffer
2023-10-03 20:50:43 +03:00
})
}
}