2023-11-09 00:20:43 +03:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2023-10-16 19:23:53 +03:00
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
import { hexDecode, hexDecodeToBuffer, hexEncode } from './hex.js'
|
|
|
|
import {
|
|
|
|
hexDecode as hexDecodeWeb,
|
|
|
|
hexDecodeToBuffer as hexDecodeToBufferWeb,
|
|
|
|
hexEncode as hexEncodeWeb,
|
|
|
|
} from './hex.web.js'
|
2023-10-16 19:23:53 +03:00
|
|
|
|
|
|
|
describe('hex', () => {
|
|
|
|
it('should decode hex string to existing buffer', () => {
|
|
|
|
const buf = new Uint8Array(4)
|
|
|
|
hexDecode(buf, '01020304')
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(buf).toEqual(new Uint8Array([1, 2, 3, 4]))
|
2023-10-16 19:23:53 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should decode hex string to new buffer', () => {
|
|
|
|
const buf = hexDecodeToBuffer('01020304')
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(new Uint8Array(buf)).toEqual(new Uint8Array([1, 2, 3, 4]))
|
2023-10-16 19:23:53 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should encode buffer to hex string', () => {
|
|
|
|
const buf = new Uint8Array([1, 2, 3, 4])
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(hexEncode(buf)).toEqual('01020304')
|
2023-10-16 19:23:53 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('hex.web', () => {
|
|
|
|
it('should decode hex string to existing buffer', () => {
|
|
|
|
const buf = new Uint8Array(4)
|
|
|
|
hexDecodeWeb(buf, '01020304')
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(buf).toEqual(new Uint8Array([1, 2, 3, 4]))
|
2023-10-16 19:23:53 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should decode hex string to new buffer', () => {
|
|
|
|
const buf = hexDecodeToBufferWeb('01020304')
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(buf).toEqual(new Uint8Array([1, 2, 3, 4]))
|
2023-10-16 19:23:53 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should encode buffer to hex string', () => {
|
|
|
|
const buf = new Uint8Array([1, 2, 3, 4])
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(hexEncodeWeb(buf)).toEqual('01020304')
|
2023-10-16 19:23:53 +03:00
|
|
|
})
|
|
|
|
})
|