Skip to content

Commit

Permalink
👔 up: arr,byte - update and add more unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 5, 2023
1 parent 50b5445 commit 133da19
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 12 deletions.
11 changes: 5 additions & 6 deletions arrutil/check.go
Expand Up @@ -15,12 +15,11 @@ func SliceHas[T comdef.ScalarType](slice []T, val T) bool {
return true
}
}

return false
}

// IntsHas check the []int contains the given value
func IntsHas(ints []int, val int) bool {
// IntsHas check the []comdef.Integer contains the given value
func IntsHas[T comdef.Integer](ints []T, val T) bool {
for _, ele := range ints {
if ele == val {
return true
Expand All @@ -39,9 +38,6 @@ func Int64sHas(ints []int64, val int64) bool {
return false
}

// InStrings alias of StringsHas()
func InStrings(elem string, ss []string) bool { return StringsHas(ss, elem) }

// StringsHas check the []string contains the given element
func StringsHas(ss []string, val string) bool {
for _, ele := range ss {
Expand All @@ -52,6 +48,9 @@ func StringsHas(ss []string, val string) bool {
return false
}

// InStrings alias of StringsHas()
func InStrings(elem string, ss []string) bool { return StringsHas(ss, elem) }

// NotIn check the given value whether not in the list
func NotIn[T comdef.ScalarType](value T, list []T) bool {
return !In(value, list)
Expand Down
4 changes: 4 additions & 0 deletions arrutil/check_test.go
Expand Up @@ -26,6 +26,10 @@ func TestIntsHas(t *testing.T) {
assert.True(t, arrutil.IntsHas(ints, 2))
assert.True(t, arrutil.IntsHas(ints, 5))
assert.False(t, arrutil.IntsHas(ints, 3))

uints := []uint{2, 4, 5}
assert.True(t, arrutil.IntsHas(uints, 2))
assert.False(t, arrutil.IntsHas(uints, 3))
}

func TestInt64sHas(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions arrutil/convert_test.go
Expand Up @@ -91,6 +91,9 @@ func TestAnyToSlice(t *testing.T) {
sl, err := arrutil.AnyToSlice([]int{1, 2})
is.NoErr(err)
is.Eq("[]interface {}{1, 2}", fmt.Sprintf("%#v", sl))

_, err = arrutil.AnyToSlice(123)
is.Err(err)
}

func TestConvType(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions byteutil/buffer.go
Expand Up @@ -6,11 +6,13 @@ import (
)

// Buffer wrap and extends the bytes.Buffer, add some useful methods
// and implements the io.Writer, io.Closer and stdio.Flusher interfaces
type Buffer struct {
bytes.Buffer
// custom error for testing
CloseErr error
FlushErr error
SyncErr error
}

// NewBuffer instance
Expand Down Expand Up @@ -117,3 +119,8 @@ func (b *Buffer) Close() error {
func (b *Buffer) Flush() error {
return b.FlushErr
}

// Sync anf flush buffer
func (b *Buffer) Sync() error {
return b.SyncErr
}
1 change: 1 addition & 0 deletions byteutil/buffer_test.go
Expand Up @@ -26,6 +26,7 @@ func TestBuffer_WriteAny(t *testing.T) {

assert.NoErr(t, buf.Close())
assert.NoErr(t, buf.Flush())
assert.NoErr(t, buf.Sync())

buf.WriteStr1Nl("abc")
assert.Eq(t, "abc\n", buf.ResetAndGet())
Expand Down
11 changes: 5 additions & 6 deletions byteutil/pool.go
Expand Up @@ -13,14 +13,14 @@ package byteutil
// from https://github.com/minio/minio/blob/master/internal/bpool/bpool.go
type ChanPool struct {
c chan []byte
w int
wcap int
w int // init byte width
wcap int // set byte cap
}

// NewChanPool instance
func NewChanPool(maxSize int, width int, capWidth int) *ChanPool {
func NewChanPool(chSize int, width int, capWidth int) *ChanPool {
return &ChanPool{
c: make(chan []byte, maxSize),
c: make(chan []byte, chSize),
w: width,
wcap: capWidth,
}
Expand All @@ -30,8 +30,7 @@ func NewChanPool(maxSize int, width int, capWidth int) *ChanPool {
// available in the pool.
func (bp *ChanPool) Get() (b []byte) {
select {
case b = <-bp.c:
// reuse existing buffer
case b = <-bp.c: // reuse existing buffer
default:
// create new buffer
if bp.wcap > 0 {
Expand Down
19 changes: 19 additions & 0 deletions byteutil/pool_test.go
@@ -1,6 +1,7 @@
package byteutil_test

import (
"sync"
"testing"

"github.com/gookit/goutil/byteutil"
Expand All @@ -15,4 +16,22 @@ func TestNewChanPool(t *testing.T) {

p.Put([]byte("abc"))
assert.Equal(t, []byte("abc"), p.Get())

// test concurrent get and put
t.Run("concurrent", func(t *testing.T) {
p := byteutil.NewChanPool(10, 8, 8)
wg := sync.WaitGroup{}

for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
p.Put([]byte("abc"))
assert.Equal(t, []byte("abc"), p.Get())
wg.Done()
}(i)
}

p.Put([]byte("abc"))
assert.Equal(t, []byte("abc"), p.Get())
})
}

0 comments on commit 133da19

Please sign in to comment.