fifo_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package collection
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestFifo(t *testing.T) {
  7. elements := [][]byte{
  8. []byte("hello"),
  9. []byte("world"),
  10. []byte("again"),
  11. }
  12. queue := NewQueue(8)
  13. for i := range elements {
  14. queue.Put(elements[i])
  15. }
  16. for _, element := range elements {
  17. body, ok := queue.Take()
  18. assert.True(t, ok)
  19. assert.Equal(t, string(element), string(body.([]byte)))
  20. }
  21. }
  22. func TestTakeTooMany(t *testing.T) {
  23. elements := [][]byte{
  24. []byte("hello"),
  25. []byte("world"),
  26. []byte("again"),
  27. }
  28. queue := NewQueue(8)
  29. for i := range elements {
  30. queue.Put(elements[i])
  31. }
  32. for range elements {
  33. queue.Take()
  34. }
  35. assert.True(t, queue.Empty())
  36. _, ok := queue.Take()
  37. assert.False(t, ok)
  38. }
  39. func TestPutMore(t *testing.T) {
  40. elements := [][]byte{
  41. []byte("hello"),
  42. []byte("world"),
  43. []byte("again"),
  44. }
  45. queue := NewQueue(2)
  46. for i := range elements {
  47. queue.Put(elements[i])
  48. }
  49. for _, element := range elements {
  50. body, ok := queue.Take()
  51. assert.True(t, ok)
  52. assert.Equal(t, string(element), string(body.([]byte)))
  53. }
  54. }