更新ci测试用例
This commit is contained in:
@@ -1,91 +1,247 @@
|
|||||||
import {
|
import {
|
||||||
BigIntFactory,
|
BitMask64Data,
|
||||||
IBigIntLike
|
BitMask64Utils
|
||||||
} from '../../../src/ECS/Utils/BigIntCompatibility';
|
} from '../../../src/ECS/Utils/BigIntCompatibility';
|
||||||
|
|
||||||
describe('BigInt兼容性测试', () => {
|
describe('64位掩码兼容性测试', () => {
|
||||||
describe('基本功能', () => {
|
describe('基本功能', () => {
|
||||||
it('应该能够创建和获取数值', () => {
|
it('应该能够创建和检查掩码', () => {
|
||||||
const zero = BigIntFactory.zero();
|
const zero = BitMask64Utils.ZERO;
|
||||||
const one = BigIntFactory.one();
|
const mask1 = BitMask64Utils.create(0);
|
||||||
const value = BigIntFactory.create(42);
|
const mask2 = BitMask64Utils.create(5);
|
||||||
|
|
||||||
expect(zero.isZero()).toBe(true);
|
expect(BitMask64Utils.isZero(zero)).toBe(true);
|
||||||
expect(one.valueOf()).toBe(1);
|
expect(BitMask64Utils.isZero(mask1)).toBe(false);
|
||||||
expect(value.valueOf()).toBe(42);
|
expect(BitMask64Utils.isZero(mask2)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('应该支持字符串创建', () => {
|
it('应该支持数字创建', () => {
|
||||||
const value = BigIntFactory.create('123');
|
const mask = BitMask64Utils.fromNumber(42);
|
||||||
expect(value.valueOf()).toBe(123);
|
expect(mask.lo).toBe(42);
|
||||||
|
expect(mask.hi).toBe(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('位运算', () => {
|
describe('位运算', () => {
|
||||||
let value1: IBigIntLike;
|
let mask1: BitMask64Data;
|
||||||
let value2: IBigIntLike;
|
let mask2: BitMask64Data;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
value1 = BigIntFactory.create(5); // 101
|
mask1 = BitMask64Utils.create(2); // 位2
|
||||||
value2 = BigIntFactory.create(3); // 011
|
mask2 = BitMask64Utils.create(3); // 位3
|
||||||
});
|
});
|
||||||
|
|
||||||
it('AND运算', () => {
|
it('hasAny运算', () => {
|
||||||
const result = value1.and(value2); // 101 & 011 = 001
|
const combined = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
expect(result.valueOf()).toBe(1);
|
BitMask64Utils.orInPlace(combined, mask1);
|
||||||
|
BitMask64Utils.orInPlace(combined, mask2);
|
||||||
|
|
||||||
|
expect(BitMask64Utils.hasAny(combined, mask1)).toBe(true);
|
||||||
|
expect(BitMask64Utils.hasAny(combined, mask2)).toBe(true);
|
||||||
|
|
||||||
|
const mask4 = BitMask64Utils.create(4);
|
||||||
|
expect(BitMask64Utils.hasAny(combined, mask4)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('OR运算', () => {
|
it('hasAll运算', () => {
|
||||||
const result = value1.or(value2); // 101 | 011 = 111
|
const combined = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
expect(result.valueOf()).toBe(7);
|
BitMask64Utils.orInPlace(combined, mask1);
|
||||||
|
BitMask64Utils.orInPlace(combined, mask2);
|
||||||
|
|
||||||
|
expect(BitMask64Utils.hasAll(combined, mask1)).toBe(true);
|
||||||
|
expect(BitMask64Utils.hasAll(combined, mask2)).toBe(true);
|
||||||
|
|
||||||
|
const both = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
|
BitMask64Utils.orInPlace(both, mask1);
|
||||||
|
BitMask64Utils.orInPlace(both, mask2);
|
||||||
|
expect(BitMask64Utils.hasAll(combined, both)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('XOR运算', () => {
|
it('hasNone运算', () => {
|
||||||
const result = value1.xor(value2); // 101 ^ 011 = 110
|
const mask4 = BitMask64Utils.create(4);
|
||||||
expect(result.valueOf()).toBe(6);
|
const mask5 = BitMask64Utils.create(5);
|
||||||
|
|
||||||
|
expect(BitMask64Utils.hasNone(mask1, mask2)).toBe(true);
|
||||||
|
expect(BitMask64Utils.hasNone(mask1, mask4)).toBe(true);
|
||||||
|
expect(BitMask64Utils.hasNone(mask1, mask1)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('NOT运算', () => {
|
it('原地位运算', () => {
|
||||||
const value = BigIntFactory.create(5); // 00000101
|
const target = BitMask64Utils.clone(mask1);
|
||||||
const result = value.not(8); // 11111010
|
|
||||||
expect(result.valueOf()).toBe(250);
|
// OR操作
|
||||||
|
BitMask64Utils.orInPlace(target, mask2);
|
||||||
|
expect(BitMask64Utils.hasAll(target, mask1)).toBe(true);
|
||||||
|
expect(BitMask64Utils.hasAll(target, mask2)).toBe(true);
|
||||||
|
|
||||||
|
// AND操作
|
||||||
|
const andTarget = BitMask64Utils.clone(target);
|
||||||
|
BitMask64Utils.andInPlace(andTarget, mask1);
|
||||||
|
expect(BitMask64Utils.hasAll(andTarget, mask1)).toBe(true);
|
||||||
|
expect(BitMask64Utils.hasAny(andTarget, mask2)).toBe(false);
|
||||||
|
|
||||||
|
// XOR操作
|
||||||
|
const xorTarget = BitMask64Utils.clone(target);
|
||||||
|
BitMask64Utils.xorInPlace(xorTarget, mask1);
|
||||||
|
expect(BitMask64Utils.hasAny(xorTarget, mask1)).toBe(false);
|
||||||
|
expect(BitMask64Utils.hasAll(xorTarget, mask2)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('移位运算', () => {
|
it('设置和清除位', () => {
|
||||||
const value = BigIntFactory.create(5);
|
const mask = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
const left = value.shiftLeft(1);
|
|
||||||
const right = value.shiftRight(1);
|
|
||||||
|
|
||||||
expect(left.valueOf()).toBe(10);
|
BitMask64Utils.setBit(mask, 5);
|
||||||
expect(right.valueOf()).toBe(2);
|
expect(BitMask64Utils.hasAny(mask, BitMask64Utils.create(5))).toBe(true);
|
||||||
|
|
||||||
|
BitMask64Utils.clearBit(mask, 5);
|
||||||
|
expect(BitMask64Utils.isZero(mask)).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('字符串解析', () => {
|
describe('字符串表示', () => {
|
||||||
it('二进制字符串', () => {
|
it('二进制字符串', () => {
|
||||||
const value = BigIntFactory.fromBinaryString('10101');
|
const mask = BitMask64Utils.create(5); // 位5设置为1
|
||||||
expect(value.valueOf()).toBe(21);
|
const binaryStr = BitMask64Utils.toString(mask, 2);
|
||||||
|
expect(binaryStr).toBe('100000'); // 位5为1
|
||||||
});
|
});
|
||||||
|
|
||||||
it('十六进制字符串', () => {
|
it('十六进制字符串', () => {
|
||||||
const value = BigIntFactory.fromHexString('0xFF');
|
const mask = BitMask64Utils.fromNumber(255);
|
||||||
expect(value.valueOf()).toBe(255);
|
const hexStr = BitMask64Utils.toString(mask, 16);
|
||||||
|
expect(hexStr).toBe('0xFF');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('大数字的十六进制表示', () => {
|
||||||
|
const mask: BitMask64Data = { lo: 0xFFFFFFFF, hi: 0x12345678 };
|
||||||
|
const hexStr = BitMask64Utils.toString(mask, 16);
|
||||||
|
expect(hexStr).toBe('0x12345678FFFFFFFF');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('ECS位掩码操作', () => {
|
describe('位计数', () => {
|
||||||
it('组件掩码操作', () => {
|
it('popCount应该正确计算设置位的数量', () => {
|
||||||
const componentMasks: IBigIntLike[] = [];
|
const mask = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
|
expect(BitMask64Utils.popCount(mask)).toBe(0);
|
||||||
|
|
||||||
|
BitMask64Utils.setBit(mask, 0);
|
||||||
|
BitMask64Utils.setBit(mask, 2);
|
||||||
|
BitMask64Utils.setBit(mask, 4);
|
||||||
|
expect(BitMask64Utils.popCount(mask)).toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('大数的popCount', () => {
|
||||||
|
const mask = BitMask64Utils.fromNumber(0xFF); // 8个1
|
||||||
|
expect(BitMask64Utils.popCount(mask)).toBe(8);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('ECS组件掩码操作', () => {
|
||||||
|
it('多组件掩码组合', () => {
|
||||||
|
const componentMasks: BitMask64Data[] = [];
|
||||||
for (let i = 0; i < 10; i++) {
|
for (let i = 0; i < 10; i++) {
|
||||||
componentMasks.push(BigIntFactory.one().shiftLeft(i));
|
componentMasks.push(BitMask64Utils.create(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
let combinedMask = BigIntFactory.zero();
|
let combinedMask = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
for (const mask of componentMasks) {
|
for (const mask of componentMasks) {
|
||||||
combinedMask = combinedMask.or(mask);
|
BitMask64Utils.orInPlace(combinedMask, mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(combinedMask.valueOf()).toBe(1023); // 2^10 - 1
|
expect(BitMask64Utils.popCount(combinedMask)).toBe(10);
|
||||||
|
|
||||||
|
// 检查所有位都设置了
|
||||||
|
for (let i = 0; i < 10; i++) {
|
||||||
|
expect(BitMask64Utils.hasAny(combinedMask, BitMask64Utils.create(i))).toBe(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('实体匹配模拟', () => {
|
||||||
|
// 模拟实体具有组件0, 2, 4
|
||||||
|
const entityMask = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
|
BitMask64Utils.setBit(entityMask, 0);
|
||||||
|
BitMask64Utils.setBit(entityMask, 2);
|
||||||
|
BitMask64Utils.setBit(entityMask, 4);
|
||||||
|
|
||||||
|
// 查询需要组件0和2
|
||||||
|
const queryMask = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
|
BitMask64Utils.setBit(queryMask, 0);
|
||||||
|
BitMask64Utils.setBit(queryMask, 2);
|
||||||
|
|
||||||
|
expect(BitMask64Utils.hasAll(entityMask, queryMask)).toBe(true);
|
||||||
|
|
||||||
|
// 查询需要组件1和3
|
||||||
|
const queryMask2 = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
|
BitMask64Utils.setBit(queryMask2, 1);
|
||||||
|
BitMask64Utils.setBit(queryMask2, 3);
|
||||||
|
|
||||||
|
expect(BitMask64Utils.hasAll(entityMask, queryMask2)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('边界测试', () => {
|
||||||
|
it('应该处理64位边界', () => {
|
||||||
|
expect(() => BitMask64Utils.create(63)).not.toThrow();
|
||||||
|
expect(() => BitMask64Utils.create(64)).toThrow();
|
||||||
|
expect(() => BitMask64Utils.create(-1)).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('设置和清除边界位', () => {
|
||||||
|
const mask = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
|
|
||||||
|
BitMask64Utils.setBit(mask, 63);
|
||||||
|
expect(BitMask64Utils.hasAny(mask, BitMask64Utils.create(63))).toBe(true);
|
||||||
|
expect(mask.hi).not.toBe(0);
|
||||||
|
expect(mask.lo).toBe(0);
|
||||||
|
|
||||||
|
BitMask64Utils.clearBit(mask, 63);
|
||||||
|
expect(BitMask64Utils.isZero(mask)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('高32位和低32位操作', () => {
|
||||||
|
const lowMask = BitMask64Utils.create(15); // 低32位
|
||||||
|
const highMask = BitMask64Utils.create(47); // 高32位
|
||||||
|
|
||||||
|
expect(lowMask.hi).toBe(0);
|
||||||
|
expect(lowMask.lo).not.toBe(0);
|
||||||
|
|
||||||
|
expect(highMask.hi).not.toBe(0);
|
||||||
|
expect(highMask.lo).toBe(0);
|
||||||
|
|
||||||
|
const combined = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
|
BitMask64Utils.orInPlace(combined, lowMask);
|
||||||
|
BitMask64Utils.orInPlace(combined, highMask);
|
||||||
|
|
||||||
|
expect(combined.hi).not.toBe(0);
|
||||||
|
expect(combined.lo).not.toBe(0);
|
||||||
|
expect(BitMask64Utils.popCount(combined)).toBe(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('复制和相等性', () => {
|
||||||
|
it('clone应该创建独立副本', () => {
|
||||||
|
const original = BitMask64Utils.create(5);
|
||||||
|
const cloned = BitMask64Utils.clone(original);
|
||||||
|
|
||||||
|
expect(BitMask64Utils.equals(original, cloned)).toBe(true);
|
||||||
|
|
||||||
|
BitMask64Utils.setBit(cloned, 6);
|
||||||
|
expect(BitMask64Utils.equals(original, cloned)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('copy应该正确复制', () => {
|
||||||
|
const source = BitMask64Utils.create(10);
|
||||||
|
const target = BitMask64Utils.clone(BitMask64Utils.ZERO);
|
||||||
|
|
||||||
|
BitMask64Utils.copy(source, target);
|
||||||
|
expect(BitMask64Utils.equals(source, target)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('clear应该清除所有位', () => {
|
||||||
|
const mask = BitMask64Utils.create(20);
|
||||||
|
expect(BitMask64Utils.isZero(mask)).toBe(false);
|
||||||
|
|
||||||
|
BitMask64Utils.clear(mask);
|
||||||
|
expect(BitMask64Utils.isZero(mask)).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Bits } from '../../../src/ECS/Utils/Bits';
|
import { Bits } from '../../../src/ECS/Utils/Bits';
|
||||||
import { BigIntFactory } from '../../../src/ECS/Utils/BigIntCompatibility';
|
import { BitMask64Utils } from '../../../src/ECS/Utils/BigIntCompatibility';
|
||||||
|
|
||||||
describe('Bits - 高性能位操作类测试', () => {
|
describe('Bits - 高性能位操作类测试', () => {
|
||||||
let bits: Bits;
|
let bits: Bits;
|
||||||
@@ -12,12 +12,12 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
it('应该能够创建空的Bits对象', () => {
|
it('应该能够创建空的Bits对象', () => {
|
||||||
expect(bits).toBeDefined();
|
expect(bits).toBeDefined();
|
||||||
expect(bits.isEmpty()).toBe(true);
|
expect(bits.isEmpty()).toBe(true);
|
||||||
expect(bits.getValue().isZero()).toBe(true);
|
expect(BitMask64Utils.isZero(bits.getValue())).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('应该能够使用初始值创建Bits对象', () => {
|
it('应该能够使用初始值创建Bits对象', () => {
|
||||||
const bitsWithValue = new Bits(BigIntFactory.create(5)); // 二进制: 101
|
const bitsWithValue = new Bits(5); // 二进制: 101
|
||||||
expect(bitsWithValue.getValue().toString()).toBe('5');
|
expect(bitsWithValue.toHexString()).toBe('0x5');
|
||||||
expect(bitsWithValue.isEmpty()).toBe(false);
|
expect(bitsWithValue.isEmpty()).toBe(false);
|
||||||
expect(bitsWithValue.get(0)).toBe(true); // 第0位
|
expect(bitsWithValue.get(0)).toBe(true); // 第0位
|
||||||
expect(bitsWithValue.get(1)).toBe(false); // 第1位
|
expect(bitsWithValue.get(1)).toBe(false); // 第1位
|
||||||
@@ -26,7 +26,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
|
|
||||||
it('默认构造函数应该创建值为0的对象', () => {
|
it('默认构造函数应该创建值为0的对象', () => {
|
||||||
const defaultBits = new Bits();
|
const defaultBits = new Bits();
|
||||||
expect(defaultBits.getValue().isZero()).toBe(true);
|
expect(BitMask64Utils.isZero(defaultBits.getValue())).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -34,22 +34,22 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
it('应该能够设置指定位置的位', () => {
|
it('应该能够设置指定位置的位', () => {
|
||||||
bits.set(0);
|
bits.set(0);
|
||||||
expect(bits.get(0)).toBe(true);
|
expect(bits.get(0)).toBe(true);
|
||||||
expect(bits.getValue().toString()).toBe('1');
|
expect(bits.toHexString()).toBe('0x1');
|
||||||
|
|
||||||
bits.set(3);
|
bits.set(3);
|
||||||
expect(bits.get(3)).toBe(true);
|
expect(bits.get(3)).toBe(true);
|
||||||
expect(bits.getValue().toString()).toBe('9'); // 1001 in binary
|
expect(bits.toHexString()).toBe('0x9'); // 1001 in binary
|
||||||
});
|
});
|
||||||
|
|
||||||
it('应该能够清除指定位置的位', () => {
|
it('应该能够清除指定位置的位', () => {
|
||||||
bits.set(0);
|
bits.set(0);
|
||||||
bits.set(1);
|
bits.set(1);
|
||||||
bits.set(2);
|
bits.set(2);
|
||||||
expect(bits.getValue().toString()).toBe('7'); // 111 in binary
|
expect(bits.toHexString()).toBe('0x7'); // 111 in binary
|
||||||
|
|
||||||
bits.clear(1);
|
bits.clear(1);
|
||||||
expect(bits.get(1)).toBe(false);
|
expect(bits.get(1)).toBe(false);
|
||||||
expect(bits.getValue().toString()).toBe('5'); // 101 in binary
|
expect(bits.toHexString()).toBe('0x5'); // 101 in binary
|
||||||
});
|
});
|
||||||
|
|
||||||
it('重复设置同一位应该保持不变', () => {
|
it('重复设置同一位应该保持不变', () => {
|
||||||
@@ -57,12 +57,12 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
const value1 = bits.getValue();
|
const value1 = bits.getValue();
|
||||||
bits.set(0);
|
bits.set(0);
|
||||||
const value2 = bits.getValue();
|
const value2 = bits.getValue();
|
||||||
expect(value1.equals(value2)).toBe(true);
|
expect(BitMask64Utils.equals(value1, value2)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('清除未设置的位应该安全', () => {
|
it('清除未设置的位应该安全', () => {
|
||||||
bits.clear(5);
|
bits.clear(5);
|
||||||
expect(bits.getValue().isZero()).toBe(true);
|
expect(BitMask64Utils.isZero(bits.getValue())).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('设置负索引应该抛出错误', () => {
|
it('设置负索引应该抛出错误', () => {
|
||||||
@@ -123,7 +123,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
|
|
||||||
it('AND运算应该正确', () => {
|
it('AND运算应该正确', () => {
|
||||||
const result = bits.and(otherBits);
|
const result = bits.and(otherBits);
|
||||||
expect(result.getValue().toString()).toBe('4'); // 10101 & 01110 = 00100 = 4
|
expect(result.toHexString()).toBe('0x4'); // 10101 & 01110 = 00100 = 4
|
||||||
expect(result.get(2)).toBe(true);
|
expect(result.get(2)).toBe(true);
|
||||||
expect(result.get(0)).toBe(false);
|
expect(result.get(0)).toBe(false);
|
||||||
expect(result.get(1)).toBe(false);
|
expect(result.get(1)).toBe(false);
|
||||||
@@ -131,7 +131,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
|
|
||||||
it('OR运算应该正确', () => {
|
it('OR运算应该正确', () => {
|
||||||
const result = bits.or(otherBits);
|
const result = bits.or(otherBits);
|
||||||
expect(result.getValue().toString()).toBe('31'); // 10101 | 01110 = 11111 = 31
|
expect(result.toHexString()).toBe('0x1F'); // 10101 | 01110 = 11111 = 31
|
||||||
expect(result.get(0)).toBe(true);
|
expect(result.get(0)).toBe(true);
|
||||||
expect(result.get(1)).toBe(true);
|
expect(result.get(1)).toBe(true);
|
||||||
expect(result.get(2)).toBe(true);
|
expect(result.get(2)).toBe(true);
|
||||||
@@ -141,7 +141,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
|
|
||||||
it('XOR运算应该正确', () => {
|
it('XOR运算应该正确', () => {
|
||||||
const result = bits.xor(otherBits);
|
const result = bits.xor(otherBits);
|
||||||
expect(result.getValue().toString()).toBe('27'); // 10101 ^ 01110 = 11011 = 27
|
expect(result.toHexString()).toBe('0x1B'); // 10101 ^ 01110 = 11011 = 27
|
||||||
expect(result.get(0)).toBe(true);
|
expect(result.get(0)).toBe(true);
|
||||||
expect(result.get(1)).toBe(true);
|
expect(result.get(1)).toBe(true);
|
||||||
expect(result.get(2)).toBe(false); // 相同位XOR为0
|
expect(result.get(2)).toBe(false); // 相同位XOR为0
|
||||||
@@ -150,16 +150,18 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('NOT运算应该正确', () => {
|
it('NOT运算应该正确', () => {
|
||||||
const simpleBits = new Bits(BigIntFactory.create(5)); // 101 in binary
|
const simpleBits = new Bits(5); // 101 in binary
|
||||||
const result = simpleBits.not(8); // 限制为8位
|
const result = simpleBits.not(8); // 限制为8位
|
||||||
expect(result.getValue().toString()).toBe('250'); // ~00000101 = 11111010 = 250 (8位)
|
expect(result.toHexString()).toBe('0xFA'); // ~00000101 = 11111010 = 250 (8位)
|
||||||
});
|
});
|
||||||
|
|
||||||
it('NOT运算默认64位应该正确', () => {
|
it('NOT运算默认64位应该正确', () => {
|
||||||
const simpleBits = new Bits(BigIntFactory.create(1));
|
const simpleBits = new Bits(1);
|
||||||
const result = simpleBits.not();
|
const result = simpleBits.not();
|
||||||
const expected = BigIntFactory.one().shiftLeft(64).valueOf() - 2; // 64位全1减去最低位
|
// 64位全1减去最低位,但由于JavaScript数字精度问题,我们检查特定位
|
||||||
expect(result.getValue().valueOf()).toBe(expected);
|
expect(result.get(0)).toBe(false); // 位0应该是0
|
||||||
|
expect(result.get(1)).toBe(true); // 位1应该是1
|
||||||
|
expect(result.get(63)).toBe(true); // 最高位应该是1
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -234,12 +236,12 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
expect(bits.cardinality()).toBe(2);
|
expect(bits.cardinality()).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('大数值的cardinality应该正确', () => {
|
it('最大64位的cardinality应该正确', () => {
|
||||||
// 设置很多位
|
// 设置很多位,但不超过64位限制
|
||||||
for (let i = 0; i < 100; i += 2) {
|
for (let i = 0; i < 64; i += 2) {
|
||||||
bits.set(i);
|
bits.set(i);
|
||||||
}
|
}
|
||||||
expect(bits.cardinality()).toBe(50);
|
expect(bits.cardinality()).toBe(32); // 32个偶数位
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -254,7 +256,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
expect(bits.isEmpty()).toBe(false);
|
expect(bits.isEmpty()).toBe(false);
|
||||||
bits.clearAll();
|
bits.clearAll();
|
||||||
expect(bits.isEmpty()).toBe(true);
|
expect(bits.isEmpty()).toBe(true);
|
||||||
expect(bits.getValue().isZero()).toBe(true);
|
expect(BitMask64Utils.isZero(bits.getValue())).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('clearAll后应该能重新设置位', () => {
|
it('clearAll后应该能重新设置位', () => {
|
||||||
@@ -276,14 +278,14 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
const newBits = new Bits();
|
const newBits = new Bits();
|
||||||
newBits.copyFrom(bits);
|
newBits.copyFrom(bits);
|
||||||
|
|
||||||
expect(newBits.getValue().equals(bits.getValue())).toBe(true);
|
expect(BitMask64Utils.equals(newBits.getValue(), bits.getValue())).toBe(true);
|
||||||
expect(newBits.equals(bits)).toBe(true);
|
expect(newBits.equals(bits)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('clone应该创建相同的副本', () => {
|
it('clone应该创建相同的副本', () => {
|
||||||
const clonedBits = bits.clone();
|
const clonedBits = bits.clone();
|
||||||
|
|
||||||
expect(clonedBits.getValue().equals(bits.getValue())).toBe(true);
|
expect(BitMask64Utils.equals(clonedBits.getValue(), bits.getValue())).toBe(true);
|
||||||
expect(clonedBits.equals(bits)).toBe(true);
|
expect(clonedBits.equals(bits)).toBe(true);
|
||||||
expect(clonedBits).not.toBe(bits); // 应该是不同的对象
|
expect(clonedBits).not.toBe(bits); // 应该是不同的对象
|
||||||
});
|
});
|
||||||
@@ -300,7 +302,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
describe('值操作', () => {
|
describe('值操作', () => {
|
||||||
it('getValue和setValue应该正确工作', () => {
|
it('getValue和setValue应该正确工作', () => {
|
||||||
bits.setValue(42);
|
bits.setValue(42);
|
||||||
expect(bits.getValue().toString()).toBe('42');
|
expect(bits.toHexString()).toBe('0x2A');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('setValue应该正确反映在位操作中', () => {
|
it('setValue应该正确反映在位操作中', () => {
|
||||||
@@ -352,24 +354,24 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
|
|
||||||
it('fromBinaryString应该正确解析', () => {
|
it('fromBinaryString应该正确解析', () => {
|
||||||
const parsedBits = Bits.fromBinaryString('10101');
|
const parsedBits = Bits.fromBinaryString('10101');
|
||||||
expect(parsedBits.getValue().toString()).toBe('21');
|
expect(parsedBits.toHexString()).toBe('0x15');
|
||||||
expect(parsedBits.equals(bits)).toBe(true);
|
expect(parsedBits.equals(bits)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fromBinaryString应该处理带空格的字符串', () => {
|
it('fromBinaryString应该处理带空格的字符串', () => {
|
||||||
const parsedBits = Bits.fromBinaryString('0001 0101');
|
const parsedBits = Bits.fromBinaryString('0001 0101');
|
||||||
expect(parsedBits.getValue().toString()).toBe('21');
|
expect(parsedBits.toHexString()).toBe('0x15');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fromHexString应该正确解析', () => {
|
it('fromHexString应该正确解析', () => {
|
||||||
const parsedBits = Bits.fromHexString('0x15');
|
const parsedBits = Bits.fromHexString('0x15');
|
||||||
expect(parsedBits.getValue().toString()).toBe('21');
|
expect(parsedBits.toHexString()).toBe('0x15');
|
||||||
expect(parsedBits.equals(bits)).toBe(true);
|
expect(parsedBits.equals(bits)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fromHexString应该处理不带0x前缀的字符串', () => {
|
it('fromHexString应该处理不带0x前缀的字符串', () => {
|
||||||
const parsedBits = Bits.fromHexString('15');
|
const parsedBits = Bits.fromHexString('15');
|
||||||
expect(parsedBits.getValue().toString()).toBe('21');
|
expect(parsedBits.toHexString()).toBe('0x15');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -429,101 +431,76 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('大数值处理', () => {
|
describe('64位边界处理', () => {
|
||||||
const supportsBigInt = (() => {
|
it('应该正确处理64位边界', () => {
|
||||||
try {
|
bits.set(63); // 最高位
|
||||||
return typeof BigInt !== 'undefined' && BigInt(0) === 0n;
|
expect(bits.get(63)).toBe(true);
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
it('应该能够处理超过64位的数值', () => {
|
|
||||||
if (!supportsBigInt) {
|
|
||||||
pending('BigInt not supported in this environment');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bits.set(100);
|
|
||||||
expect(bits.get(100)).toBe(true);
|
|
||||||
expect(bits.cardinality()).toBe(1);
|
expect(bits.cardinality()).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('应该能够处理非常大的位索引', () => {
|
it('超过64位索引应该抛出错误', () => {
|
||||||
if (!supportsBigInt) {
|
expect(() => bits.set(64)).toThrow('Bit index exceeds 64-bit limit');
|
||||||
pending('BigInt not supported in this environment');
|
expect(() => bits.set(100)).toThrow('Bit index exceeds 64-bit limit');
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bits.set(1000);
|
|
||||||
bits.set(2000);
|
|
||||||
expect(bits.get(1000)).toBe(true);
|
|
||||||
expect(bits.get(2000)).toBe(true);
|
|
||||||
expect(bits.cardinality()).toBe(2);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('大数值的位运算应该正确', () => {
|
it('64位范围内的位运算应该正确', () => {
|
||||||
if (!supportsBigInt) {
|
const bits1 = new Bits();
|
||||||
pending('BigInt not supported in this environment');
|
const bits2 = new Bits();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const largeBits1 = new Bits();
|
bits1.set(10);
|
||||||
const largeBits2 = new Bits();
|
bits1.set(20);
|
||||||
|
|
||||||
largeBits1.set(100);
|
bits2.set(10);
|
||||||
largeBits1.set(200);
|
bits2.set(30);
|
||||||
|
|
||||||
largeBits2.set(100);
|
const result = bits1.and(bits2);
|
||||||
largeBits2.set(150);
|
expect(result.get(10)).toBe(true);
|
||||||
|
expect(result.get(20)).toBe(false);
|
||||||
const result = largeBits1.and(largeBits2);
|
expect(result.get(30)).toBe(false);
|
||||||
expect(result.get(100)).toBe(true);
|
|
||||||
expect(result.get(150)).toBe(false);
|
|
||||||
expect(result.get(200)).toBe(false);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('性能测试', () => {
|
describe('性能测试', () => {
|
||||||
it('大量位设置操作应该高效', () => {
|
it('64位范围内的位设置操作应该高效', () => {
|
||||||
const startTime = performance.now();
|
const startTime = performance.now();
|
||||||
|
|
||||||
for (let i = 0; i < 10000; i++) {
|
// 只在64位范围内设置
|
||||||
|
for (let i = 0; i < 64; i++) {
|
||||||
bits.set(i);
|
bits.set(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
const endTime = performance.now();
|
const endTime = performance.now();
|
||||||
expect(endTime - startTime).toBeLessThan(1000); // 应该在1秒内完成
|
expect(endTime - startTime).toBeLessThan(100); // 应该在100ms内完成
|
||||||
expect(bits.cardinality()).toBe(10000);
|
expect(bits.cardinality()).toBe(64);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('大量位查询操作应该高效', () => {
|
it('64位范围内的位查询操作应该高效', () => {
|
||||||
// 先设置一些位
|
// 先设置一些位
|
||||||
for (let i = 0; i < 1000; i += 2) {
|
for (let i = 0; i < 64; i += 2) {
|
||||||
bits.set(i);
|
bits.set(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
const startTime = performance.now();
|
const startTime = performance.now();
|
||||||
|
|
||||||
let trueCount = 0;
|
let trueCount = 0;
|
||||||
for (let i = 0; i < 10000; i++) {
|
for (let i = 0; i < 64; i++) {
|
||||||
if (bits.get(i)) {
|
if (bits.get(i)) {
|
||||||
trueCount++;
|
trueCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const endTime = performance.now();
|
const endTime = performance.now();
|
||||||
expect(endTime - startTime).toBeLessThan(100); // 应该在100ms内完成
|
expect(endTime - startTime).toBeLessThan(10); // 应该在10ms内完成
|
||||||
expect(trueCount).toBe(500); // 500个偶数位
|
expect(trueCount).toBe(32); // 32个偶数位
|
||||||
});
|
});
|
||||||
|
|
||||||
it('位运算操作应该高效', () => {
|
it('位运算操作应该高效', () => {
|
||||||
const otherBits = new Bits();
|
const otherBits = new Bits();
|
||||||
|
|
||||||
// 设置一些位
|
// 设置一些位,限制在64位范围内
|
||||||
for (let i = 0; i < 1000; i++) {
|
for (let i = 0; i < 32; i++) {
|
||||||
bits.set(i * 2);
|
bits.set(i * 2); // 偶数位
|
||||||
otherBits.set(i * 2 + 1);
|
otherBits.set(i * 2 + 1); // 奇数位
|
||||||
}
|
}
|
||||||
|
|
||||||
const startTime = performance.now();
|
const startTime = performance.now();
|
||||||
@@ -533,13 +510,13 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const endTime = performance.now();
|
const endTime = performance.now();
|
||||||
expect(endTime - startTime).toBeLessThan(100); // 应该在100ms内完成
|
expect(endTime - startTime).toBeLessThan(50); // 应该在50ms内完成
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('边界情况和错误处理', () => {
|
describe('边界情况和错误处理', () => {
|
||||||
it('应该处理0值的各种操作', () => {
|
it('应该处理0值的各种操作', () => {
|
||||||
const zeroBits = new Bits(BigIntFactory.zero());
|
const zeroBits = new Bits(0);
|
||||||
expect(zeroBits.isEmpty()).toBe(true);
|
expect(zeroBits.isEmpty()).toBe(true);
|
||||||
expect(zeroBits.cardinality()).toBe(0);
|
expect(zeroBits.cardinality()).toBe(0);
|
||||||
expect(zeroBits.getHighestBitIndex()).toBe(-1);
|
expect(zeroBits.getHighestBitIndex()).toBe(-1);
|
||||||
@@ -547,7 +524,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('应该处理最大BigInt值', () => {
|
it('应该处理最大BigInt值', () => {
|
||||||
const maxBits = new Bits(BigIntFactory.create(Number.MAX_SAFE_INTEGER));
|
const maxBits = new Bits(Number.MAX_SAFE_INTEGER);
|
||||||
expect(maxBits.isEmpty()).toBe(false);
|
expect(maxBits.isEmpty()).toBe(false);
|
||||||
expect(maxBits.cardinality()).toBeGreaterThan(0);
|
expect(maxBits.cardinality()).toBeGreaterThan(0);
|
||||||
});
|
});
|
||||||
@@ -563,7 +540,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('连续的设置和清除操作应该正确', () => {
|
it('连续的设置和清除操作应该正确', () => {
|
||||||
for (let i = 0; i < 100; i++) {
|
for (let i = 0; i < 64; i++) {
|
||||||
bits.set(i);
|
bits.set(i);
|
||||||
expect(bits.get(i)).toBe(true);
|
expect(bits.get(i)).toBe(true);
|
||||||
bits.clear(i);
|
bits.clear(i);
|
||||||
|
|||||||
Reference in New Issue
Block a user