/sustentacion_2/repositorio/kafka/node_modules/no-kafka/node_modules/bin-protocol/test/pb_types.js

https://gitlab.com/disruptivo/uniandes · JavaScript · 309 lines · 254 code · 54 blank · 1 comment · 0 complexity · ef55e256a85527620216b7c1400a4702 MD5 · raw file

  1. 'use strict';
  2. /* global describe, it, expect */
  3. var Protocol = require('../lib/index');
  4. var Long = require('long');
  5. var ProtobufProtocol = Protocol.createProtobufProtocol();
  6. var protocol = new ProtobufProtocol();
  7. describe('Protocol buffers types', function () {
  8. var MAX_UINT32 = Math.pow(2, 32) - 1, MIN_UINT32 = 0,
  9. MAX_INT32 = 2147483647, MIN_INT32 = -2147483648,
  10. MAX_UINT64 = Long.MAX_UNSIGNED_VALUE, MIN_UINT64 = Long.UZERO,
  11. MAX_INT64 = Long.MAX_VALUE, MIN_INT64 = Long.MIN_VALUE;
  12. describe('Base 128 Varints', function () {
  13. it('uint32 max', function () {
  14. var encoded = protocol.write().uint32(MAX_UINT32).result;
  15. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0x0f]));
  16. protocol.read(encoded).uint32('v').result.v.should.be.eql(MAX_UINT32);
  17. });
  18. it('uint32 min', function () {
  19. var encoded = protocol.write().uint32(MIN_UINT32).result;
  20. encoded.should.be.eql(new Buffer([0x00]));
  21. protocol.read(encoded).uint32('v').result.v.should.be.eql(MIN_UINT32);
  22. });
  23. it('bool', function () {
  24. var encoded = protocol.write().bool(true).result;
  25. encoded.should.be.eql(new Buffer([0x01]));
  26. protocol.read(encoded).bool('v').result.v.should.be.eql(true);
  27. protocol.writer.reset();
  28. encoded = protocol.write().bool(false).result;
  29. encoded.should.be.eql(new Buffer([0x00]));
  30. protocol.read(encoded).bool('v').result.v.should.be.eql(false);
  31. });
  32. it('int32 max', function () {
  33. var encoded = protocol.write().int32(MAX_INT32).result;
  34. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0x07]));
  35. protocol.read(encoded).int32('v').result.v.should.be.eql(MAX_INT32);
  36. });
  37. it('int32 -1', function () {
  38. var encoded = protocol.write().int32(-1).result;
  39. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01]));
  40. protocol.read(encoded).int32('v').result.v.should.be.eql(-1);
  41. });
  42. it('int32 1', function () {
  43. var encoded = protocol.write().int32(1).result;
  44. encoded.should.be.eql(new Buffer([0x01]));
  45. protocol.read(encoded).int32('v').result.v.should.be.eql(1);
  46. });
  47. it('int32 min', function () {
  48. var encoded = protocol.write().int32(MIN_INT32).result;
  49. encoded.should.be.eql(new Buffer([0x80, 0x80, 0x80, 0x80, 0xf8, 0xff, 0xff, 0xff, 0xff, 0x01]));
  50. protocol.read(encoded).int32('v').result.v.should.be.eql(MIN_INT32);
  51. });
  52. it('sint32 max', function () {
  53. var encoded = protocol.write().sint32(MAX_INT32).result;
  54. encoded.should.be.eql(new Buffer([0xfe, 0xff, 0xff, 0xff, 0x0f]));
  55. protocol.read(encoded).sint32('v').result.v.should.be.eql(MAX_INT32);
  56. });
  57. it('sint32 -1', function () {
  58. var encoded = protocol.write().sint32(-1).result;
  59. encoded.should.be.eql(new Buffer([0x01]));
  60. protocol.read(encoded).sint32('v').result.v.should.be.eql(-1);
  61. });
  62. it('sint32 1', function () {
  63. var encoded = protocol.write().sint32(1).result;
  64. encoded.should.be.eql(new Buffer([0x02]));
  65. protocol.read(encoded).sint32('v').result.v.should.be.eql(1);
  66. });
  67. it('sint32 min', function () {
  68. var encoded = protocol.write().sint32(MIN_INT32).result;
  69. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0x0f]));
  70. protocol.read(encoded).sint32('v').result.v.should.be.eql(MIN_INT32);
  71. });
  72. it('uint64 max', function () {
  73. var encoded = protocol.write().uint64(MAX_UINT64).result;
  74. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01]));
  75. protocol.read(encoded).uint64('v').result.v.should.be.eql(MAX_UINT64);
  76. });
  77. it('uint64 min', function () {
  78. var encoded = protocol.write().uint64(MIN_UINT64).result;
  79. encoded.should.be.eql(new Buffer([0x00]));
  80. protocol.read(encoded).uint64('v').result.v.should.be.eql(MIN_UINT64);
  81. });
  82. it('int64 max', function () {
  83. var encoded = protocol.write().int64(MAX_INT64).result;
  84. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]));
  85. protocol.read(encoded).int64('v').result.v.should.be.eql(MAX_INT64);
  86. });
  87. it('int64 -1', function () {
  88. var encoded = protocol.write().int64(Long.NEG_ONE).result;
  89. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01]));
  90. protocol.read(encoded).int64('v').result.v.should.be.eql(Long.NEG_ONE);
  91. });
  92. it('int64 1', function () {
  93. var encoded = protocol.write().int64(Long.ONE).result;
  94. encoded.should.be.eql(new Buffer([0x01]));
  95. protocol.read(encoded).int64('v').result.v.should.be.eql(Long.ONE);
  96. });
  97. it('int64 min', function () {
  98. var encoded = protocol.write().int64(MIN_INT64).result;
  99. encoded.should.be.eql(new Buffer([0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01]));
  100. protocol.read(encoded).int64('v').result.v.should.be.eql(MIN_INT64);
  101. });
  102. it('sint64 max', function () {
  103. var encoded = protocol.write().sint64(MAX_INT64).result;
  104. encoded.should.be.eql(new Buffer([0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01]));
  105. protocol.read(encoded).sint64('v').result.v.should.be.eql(MAX_INT64);
  106. });
  107. it('sint64 -1', function () {
  108. var encoded = protocol.write().sint64(Long.NEG_ONE).result;
  109. encoded.should.be.eql(new Buffer([0x01]));
  110. protocol.read(encoded).sint64('v').result.v.should.be.eql(Long.NEG_ONE);
  111. });
  112. it('sint64 1', function () {
  113. var encoded = protocol.write().sint64(Long.ONE).result;
  114. encoded.should.be.eql(new Buffer([0x02]));
  115. protocol.read(encoded).sint64('v').result.v.should.be.eql(Long.ONE);
  116. });
  117. it('sint64 min', function () {
  118. var encoded = protocol.write().sint64(MIN_INT64).result;
  119. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01]));
  120. protocol.read(encoded).sint64('v').result.v.should.be.eql(MIN_INT64);
  121. });
  122. });
  123. describe('Fixed length numeric', function () {
  124. it('fixed32 max', function () {
  125. var encoded = protocol.write().fixed32(MAX_UINT32).result;
  126. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff]));
  127. protocol.read(encoded).fixed32('v').result.v.should.be.eql(MAX_UINT32);
  128. });
  129. it('fixed32 min', function () {
  130. var encoded = protocol.write().fixed32(MIN_UINT32).result;
  131. encoded.should.be.eql(new Buffer([0x00, 0x00, 0x00, 0x00]));
  132. protocol.read(encoded).fixed32('v').result.v.should.be.eql(MIN_UINT32);
  133. });
  134. it('sfixed32 max', function () {
  135. var encoded = protocol.write().sfixed32(MAX_INT32).result;
  136. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0x7f]));
  137. protocol.read(encoded).sfixed32('v').result.v.should.be.eql(MAX_INT32);
  138. });
  139. it('sfixed32 -1', function () {
  140. var encoded = protocol.write().sfixed32(-1).result;
  141. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff]));
  142. protocol.read(encoded).sfixed32('v').result.v.should.be.eql(-1);
  143. });
  144. it('sfixed32 1', function () {
  145. var encoded = protocol.write().sfixed32(1).result;
  146. encoded.should.be.eql(new Buffer([0x01, 0x00, 0x00, 0x00]));
  147. protocol.read(encoded).sfixed32('v').result.v.should.be.eql(1);
  148. });
  149. it('sfixed32 min', function () {
  150. var encoded = protocol.write().sfixed32(MIN_INT32).result;
  151. encoded.should.be.eql(new Buffer([0x00, 0x00, 0x00, 0x80]));
  152. protocol.read(encoded).sfixed32('v').result.v.should.be.eql(MIN_INT32);
  153. });
  154. it('float max', function () {
  155. var encoded = protocol.write().float(+Infinity).result;
  156. encoded.should.be.eql(new Buffer([0x00, 0x00, 0x80, 0x7f]));
  157. protocol.read(encoded).float('v').result.v.should.be.eql(+Infinity);
  158. });
  159. it('float 1', function () {
  160. var encoded = protocol.write().float(1).result;
  161. encoded.should.be.eql(new Buffer([0x00, 0x00, 0x80, 0x3f]));
  162. protocol.read(encoded).float('v').result.v.should.be.eql(1);
  163. });
  164. it('float -1', function () {
  165. var encoded = protocol.write().float(-1).result;
  166. encoded.should.be.eql(new Buffer([0x00, 0x00, 0x80, 0xbf]));
  167. protocol.read(encoded).float('v').result.v.should.be.eql(-1);
  168. });
  169. it('float min', function () {
  170. var encoded = protocol.write().float(-Infinity).result;
  171. encoded.should.be.eql(new Buffer([0x00, 0x00, 0x80, 0xff]));
  172. protocol.read(encoded).float('v').result.v.should.be.eql(-Infinity);
  173. });
  174. it('fixed64 max', function () {
  175. var encoded = protocol.write().fixed64(MAX_UINT64).result;
  176. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]));
  177. protocol.read(encoded).fixed64('v').result.v.should.be.eql(MAX_UINT64);
  178. });
  179. it('fixed64 min', function () {
  180. var encoded = protocol.write().fixed64(MIN_UINT64).result;
  181. encoded.should.be.eql(new Buffer([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]));
  182. protocol.read(encoded).fixed64('v').result.v.should.be.eql(MIN_UINT64);
  183. });
  184. it('double max', function () {
  185. var encoded = protocol.write().double(Number.MAX_VALUE).result;
  186. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f]));
  187. protocol.read(encoded).double('v').result.v.should.be.eql(Number.MAX_VALUE);
  188. });
  189. it('double 1', function () {
  190. var encoded = protocol.write().double(1).result;
  191. encoded.should.be.eql(new Buffer([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f]));
  192. protocol.read(encoded).double('v').result.v.should.be.eql(1);
  193. });
  194. it('double -1', function () {
  195. var encoded = protocol.write().double(-1).result;
  196. encoded.should.be.eql(new Buffer([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xbf]));
  197. protocol.read(encoded).double('v').result.v.should.be.eql(-1);
  198. });
  199. it('double min', function () {
  200. var encoded = protocol.write().double(Number.MIN_VALUE).result;
  201. encoded.should.be.eql(new Buffer([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]));
  202. protocol.read(encoded).double('v').result.v.should.be.eql(Number.MIN_VALUE);
  203. });
  204. it('sfixed64 max', function () {
  205. var encoded = protocol.write().sfixed64(MAX_INT64).result;
  206. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]));
  207. protocol.read(encoded).sfixed64('v').result.v.should.be.eql(MAX_INT64);
  208. });
  209. it('sfixed64 -1', function () {
  210. var encoded = protocol.write().sfixed64(Long.NEG_ONE).result;
  211. encoded.should.be.eql(new Buffer([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]));
  212. protocol.read(encoded).sfixed64('v').result.v.should.be.eql(Long.NEG_ONE);
  213. });
  214. it('sfixed64 1', function () {
  215. var encoded = protocol.write().sfixed64(Long.ONE).result;
  216. encoded.should.be.eql(new Buffer([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]));
  217. protocol.read(encoded).sfixed64('v').result.v.should.be.eql(Long.ONE);
  218. });
  219. it('sfixed64 min', function () {
  220. var encoded = protocol.write().sfixed64(MIN_INT64).result;
  221. encoded.should.be.eql(new Buffer([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]));
  222. protocol.read(encoded).sfixed64('v').result.v.should.be.eql(MIN_INT64);
  223. });
  224. });
  225. it('bytes', function () {
  226. var buf = new Buffer('abcde');
  227. var encoded = protocol.write().bytes(buf).result;
  228. encoded.should.be.eql(new Buffer([0x05, 0x61, 0x62, 0x63, 0x64, 0x65]));
  229. protocol.read(encoded).bytes('v').result.v.should.be.eql(buf);
  230. });
  231. it('bytes - UTF8 string', function () {
  232. var str = '人人生而自由,在尊嚴和權利上一律平等。';
  233. var encoded = protocol.write().bytes(str).result;
  234. protocol.read(encoded).bytes().result.toString('utf8').should.be.eql(str);
  235. });
  236. it('bytes - zero length', function () {
  237. var buf = new Buffer(0);
  238. var encoded = protocol.write().bytes(buf).result;
  239. encoded.should.be.eql(new Buffer([0x00]));
  240. expect(protocol.read(encoded).bytes('v').result.v).to.be.eql(null);
  241. });
  242. it('bytes - null', function () {
  243. var encoded = protocol.write().bytes(null).result;
  244. encoded.should.be.eql(new Buffer(0));
  245. });
  246. it('bytes - not a buffer', function () {
  247. function f() {return protocol.write().bytes(123).result; }
  248. expect(f).to.throw(Error);
  249. });
  250. it('string', function () {
  251. var str = '人人生而自由,在尊嚴和權利上一律平等。';
  252. var encoded = protocol.write().string(str).result;
  253. protocol.read(encoded).string('v').result.v.should.be.eql(str);
  254. });
  255. });