You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
9.7 KiB

4 years ago
  1. var assert = require('assert');
  2. var WriteBuffer = require('../');
  3. describe('WriteBuffer', function() {
  4. var w;
  5. beforeEach(function() {
  6. w = new WriteBuffer();
  7. });
  8. function join(arr) {
  9. return arr.map(function(buf) {
  10. return buf.toString('hex');
  11. }).join('');
  12. }
  13. describe('.writeUInt8', function() {
  14. it('should write bytes', function() {
  15. w.writeUInt8(1);
  16. w.writeUInt8(2);
  17. w.writeUInt8(3);
  18. w.writeUInt8(4);
  19. assert.equal(join(w.render()), '01020304');
  20. });
  21. it('should correctly handle overflow', function() {
  22. w.reserve(3);
  23. w.writeUInt8(1);
  24. w.writeUInt8(2);
  25. w.writeUInt8(3);
  26. w.writeUInt8(4);
  27. assert.equal(join(w.render()), '01020304');
  28. });
  29. });
  30. describe('.writeInt8', function() {
  31. it('should write bytes', function() {
  32. w.writeInt8(-1);
  33. w.writeInt8(2);
  34. assert.equal(join(w.render()), 'ff02');
  35. });
  36. });
  37. describe('.writeUInt16BE', function() {
  38. it('should write bytes', function() {
  39. w.writeUInt16BE(0x0102);
  40. w.writeUInt16BE(0x0304);
  41. assert.equal(join(w.render()), '01020304');
  42. });
  43. it('should correctly handle overflow', function() {
  44. w.reserve(2);
  45. w.reserve(3);
  46. w.writeUInt16BE(0x0102);
  47. w.writeUInt16BE(0x0304);
  48. w.writeUInt16BE(0x0506);
  49. assert.equal(join(w.render()), '010203040506');
  50. });
  51. });
  52. describe('.writeInt16BE', function() {
  53. it('should write bytes', function() {
  54. w.writeInt16BE(-0x0102);
  55. w.writeInt16BE(0x0304);
  56. assert.equal(join(w.render()), 'fefe0304');
  57. });
  58. });
  59. describe('.writeUInt16LE', function() {
  60. it('should write bytes', function() {
  61. w.writeUInt16LE(0x0102);
  62. w.writeUInt16LE(0x0304);
  63. assert.equal(join(w.render()), '02010403');
  64. });
  65. it('should correctly handle overflow', function() {
  66. w.reserve(2);
  67. w.reserve(3);
  68. w.writeUInt16LE(0x0102);
  69. w.writeUInt16LE(0x0304);
  70. w.writeUInt16LE(0x0506);
  71. assert.equal(join(w.render()), '020104030605');
  72. });
  73. });
  74. describe('.writeInt16LE', function() {
  75. it('should write bytes', function() {
  76. w.writeInt16LE(-0x0201);
  77. w.writeInt16LE(0x0304);
  78. assert.equal(join(w.render()), 'fffd0403');
  79. });
  80. });
  81. describe('.writeUInt24BE', function() {
  82. it('should write bytes', function() {
  83. w.writeUInt24BE(0x010203);
  84. w.writeUInt24BE(0x040506);
  85. assert.equal(join(w.render()), '010203040506');
  86. });
  87. it('should correctly set avail on boundary', function() {
  88. w = new WriteBuffer();
  89. w.reserveRate = 4;
  90. w.writeUInt16BE(1);
  91. w.writeUInt24BE(1);
  92. assert.equal(w.avail, 3);
  93. });
  94. });
  95. describe('.writeInt24BE', function() {
  96. it('should write bytes', function() {
  97. w.writeInt24BE(-0x010203);
  98. w.writeInt24BE(0x040506);
  99. assert.equal(join(w.render()), 'fefdfd040506');
  100. });
  101. });
  102. describe('.writeUInt24LE', function() {
  103. it('should write bytes', function() {
  104. w.writeUInt24LE(0x010203);
  105. w.writeUInt24LE(0x040506);
  106. assert.equal(join(w.render()), '030201060504');
  107. });
  108. });
  109. describe('.writeInt24LE', function() {
  110. it('should write bytes', function() {
  111. w.writeInt24LE(-0x010203);
  112. w.writeInt24LE(0x040506);
  113. assert.equal(join(w.render()), 'fdfdfe060504');
  114. });
  115. });
  116. describe('.writeUInt32BE', function() {
  117. it('should write bytes', function() {
  118. w.writeUInt32BE(0x01020304);
  119. w.writeUInt32BE(0x05060708);
  120. assert.equal(join(w.render()), '0102030405060708');
  121. });
  122. it('should write bytes on the boundary', function() {
  123. w.reserve(4);
  124. w.writeUInt8(0x00);
  125. w.writeUInt32BE(0x01020304);
  126. assert.equal(join(w.render()), '0001020304');
  127. });
  128. });
  129. describe('.writeInt32BE', function() {
  130. it('should write bytes', function() {
  131. w.writeInt32BE(-0x01020304);
  132. w.writeInt32BE(0x05060708);
  133. assert.equal(join(w.render()), 'fefdfcfc05060708');
  134. });
  135. });
  136. describe('.writeUInt32LE', function() {
  137. it('should write bytes', function() {
  138. w.writeUInt32LE(0x01020304);
  139. w.writeUInt32LE(0x05060708);
  140. assert.equal(join(w.render()), '0403020108070605');
  141. });
  142. it('should write max uint32 value', function() {
  143. w.writeUInt32LE(0xffffffff);
  144. assert.equal(join(w.render()), 'ffffffff');
  145. });
  146. });
  147. describe('.combWrite', function() {
  148. it('should write bytes', function() {
  149. w.writeComb(1, 'le', 0x01);
  150. w.writeComb(1, 'be', 0x02);
  151. w.writeComb(2, 'le', 0x0102);
  152. w.writeComb(2, 'be', 0x0304);
  153. w.writeComb(3, 'le', 0x010203);
  154. w.writeComb(3, 'be', 0x040506);
  155. w.writeComb(4, 'le', 0x01020304);
  156. w.writeComb(4, 'be', 0x05060708);
  157. assert.equal(join(w.render()),
  158. '0102020103040302010405060403020105060708');
  159. });
  160. it('should write max uint32 value', function() {
  161. w.writeUInt32LE(0xffffffff);
  162. assert.equal(join(w.render()), 'ffffffff');
  163. });
  164. });
  165. describe('.writeInt32LE', function() {
  166. it('should write bytes', function() {
  167. w.writeInt32LE(-0x01020304);
  168. w.writeInt32LE(0x05060708);
  169. assert.equal(join(w.render()), 'fcfcfdfe08070605');
  170. });
  171. });
  172. describe('.skip', function() {
  173. it('should skip bytes', function() {
  174. w.skip(4);
  175. w.writeUInt32BE(0xdeadbeef);
  176. assert(/^.{8}deadbeef$/.test(join(w.render())));
  177. });
  178. it('should skip 0 bytes', function() {
  179. var skip = w.skip(0);
  180. assert.equal(skip.size, 0);
  181. w.writeUInt32BE(0xdeadbeef);
  182. assert(/^deadbeef$/.test(join(w.render())));
  183. });
  184. it('should skip bytes on the boundary', function() {
  185. w.reserve(4);
  186. w.writeUInt8(0x01);
  187. var skip = w.skip(4);
  188. w.writeUInt32BE(0xdeadbeef);
  189. skip.writeUInt32BE(0xabbabaab);
  190. assert(/^01abbabaabdeadbeef$/.test(join(w.render())));
  191. });
  192. it('should skip bytes on the boundary in two chunks', function() {
  193. w.reserve(4);
  194. var skip1 = w.skip(2);
  195. var skip2 = w.skip(2);
  196. w.writeUInt32BE(0xdeadbeef);
  197. skip1.writeUInt16BE(0xabba);
  198. skip2.writeUInt16BE(0xbaba);
  199. assert(/^abbababadeadbeef$/.test(join(w.render())));
  200. });
  201. });
  202. describe('.slice', function() {
  203. it('should return empty slice', function() {
  204. w.writeUInt32BE(0xabbadead);
  205. assert.equal(join(w.slice(4, 4).render()), '');
  206. assert.equal(join(w.render()), 'abbadead');
  207. });
  208. it('should return full slice', function() {
  209. w.writeUInt32BE(0xabbadead);
  210. var slice = w.slice(0, 4);
  211. slice.writeUInt32BE(0xdeadbeef);
  212. assert.equal(join(slice.render()), 'deadbeef');
  213. assert.equal(join(w.render()), 'deadbeef');
  214. });
  215. it('should return partial slice', function() {
  216. w.writeUInt32BE(0xabbadead);
  217. var slice = w.slice(0, 3);
  218. slice.writeUInt24BE(0xdeadbe);
  219. assert.equal(join(slice.render()), 'deadbe');
  220. assert.equal(join(w.render()), 'deadbead');
  221. });
  222. it('should return over-the-boundary slice', function() {
  223. for (var i = 0; i < 16; i++) {
  224. w.reserve(3);
  225. w.writeUInt24BE(i);
  226. }
  227. assert.equal(join(w.render()),
  228. '000000000001000002000003000004000005000006000007' +
  229. '00000800000900000a00000b00000c00000d00000e00000f');
  230. var slice = w.slice(5, 12);
  231. slice.writeUInt24BE(0xaaabac);
  232. slice.writeUInt24BE(0xbabbbc);
  233. slice.writeUInt8(0xcc);
  234. assert.equal(join(slice.render()), 'aaabacbabbbccc');
  235. assert.equal(join(w.render()),
  236. '0000000000aaabacbabbbccc000004000005000006000007' +
  237. '00000800000900000a00000b00000c00000d00000e00000f');
  238. });
  239. });
  240. describe('.copyFrom', function() {
  241. it('should copy bytes', function() {
  242. var tmp = new Buffer(128);
  243. for (var i = 0; i < tmp.length; i++)
  244. tmp[i] = i;
  245. w.writeUInt32BE(0xdeadbeef);
  246. w.copyFrom(tmp);
  247. w.writeUInt32BE(0xabbadead);
  248. assert.equal(
  249. join(w.render()),
  250. 'deadbeef000102030405060708090a0b0c0d0e0f101112131415161718191a1b' +
  251. '1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b' +
  252. '3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b' +
  253. '5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b' +
  254. '7c7d7e7fabbadead');
  255. });
  256. it('should copy bytes using offset', function() {
  257. var tmp = new Buffer(128);
  258. for (var i = 0; i < tmp.length; i++)
  259. tmp[i] = i;
  260. w.writeUInt32BE(0xdeadbeef);
  261. w.copyFrom(tmp, 10, 12);
  262. w.writeUInt32BE(0xabbadead);
  263. assert.equal(
  264. join(w.render()),
  265. 'deadbeef0a0babbadead');
  266. });
  267. });
  268. describe('.write', function() {
  269. it('should write utf8 string', function() {
  270. w.writeUInt32BE(0xdeadbeef);
  271. w.write('ohai\u1023');
  272. w.writeUInt32BE(0xabbadead);
  273. assert.equal(
  274. join(w.render()),
  275. 'deadbeef' +
  276. '6f6861691023' +
  277. 'abbadead');
  278. });
  279. it('should copy bytes using offset', function() {
  280. var tmp = new Buffer(128);
  281. for (var i = 0; i < tmp.length; i++)
  282. tmp[i] = i;
  283. w.writeUInt32BE(0xdeadbeef);
  284. w.copyFrom(tmp, 10, 12);
  285. w.writeUInt32BE(0xabbadead);
  286. assert.equal(
  287. join(w.render()),
  288. 'deadbeef0a0babbadead');
  289. });
  290. });
  291. describe('.skip', function() {
  292. it('should copy bytes', function() {
  293. w.reserve(5);
  294. var h = w.skip(4);
  295. w.writeUInt32BE(0xabbadead);
  296. h.writeUInt32BE(0xdeadbeef);
  297. assert.equal(
  298. join(w.render()),
  299. 'deadbeefabbadead');
  300. });
  301. });
  302. describe('.forceReserve = true', function() {
  303. it('should allocate more bytes', function() {
  304. w.forceReserve = true;
  305. w.reserve(4);
  306. w.writeUInt32BE(0xabbadead);
  307. w.writeUInt32BE(0xabbadead);
  308. assert.equal(w.render().length, 1);
  309. });
  310. });
  311. });