1
0

readBuffer.spec.ts 4.5 KB

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