buffers.js
6.39 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var assert = require('assert');
var Buffers = require('../');
function create (xs, split) {
var bufs = Buffers();
var offset = 0;
split.forEach(function (i) {
bufs.push(new Buffer(xs.slice(offset, offset + i)));
offset += i;
});
return bufs;
}
exports.slice = function () {
var xs = [0,1,2,3,4,5,6,7,8,9];
var splits = [ [4,2,3,1], [2,2,2,2,2], [1,6,3,1], [9,2], [10], [5,5] ];
splits.forEach(function (split) {
var bufs = create(xs, split);
assert.eql(new Buffer(xs), bufs.slice(),
'[' + xs.join(',') + ']'
+ ' != ' +
'[' + [].join.call(bufs.slice(), ',') + ']'
);
for (var i = 0; i < xs.length; i++) {
for (var j = i; j < xs.length; j++) {
var a = bufs.slice(i,j);
var b = new Buffer(xs.slice(i,j));
assert.eql(a, b,
'[' + [].join.call(a, ',') + ']'
+ ' != ' +
'[' + [].join.call(b, ',') + ']'
);
}
}
});
};
exports.splice = function () {
var xs = [0,1,2,3,4,5,6,7,8,9];
var splits = [ [4,2,3,1], [2,2,2,2,2], [1,6,3,1], [9,2], [10], [5,5] ];
splits.forEach(function (split) {
for (var i = 0; i < xs.length; i++) {
for (var j = i; j < xs.length; j++) {
var bufs = create(xs, split);
var xs_ = xs.slice();
var a_ = bufs.splice(i,j);
var a = [].slice.call(a_.slice());
var b = xs_.splice(i,j);
assert.eql(a, b,
'[' + a.join(',') + ']'
+ ' != ' +
'[' + b.join(',') + ']'
);
assert.eql(bufs.slice(), new Buffer(xs_),
'[' + [].join.call(bufs.slice(), ',') + ']'
+ ' != ' +
'[' + [].join.call(xs_, ',') + ']'
);
}
}
});
};
exports.spliceRep = function () {
var xs = [0,1,2,3,4,5,6,7,8,9];
var splits = [ [4,2,3,1], [2,2,2,2,2], [1,6,3,1], [9,2], [10], [5,5] ];
var reps = [ [], [1], [5,6], [3,1,3,3,7], [9,8,7,6,5,4,3,2,1,2,3,4,5] ];
splits.forEach(function (split) {
reps.forEach(function (rep) {
for (var i = 0; i < xs.length; i++) {
for (var j = i; j < xs.length; j++) {
var bufs = create(xs, split);
var xs_ = xs.slice();
var a_ = bufs.splice.apply(
bufs, [ i, j ].concat(new Buffer(rep))
);
var a = [].slice.call(a_.slice());
var b = xs_.splice.apply(xs_, [ i, j ].concat(rep));
assert.eql(a, b,
'[' + a.join(',') + ']'
+ ' != ' +
'[' + b.join(',') + ']'
);
assert.eql(bufs.slice(), new Buffer(xs_),
'[' + [].join.call(bufs.slice(), ',') + ']'
+ ' != ' +
'[' + [].join.call(xs_, ',') + ']'
);
}
}
});
});
};
exports.copy = function () {
var xs = [0,1,2,3,4,5,6,7,8,9];
var splits = [ [4,2,3,1], [2,2,2,2,2], [1,6,3,1], [9,2], [10], [5,5] ];
splits.forEach(function (split) {
var bufs = create(xs, split);
var buf = new Buffer(xs);
for (var i = 0; i < xs.length; i++) {
for (var j = i; j < xs.length; j++) {
var t0 = new Buffer(j - i);
var t1 = new Buffer(j - i);
assert.eql(
bufs.copy(t0, 0, i, j),
buf.copy(t1, 0, i, j)
);
assert.eql(
[].slice.call(t0),
[].slice.call(t1)
);
}
}
});
};
exports.push = function () {
var bufs = Buffers();
bufs.push(new Buffer([0]));
bufs.push(new Buffer([1,2,3]));
bufs.push(new Buffer([4,5]));
bufs.push(new Buffer([6,7,8,9]));
assert.eql(
[].slice.call(bufs.slice()),
[0,1,2,3,4,5,6,7,8,9]
);
assert.throws(function () {
bufs.push(new Buffer([11,12]), 'moo');
});
assert.eql(bufs.buffers.length, 4);
};
exports.unshift = function () {
var bufs = Buffers();
bufs.unshift(new Buffer([6,7,8,9]));
bufs.unshift(new Buffer([4,5]));
bufs.unshift(new Buffer([1,2,3]));
bufs.unshift(new Buffer([0]));
assert.eql(
[].slice.call(bufs.slice()),
[0,1,2,3,4,5,6,7,8,9]
);
assert.throws(function () {
bufs.unshift(new Buffer([-2,-1]), 'moo');
});
assert.eql(bufs.buffers.length, 4);
};
exports.get = function () {
var bufs = Buffers();
bufs.unshift(new Buffer([6,7,8,9]));
bufs.unshift(new Buffer([4,5]));
bufs.unshift(new Buffer([1,2,3]));
bufs.unshift(new Buffer([0]));
assert.eql( bufs.get(0), 0 );
assert.eql( bufs.get(1), 1 );
assert.eql( bufs.get(2), 2 );
assert.eql( bufs.get(3), 3 );
assert.eql( bufs.get(4), 4 );
assert.eql( bufs.get(5), 5 );
assert.eql( bufs.get(6), 6 );
assert.eql( bufs.get(7), 7 );
assert.eql( bufs.get(8), 8 );
assert.eql( bufs.get(9), 9 );
};
exports.set = function () {
var bufs = Buffers();
bufs.push(new Buffer("Hel"));
bufs.push(new Buffer("lo"));
bufs.push(new Buffer("!"));
bufs.set(0, 'h'.charCodeAt(0) );
bufs.set(3, 'L'.charCodeAt(0) );
bufs.set(5, '.'.charCodeAt(0) );
assert.eql( bufs.slice(0).toString(), 'helLo.' );
};
exports.indexOf = function () {
var bufs = Buffers();
bufs.push(new Buffer("Hel"));
bufs.push(new Buffer("lo,"));
bufs.push(new Buffer(" how are "));
bufs.push(new Buffer("you"));
bufs.push(new Buffer("?"));
assert.eql( bufs.indexOf("Hello"), 0 );
assert.eql( bufs.indexOf("Hello", 1), -1 );
assert.eql( bufs.indexOf("ello"), 1 );
assert.eql( bufs.indexOf("ello", 1), 1 );
assert.eql( bufs.indexOf("ello", 2), -1 );
assert.eql( bufs.indexOf("e"), 1 );
assert.eql( bufs.indexOf("e", 2), 13 );
assert.eql( bufs.indexOf(new Buffer([0x65]), 2), 13 );
};