2021-04-08 12:19:38 +03:00
|
|
|
import bigInt from 'big-integer'
|
2023-06-05 03:30:48 +03:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import { describe, it } from 'mocha'
|
2021-04-08 12:19:38 +03:00
|
|
|
|
2023-09-21 13:18:09 +03:00
|
|
|
import { bigIntToBuffer, bufferToBigInt } from '../utils'
|
2022-06-30 16:32:56 +03:00
|
|
|
|
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', () => {
|
|
|
|
expect([...bigIntToBuffer(bigInt('10495708'), 0, false)]).eql([
|
2022-06-30 16:32:56 +03:00
|
|
|
0xa0, 0x26, 0xdc,
|
2021-04-08 12:19:38 +03:00
|
|
|
])
|
|
|
|
expect([...bigIntToBuffer(bigInt('10495708'), 4, false)]).eql([
|
2022-06-30 16:32:56 +03:00
|
|
|
0x00, 0xa0, 0x26, 0xdc,
|
2021-04-08 12:19:38 +03:00
|
|
|
])
|
|
|
|
expect([...bigIntToBuffer(bigInt('10495708'), 8, false)]).eql([
|
2022-06-30 16:32:56 +03:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x26, 0xdc,
|
2021-04-08 12:19:38 +03:00
|
|
|
])
|
|
|
|
expect([...bigIntToBuffer(bigInt('3038102549'), 4, false)]).eql([
|
2022-06-30 16:32:56 +03:00
|
|
|
0xb5, 0x15, 0xc4, 0x15,
|
2021-04-08 12:19:38 +03:00
|
|
|
])
|
|
|
|
expect([
|
|
|
|
...bigIntToBuffer(bigInt('9341376580368336208'), 8, false),
|
|
|
|
]).eql([...Buffer.from('81A33C81D2020550', 'hex')])
|
|
|
|
})
|
|
|
|
it('should handle writing to LE', () => {
|
|
|
|
expect([...bigIntToBuffer(bigInt('10495708'), 0, true)]).eql([
|
2022-06-30 16:32:56 +03:00
|
|
|
0xdc, 0x26, 0xa0,
|
2021-04-08 12:19:38 +03:00
|
|
|
])
|
|
|
|
expect([...bigIntToBuffer(bigInt('10495708'), 4, true)]).eql([
|
2022-06-30 16:32:56 +03:00
|
|
|
0xdc, 0x26, 0xa0, 0x00,
|
2021-04-08 12:19:38 +03:00
|
|
|
])
|
|
|
|
expect([...bigIntToBuffer(bigInt('10495708'), 8, true)]).eql([
|
2022-06-30 16:32:56 +03:00
|
|
|
0xdc, 0x26, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2021-04-08 12:19:38 +03:00
|
|
|
])
|
|
|
|
expect([...bigIntToBuffer(bigInt('3038102549'), 4, true)]).eql([
|
2022-06-30 16:32:56 +03:00
|
|
|
0x15, 0xc4, 0x15, 0xb5,
|
2021-04-08 12:19:38 +03:00
|
|
|
])
|
2022-06-30 16:32:56 +03:00
|
|
|
expect([...bigIntToBuffer(bigInt('9341376580368336208'), 8, true)]).eql(
|
2023-06-05 03:30:48 +03:00
|
|
|
[...Buffer.from('81A33C81D2020550', 'hex').reverse()],
|
2022-06-30 16:32:56 +03:00
|
|
|
)
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('bufferToBigInt', () => {
|
|
|
|
it('should handle reading BE', () => {
|
|
|
|
expect(
|
|
|
|
bufferToBigInt(
|
|
|
|
Buffer.from([0xa0, 0x26, 0xdc]),
|
|
|
|
0,
|
|
|
|
3,
|
2023-06-05 03:30:48 +03:00
|
|
|
false,
|
|
|
|
).toString(),
|
2021-04-08 12:19:38 +03:00
|
|
|
).eq('10495708')
|
|
|
|
expect(
|
|
|
|
bufferToBigInt(
|
|
|
|
Buffer.from([0x00, 0xa0, 0x26, 0xdc]),
|
|
|
|
0,
|
|
|
|
4,
|
2023-06-05 03:30:48 +03:00
|
|
|
false,
|
|
|
|
).toString(),
|
2021-04-08 12:19:38 +03:00
|
|
|
).eq('10495708')
|
|
|
|
expect(
|
|
|
|
bufferToBigInt(
|
|
|
|
Buffer.from([0xb5, 0x15, 0xc4, 0x15]),
|
|
|
|
0,
|
|
|
|
4,
|
2023-06-05 03:30:48 +03:00
|
|
|
false,
|
|
|
|
).toString(),
|
2021-04-08 12:19:38 +03:00
|
|
|
).eq('3038102549')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should handle reading LE', () => {
|
|
|
|
expect(
|
|
|
|
bufferToBigInt(
|
|
|
|
Buffer.from([0xdc, 0x26, 0xa0]),
|
|
|
|
0,
|
|
|
|
3,
|
2023-06-05 03:30:48 +03:00
|
|
|
true,
|
|
|
|
).toString(),
|
2021-04-08 12:19:38 +03:00
|
|
|
).eq('10495708')
|
|
|
|
expect(
|
|
|
|
bufferToBigInt(
|
|
|
|
Buffer.from([0xdc, 0x26, 0xa0, 0x00]),
|
|
|
|
0,
|
|
|
|
4,
|
2023-06-05 03:30:48 +03:00
|
|
|
true,
|
|
|
|
).toString(),
|
2021-04-08 12:19:38 +03:00
|
|
|
).eq('10495708')
|
|
|
|
expect(
|
|
|
|
bufferToBigInt(
|
|
|
|
Buffer.from([0x15, 0xc4, 0x15, 0xb5]),
|
|
|
|
0,
|
|
|
|
4,
|
2023-06-05 03:30:48 +03:00
|
|
|
true,
|
|
|
|
).toString(),
|
2021-04-08 12:19:38 +03:00
|
|
|
).eq('3038102549')
|
|
|
|
})
|
|
|
|
})
|