123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- /*
- * Test dependencies
- */
-
- var sliceBuffer = require('../index.js');
- var expect = require('expect.js');
-
- /**
- * Tests
- */
-
- describe('sliceBuffer', function() {
- describe('using standard slice', function() {
- it('should slice correctly with only start provided', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
-
- var sliced = sliceBuffer(abv.buffer, 3);
- var sabv = new Uint8Array(sliced);
- for (var i = 3, ii = 0; i < abv.length; i++, ii++) {
- expect(abv[i]).to.equal(sabv[ii]);
- }
- });
-
- it('should slice correctly with start and end provided', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
-
- var sliced = sliceBuffer(abv.buffer, 3, 8);
- var sabv = new Uint8Array(sliced);
- for (var i = 3, ii = 0; i < 8; i++, ii++) {
- expect(abv[i]).to.equal(sabv[ii]);
- }
- });
-
- it('should slice correctly with negative start', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
-
- var sliced = sliceBuffer(abv.buffer, -3);
- var sabv = new Uint8Array(sliced);
- for (var i = abv.length - 3, ii = 0; i < abv.length; i++, ii++) {
- expect(abv[i]).to.equal(sabv[ii]);
- }
- });
-
- it('should slice correctly with negative end', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
-
- var sliced = sliceBuffer(abv.buffer, 0, -3);
- var sabv = new Uint8Array(sliced);
- for (var i = 0, ii = 0; i < abv.length - 3; i++, ii++) {
- expect(abv[i]).to.equal(sabv[ii]);
- }
- });
-
- it('should slice correctly with negative start and end', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
-
- var sliced = sliceBuffer(abv.buffer, -6, -3);
- var sabv = new Uint8Array(sliced);
- for (var i = abv.length - 6, ii = 0; i < abv.length - 3; i++, ii++) {
- expect(abv[i]).to.equal(sabv[ii]);
- }
- });
-
- it('should slice correctly with equal start and end', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
-
- var sliced = sliceBuffer(abv.buffer, 1, 1);
- expect(sliced.byteLength).to.equal(0);
- });
-
- it('should slice correctly when end larger than buffer', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
-
- var sliced = sliceBuffer(abv.buffer, 0, 100);
- expect(new Uint8Array(sliced)).to.eql(abv);
- });
-
- it('shoud slice correctly when start larger than end', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
-
- var sliced = sliceBuffer(abv.buffer, 6, 5);
- expect(sliced.byteLength).to.equal(0);
- });
- });
-
- describe('using fallback', function() {
- it('should slice correctly with only start provided', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
- var ab = abv.buffer;
- ab.slice = undefined;
-
- var sliced = sliceBuffer(ab, 3);
- var sabv = new Uint8Array(sliced);
- for (var i = 3, ii = 0; i < abv.length; i++, ii++) {
- expect(abv[i]).to.equal(sabv[ii]);
- }
- });
-
- it('should slice correctly with start and end provided', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
- var ab = abv.buffer;
- ab.slice = undefined;
-
-
- var sliced = sliceBuffer(ab, 3, 8);
- var sabv = new Uint8Array(sliced);
- for (var i = 3, ii = 0; i < 8; i++, ii++) {
- expect(abv[i]).to.equal(sabv[ii]);
- }
- });
-
- it('should slice correctly with negative start', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
- var ab = abv.buffer;
- ab.slice = undefined;
-
-
- var sliced = sliceBuffer(ab, -3);
- var sabv = new Uint8Array(sliced);
- for (var i = abv.length - 3, ii = 0; i < abv.length; i++, ii++) {
- expect(abv[i]).to.equal(sabv[ii]);
- }
- });
-
- it('should slice correctly with negative end', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
- var ab = abv.buffer;
- ab.slice = undefined;
-
- var sliced = sliceBuffer(ab, 0, -3);
- var sabv = new Uint8Array(sliced);
- for (var i = 0, ii = 0; i < abv.length - 3; i++, ii++) {
- expect(abv[i]).to.equal(sabv[ii]);
- }
- });
-
- it('should slice correctly with negative start and end', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
- var ab = abv.buffer;
- ab.slice = undefined;
-
- var sliced = sliceBuffer(ab, -6, -3);
- var sabv = new Uint8Array(sliced);
- for (var i = abv.length - 6, ii = 0; i < abv.length - 3; i++, ii++) {
- expect(abv[i]).to.equal(sabv[ii]);
- }
- });
-
- it('should slice correctly with equal start and end', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
- var ab = abv.buffer;
- ab.slice = undefined;
-
- var sliced = sliceBuffer(ab, 1, 1);
- expect(sliced.byteLength).to.equal(0);
- });
-
- it('should slice correctly when end larger than buffer', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
- var ab = abv.buffer;
- ab.slice = undefined;
-
- var sliced = sliceBuffer(ab, 0, 100);
- var sabv = new Uint8Array(sliced);
- for (var i = 0; i < abv.length; i++) {
- expect(abv[i]).to.equal(sabv[i]);
- }
- });
-
- it('shoud slice correctly when start larger than end', function() {
- var abv = new Uint8Array(10);
- for (var i = 0; i < abv.length; i++) {
- abv[i] = i;
- }
- var ab = abv.buffer;
- ab.slice = undefined;
-
- var sliced = sliceBuffer(ab, 6, 5);
- expect(sliced.byteLength).to.equal(0);
- });
- });
- });
|