github.com/sandwich-go/boost@v1.3.29/xcontainer/ringbuf/ringbuf_test.go (about) 1 package ringbuf 2 3 import ( 4 "testing" 5 6 . "github.com/smartystreets/goconvey/convey" 7 ) 8 9 var buf = New(10) 10 11 func TestRingbuf(t *testing.T) { 12 Convey("ring buf test", t, func() { 13 So(buf.Capacity(), ShouldEqual, 10) 14 err := buf.Write([]byte("helloworld")) 15 //helloworld 16 So(err, ShouldBeNil) 17 So(buf.Size(), ShouldEqual, 10) 18 So(buf.start, ShouldEqual, 0) 19 err = buf.Write([]byte("a")) 20 So(err, ShouldEqual, NotEnoughMem) 21 22 tmp := make([]byte, 5) 23 n := buf.Read(tmp, 5) 24 //world 25 So(n, ShouldEqual, 5) 26 So(len(tmp), ShouldEqual, 5) 27 So(buf.Size(), ShouldEqual, 5) 28 So(buf.start, ShouldEqual, 5) 29 So(string(tmp), ShouldEqual, "hello") 30 31 err = buf.Write([]byte("1234")) 32 // world1234 33 So(err, ShouldBeNil) 34 So(buf.Size(), ShouldEqual, 9) 35 So(buf.start, ShouldEqual, 5) 36 37 n = buf.Read(tmp, 4) 38 // d1234 39 So(n, ShouldEqual, 4) 40 So(string(tmp), ShouldEqual, "worlo") 41 So(buf.Size(), ShouldEqual, 5) 42 So(buf.start, ShouldEqual, 9) 43 44 So(len(buf.PreUse(5)), ShouldEqual, 5) 45 buf.Read(tmp, 3) 46 So(string(tmp), ShouldEqual, "d12lo") 47 48 buf.RealUse(1) 49 buf.Read(tmp, 3) 50 So(string(tmp), ShouldEqual, "34olo") 51 52 }) 53 }