github.com/claygod/queue@v0.0.0-20221013165802-9870c6dace8e/queue_test.go (about)

     1  package queue
     2  
     3  // Queue
     4  // Test
     5  // Copyright © 2016-2018 Eduard Sesigin. All rights reserved. Contacts: <claygod@yandex.ru>
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestLenQueue(t *testing.T) {
    12  	q := New(10)
    13  	q.PushTail(1)
    14  	q.PushTail(2)
    15  	if q.LenQueue() != 2 {
    16  		t.Error("LenQueue 2 != ", q.LenQueue())
    17  	}
    18  }
    19  
    20  func TestSizeQueue10(t *testing.T) {
    21  	q := New(10)
    22  	q.PushTail(1)
    23  	if q.SizeQueue() != 10 {
    24  		t.Error("SizeQueue 10 != ", q.LenQueue())
    25  	}
    26  }
    27  
    28  func TestSizeQueue20(t *testing.T) {
    29  	q := New(10)
    30  	for i := 0; i < 5; i++ {
    31  		q.PushTail(i)
    32  	}
    33  	if q.SizeQueue() != 20 {
    34  		t.Error("SizeQueue 20 != ", q.LenQueue())
    35  	}
    36  }
    37  
    38  /*
    39  func TestClear(t *testing.T) {
    40  	q := New(10)
    41  	for i := 0; i < 30; i++ {
    42  		q.PushTail(i)
    43  	}
    44  	for i := 0; i < 30; i++ {
    45  		q.PopTail()
    46  	}
    47  	if q.SizeQueue() != 10 {
    48  		t.Error("Clear not correct! SizeQueue 10 != ", q.SizeQueue())
    49  	}
    50  }
    51  */
    52  func TestDatabase(t *testing.T) {
    53  	q := New(10)
    54  	for i := 0; i < 100; i++ {
    55  		q.PushTail(i)
    56  	}
    57  	for i := 99; i >= 0; i-- {
    58  		m, _ := q.PopTail()
    59  		if m.(int) != i {
    60  			t.Error("Error in database", i, " != ", m)
    61  		}
    62  	}
    63  }
    64  func TestPopHead(t *testing.T) {
    65  	q := New(10)
    66  	q.PushTail(100)
    67  	q.PushHead(200)
    68  	q.PushTail(300)
    69  
    70  	if m, _ := q.PopHead(); m.(int) != 200 {
    71  		t.Error("Error PopHead: 200 != ", m.(int))
    72  	}
    73  	if m, _ := q.PopHead(); m.(int) != 100 {
    74  		t.Error("Error PopHead: 100 != ", m.(int))
    75  	}
    76  	if m, _ := q.PopHead(); m.(int) != 300 {
    77  		t.Error("Error PopHead: 300 != ", m.(int))
    78  	}
    79  }
    80  
    81  func TestPopTail(t *testing.T) {
    82  	q := New(10)
    83  	q.PushTail(100)
    84  	q.PushHead(200)
    85  	q.PushTail(300)
    86  
    87  	if m, _ := q.PopTail(); m.(int) != 300 {
    88  		t.Error("Error PopTail: 300 != ", m.(int))
    89  	}
    90  	if m, _ := q.PopTail(); m.(int) != 100 {
    91  		t.Error("Error PopTail: 100 != ", m.(int))
    92  	}
    93  	if m, _ := q.PopTail(); m.(int) != 200 {
    94  		t.Error("Error PopTail: 200 != ", m.(int))
    95  	}
    96  }
    97  
    98  func TestQueuePushList(t *testing.T) {
    99  	q := New(10)
   100  	for i := 0; i < 10; i++ {
   101  		q.PushTail(i)
   102  	}
   103  	lst := q.PopHeadList(3)
   104  	if len(lst) != 3 {
   105  		t.Error("Not received 3 items, although there are 10 in the database.")
   106  	}
   107  	if lst[0].(int) != 0 {
   108  		t.Error("Received an incorrect answer.")
   109  	}
   110  }
   111  
   112  func TestQueuePushList2(t *testing.T) {
   113  	q := New(10)
   114  	for i := 0; i < 10; i++ {
   115  		q.PushTail(i)
   116  	}
   117  	lst := q.PopHeadList(13)
   118  	if len(lst) != 10 {
   119  		t.Error("Amount is incorrect.")
   120  	}
   121  
   122  }