bits多态改为POD+原地操作

This commit is contained in:
YHH
2025-09-03 10:29:43 +08:00
parent bda547dd2e
commit 4869f5741e
6 changed files with 325 additions and 504 deletions

View File

@@ -5,7 +5,7 @@ import {
ComponentType
} from '../../../src/ECS/Core/ComponentStorage';
import { Component } from '../../../src/ECS/Component';
import { BigIntFactory } from '../../../src/ECS/Utils/BigIntCompatibility';
import { BitMask64Utils } from '../../../src/ECS/Utils/BigIntCompatibility';
// 测试组件类(默认使用原始存储)
class TestComponent extends Component {
@@ -116,8 +116,8 @@ describe('ComponentRegistry - 组件注册表测试', () => {
const mask1 = ComponentRegistry.getBitMask(TestComponent);
const mask2 = ComponentRegistry.getBitMask(PositionComponent);
expect(mask1.toString()).toBe('1'); // 2^0
expect(mask2.toString()).toBe('2'); // 2^1
expect(mask1.lo).toBe(1); // 2^0
expect(mask2.lo).toBe(2); // 2^1
});
test('应该能够获取组件的位索引', () => {
@@ -483,12 +483,12 @@ describe('ComponentStorageManager - 组件存储管理器测试', () => {
const mask = manager.getComponentMask(1);
// 应该包含TestComponent(位0)和PositionComponent(位1)的掩码
expect(mask.toString()).toBe('3'); // 1 | 2 = 3
expect(mask.lo).toBe(3); // 1 | 2 = 3
});
test('没有组件的实体应该有零掩码', () => {
const mask = manager.getComponentMask(999);
expect(mask.isZero()).toBe(true);
expect(BitMask64Utils.equals(mask, BitMask64Utils.ZERO)).toBe(true);
});
test('添加和移除组件应该更新掩码', () => {
@@ -497,15 +497,15 @@ describe('ComponentStorageManager - 组件存储管理器测试', () => {
manager.addComponent(1, new TestComponent(100));
let mask = manager.getComponentMask(1);
expect(mask.toString()).toBe('1');
expect(mask.lo).toBe(1);
manager.addComponent(1, new PositionComponent(10, 20));
mask = manager.getComponentMask(1);
expect(mask.toString()).toBe('3'); // 0b11
expect(mask.lo).toBe(3); // 0b11
manager.removeComponent(1, TestComponent);
mask = manager.getComponentMask(1);
expect(mask.toString()).toBe('2'); // 0b10
expect(mask.lo).toBe(2); // 0b10
});
});

View File

@@ -430,13 +430,31 @@ describe('Bits - 高性能位操作类测试', () => {
});
describe('大数值处理', () => {
const supportsBigInt = (() => {
try {
return typeof BigInt !== 'undefined' && BigInt(0) === 0n;
} 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);
});
it('应该能够处理非常大的位索引', () => {
if (!supportsBigInt) {
pending('BigInt not supported in this environment');
return;
}
bits.set(1000);
bits.set(2000);
expect(bits.get(1000)).toBe(true);
@@ -445,6 +463,11 @@ describe('Bits - 高性能位操作类测试', () => {
});
it('大数值的位运算应该正确', () => {
if (!supportsBigInt) {
pending('BigInt not supported in this environment');
return;
}
const largeBits1 = new Bits();
const largeBits2 = new Bits();