buffers.js 6.4 KB

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