Small Test

进程模块的简单测试

在完成了雏形之后,我们需要为进程模块写一些单元测试。

创建多个进程并运行

it('创建多个进程并运行', () => {
    const module = new PMI()

    Game.time = 0

    let cnt = 0
    let proc0 = module.createProc([() => { cnt++; return OK; }], 'cnt++') // cnt++ 一次
    let proc1 = module.createProc([() => { cnt++; return OK; }], 'cnt++') // cnt++ 一次
    expect(proc0 != proc1).toEqual(true)
    
    module.tick()
    expect(cnt).toEqual(2)
    expect(() => module.tick()).toThrow()
    Game.time = 1

    module.createProc([() => { cnt += 3; return OK_STOP_CURRENT; }], 'cnt+=3') // cnt+=3 无数次
    
    module.tick()
    expect(cnt).toEqual(5)
    Game.time = 2
    module.tick()
    expect(cnt).toEqual(8)

    module.createProc([["START", () => {cnt += 1; return OK_STOP_NEXT;}], ["JUMP", () => true, "START"]], 'cnt+=1') // cnt += 1 无数次

    Game.time = 3
    module.tick()
    expect(cnt).toEqual(12)

    expect(() => {
        for (let i = 0; i < MAX_PROC_ID - 2 + 1; i++)
            module.createProc([() => OK], '')
    }).toThrow()
    
    Game.time = 4
    module.tick()

    let _cnt = 0
    module.createProc([
        ["0", () => { _cnt += 1; return [OK_STOP_CUSTOM, "2"] }], 
        ["1", () => { _cnt += 2; return [OK_STOP_CUSTOM, "3"] }],
        ["2", () => { _cnt += 3; return [OK_STOP_CUSTOM, "1"] }],
        ["3", () => { _cnt += 4; return OK }],
    ], "")

    Game.time++
    module.tick()
    expect(_cnt).toBe(1)

    Game.time++
    module.tick()
    expect(_cnt).toBe(4)

    Game.time++
    module.tick()
    expect(_cnt).toBe(6)

    Game.time++
    module.tick()
    expect(_cnt).toBe(10)

    module.createProc([() => [OK_STOP_CUSTOM, "not_exist"]], "")
    Game.time++
    expect(() => module.tick()).toThrow()
})

锁的创建,获得,释放与销毁

信号集的创建, 获得, 激活与释放

Last updated