Browse Source

Testing FIFOBuffer empty/overflow panics.

Steve Thielemann 3 years ago
parent
commit
eb1d11ffa8
2 changed files with 39 additions and 5 deletions
  1. 5 5
      door/door.go
  2. 34 0
      door/door_test.go

+ 5 - 5
door/door.go

@@ -57,16 +57,16 @@ func (f *FIFOBuffer) Empty() bool {
 }
 
 func (f *FIFOBuffer) Push(value int) {
+	if f.index >= len(f.data) {
+		log.Panicf("Exceeded FIFOBuffer index %d size %d %#v", f.index, len(f.data), f.data)
+	}
 	f.data[f.index] = value
 	f.index++
-	if f.index > len(f.data) {
-		log.Panicf("Exceeded FIFOBuffer size %d %#v", len(f.data), f)
-	}
 }
 
 func (f *FIFOBuffer) Pop() int {
 	if f.index == 0 {
-		log.Panic("Pop from empty FIFOBuffer.")
+		log.Panicf("Pop from empty FIFOBuffer (size %d).", len(f.data))
 	}
 	f.index--
 	return f.data[f.index]
@@ -290,7 +290,7 @@ func (d *Door) Init(doorname string) {
 
 	log.SetOutput(logf)
 	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
-	// log.SetPrefix(doorname + " ")
+	log.SetPrefix(doorname + " ")
 
 	//= log.New(logf, fmt.Sprintf("%s-%d", doorname, d.Config.Node), log.Ldate|log.Ltime|log.Lshortfile)
 	log.Printf("Loading dropfile %s\n", dropfile)

+ 34 - 0
door/door_test.go

@@ -11,6 +11,40 @@ import (
 	"time"
 )
 
+func TestFIFOEmpty(t *testing.T) {
+	buffer := NewFIFOBuffer(3)
+
+	defer func() {
+		if r := recover(); r == nil {
+			t.Error("Pop of empty FIFO Buffer did not panic.")
+		}
+	}()
+
+	buffer.Push(1)
+	x := buffer.Pop()
+	if x != 1 {
+		t.Errorf("Buffer did not return expected value 1: %d", x)
+	}
+
+	_ = buffer.Pop()
+}
+
+func TestFIFOOverflow(t *testing.T) {
+	buffer := NewFIFOBuffer(3)
+
+	defer func() {
+		if r := recover(); r == nil {
+			t.Error("Pop of empty FIFO Buffer did not panic.")
+		}
+	}()
+
+	buffer.Push(1)
+	buffer.Push(2)
+	buffer.Push(3)
+	buffer.Push(4)
+
+}
+
 func TestGoto(t *testing.T) {
 	GotoMap := map[string][]int{"\x1b[10;20H": {20, 10},
 		"\x1b[20;10H":  {10, 20},