slice-buffer.js (6411B)
1 /* 2 * Test dependencies 3 */ 4 5 var sliceBuffer = require('../index.js'); 6 var expect = require('expect.js'); 7 8 /** 9 * Tests 10 */ 11 12 describe('sliceBuffer', function() { 13 describe('using standard slice', function() { 14 it('should slice correctly with only start provided', function() { 15 var abv = new Uint8Array(10); 16 for (var i = 0; i < abv.length; i++) { 17 abv[i] = i; 18 } 19 20 var sliced = sliceBuffer(abv.buffer, 3); 21 var sabv = new Uint8Array(sliced); 22 for (var i = 3, ii = 0; i < abv.length; i++, ii++) { 23 expect(abv[i]).to.equal(sabv[ii]); 24 } 25 }); 26 27 it('should slice correctly with start and end provided', function() { 28 var abv = new Uint8Array(10); 29 for (var i = 0; i < abv.length; i++) { 30 abv[i] = i; 31 } 32 33 var sliced = sliceBuffer(abv.buffer, 3, 8); 34 var sabv = new Uint8Array(sliced); 35 for (var i = 3, ii = 0; i < 8; i++, ii++) { 36 expect(abv[i]).to.equal(sabv[ii]); 37 } 38 }); 39 40 it('should slice correctly with negative start', function() { 41 var abv = new Uint8Array(10); 42 for (var i = 0; i < abv.length; i++) { 43 abv[i] = i; 44 } 45 46 var sliced = sliceBuffer(abv.buffer, -3); 47 var sabv = new Uint8Array(sliced); 48 for (var i = abv.length - 3, ii = 0; i < abv.length; i++, ii++) { 49 expect(abv[i]).to.equal(sabv[ii]); 50 } 51 }); 52 53 it('should slice correctly with negative end', function() { 54 var abv = new Uint8Array(10); 55 for (var i = 0; i < abv.length; i++) { 56 abv[i] = i; 57 } 58 59 var sliced = sliceBuffer(abv.buffer, 0, -3); 60 var sabv = new Uint8Array(sliced); 61 for (var i = 0, ii = 0; i < abv.length - 3; i++, ii++) { 62 expect(abv[i]).to.equal(sabv[ii]); 63 } 64 }); 65 66 it('should slice correctly with negative start and end', function() { 67 var abv = new Uint8Array(10); 68 for (var i = 0; i < abv.length; i++) { 69 abv[i] = i; 70 } 71 72 var sliced = sliceBuffer(abv.buffer, -6, -3); 73 var sabv = new Uint8Array(sliced); 74 for (var i = abv.length - 6, ii = 0; i < abv.length - 3; i++, ii++) { 75 expect(abv[i]).to.equal(sabv[ii]); 76 } 77 }); 78 79 it('should slice correctly with equal start and end', function() { 80 var abv = new Uint8Array(10); 81 for (var i = 0; i < abv.length; i++) { 82 abv[i] = i; 83 } 84 85 var sliced = sliceBuffer(abv.buffer, 1, 1); 86 expect(sliced.byteLength).to.equal(0); 87 }); 88 89 it('should slice correctly when end larger than buffer', function() { 90 var abv = new Uint8Array(10); 91 for (var i = 0; i < abv.length; i++) { 92 abv[i] = i; 93 } 94 95 var sliced = sliceBuffer(abv.buffer, 0, 100); 96 expect(new Uint8Array(sliced)).to.eql(abv); 97 }); 98 99 it('shoud slice correctly when start larger than end', function() { 100 var abv = new Uint8Array(10); 101 for (var i = 0; i < abv.length; i++) { 102 abv[i] = i; 103 } 104 105 var sliced = sliceBuffer(abv.buffer, 6, 5); 106 expect(sliced.byteLength).to.equal(0); 107 }); 108 }); 109 110 describe('using fallback', function() { 111 it('should slice correctly with only start provided', function() { 112 var abv = new Uint8Array(10); 113 for (var i = 0; i < abv.length; i++) { 114 abv[i] = i; 115 } 116 var ab = abv.buffer; 117 ab.slice = undefined; 118 119 var sliced = sliceBuffer(ab, 3); 120 var sabv = new Uint8Array(sliced); 121 for (var i = 3, ii = 0; i < abv.length; i++, ii++) { 122 expect(abv[i]).to.equal(sabv[ii]); 123 } 124 }); 125 126 it('should slice correctly with start and end provided', function() { 127 var abv = new Uint8Array(10); 128 for (var i = 0; i < abv.length; i++) { 129 abv[i] = i; 130 } 131 var ab = abv.buffer; 132 ab.slice = undefined; 133 134 135 var sliced = sliceBuffer(ab, 3, 8); 136 var sabv = new Uint8Array(sliced); 137 for (var i = 3, ii = 0; i < 8; i++, ii++) { 138 expect(abv[i]).to.equal(sabv[ii]); 139 } 140 }); 141 142 it('should slice correctly with negative start', function() { 143 var abv = new Uint8Array(10); 144 for (var i = 0; i < abv.length; i++) { 145 abv[i] = i; 146 } 147 var ab = abv.buffer; 148 ab.slice = undefined; 149 150 151 var sliced = sliceBuffer(ab, -3); 152 var sabv = new Uint8Array(sliced); 153 for (var i = abv.length - 3, ii = 0; i < abv.length; i++, ii++) { 154 expect(abv[i]).to.equal(sabv[ii]); 155 } 156 }); 157 158 it('should slice correctly with negative end', function() { 159 var abv = new Uint8Array(10); 160 for (var i = 0; i < abv.length; i++) { 161 abv[i] = i; 162 } 163 var ab = abv.buffer; 164 ab.slice = undefined; 165 166 var sliced = sliceBuffer(ab, 0, -3); 167 var sabv = new Uint8Array(sliced); 168 for (var i = 0, ii = 0; i < abv.length - 3; i++, ii++) { 169 expect(abv[i]).to.equal(sabv[ii]); 170 } 171 }); 172 173 it('should slice correctly with negative start and end', function() { 174 var abv = new Uint8Array(10); 175 for (var i = 0; i < abv.length; i++) { 176 abv[i] = i; 177 } 178 var ab = abv.buffer; 179 ab.slice = undefined; 180 181 var sliced = sliceBuffer(ab, -6, -3); 182 var sabv = new Uint8Array(sliced); 183 for (var i = abv.length - 6, ii = 0; i < abv.length - 3; i++, ii++) { 184 expect(abv[i]).to.equal(sabv[ii]); 185 } 186 }); 187 188 it('should slice correctly with equal start and end', function() { 189 var abv = new Uint8Array(10); 190 for (var i = 0; i < abv.length; i++) { 191 abv[i] = i; 192 } 193 var ab = abv.buffer; 194 ab.slice = undefined; 195 196 var sliced = sliceBuffer(ab, 1, 1); 197 expect(sliced.byteLength).to.equal(0); 198 }); 199 200 it('should slice correctly when end larger than buffer', function() { 201 var abv = new Uint8Array(10); 202 for (var i = 0; i < abv.length; i++) { 203 abv[i] = i; 204 } 205 var ab = abv.buffer; 206 ab.slice = undefined; 207 208 var sliced = sliceBuffer(ab, 0, 100); 209 var sabv = new Uint8Array(sliced); 210 for (var i = 0; i < abv.length; i++) { 211 expect(abv[i]).to.equal(sabv[i]); 212 } 213 }); 214 215 it('shoud slice correctly when start larger than end', function() { 216 var abv = new Uint8Array(10); 217 for (var i = 0; i < abv.length; i++) { 218 abv[i] = i; 219 } 220 var ab = abv.buffer; 221 ab.slice = undefined; 222 223 var sliced = sliceBuffer(ab, 6, 5); 224 expect(sliced.byteLength).to.equal(0); 225 }); 226 }); 227 });