func TestMillion(t *testing.T) { const big = 1000000 // A million Convey("A Million elements", t, func() { var rBuf = ringbuffer.New(big) So(rBuf, ShouldNotBeNil) So(0, ShouldEqual, rBuf.Leng()) //fmt.Println("———————→ Million Buffer March ←———————") nnn := 0 for { e := rBuf.Write(&kitchenSink{words: "bogosity", nums: [4]int{nnn, 1 + nnn, 2 + nnn, 3 + nnn}}) if nil != e { //fmt.Printf("Fatal Error Required: %v", e) break } nnn += 4 } rc := 0 for rBuf.HasAny() { _ = rBuf.Read() rc++ } fmt.Println("Read ", rc, " times.") }) //fmt.Println("Done") }
// Write only a byte at a time. func BenchmarkInsertByteEr(b *testing.B) { rb := ringbuffer.New(b.N) b.ResetTimer() for i := 0; i < b.N; i++ { e := rb.Write(byte(7)) if nil != e { b.Fatalf("Write: %v\n", e) } } }
func TestFull(t *testing.T) { const quantity = 7 Convey("Full 7", t, func() { var rBuf = ringbuffer.New(quantity) So(rBuf, ShouldNotBeNil) So(rBuf.Full(), ShouldBeFalse) So(rBuf.HasAny(), ShouldBeFalse) So(rBuf.Leng(), ShouldEqual, 0) // Read empty ringbuffer: x := rBuf.Read() So(rBuf.Full(), ShouldBeFalse) So(rBuf.HasAny(), ShouldBeFalse) So(rBuf.Leng(), ShouldEqual, 0) So(x, ShouldBeNil) /// Return nil on empty. var i int for i = 0; i < quantity; i++ { e := rBuf.Write(i) if nil != e { t.Fatalf("Can't write %d of %d\n", i, quantity) } So((i < (quantity-1)) && rBuf.Full(), ShouldBeFalse) } // Test Dumping: rBuf.Dump() // Overflow e := rBuf.Write(i + 1) // Overflow must return error. So(e, ShouldNotBeNil) var rbe *ringbuffer.RingBufferError So(e, ShouldHaveSameTypeAs, rbe) /// So(rBuf.Full(), ShouldBeTrue) So(rBuf.HasAny(), ShouldBeTrue) So(rBuf.Leng(), ShouldEqual, quantity) x = rBuf.Read() So(rBuf.Full(), ShouldBeFalse) So(rBuf.HasAny(), ShouldBeTrue) So(rBuf.Leng(), ShouldEqual, quantity-1) So(x, ShouldEqual, 0) So(x, ShouldHaveSameTypeAs, quantity) rBuf.Clear() So(rBuf.Leng(), ShouldEqual, 0) So(rBuf.Full(), ShouldBeFalse) So(rBuf.HasAny(), ShouldBeFalse) }) }
// Same as above, copying the struct in and out. // Copying is a little faster!! func BenchmarkKitchenCopy(b *testing.B) { rb := ringbuffer.New(b.N) b.ResetTimer() nnn := 0 for i := 0; i < b.N; i++ { _ = rb.Write(kitchenSink{words: "Benchmarking", nums: [4]int{nnn, 1 + nnn, 2 + nnn, 3 + nnn}}) nnn += 4 } //Should we check the values as they come out? for j := 0; j < b.N; j++ { ksTmp, ok := rb.Read().(kitchenSink) if !ok { b.Fatalf("(Copy) Read Type: %T\n", ksTmp) } } }
func TestKitchenSmall(t *testing.T) { const quantity = 11 var rBuf = ringbuffer.New(quantity) // Create the ring buffer with the specified size. Convey("Blank Buffer 11", t, func() { So(rBuf.Leng(), ShouldEqual, 0) }) for _, va := range ksa { // Add in the kitchenSink structs. e := rBuf.Write(va) if nil != e { t.Fatalf("ksa Oopsie\n") } } Convey("Full Buffer Wrote 11", t, func() { So(rBuf.Leng(), ShouldEqual, len(ksa)) }) // Now get them all out: for rBuf.HasAny() { fmt.Printf("→ %v\n", rBuf.Read()) } Convey("Empty Buffer 11", t, func() { So(rBuf.Leng(), ShouldEqual, 0) }) }