mtcute/packages/core/tests/bigint-utils.spec.ts

42 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-04-08 12:19:38 +03:00
import bigInt from 'big-integer'
import { expect } from 'chai'
import { describe, it } from 'mocha'
2021-04-08 12:19:38 +03:00
import { bigIntToBuffer, bufferToBigInt } from '../utils'
2021-04-08 12:19:38 +03:00
// since bigIntToBuffer is a tiny wrapper over writeBigInt, no need to test it individually
describe('bigIntToBuffer', () => {
it('should handle writing to BE', () => {
2023-09-24 01:32:22 +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([
...Buffer.from('81A33C81D2020550', 'hex'),
2021-04-08 12:19:38 +03:00
])
})
it('should handle writing to LE', () => {
2023-09-24 01:32:22 +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([
...Buffer.from('81A33C81D2020550', 'hex').reverse(),
2021-04-08 12:19:38 +03:00
])
})
})
describe('bufferToBigInt', () => {
it('should handle reading BE', () => {
2023-09-24 01:32:22 +03:00
expect(bufferToBigInt(Buffer.from([0xa0, 0x26, 0xdc]), 0, 3, false).toString()).eq('10495708')
expect(bufferToBigInt(Buffer.from([0x00, 0xa0, 0x26, 0xdc]), 0, 4, false).toString()).eq('10495708')
expect(bufferToBigInt(Buffer.from([0xb5, 0x15, 0xc4, 0x15]), 0, 4, false).toString()).eq('3038102549')
2021-04-08 12:19:38 +03:00
})
it('should handle reading LE', () => {
2023-09-24 01:32:22 +03:00
expect(bufferToBigInt(Buffer.from([0xdc, 0x26, 0xa0]), 0, 3, true).toString()).eq('10495708')
expect(bufferToBigInt(Buffer.from([0xdc, 0x26, 0xa0, 0x00]), 0, 4, true).toString()).eq('10495708')
expect(bufferToBigInt(Buffer.from([0x15, 0xc4, 0x15, 0xb5]), 0, 4, true).toString()).eq('3038102549')
2021-04-08 12:19:38 +03:00
})
})