circular-buffer.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import CircularBuffer, {
  2. BufferFullError,
  3. BufferEmptyError,
  4. } from './circular-buffer'
  5. describe('CircularBuffer', () => {
  6. it('reading an empty buffer throws a BufferEmptyError', () => {
  7. const buffer = new CircularBuffer<string>(1)
  8. expect(() => buffer.read()).toThrow(BufferEmptyError)
  9. })
  10. it('write and read back one item', () => {
  11. const buffer = new CircularBuffer<string>(1)
  12. buffer.write('1')
  13. expect(buffer.read()).toBe('1')
  14. expect(() => buffer.read()).toThrow(BufferEmptyError)
  15. })
  16. it('write and read back multiple items', () => {
  17. const buffer = new CircularBuffer<string>(2)
  18. buffer.write('1')
  19. buffer.write('2')
  20. expect(buffer.read()).toBe('1')
  21. expect(buffer.read()).toBe('2')
  22. expect(() => buffer.read()).toThrow(BufferEmptyError)
  23. })
  24. it('clearing a buffer', () => {
  25. const buffer = new CircularBuffer<string>(2)
  26. buffer.write('1')
  27. buffer.write('2')
  28. buffer.clear()
  29. expect(() => buffer.read()).toThrow(BufferEmptyError)
  30. buffer.write('3')
  31. buffer.write('4')
  32. expect(buffer.read()).toBe('3')
  33. expect(buffer.read()).toBe('4')
  34. })
  35. it('alternate write and read', () => {
  36. const buffer = new CircularBuffer<string>(2)
  37. buffer.write('1')
  38. expect(buffer.read()).toBe('1')
  39. buffer.write('2')
  40. expect(buffer.read()).toBe('2')
  41. })
  42. it('reads back oldest item', () => {
  43. const buffer = new CircularBuffer<string>(3)
  44. buffer.write('1')
  45. buffer.write('2')
  46. buffer.read()
  47. buffer.write('3')
  48. expect(buffer.read()).toBe('2')
  49. expect(buffer.read()).toBe('3')
  50. })
  51. it('writing to a full buffer throws a BufferFullError', () => {
  52. const buffer = new CircularBuffer<string>(2)
  53. buffer.write('1')
  54. buffer.write('2')
  55. expect(() => buffer.write('A')).toThrow(BufferFullError)
  56. })
  57. it('forced writes over write oldest item in a full buffer', () => {
  58. const buffer = new CircularBuffer<string>(2)
  59. buffer.write('1')
  60. buffer.write('2')
  61. buffer.forceWrite('A')
  62. expect(buffer.read()).toBe('2')
  63. expect(buffer.read()).toBe('A')
  64. expect(() => buffer.read()).toThrow(BufferEmptyError)
  65. })
  66. it('forced writes act like write in a non-full buffer', () => {
  67. const buffer = new CircularBuffer<string>(2)
  68. buffer.write('1')
  69. buffer.forceWrite('2')
  70. expect(buffer.read()).toBe('1')
  71. expect(buffer.read()).toBe('2')
  72. expect(() => buffer.read()).toThrow(BufferEmptyError)
  73. })
  74. it('alternate force write and read into full buffer', () => {
  75. const buffer = new CircularBuffer<string>(5)
  76. buffer.write('1')
  77. buffer.write('2')
  78. buffer.write('3')
  79. buffer.read()
  80. buffer.read()
  81. buffer.write('4')
  82. buffer.read()
  83. buffer.write('5')
  84. buffer.write('6')
  85. buffer.write('7')
  86. buffer.write('8')
  87. buffer.forceWrite('A')
  88. buffer.forceWrite('B')
  89. expect(buffer.read()).toBe('6')
  90. expect(buffer.read()).toBe('7')
  91. expect(buffer.read()).toBe('8')
  92. expect(buffer.read()).toBe('A')
  93. expect(buffer.read()).toBe('B')
  94. expect(() => buffer.read()).toThrow(BufferEmptyError)
  95. })
  96. })