readBuffer.spec.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const Helpers = require("../../gramjs/Helpers");
  2. describe("readBufferFromBigInt 8 bytes function", () => {
  3. test("it should return 0x20a13b25e1726bfc", () => {
  4. const input = BigInt("-257986242325798624");
  5. const output = Buffer.from("20a13b25e1726bfc", "hex");
  6. expect(Helpers.readBufferFromBigInt(input, 8, true, true)).toEqual(output);
  7. });
  8. test("it should return 0xe05ec4da1e8d9403", () => {
  9. const input = BigInt("257986242325798624");
  10. const output = Buffer.from("e05ec4da1e8d9403", "hex");
  11. expect(Helpers.readBufferFromBigInt(input, 8, true, false)).toEqual(output);
  12. });
  13. test("it should return 0xfc6b72e1253ba120", () => {
  14. const input = BigInt("-257986242325798624");
  15. const output = Buffer.from("fc6b72e1253ba120", "hex");
  16. expect(Helpers.readBufferFromBigInt(input, 8, false, true)).toEqual(output);
  17. });
  18. test("it should return 0x03948d1edac45ee0", () => {
  19. const input = BigInt("257986242325798624");
  20. const output = Buffer.from("03948d1edac45ee0", "hex");
  21. expect(Helpers.readBufferFromBigInt(input, 8, false, false)).toEqual(
  22. output
  23. );
  24. });
  25. });
  26. describe("readBufferFromBigInt 16 bytes function", () => {
  27. test("it should return 0x8416c07962dac053b4346df39e5d97ec", () => {
  28. const input = BigInt("-25798624232579862436622316998984984956");
  29. const output = Buffer.from("8416c07962dac053b4346df39e5d97ec", "hex");
  30. expect(Helpers.readBufferFromBigInt(input, 16, true, true)).toEqual(output);
  31. });
  32. test("it should return 0x7ce93f869d253fac4bcb920c61a26813", () => {
  33. const input = BigInt("25798624232579862436622316998984984956");
  34. const output = Buffer.from("7ce93f869d253fac4bcb920c61a26813", "hex");
  35. expect(Helpers.readBufferFromBigInt(input, 16, true, false)).toEqual(
  36. output
  37. );
  38. });
  39. test("it should return 0xec975d9ef36d34b453c0da6279c01684", () => {
  40. const input = BigInt("-25798624232579862436622316998984984956");
  41. const output = Buffer.from("ec975d9ef36d34b453c0da6279c01684", "hex");
  42. expect(Helpers.readBufferFromBigInt(input, 16, false, true)).toEqual(
  43. output
  44. );
  45. });
  46. test("it should return 0x1368a2610c92cb4bac3f259d863fe97c", () => {
  47. const input = BigInt("25798624232579862436622316998984984956");
  48. const output = Buffer.from("1368a2610c92cb4bac3f259d863fe97c", "hex");
  49. expect(Helpers.readBufferFromBigInt(input, 16, false, false)).toEqual(
  50. output
  51. );
  52. });
  53. });
  54. describe("readBufferFromBigInt 32 bytes function", () => {
  55. test("it should return 0x7f113f5e2096936ec90cc4c73cc7bd3c96d20c115bf9ceb05c34232c037ff6c6", () => {
  56. const input = BigInt(
  57. "-25798624232579862436622316998984984912345482145214526587420145210501554564737"
  58. );
  59. const output = Buffer.from(
  60. "7f113f5e2096936ec90cc4c73cc7bd3c96d20c115bf9ceb05c34232c037ff6c6",
  61. "hex"
  62. );
  63. expect(Helpers.readBufferFromBigInt(input, 32, true, true)).toEqual(output);
  64. });
  65. test("it should return 0x81eec0a1df696c9136f33b38c33842c3692df3eea406314fa3cbdcd3fc800939", () => {
  66. const input = BigInt(
  67. "25798624232579862436622316998984984912345482145214526587420145210501554564737"
  68. );
  69. const output = Buffer.from(
  70. "81eec0a1df696c9136f33b38c33842c3692df3eea406314fa3cbdcd3fc800939",
  71. "hex"
  72. );
  73. expect(Helpers.readBufferFromBigInt(input, 32, true, false)).toEqual(
  74. output
  75. );
  76. });
  77. test("it should return 0xc6f67f032c23345cb0cef95b110cd2963cbdc73cc7c40cc96e9396205e3f117f", () => {
  78. const input = BigInt(
  79. "-25798624232579862436622316998984984912345482145214526587420145210501554564737"
  80. );
  81. const output = Buffer.from(
  82. "c6f67f032c23345cb0cef95b110cd2963cbdc73cc7c40cc96e9396205e3f117f",
  83. "hex"
  84. );
  85. expect(Helpers.readBufferFromBigInt(input, 32, false, true)).toEqual(
  86. output
  87. );
  88. });
  89. test("it should return 0x390980fcd3dccba34f3106a4eef32d69c34238c3383bf336916c69dfa1c0ee81", () => {
  90. const input = BigInt(
  91. "25798624232579862436622316998984984912345482145214526587420145210501554564737"
  92. );
  93. const output = Buffer.from(
  94. "390980fcd3dccba34f3106a4eef32d69c34238c3383bf336916c69dfa1c0ee81",
  95. "hex"
  96. );
  97. expect(Helpers.readBufferFromBigInt(input, 32, false, false)).toEqual(
  98. output
  99. );
  100. });
  101. });