slice-buffer.js 6.3 KB

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