67 lines
3.6 KiB
TypeScript
67 lines
3.6 KiB
TypeScript
import { type } from "./common"
|
|
import _ from "lodash"
|
|
import { expect, test } from "bun:test"
|
|
|
|
const CYCLES = 10000
|
|
test("Test Integer and Unsigned Integer", () => {
|
|
for (let i = 0; i < CYCLES; i++) {
|
|
|
|
// Unsigned Integer
|
|
expect(type.UInteger.safeParse(_.random(0, Math.pow(2, 53), false)).success).toBeTrue()
|
|
expect(type.UInt8.safeParse(_.random(0, Math.pow(2, 8) - 1, false)).success).toBeTrue()
|
|
expect(type.UInt8.safeParse(_.random(Math.pow(2, 8), Math.pow(2, 53), false)).success).toBeFalse()
|
|
expect(type.UInt16.safeParse(_.random(0, Math.pow(2, 16) - 1, false)).success).toBeTrue()
|
|
expect(type.UInt16.safeParse(_.random(Math.pow(2, 16), Math.pow(2, 53), false)).success).toBeFalse()
|
|
expect(type.UInt32.safeParse(_.random(0, Math.pow(2, 32) - 1, false)).success).toBeTrue()
|
|
expect(type.UInt32.safeParse(_.random(Math.pow(2, 32), Math.pow(2, 53), false)).success).toBeFalse()
|
|
|
|
// Integer
|
|
expect(type.Integer.safeParse(_.random(-Math.pow(2, 52), Math.pow(2, 52) - 1, false)).success).toBeTrue()
|
|
expect(type.Int8.safeParse(_.random(-Math.pow(2, 7), Math.pow(2, 7) - 1, false)).success).toBeTrue()
|
|
expect(type.Int8.safeParse(_.random(Math.pow(2, 7), Math.pow(2, 52), false)).success).toBeFalse()
|
|
expect(type.Int8.safeParse(_.random(-Math.pow(2, 52), -Math.pow(2, 7) - 1, false)).success).toBeFalse()
|
|
expect(type.Int16.safeParse(_.random(-Math.pow(2, 15), Math.pow(2, 15) - 1, false)).success).toBeTrue()
|
|
expect(type.Int16.safeParse(_.random(Math.pow(2, 15), Math.pow(2, 52), false)).success).toBeFalse()
|
|
expect(type.Int16.safeParse(_.random(-Math.pow(2, 52), -Math.pow(2, 15) - 1, false)).success).toBeFalse()
|
|
expect(type.Int32.safeParse(_.random(-Math.pow(2, 31), Math.pow(2, 31) - 1, false)).success).toBeTrue()
|
|
expect(type.Int32.safeParse(_.random(Math.pow(2, 31), Math.pow(2, 52), false)).success).toBeFalse()
|
|
expect(type.Int32.safeParse(_.random(-Math.pow(2, 52), -Math.pow(2, 31) - 1, false)).success).toBeFalse()
|
|
}
|
|
})
|
|
|
|
test("Test Number Processor Function", () => {
|
|
// Convert Number to Uint8Array
|
|
expect(type.numberToBytes(0xFF, 1).unwrap()[0]).toBe(255)
|
|
expect(type.numberToBytes(0xAAAA, 2).unwrap()).toEqual(new Uint8Array([0xAA, 0xAA]))
|
|
expect(type.numberToBytes(0x12345678, 4).unwrap()).toEqual(new Uint8Array([0x78, 0x56, 0x34, 0x12]))
|
|
expect(type.numberToBytes(0x12345678, 4, true).unwrap()).toEqual(new Uint8Array([0x12, 0x34, 0x56, 0x78]))
|
|
|
|
// Convert Uint8Array to Number
|
|
expect(type.bytesToNumber(new Uint8Array([0xFF]))).toBe(255)
|
|
expect(type.bytesToNumber(new Uint8Array([0xAA, 0xAA]))).toEqual(0xAAAA)
|
|
expect(type.bytesToNumber(new Uint8Array([0x78, 0x56, 0x34, 0x12]))).toEqual(0x12345678)
|
|
expect(type.bytesToNumber(new Uint8Array([0x12, 0x34, 0x56, 0x78]), true)).toEqual(0x12345678)
|
|
|
|
|
|
// Number Match
|
|
for (let i = 0; i < CYCLES; i++) {
|
|
const num1 = _.random(CYCLES / 2, false)
|
|
const num2 = _.random(CYCLES / 2, false)
|
|
|
|
expect(type.numberMatch(num1, num2)).toBe((num1 & num2) === num2 ? true : false)
|
|
expect(type.numberMatch(num1, num2, "True", "False")).toBe((num1 & num2) === num2 ? "True" : "False")
|
|
}
|
|
|
|
// Number Set, Unset, Toggle and Get Bit
|
|
expect(type.numberSetBit(0, 5)).toBe(0b10000)
|
|
expect(type.numberUnsetBit(0b1111, 3)).toBe(0b1011)
|
|
expect(type.numberToggleBit(0b1010, 3)).toBe(0b1110)
|
|
expect(type.numberBit(0b1100, 2)).toBe(0)
|
|
|
|
// Get High / Low Bits Num
|
|
expect(type.numberHighBitsNum(0xFF).unwrap()).toBe(8)
|
|
expect(type.numberHighBitsNum(0xAA).unwrap()).toBe(4)
|
|
expect(type.numberLowBitsNum(0xFF, 8).unwrap()).toBe(0)
|
|
expect(type.numberLowBitsNum(0xAA, 8).unwrap()).toBe(4)
|
|
})
|