summaryrefslogtreecommitdiffstats
path: root/test/fixed-queue.js
blob: 812f421b280c7ddec56273c5f1a99ef85b3bea5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict'

const { test } = require('tap')

const FixedQueue = require('../lib/node/fixed-queue')

test('fixed queue 1', (t) => {
  t.plan(5)

  const queue = new FixedQueue()
  t.equal(queue.head, queue.tail)
  t.ok(queue.isEmpty())
  queue.push('a')
  t.ok(!queue.isEmpty())
  t.equal(queue.shift(), 'a')
  t.equal(queue.shift(), null)
})

test('fixed queue 2', (t) => {
  t.plan(7 + 2047)

  const queue = new FixedQueue()
  for (let i = 0; i < 2047; i++) {
    queue.push('a')
  }
  t.ok(queue.head.isFull())
  queue.push('a')
  t.ok(!queue.head.isFull())

  t.not(queue.head, queue.tail)
  for (let i = 0; i < 2047; i++) {
    t.equal(queue.shift(), 'a')
  }
  t.equal(queue.head, queue.tail)
  t.ok(!queue.isEmpty())
  t.equal(queue.shift(), 'a')
  t.ok(queue.isEmpty())
})