• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3require('../common');
4const assert = require('assert');
5const BufferList = require('internal/streams/buffer_list');
6
7// Test empty buffer list.
8const emptyList = new BufferList();
9
10emptyList.shift();
11assert.deepStrictEqual(emptyList, new BufferList());
12
13assert.strictEqual(emptyList.join(','), '');
14
15assert.deepStrictEqual(emptyList.concat(0), Buffer.alloc(0));
16
17const buf = Buffer.from('foo');
18
19function testIterator(list, count) {
20  // test iterator
21  let len = 0;
22  // eslint-disable-next-line no-unused-vars
23  for (const x of list) {
24    len++;
25  }
26  assert.strictEqual(len, count);
27}
28
29// Test buffer list with one element.
30const list = new BufferList();
31testIterator(list, 0);
32
33list.push(buf);
34testIterator(list, 1);
35for (const x of list) {
36  assert.strictEqual(x, buf);
37}
38
39const copy = list.concat(3);
40testIterator(copy, 3);
41
42assert.notStrictEqual(copy, buf);
43assert.deepStrictEqual(copy, buf);
44
45assert.strictEqual(list.join(','), 'foo');
46
47const shifted = list.shift();
48testIterator(list, 0);
49assert.strictEqual(shifted, buf);
50assert.deepStrictEqual(list, new BufferList());
51
52{
53  const list = new BufferList();
54  list.push('foo');
55  list.push('bar');
56  list.push('foo');
57  list.push('bar');
58  assert.strictEqual(list.consume(6, true), 'foobar');
59  assert.strictEqual(list.consume(6, true), 'foobar');
60}
61
62{
63  const list = new BufferList();
64  list.push('foo');
65  list.push('bar');
66  assert.strictEqual(list.consume(5, true), 'fooba');
67}
68
69{
70  const list = new BufferList();
71  list.push(buf);
72  list.push(buf);
73  list.push(buf);
74  list.push(buf);
75  assert.strictEqual(list.consume(6).toString(), 'foofoo');
76  assert.strictEqual(list.consume(6).toString(), 'foofoo');
77}
78
79{
80  const list = new BufferList();
81  list.push(buf);
82  list.push(buf);
83  assert.strictEqual(list.consume(5).toString(), 'foofo');
84}
85