buffer-test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. var assert = require('assert');
  2. var OffsetBuffer = require('../');
  3. describe('OffsetBuffer', function() {
  4. var o;
  5. beforeEach(function() {
  6. o = new OffsetBuffer();
  7. });
  8. describe('.take()', function() {
  9. it('should return empty buffer', function() {
  10. var b = new Buffer('hello world');
  11. o.push(b);
  12. var r = o.take(0);
  13. assert.equal(r.length, 0);
  14. assert.equal(o.size, b.length);
  15. });
  16. it('should return the first buffer itself', function() {
  17. var b = new Buffer('hello world');
  18. o.push(b);
  19. var r = o.take(b.length);
  20. assert(r === b);
  21. assert(o.isEmpty());
  22. });
  23. it('should return the slice of the buffer ', function() {
  24. var b = new Buffer('hello world');
  25. o.push(b);
  26. assert.equal(o.take(5).toString(), 'hello');
  27. assert.equal(o.take(1).toString(), ' ');
  28. assert.equal(o.take(5).toString(), 'world');
  29. assert(o.isEmpty());
  30. });
  31. it('should concat buffers', function() {
  32. o.push(new Buffer('hello'));
  33. o.push(new Buffer(' '));
  34. o.push(new Buffer('world!'));
  35. assert.equal(o.take(11).toString(), 'hello world');
  36. assert.equal(o.take(1).toString(), '!');
  37. assert(o.isEmpty());
  38. });
  39. });
  40. describe('.skip', function() {
  41. it('should skip bytes', function() {
  42. o.push(new Buffer('hello '));
  43. o.push(new Buffer('world'));
  44. o.push(new Buffer(' oh gosh'));
  45. assert.equal(o.take(2).toString(), 'he');
  46. o.skip(1);
  47. assert.equal(o.take(2).toString(), 'lo');
  48. o.skip(1);
  49. assert.equal(o.take(2).toString(), 'wo');
  50. o.skip(4);
  51. assert.equal(o.take(7).toString(), 'oh gosh');
  52. assert(o.isEmpty());
  53. });
  54. });
  55. describe('.peekUInt8', function() {
  56. it('should return and not move by one byte', function() {
  57. o.push(new Buffer([ 0x1, 0x2 ]));
  58. assert.equal(o.peekUInt8(), 1);
  59. assert.equal(o.readUInt8(), 1);
  60. assert.equal(o.peekUInt8(), 2);
  61. assert.equal(o.readUInt8(), 2);
  62. assert(o.isEmpty());
  63. });
  64. });
  65. describe('.peekInt8', function() {
  66. it('should return signed number', function() {
  67. o.push(new Buffer([ 0x80 ]));
  68. assert.equal(o.peekInt8(), -128);
  69. assert.equal(o.readInt8(), -128);
  70. assert(o.isEmpty());
  71. });
  72. });
  73. describe('.readUInt8', function() {
  74. it('should return and move by one byte', function() {
  75. o.push(new Buffer([ 0x1, 0x2 ]));
  76. o.push(new Buffer([ 0x3, 0x4 ]));
  77. assert.equal(o.readUInt8(), 1);
  78. assert.equal(o.readUInt8(), 2);
  79. assert.equal(o.readUInt8(), 3);
  80. assert.equal(o.readUInt8(), 4);
  81. assert(o.isEmpty());
  82. });
  83. });
  84. describe('.readInt8', function() {
  85. it('should return signed number', function() {
  86. o.push(new Buffer([ 0x8f, 0x7f ]));
  87. assert.equal(o.readInt8(), -113);
  88. assert.equal(o.readInt8(), 127);
  89. assert(o.isEmpty());
  90. });
  91. });
  92. describe('.readUInt16LE', function() {
  93. it('should return and move by two bytes', function() {
  94. o.push(new Buffer([ 0x1, 0x2, 0x3 ]));
  95. o.push(new Buffer([ 0x4, 0x5, 0x6 ]));
  96. assert.equal(o.readUInt16LE(), 0x0201);
  97. assert.equal(o.readUInt16LE(), 0x0403);
  98. assert.equal(o.readUInt16LE(), 0x0605);
  99. assert(o.isEmpty());
  100. });
  101. it('should return and move by two bytes (regression #1)', function() {
  102. o.push(new Buffer([ 0x1 ]));
  103. o.push(new Buffer([ 0x2, 0x3, 0x4 ]));
  104. assert.equal(o.readUInt16LE(), 0x0201);
  105. assert.equal(o.readUInt16LE(), 0x0403);
  106. assert(o.isEmpty());
  107. });
  108. });
  109. describe('.readInt16LE', function() {
  110. it('should return signed number', function() {
  111. o.push(new Buffer([ 0x23, 0x81 ]));
  112. assert.equal(o.readInt16LE(), -32477);
  113. assert(o.isEmpty());
  114. });
  115. });
  116. describe('.readUInt24LE', function() {
  117. it('should return and move by three bytes', function() {
  118. o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5 ]));
  119. o.push(new Buffer([ 0x6, 0x7 ]));
  120. o.push(new Buffer([ 0x8, 0x9 ]));
  121. assert.equal(o.readUInt24LE(), 0x030201);
  122. assert.equal(o.readUInt24LE(), 0x060504);
  123. assert.equal(o.readUInt24LE(), 0x090807);
  124. assert(o.isEmpty());
  125. });
  126. it('should return and move by three bytes (regression #1)', function() {
  127. o.push(new Buffer([ 0x1, 0x2 ]));
  128. o.push(new Buffer([ 0x3 ]));
  129. assert.equal(o.readUInt24LE(), 0x030201);
  130. assert.equal(o.buffers.length, 0);
  131. assert(o.isEmpty());
  132. });
  133. });
  134. describe('.readInt24LE', function() {
  135. it('should return signed number', function() {
  136. o.push(new Buffer([ 0x23, 0x45, 0x81 ]));
  137. assert.equal(o.readInt24LE(), -8305373);
  138. assert(o.isEmpty());
  139. });
  140. });
  141. describe('.readUInt32LE', function() {
  142. it('should return and move by four bytes', function() {
  143. o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 ]));
  144. o.push(new Buffer([ 0x8, 0x9, 0xa ]));
  145. o.push(new Buffer([ 0xb, 0xc, 0xd ]));
  146. o.push(new Buffer([ 0xe, 0xf, 0x10 ]));
  147. assert.equal(o.readUInt32LE(), 0x04030201);
  148. assert.equal(o.readUInt32LE(), 0x08070605);
  149. assert.equal(o.readUInt32LE(), 0x0c0b0a09);
  150. assert.equal(o.readUInt32LE(), 0x100f0e0d);
  151. assert(o.isEmpty());
  152. });
  153. it('should return and move by four bytes (regression #1)', function() {
  154. o.push(new Buffer([ 0x1, 0x2, 0x3 ]));
  155. o.push(new Buffer([ 0x4 ]));
  156. assert.equal(o.readUInt32LE(), 0x04030201);
  157. assert.equal(o.buffers.length, 0);
  158. assert(o.isEmpty());
  159. });
  160. });
  161. describe('.readInt32LE', function() {
  162. it('should return signed number', function() {
  163. o.push(new Buffer([ 0xff, 0xff, 0xff, 0xff ]));
  164. assert.equal(o.readInt32LE(), -1);
  165. assert(o.isEmpty());
  166. });
  167. });
  168. describe('.readUInt16BE', function() {
  169. it('should return and move by two bytes', function() {
  170. o.push(new Buffer([ 0x1, 0x2, 0x3 ]));
  171. o.push(new Buffer([ 0x4, 0x5, 0x6 ]));
  172. assert.equal(o.readUInt16BE(), 0x0102);
  173. assert.equal(o.readUInt16BE(), 0x0304);
  174. assert.equal(o.readUInt16BE(), 0x0506);
  175. assert(o.isEmpty());
  176. });
  177. });
  178. describe('.readInt16BE', function() {
  179. it('should return signed number', function() {
  180. o.push(new Buffer([ 0x81, 0x23 ]));
  181. assert.equal(o.readInt16BE(), -32477);
  182. assert(o.isEmpty());
  183. });
  184. });
  185. describe('.readUInt24BE', function() {
  186. it('should return and move by three bytes', function() {
  187. o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5 ]));
  188. o.push(new Buffer([ 0x6, 0x7 ]));
  189. o.push(new Buffer([ 0x8, 0x9 ]));
  190. assert.equal(o.readUInt24BE(), 0x010203);
  191. assert.equal(o.readUInt24BE(), 0x040506);
  192. assert.equal(o.readUInt24BE(), 0x070809);
  193. assert(o.isEmpty());
  194. });
  195. });
  196. describe('.readInt24BE', function() {
  197. it('should return signed number', function() {
  198. o.push(new Buffer([ 0x81, 0x45, 0x23 ]));
  199. assert.equal(o.readInt24BE(), -8305373);
  200. assert(o.isEmpty());
  201. });
  202. });
  203. describe('.readUInt32BE', function() {
  204. it('should return and move by four bytes', function() {
  205. o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 ]));
  206. o.push(new Buffer([ 0x8, 0x9, 0xa ]));
  207. o.push(new Buffer([ 0xb, 0xc, 0xd ]));
  208. o.push(new Buffer([ 0xe, 0xf, 0x10 ]));
  209. assert.equal(o.readUInt32BE(), 0x01020304);
  210. assert.equal(o.readUInt32BE(), 0x05060708);
  211. assert.equal(o.readUInt32BE(), 0x090a0b0c);
  212. assert.equal(o.readUInt32BE(), 0x0d0e0f10);
  213. assert(o.isEmpty());
  214. });
  215. it('should return positive values', function() {
  216. o.push(new Buffer([ 0xff, 0xff, 0xff, 0xff ]));
  217. assert.equal(o.readUInt32BE(), 0xffffffff);
  218. assert(o.isEmpty());
  219. });
  220. });
  221. describe('.readInt32BE', function() {
  222. it('should return signed number', function() {
  223. o.push(new Buffer([ 0xff, 0xff, 0xff, 0xff ]));
  224. assert.equal(o.readInt32BE(), -1);
  225. assert(o.isEmpty());
  226. });
  227. });
  228. describe('.has', function() {
  229. it('should properly check the amount of the remaining bytes', function() {
  230. o.push(new Buffer([ 1, 2, 3 ]));
  231. assert(o.has(3));
  232. assert.equal(o.readUInt8(), 0x01);
  233. assert(!o.has(3));
  234. assert(o.has(2));
  235. assert.equal(o.readUInt16BE(), 0x0203);
  236. assert(!o.has(1));
  237. });
  238. });
  239. });