github.com/iainanderson83/datastructures@v0.0.4-0.20191103204413-889e20b53bcf/queue/queue_test.go (about)

     1  package queue
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/iainanderson83/datastructures/stack"
     7  )
     8  
     9  func TestQueues(t *testing.T) {
    10  	in := []int{1, 3, 5, 4, 2}
    11  
    12  	tests := map[string]Queue{
    13  		"ArrQueue":   &ArrQueue{},
    14  		"ListQueue":  NewListQueue(),
    15  		"StackQueue": &StackQueue{s1: &stack.ArrStack{}, s2: &stack.ArrStack{}},
    16  	}
    17  
    18  	for name, q := range tests {
    19  		t.Run(name, func(t *testing.T) {
    20  			for _, i := range in {
    21  				q.Enqueue(i)
    22  			}
    23  
    24  			if q.Len() != 5 {
    25  				t.Fatalf("expected 5, got %d", q.Len())
    26  			}
    27  
    28  			for _, i := range in {
    29  				if q.Dequeue() != i {
    30  					t.Fatal("out of order")
    31  				}
    32  			}
    33  		})
    34  	}
    35  }
    36  
    37  func TestQueues2(t *testing.T) {
    38  	in := []int{1, 3, 5, 4, 2}
    39  
    40  	tests := map[string]Queue{
    41  		"ArrQueue":   &ArrQueue{},
    42  		"ListQueue":  NewListQueue(),
    43  		"StackQueue": &StackQueue{s1: &stack.ArrStack{}, s2: &stack.ArrStack{}},
    44  	}
    45  
    46  	for name, q := range tests {
    47  		t.Run(name, func(t *testing.T) {
    48  			q.Enqueue(in[0])
    49  			q.Enqueue(in[1])
    50  			q.Enqueue(in[2])
    51  
    52  			q.Dequeue()
    53  			q.Dequeue()
    54  
    55  			q.Enqueue(in[3])
    56  			q.Enqueue(in[4])
    57  
    58  			if v := q.Dequeue(); v != 5 {
    59  				t.Fatalf("expected %d, got %d", 5, v)
    60  			}
    61  
    62  			if v := q.Dequeue(); v != 4 {
    63  				t.Fatalf("expected %d, got %d", 4, v)
    64  			}
    65  
    66  			if q.Len() != 1 {
    67  				t.Fatalf("expected 3, got %d", q.Len())
    68  			}
    69  		})
    70  	}
    71  }