Skip to content

Commit

Permalink
test(logic): put BoundedBuffer into unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Apr 5, 2023
1 parent 0ae4d43 commit 71fbaeb
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions x/logic/util/buffer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package util

import (
"fmt"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestBoundedBuffer(t *testing.T) {
Convey("Given test cases", t, func() {
tcs := []struct {
size int
bytes [][]byte
wantResult string
}{
{20, [][]byte{[]byte("hello world")}, "hello world"},
{11, [][]byte{[]byte("hello"), []byte(" "), []byte("world")}, "hello world"},
{10, [][]byte{[]byte("hello"), []byte(" "), []byte("world")}, "ello world"},
{5, [][]byte{[]byte("hello world")}, "world"},
{1, [][]byte{[]byte("hello"), []byte(" "), []byte("world")}, "d"},
{1, [][]byte{}, ""},
{0, [][]byte{[]byte("hello world")}, ""},
}

for tn, tc := range tcs {
Convey(fmt.Sprintf("Given a BoundedBuffer with size %d (%d)", tc.size, tn), func() {
buffer, err := NewBoundedBuffer(tc.size)
So(err, ShouldBeNil)

Convey("When calling the Write() function", func() {
wantCount := 0
count := 0
for _, b := range tc.bytes {
n, err := buffer.Write(b)
count += n
wantCount += len(b)
So(err, ShouldBeNil)
}

Convey("Then we should get %s", func() {
So(buffer.String(), ShouldEqual, tc.wantResult)
})
Convey(fmt.Sprintf("And the number of bytes written should be %d", wantCount), func() {
So(count, ShouldEqual, wantCount)
})
})
})
}
})
}

0 comments on commit 71fbaeb

Please sign in to comment.