fifobuffer_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package door
  2. // Need net, flag for setupSockets
  3. import (
  4. "testing"
  5. )
  6. func TestFIFOEmptyRune(t *testing.T) {
  7. buffer := NewFIFOBuffer[rune](3)
  8. defer func() {
  9. if r := recover(); r == nil {
  10. t.Error("Pop of empty FIFO Buffer did not panic.")
  11. }
  12. }()
  13. buffer.Push(rune(1))
  14. x := buffer.Pop()
  15. if x != rune(1) {
  16. t.Errorf("Buffer did not return expected value 1: %d", x)
  17. }
  18. _ = buffer.Pop()
  19. }
  20. func TestFIFOOverflowRune(t *testing.T) {
  21. buffer := NewFIFOBuffer[rune](3)
  22. defer func() {
  23. if r := recover(); r == nil {
  24. t.Error("Pop of empty FIFO Buffer did not panic.")
  25. }
  26. }()
  27. buffer.Push(rune(1))
  28. buffer.Push(rune(2))
  29. buffer.Push(rune(3))
  30. buffer.Push(rune(4))
  31. t.Errorf("buffer.Push should have paniced.")
  32. }
  33. func TestFIFOEmptyInt(t *testing.T) {
  34. buffer := NewFIFOBuffer[int](3)
  35. defer func() {
  36. if r := recover(); r == nil {
  37. t.Error("Pop of empty FIFO Buffer did not panic.")
  38. }
  39. }()
  40. buffer.Push(1)
  41. x := buffer.Pop()
  42. if x != 1 {
  43. t.Errorf("Buffer did not return expected value 1: %d", x)
  44. }
  45. _ = buffer.Pop()
  46. }
  47. func TestFIFOOverflowInt(t *testing.T) {
  48. buffer := NewFIFOBuffer[int](3)
  49. defer func() {
  50. if r := recover(); r == nil {
  51. t.Error("Pop of empty FIFO Buffer did not panic.")
  52. }
  53. }()
  54. buffer.Push(1)
  55. buffer.Push(2)
  56. buffer.Push(3)
  57. buffer.Push(4)
  58. t.Errorf("buffer.Push should have paniced.")
  59. }
  60. func TestFIFOInt(t *testing.T) {
  61. buffer := NewFIFOBuffer[int](3)
  62. buffer.Push(10)
  63. buffer.Push(20)
  64. x := buffer.Pop()
  65. if x != 20 {
  66. t.Errorf("Buffer did not return expected value 20: %d", x)
  67. }
  68. x = buffer.Pop()
  69. if x != 10 {
  70. t.Errorf("Buffer did not return expected value 10: %d", x)
  71. }
  72. if !buffer.Empty() {
  73. t.Errorf("Buffer is not empty.")
  74. }
  75. buffer.Push(30)
  76. x = buffer.Pop()
  77. if x != 30 {
  78. t.Errorf("Buffer did not return expected value 30: %d", x)
  79. }
  80. }
  81. func TestFIFOStr(t *testing.T) {
  82. buffer := NewFIFOBuffer[string](2)
  83. buffer.Push("Fish")
  84. buffer.Push("Cat")
  85. x := buffer.Pop()
  86. if x != "Cat" {
  87. t.Errorf("Expected Cat, got %s", x)
  88. }
  89. x = buffer.Pop()
  90. if x != "Fish" {
  91. t.Errorf("Expected Fish, got %s", x)
  92. }
  93. }