mtcute/packages/core/src/utils/bigint-utils.test.ts

66 lines
3.2 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from 'vitest'
2021-04-08 12:19:38 +03:00
import { hexDecodeToBuffer } from '@mtcute/tl-runtime'
import { bigIntToBuffer, bufferToBigInt } from './index.js'
2021-04-08 12:19:38 +03:00
describe('bigIntToBuffer', () => {
it('should handle writing to BE', () => {
2023-11-06 02:28:35 +03:00
expect([...bigIntToBuffer(BigInt('10495708'), 0, false)]).eql([0xa0, 0x26, 0xdc])
expect([...bigIntToBuffer(BigInt('10495708'), 4, false)]).eql([0x00, 0xa0, 0x26, 0xdc])
expect([...bigIntToBuffer(BigInt('10495708'), 8, false)]).eql([0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x26, 0xdc])
expect([...bigIntToBuffer(BigInt('3038102549'), 4, false)]).eql([0xb5, 0x15, 0xc4, 0x15])
expect([...bigIntToBuffer(BigInt('9341376580368336208'), 8, false)]).eql([
...hexDecodeToBuffer('81A33C81D2020550'),
2021-04-08 12:19:38 +03:00
])
})
2023-11-06 02:28:35 +03:00
2021-04-08 12:19:38 +03:00
it('should handle writing to LE', () => {
2023-11-06 02:28:35 +03:00
expect([...bigIntToBuffer(BigInt('10495708'), 0, true)]).eql([0xdc, 0x26, 0xa0])
expect([...bigIntToBuffer(BigInt('10495708'), 4, true)]).eql([0xdc, 0x26, 0xa0, 0x00])
expect([...bigIntToBuffer(BigInt('10495708'), 8, true)]).eql([0xdc, 0x26, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00])
expect([...bigIntToBuffer(BigInt('3038102549'), 4, true)]).eql([0x15, 0xc4, 0x15, 0xb5])
expect([...bigIntToBuffer(BigInt('9341376580368336208'), 8, true)]).eql([
...hexDecodeToBuffer('81A33C81D2020550').reverse(),
2021-04-08 12:19:38 +03:00
])
})
2023-11-06 02:28:35 +03:00
it('should handle large integers', () => {
const buf = hexDecodeToBuffer(
'1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538',
)
const num = BigInt(
'0x1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538',
)
expect([...bigIntToBuffer(num, 0, false)]).eql([...buf])
expect([...bigIntToBuffer(num, 0, true)]).eql([...buf.reverse()])
})
2021-04-08 12:19:38 +03:00
})
describe('bufferToBigInt', () => {
it('should handle reading BE', () => {
2023-11-06 02:28:35 +03:00
expect(bufferToBigInt(new Uint8Array([0xa0, 0x26, 0xdc]), false).toString()).eq('10495708')
expect(bufferToBigInt(new Uint8Array([0x00, 0xa0, 0x26, 0xdc]), false).toString()).eq('10495708')
expect(bufferToBigInt(new Uint8Array([0xb5, 0x15, 0xc4, 0x15]), false).toString()).eq('3038102549')
2021-04-08 12:19:38 +03:00
})
it('should handle reading LE', () => {
2023-11-06 02:28:35 +03:00
expect(bufferToBigInt(new Uint8Array([0xdc, 0x26, 0xa0]), true).toString()).eq('10495708')
expect(bufferToBigInt(new Uint8Array([0xdc, 0x26, 0xa0, 0x00]), true).toString()).eq('10495708')
expect(bufferToBigInt(new Uint8Array([0x15, 0xc4, 0x15, 0xb5]), true).toString()).eq('3038102549')
})
it('should handle large integers', () => {
const buf = hexDecodeToBuffer(
'1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538',
)
const num = BigInt(
'0x1a981ce8bf86bf4a1bd79c2ef829914172f8d0e54cb7ad807552d56977e1c946872e2c7bd77052be30e7e9a7a35c4feff848a25759f5f2f5b0e96538',
)
expect(bufferToBigInt(buf, false).toString()).eq(num.toString())
expect(bufferToBigInt(buf.reverse(), true).toString()).eq(num.toString())
2021-04-08 12:19:38 +03:00
})
})