对bigint进行兼容处理(不支持的环境回退到兼容模式)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { BitMaskOptimizer } from '../../../src/ECS/Core/BitMaskOptimizer';
|
||||
import { BigIntFactory } from '../../../src/ECS/Utils/BigIntCompatibility';
|
||||
|
||||
describe('BitMaskOptimizer - 位掩码优化器测试', () => {
|
||||
let optimizer: BitMaskOptimizer;
|
||||
@@ -58,15 +59,15 @@ describe('BitMaskOptimizer - 位掩码优化器测试', () => {
|
||||
|
||||
it('应该能够创建单个组件的掩码', () => {
|
||||
const mask = optimizer.createSingleComponentMask('Transform');
|
||||
expect(mask).toBe(1n);
|
||||
expect(mask.toString()).toBe('1');
|
||||
});
|
||||
|
||||
it('不同组件应该有不同的掩码', () => {
|
||||
const transformMask = optimizer.createSingleComponentMask('Transform');
|
||||
const velocityMask = optimizer.createSingleComponentMask('Velocity');
|
||||
|
||||
expect(transformMask).toBe(1n);
|
||||
expect(velocityMask).toBe(2n);
|
||||
expect(transformMask.toString()).toBe('1');
|
||||
expect(velocityMask.toString()).toBe('2');
|
||||
});
|
||||
|
||||
it('创建未注册组件的掩码应该抛出错误', () => {
|
||||
@@ -91,7 +92,7 @@ describe('BitMaskOptimizer - 位掩码优化器测试', () => {
|
||||
|
||||
it('应该能够创建多个组件的组合掩码', () => {
|
||||
const mask = optimizer.createCombinedMask(['Transform', 'Velocity']);
|
||||
expect(mask).toBe(3n); // 1n | 2n = 3n
|
||||
expect(mask.toString()).toBe('3'); // 1 | 2 = 3
|
||||
});
|
||||
|
||||
it('组件顺序不应该影响掩码结果', () => {
|
||||
@@ -102,12 +103,12 @@ describe('BitMaskOptimizer - 位掩码优化器测试', () => {
|
||||
|
||||
it('三个组件的组合掩码应该正确', () => {
|
||||
const mask = optimizer.createCombinedMask(['Transform', 'Velocity', 'Health']);
|
||||
expect(mask).toBe(7n); // 1n | 2n | 4n = 7n
|
||||
expect(mask.toString()).toBe('7'); // 1 | 2 | 4 = 7
|
||||
});
|
||||
|
||||
it('空数组应该返回0掩码', () => {
|
||||
const mask = optimizer.createCombinedMask([]);
|
||||
expect(mask).toBe(0n);
|
||||
expect(mask.isZero()).toBe(true);
|
||||
});
|
||||
|
||||
it('包含未注册组件应该抛出错误', () => {
|
||||
@@ -181,7 +182,7 @@ describe('BitMaskOptimizer - 位掩码优化器测试', () => {
|
||||
const originalMask = optimizer.createSingleComponentMask('Transform');
|
||||
const newMask = optimizer.removeComponentFromMask(originalMask, 'Velocity');
|
||||
|
||||
expect(newMask).toBe(originalMask);
|
||||
expect(newMask.equals(originalMask)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -203,7 +204,7 @@ describe('BitMaskOptimizer - 位掩码优化器测试', () => {
|
||||
});
|
||||
|
||||
it('空掩码应该返回空数组', () => {
|
||||
const componentNames = optimizer.maskToComponentNames(0n);
|
||||
const componentNames = optimizer.maskToComponentNames(BigIntFactory.zero());
|
||||
expect(componentNames).toEqual([]);
|
||||
});
|
||||
|
||||
@@ -218,7 +219,7 @@ describe('BitMaskOptimizer - 位掩码优化器测试', () => {
|
||||
});
|
||||
|
||||
it('空掩码的组件数量应该为0', () => {
|
||||
expect(optimizer.getComponentCount(0n)).toBe(0);
|
||||
expect(optimizer.getComponentCount(BigIntFactory.zero())).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -288,12 +289,12 @@ describe('BitMaskOptimizer - 位掩码优化器测试', () => {
|
||||
}
|
||||
|
||||
const mask = optimizer.createSingleComponentMask('Component63');
|
||||
expect(mask).toBe(1n << 63n);
|
||||
expect(mask.toString()).toBe(BigIntFactory.one().shiftLeft(63).toString());
|
||||
});
|
||||
|
||||
it('空组件名称数组的组合掩码', () => {
|
||||
const mask = optimizer.createCombinedMask([]);
|
||||
expect(mask).toBe(0n);
|
||||
expect(mask.isZero()).toBe(true);
|
||||
expect(optimizer.getComponentCount(mask)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
ComponentType
|
||||
} from '../../../src/ECS/Core/ComponentStorage';
|
||||
import { Component } from '../../../src/ECS/Component';
|
||||
import { BigIntFactory } from '../../../src/ECS/Utils/BigIntCompatibility';
|
||||
|
||||
// 测试组件类
|
||||
class TestComponent extends Component {
|
||||
@@ -93,8 +94,8 @@ describe('ComponentRegistry - 组件注册表测试', () => {
|
||||
const mask1 = ComponentRegistry.getBitMask(TestComponent);
|
||||
const mask2 = ComponentRegistry.getBitMask(PositionComponent);
|
||||
|
||||
expect(mask1).toBe(BigInt(1)); // 2^0
|
||||
expect(mask2).toBe(BigInt(2)); // 2^1
|
||||
expect(mask1.toString()).toBe('1'); // 2^0
|
||||
expect(mask2.toString()).toBe('2'); // 2^1
|
||||
});
|
||||
|
||||
test('应该能够获取组件的位索引', () => {
|
||||
@@ -491,13 +492,12 @@ describe('ComponentStorageManager - 组件存储管理器测试', () => {
|
||||
const mask = manager.getComponentMask(1);
|
||||
|
||||
// 应该包含TestComponent(位0)和PositionComponent(位1)的掩码
|
||||
const expectedMask = BigInt(1) | BigInt(2); // 0b11
|
||||
expect(mask).toBe(expectedMask);
|
||||
expect(mask.toString()).toBe('3'); // 1 | 2 = 3
|
||||
});
|
||||
|
||||
test('没有组件的实体应该有零掩码', () => {
|
||||
const mask = manager.getComponentMask(999);
|
||||
expect(mask).toBe(BigInt(0));
|
||||
expect(mask.isZero()).toBe(true);
|
||||
});
|
||||
|
||||
test('添加和移除组件应该更新掩码', () => {
|
||||
@@ -506,15 +506,15 @@ describe('ComponentStorageManager - 组件存储管理器测试', () => {
|
||||
|
||||
manager.addComponent(1, new TestComponent(100));
|
||||
let mask = manager.getComponentMask(1);
|
||||
expect(mask).toBe(BigInt(1));
|
||||
expect(mask.toString()).toBe('1');
|
||||
|
||||
manager.addComponent(1, new PositionComponent(10, 20));
|
||||
mask = manager.getComponentMask(1);
|
||||
expect(mask).toBe(BigInt(3)); // 0b11
|
||||
expect(mask.toString()).toBe('3'); // 0b11
|
||||
|
||||
manager.removeComponent(1, TestComponent);
|
||||
mask = manager.getComponentMask(1);
|
||||
expect(mask).toBe(BigInt(2)); // 0b10
|
||||
expect(mask.toString()).toBe('2'); // 0b10
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
338
tests/ECS/Utils/BigIntCompatibility.test.ts
Normal file
338
tests/ECS/Utils/BigIntCompatibility.test.ts
Normal file
@@ -0,0 +1,338 @@
|
||||
import {
|
||||
BigIntFactory,
|
||||
IBigIntLike,
|
||||
EnvironmentInfo
|
||||
} from '../../../src/ECS/Utils/BigIntCompatibility';
|
||||
|
||||
describe('BigInt兼容性测试', () => {
|
||||
describe('BigIntFactory环境检测', () => {
|
||||
it('应该能够检测BigInt支持情况', () => {
|
||||
const isSupported = BigIntFactory.isNativeSupported();
|
||||
expect(typeof isSupported).toBe('boolean');
|
||||
});
|
||||
|
||||
it('应该返回环境信息', () => {
|
||||
const envInfo = BigIntFactory.getEnvironmentInfo();
|
||||
expect(envInfo).toBeDefined();
|
||||
expect(typeof envInfo.supportsBigInt).toBe('boolean');
|
||||
expect(typeof envInfo.environment).toBe('string');
|
||||
expect(typeof envInfo.jsEngine).toBe('string');
|
||||
});
|
||||
|
||||
it('环境信息应该包含合理的字段', () => {
|
||||
const envInfo = BigIntFactory.getEnvironmentInfo();
|
||||
expect(envInfo.environment).not.toBe('');
|
||||
expect(envInfo.jsEngine).not.toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('BigIntFactory基本创建', () => {
|
||||
it('应该能够创建零值', () => {
|
||||
const zero = BigIntFactory.zero();
|
||||
expect(zero.isZero()).toBe(true);
|
||||
expect(zero.toString()).toBe('0');
|
||||
});
|
||||
|
||||
it('应该能够创建1值', () => {
|
||||
const one = BigIntFactory.one();
|
||||
expect(one.isZero()).toBe(false);
|
||||
expect(one.toString()).toBe('1');
|
||||
});
|
||||
|
||||
it('应该能够从数值创建', () => {
|
||||
const value = BigIntFactory.create(42);
|
||||
expect(value.toString()).toBe('42');
|
||||
expect(value.valueOf()).toBe(42);
|
||||
});
|
||||
|
||||
it('应该能够从字符串创建', () => {
|
||||
const value = BigIntFactory.create('123');
|
||||
expect(value.toString()).toBe('123');
|
||||
});
|
||||
|
||||
it('应该能够从原生BigInt创建(如果支持)', () => {
|
||||
if (BigIntFactory.isNativeSupported()) {
|
||||
const value = BigIntFactory.create(BigInt(456));
|
||||
expect(value.toString()).toBe('456');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('IBigIntLike基本操作', () => {
|
||||
let value1: IBigIntLike;
|
||||
let value2: IBigIntLike;
|
||||
|
||||
beforeEach(() => {
|
||||
value1 = BigIntFactory.create(5); // 101 in binary
|
||||
value2 = BigIntFactory.create(3); // 011 in binary
|
||||
});
|
||||
|
||||
it('should支持字符串转换', () => {
|
||||
expect(value1.toString()).toBe('5');
|
||||
expect(value2.toString()).toBe('3');
|
||||
});
|
||||
|
||||
it('应该支持十六进制转换', () => {
|
||||
const value = BigIntFactory.create(255);
|
||||
expect(value.toString(16)).toBe('FF');
|
||||
});
|
||||
|
||||
it('应该支持二进制转换', () => {
|
||||
expect(value1.toString(2)).toBe('101');
|
||||
expect(value2.toString(2)).toBe('11');
|
||||
});
|
||||
|
||||
it('应该支持相等比较', () => {
|
||||
const value1Copy = BigIntFactory.create(5);
|
||||
expect(value1.equals(value1Copy)).toBe(true);
|
||||
expect(value1.equals(value2)).toBe(false);
|
||||
});
|
||||
|
||||
it('应该支持零值检查', () => {
|
||||
const zero = BigIntFactory.zero();
|
||||
expect(zero.isZero()).toBe(true);
|
||||
expect(value1.isZero()).toBe(false);
|
||||
});
|
||||
|
||||
it('应该支持克隆操作', () => {
|
||||
const cloned = value1.clone();
|
||||
expect(cloned.equals(value1)).toBe(true);
|
||||
expect(cloned).not.toBe(value1); // 不同的对象引用
|
||||
});
|
||||
});
|
||||
|
||||
describe('位运算操作', () => {
|
||||
let value1: IBigIntLike; // 5 = 101
|
||||
let value2: IBigIntLike; // 3 = 011
|
||||
|
||||
beforeEach(() => {
|
||||
value1 = BigIntFactory.create(5);
|
||||
value2 = BigIntFactory.create(3);
|
||||
});
|
||||
|
||||
it('AND运算应该正确', () => {
|
||||
const result = value1.and(value2);
|
||||
expect(result.toString()).toBe('1'); // 101 & 011 = 001
|
||||
});
|
||||
|
||||
it('OR运算应该正确', () => {
|
||||
const result = value1.or(value2);
|
||||
expect(result.toString()).toBe('7'); // 101 | 011 = 111
|
||||
});
|
||||
|
||||
it('XOR运算应该正确', () => {
|
||||
const result = value1.xor(value2);
|
||||
expect(result.toString()).toBe('6'); // 101 ^ 011 = 110
|
||||
});
|
||||
|
||||
it('NOT运算应该正确(8位限制)', () => {
|
||||
const value = BigIntFactory.create(5); // 00000101
|
||||
const result = value.not(8);
|
||||
expect(result.toString()).toBe('250'); // 11111010 = 250
|
||||
});
|
||||
|
||||
it('左移位运算应该正确', () => {
|
||||
const result = value1.shiftLeft(2);
|
||||
expect(result.toString()).toBe('20'); // 101 << 2 = 10100 = 20
|
||||
});
|
||||
|
||||
it('右移位运算应该正确', () => {
|
||||
const result = value1.shiftRight(1);
|
||||
expect(result.toString()).toBe('2'); // 101 >> 1 = 10 = 2
|
||||
});
|
||||
|
||||
it('移位0位应该返回相同值', () => {
|
||||
const result = value1.shiftLeft(0);
|
||||
expect(result.equals(value1)).toBe(true);
|
||||
});
|
||||
|
||||
it('右移超过位数应该返回0', () => {
|
||||
const result = value1.shiftRight(10);
|
||||
expect(result.isZero()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('复杂位运算场景', () => {
|
||||
it('应该正确处理大数值位运算', () => {
|
||||
const large1 = BigIntFactory.create(0xFFFFFFFF); // 32位全1
|
||||
const large2 = BigIntFactory.create(0x12345678);
|
||||
|
||||
const andResult = large1.and(large2);
|
||||
expect(andResult.toString(16)).toBe('12345678');
|
||||
|
||||
const orResult = large1.or(large2);
|
||||
expect(orResult.toString(16)).toBe('FFFFFFFF');
|
||||
});
|
||||
|
||||
it('应该正确处理连续位运算', () => {
|
||||
let result = BigIntFactory.create(1);
|
||||
|
||||
// 构建 111111 (6个1)
|
||||
for (let i = 1; i < 6; i++) {
|
||||
const shifted = BigIntFactory.one().shiftLeft(i);
|
||||
result = result.or(shifted);
|
||||
}
|
||||
|
||||
expect(result.toString()).toBe('63'); // 111111 = 63
|
||||
expect(result.toString(2)).toBe('111111');
|
||||
});
|
||||
|
||||
it('应该正确处理掩码操作', () => {
|
||||
const value = BigIntFactory.create(0b10110101); // 181
|
||||
const mask = BigIntFactory.create(0b00001111); // 15, 低4位掩码
|
||||
|
||||
const masked = value.and(mask);
|
||||
expect(masked.toString()).toBe('5'); // 0101 = 5
|
||||
});
|
||||
});
|
||||
|
||||
describe('字符串解析功能', () => {
|
||||
it('应该支持从二进制字符串创建', () => {
|
||||
const value = BigIntFactory.fromBinaryString('10101');
|
||||
expect(value.toString()).toBe('21');
|
||||
});
|
||||
|
||||
it('应该支持从十六进制字符串创建', () => {
|
||||
const value1 = BigIntFactory.fromHexString('0xFF');
|
||||
const value2 = BigIntFactory.fromHexString('FF');
|
||||
|
||||
expect(value1.toString()).toBe('255');
|
||||
expect(value2.toString()).toBe('255');
|
||||
expect(value1.equals(value2)).toBe(true);
|
||||
});
|
||||
|
||||
it('应该正确处理大的十六进制值', () => {
|
||||
const value = BigIntFactory.fromHexString('0x12345678');
|
||||
expect(value.toString()).toBe('305419896');
|
||||
});
|
||||
|
||||
it('应该正确处理长二进制字符串', () => {
|
||||
const binaryStr = '11111111111111111111111111111111'; // 32个1
|
||||
const value = BigIntFactory.fromBinaryString(binaryStr);
|
||||
expect(value.toString()).toBe('4294967295'); // 2^32 - 1
|
||||
});
|
||||
});
|
||||
|
||||
describe('边界情况和错误处理', () => {
|
||||
it('应该正确处理零值的所有操作', () => {
|
||||
const zero = BigIntFactory.zero();
|
||||
const one = BigIntFactory.one();
|
||||
|
||||
expect(zero.and(one).isZero()).toBe(true);
|
||||
expect(zero.or(one).equals(one)).toBe(true);
|
||||
expect(zero.xor(one).equals(one)).toBe(true);
|
||||
expect(zero.shiftLeft(5).isZero()).toBe(true);
|
||||
expect(zero.shiftRight(5).isZero()).toBe(true);
|
||||
});
|
||||
|
||||
it('应该正确处理1值的位运算', () => {
|
||||
const one = BigIntFactory.one();
|
||||
const zero = BigIntFactory.zero();
|
||||
|
||||
expect(one.and(zero).isZero()).toBe(true);
|
||||
expect(one.or(zero).equals(one)).toBe(true);
|
||||
expect(one.xor(zero).equals(one)).toBe(true);
|
||||
});
|
||||
|
||||
it('应该处理不支持的字符串进制', () => {
|
||||
const value = BigIntFactory.create(255);
|
||||
expect(() => value.toString(8)).toThrow();
|
||||
});
|
||||
|
||||
it('NOT运算应该正确处理不同的位数限制', () => {
|
||||
const value = BigIntFactory.one(); // 1
|
||||
|
||||
const not8 = value.not(8);
|
||||
expect(not8.toString()).toBe('254'); // 11111110 = 254
|
||||
|
||||
const not16 = value.not(16);
|
||||
expect(not16.toString()).toBe('65534'); // 1111111111111110 = 65534
|
||||
});
|
||||
});
|
||||
|
||||
describe('性能和兼容性测试', () => {
|
||||
it('两种实现应该产生相同的运算结果', () => {
|
||||
// 测试各种运算在两种模式下的一致性
|
||||
const testCases = [
|
||||
{ a: 0, b: 0 },
|
||||
{ a: 1, b: 1 },
|
||||
{ a: 5, b: 3 },
|
||||
{ a: 255, b: 128 },
|
||||
{ a: 65535, b: 32768 }
|
||||
];
|
||||
|
||||
testCases.forEach(({ a, b }) => {
|
||||
const val1 = BigIntFactory.create(a);
|
||||
const val2 = BigIntFactory.create(b);
|
||||
|
||||
// 基本运算
|
||||
const and = val1.and(val2);
|
||||
const or = val1.or(val2);
|
||||
const xor = val1.xor(val2);
|
||||
|
||||
// 验证运算结果的一致性
|
||||
expect(and.toString()).toBe((a & b).toString());
|
||||
expect(or.toString()).toBe((a | b).toString());
|
||||
expect(xor.toString()).toBe((a ^ b).toString());
|
||||
});
|
||||
});
|
||||
|
||||
it('大量运算应该保持高性能', () => {
|
||||
const startTime = performance.now();
|
||||
|
||||
let result = BigIntFactory.zero();
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
const value = BigIntFactory.create(i);
|
||||
result = result.or(value.shiftLeft(i % 32));
|
||||
}
|
||||
|
||||
const endTime = performance.now();
|
||||
expect(endTime - startTime).toBeLessThan(1000); // 应该在1秒内完成
|
||||
expect(result.isZero()).toBe(false);
|
||||
});
|
||||
|
||||
it('应该支持ECS框架中常见的位掩码操作', () => {
|
||||
// 模拟组件位掩码
|
||||
const componentMasks: IBigIntLike[] = [];
|
||||
|
||||
// 创建64个组件的位掩码
|
||||
for (let i = 0; i < 64; i++) {
|
||||
componentMasks.push(BigIntFactory.one().shiftLeft(i));
|
||||
}
|
||||
|
||||
// 组合多个组件掩码
|
||||
let combinedMask = BigIntFactory.zero();
|
||||
for (let i = 0; i < 10; i++) {
|
||||
combinedMask = combinedMask.or(componentMasks[i * 2]);
|
||||
}
|
||||
|
||||
// 检查是否包含特定组件
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const hasComponent = !combinedMask.and(componentMasks[i * 2]).isZero();
|
||||
expect(hasComponent).toBe(true);
|
||||
|
||||
const hasOtherComponent = !combinedMask.and(componentMasks[i * 2 + 1]).isZero();
|
||||
expect(hasOtherComponent).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('与Core类集成测试', () => {
|
||||
// 这里我们不直接导入Core来避免循环依赖,而是测试工厂方法
|
||||
it('BigIntFactory应该能够为Core提供环境信息', () => {
|
||||
const envInfo = BigIntFactory.getEnvironmentInfo();
|
||||
|
||||
// 验证所有必需的字段都存在
|
||||
expect(envInfo.supportsBigInt).toBeDefined();
|
||||
expect(envInfo.environment).toBeDefined();
|
||||
expect(envInfo.jsEngine).toBeDefined();
|
||||
});
|
||||
|
||||
it('应该提供详细的环境检测信息', () => {
|
||||
const envInfo = BigIntFactory.getEnvironmentInfo();
|
||||
|
||||
// 环境信息应该有意义
|
||||
expect(envInfo.environment).not.toBe('Unknown');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Bits } from '../../../src/ECS/Utils/Bits';
|
||||
import { BigIntFactory } from '../../../src/ECS/Utils/BigIntCompatibility';
|
||||
|
||||
describe('Bits - 高性能位操作类测试', () => {
|
||||
let bits: Bits;
|
||||
@@ -11,12 +12,12 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
it('应该能够创建空的Bits对象', () => {
|
||||
expect(bits).toBeDefined();
|
||||
expect(bits.isEmpty()).toBe(true);
|
||||
expect(bits.getValue()).toBe(0n);
|
||||
expect(bits.getValue().isZero()).toBe(true);
|
||||
});
|
||||
|
||||
it('应该能够使用初始值创建Bits对象', () => {
|
||||
const bitsWithValue = new Bits(5n); // 二进制: 101
|
||||
expect(bitsWithValue.getValue()).toBe(5n);
|
||||
const bitsWithValue = new Bits(BigIntFactory.create(5)); // 二进制: 101
|
||||
expect(bitsWithValue.getValue().toString()).toBe('5');
|
||||
expect(bitsWithValue.isEmpty()).toBe(false);
|
||||
expect(bitsWithValue.get(0)).toBe(true); // 第0位
|
||||
expect(bitsWithValue.get(1)).toBe(false); // 第1位
|
||||
@@ -25,7 +26,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
|
||||
it('默认构造函数应该创建值为0的对象', () => {
|
||||
const defaultBits = new Bits();
|
||||
expect(defaultBits.getValue()).toBe(0n);
|
||||
expect(defaultBits.getValue().isZero()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -33,22 +34,22 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
it('应该能够设置指定位置的位', () => {
|
||||
bits.set(0);
|
||||
expect(bits.get(0)).toBe(true);
|
||||
expect(bits.getValue()).toBe(1n);
|
||||
expect(bits.getValue().toString()).toBe('1');
|
||||
|
||||
bits.set(3);
|
||||
expect(bits.get(3)).toBe(true);
|
||||
expect(bits.getValue()).toBe(9n); // 1001 in binary
|
||||
expect(bits.getValue().toString()).toBe('9'); // 1001 in binary
|
||||
});
|
||||
|
||||
it('应该能够清除指定位置的位', () => {
|
||||
bits.set(0);
|
||||
bits.set(1);
|
||||
bits.set(2);
|
||||
expect(bits.getValue()).toBe(7n); // 111 in binary
|
||||
expect(bits.getValue().toString()).toBe('7'); // 111 in binary
|
||||
|
||||
bits.clear(1);
|
||||
expect(bits.get(1)).toBe(false);
|
||||
expect(bits.getValue()).toBe(5n); // 101 in binary
|
||||
expect(bits.getValue().toString()).toBe('5'); // 101 in binary
|
||||
});
|
||||
|
||||
it('重复设置同一位应该保持不变', () => {
|
||||
@@ -56,12 +57,12 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
const value1 = bits.getValue();
|
||||
bits.set(0);
|
||||
const value2 = bits.getValue();
|
||||
expect(value1).toBe(value2);
|
||||
expect(value1.equals(value2)).toBe(true);
|
||||
});
|
||||
|
||||
it('清除未设置的位应该安全', () => {
|
||||
bits.clear(5);
|
||||
expect(bits.getValue()).toBe(0n);
|
||||
expect(bits.getValue().isZero()).toBe(true);
|
||||
});
|
||||
|
||||
it('设置负索引应该抛出错误', () => {
|
||||
@@ -122,7 +123,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
|
||||
it('AND运算应该正确', () => {
|
||||
const result = bits.and(otherBits);
|
||||
expect(result.getValue()).toBe(4n); // 10101 & 01110 = 00100 = 4
|
||||
expect(result.getValue().toString()).toBe('4'); // 10101 & 01110 = 00100 = 4
|
||||
expect(result.get(2)).toBe(true);
|
||||
expect(result.get(0)).toBe(false);
|
||||
expect(result.get(1)).toBe(false);
|
||||
@@ -130,7 +131,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
|
||||
it('OR运算应该正确', () => {
|
||||
const result = bits.or(otherBits);
|
||||
expect(result.getValue()).toBe(31n); // 10101 | 01110 = 11111 = 31
|
||||
expect(result.getValue().toString()).toBe('31'); // 10101 | 01110 = 11111 = 31
|
||||
expect(result.get(0)).toBe(true);
|
||||
expect(result.get(1)).toBe(true);
|
||||
expect(result.get(2)).toBe(true);
|
||||
@@ -140,7 +141,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
|
||||
it('XOR运算应该正确', () => {
|
||||
const result = bits.xor(otherBits);
|
||||
expect(result.getValue()).toBe(27n); // 10101 ^ 01110 = 11011 = 27
|
||||
expect(result.getValue().toString()).toBe('27'); // 10101 ^ 01110 = 11011 = 27
|
||||
expect(result.get(0)).toBe(true);
|
||||
expect(result.get(1)).toBe(true);
|
||||
expect(result.get(2)).toBe(false); // 相同位XOR为0
|
||||
@@ -149,16 +150,16 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
});
|
||||
|
||||
it('NOT运算应该正确', () => {
|
||||
const simpleBits = new Bits(5n); // 101 in binary
|
||||
const simpleBits = new Bits(BigIntFactory.create(5)); // 101 in binary
|
||||
const result = simpleBits.not(8); // 限制为8位
|
||||
expect(result.getValue()).toBe(250n); // ~00000101 = 11111010 = 250 (8位)
|
||||
expect(result.getValue().toString()).toBe('250'); // ~00000101 = 11111010 = 250 (8位)
|
||||
});
|
||||
|
||||
it('NOT运算默认64位应该正确', () => {
|
||||
const simpleBits = new Bits(1n);
|
||||
const simpleBits = new Bits(BigIntFactory.create(1));
|
||||
const result = simpleBits.not();
|
||||
const expected = (1n << 64n) - 2n; // 64位全1减去最低位
|
||||
expect(result.getValue()).toBe(expected);
|
||||
const expected = BigIntFactory.one().shiftLeft(64).valueOf() - 2; // 64位全1减去最低位
|
||||
expect(result.getValue().valueOf()).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -253,7 +254,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
expect(bits.isEmpty()).toBe(false);
|
||||
bits.clearAll();
|
||||
expect(bits.isEmpty()).toBe(true);
|
||||
expect(bits.getValue()).toBe(0n);
|
||||
expect(bits.getValue().isZero()).toBe(true);
|
||||
});
|
||||
|
||||
it('clearAll后应该能重新设置位', () => {
|
||||
@@ -275,14 +276,14 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
const newBits = new Bits();
|
||||
newBits.copyFrom(bits);
|
||||
|
||||
expect(newBits.getValue()).toBe(bits.getValue());
|
||||
expect(newBits.getValue().equals(bits.getValue())).toBe(true);
|
||||
expect(newBits.equals(bits)).toBe(true);
|
||||
});
|
||||
|
||||
it('clone应该创建相同的副本', () => {
|
||||
const clonedBits = bits.clone();
|
||||
|
||||
expect(clonedBits.getValue()).toBe(bits.getValue());
|
||||
expect(clonedBits.getValue().equals(bits.getValue())).toBe(true);
|
||||
expect(clonedBits.equals(bits)).toBe(true);
|
||||
expect(clonedBits).not.toBe(bits); // 应该是不同的对象
|
||||
});
|
||||
@@ -298,12 +299,12 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
|
||||
describe('值操作', () => {
|
||||
it('getValue和setValue应该正确工作', () => {
|
||||
bits.setValue(42n);
|
||||
expect(bits.getValue()).toBe(42n);
|
||||
bits.setValue(42);
|
||||
expect(bits.getValue().toString()).toBe('42');
|
||||
});
|
||||
|
||||
it('setValue应该正确反映在位操作中', () => {
|
||||
bits.setValue(5n); // 101 in binary
|
||||
bits.setValue(5); // 101 in binary
|
||||
expect(bits.get(0)).toBe(true);
|
||||
expect(bits.get(1)).toBe(false);
|
||||
expect(bits.get(2)).toBe(true);
|
||||
@@ -312,7 +313,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
it('setValue为0应该清空所有位', () => {
|
||||
bits.set(1);
|
||||
bits.set(2);
|
||||
bits.setValue(0n);
|
||||
bits.setValue(0);
|
||||
expect(bits.isEmpty()).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -351,24 +352,24 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
|
||||
it('fromBinaryString应该正确解析', () => {
|
||||
const parsedBits = Bits.fromBinaryString('10101');
|
||||
expect(parsedBits.getValue()).toBe(21n);
|
||||
expect(parsedBits.getValue().toString()).toBe('21');
|
||||
expect(parsedBits.equals(bits)).toBe(true);
|
||||
});
|
||||
|
||||
it('fromBinaryString应该处理带空格的字符串', () => {
|
||||
const parsedBits = Bits.fromBinaryString('0001 0101');
|
||||
expect(parsedBits.getValue()).toBe(21n);
|
||||
expect(parsedBits.getValue().toString()).toBe('21');
|
||||
});
|
||||
|
||||
it('fromHexString应该正确解析', () => {
|
||||
const parsedBits = Bits.fromHexString('0x15');
|
||||
expect(parsedBits.getValue()).toBe(21n);
|
||||
expect(parsedBits.getValue().toString()).toBe('21');
|
||||
expect(parsedBits.equals(bits)).toBe(true);
|
||||
});
|
||||
|
||||
it('fromHexString应该处理不带0x前缀的字符串', () => {
|
||||
const parsedBits = Bits.fromHexString('15');
|
||||
expect(parsedBits.getValue()).toBe(21n);
|
||||
expect(parsedBits.getValue().toString()).toBe('21');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -515,7 +516,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
|
||||
describe('边界情况和错误处理', () => {
|
||||
it('应该处理0值的各种操作', () => {
|
||||
const zeroBits = new Bits(0n);
|
||||
const zeroBits = new Bits(BigIntFactory.zero());
|
||||
expect(zeroBits.isEmpty()).toBe(true);
|
||||
expect(zeroBits.cardinality()).toBe(0);
|
||||
expect(zeroBits.getHighestBitIndex()).toBe(-1);
|
||||
@@ -523,7 +524,7 @@ describe('Bits - 高性能位操作类测试', () => {
|
||||
});
|
||||
|
||||
it('应该处理最大BigInt值', () => {
|
||||
const maxBits = new Bits(BigInt(Number.MAX_SAFE_INTEGER));
|
||||
const maxBits = new Bits(BigIntFactory.create(Number.MAX_SAFE_INTEGER));
|
||||
expect(maxBits.isEmpty()).toBe(false);
|
||||
expect(maxBits.cardinality()).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user