断言测试 2 个数组 deepEqual

Assertion testing 2 arrays deepEqual

我正在用 nodejs 编写一个程序来实现 Tribonacci 序列 (https://en.wikipedia.org/wiki/Tribonacci_sequence)。这个序列就像斐波那契数列,但每个数字都是前三个单词的总和。

我的函数运行良好,看起来像这样:

// the tribonacci function
// param signature : the signature which with the function will the start the rest of the tribonacci
// param iterations : how many numbers will be returned with this function
function tribonacci(signature, iterations) {
    if (signature.length == 3) { // the signature must be 3 numbers length
        var tribonacci_sequence = []; // our array that will cointains the fibonacci sequence

        for (i = 0; i < iterations; i++) {
            if (i < 3) { // to initialize our tribonacci sequence to the three numbers of signature
                tribonacci_sequence[i] = signature[i];
            }
            else { // now we can complete our sequence with the rest of tribonacci
                tribonacci_sequence[i] = tribonacci_sequence[i-1] + tribonacci_sequence[i-2] + tribonacci_sequence[i-3];
            }
        }

        return tribonacci_sequence;
    }

    server.close(); // the signature is not 3 numbers length, the tribonacci sequence cannot be done.
};

问题是,当我尝试断言测试它时,断言 return 每次 'undefined'...

// create the server function
const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');

    // let's start our tests
    res.write('tribonacci([1,1,1],10) = [1,1,1,3,5,9,17,31,57,105] ? ' +
        assert.deepEqual(tribonacci([1,1,1],10), [1,1,1,3,5,9,17,31,57,105], 'True') + '\n'
    );
    res.write('tribonacci([0,0,1],10) = [0,0,1,1,2,4,7,13,24,44] ? ' +
        assert.deepEqual(tribonacci([0,0,1],10), [0,0,1,1,2,4,7,13,24,44], 'True') + '\n'
    );
    res.write('tribonacci([0,1,1],10) = [0,1,1,2,4,7,13,24,44,81] ? ' +
        assert.deepEqual(tribonacci([0,1,1],10), [0,1,1,2,4,7,13,24,44,81], 'True') + '\n'
    );
    res.write('tribonacci([1,0,0],10) = [1,0,0,1,1,2,4,7,13,24] ? ' +
        assert.deepEqual(tribonacci([1,0,0],10), [1,0,0,1,1,2,4,7,13,24], 'True') + '\n'
    );
    res.write('tribonacci([0,0,0],10) = [0,0,0,0,0,0,0,0,0,0] ? ' +
        assert.deepEqual(tribonacci([0,0,0],10), [0,0,0,0,0,0,0,0,0,0], 'True') + '\n'
    );
    res.write('tribonacci([1,2,3],10) = [1,2,3,6,11,20,37,68,125,230] ? ' +
        assert.deepEqual(tribonacci([1,2,3],10), [1,2,3,6,11,20,37,68,125,230], 'True') + '\n'
    );
    res.write('tribonacci([3,2,1],10) = [3,2,1,6,9,16,31,56,103,190] ? ' +
        assert.deepEqual(tribonacci([3,2,1],10), [3,2,1,6,9,16,31,56,103,190], 'True') + '\n'
    );
    res.write('tribonacci([1,1,1],1) = [1] ? ' +
        assert.deepEqual(tribonacci([1,1,1],1), [1], 'True') + '\n'
    );
    res.write('tribonacci([300,200,100],0) = []) ? ' +
        assert.deepEqual(tribonacci([300,200,100],0), [], 'True') + '\n'
    );
    res.write('tribonacci([0.5,0.5,0.5],30) = [0.5,0.5,0.5,1.5,2.5,4.5,8.5,15.5,28.5,52.5,96.5,177.5,326.5,600.5,1104.5,2031.5,3736.5,6872.5,12640.5,23249.5,42762.5,78652.5,144664.5,266079.5,489396.5,900140.5,1655616.5,3045153.5,5600910.5,10301680.5]) ? ' +
        assert.deepEqual(tribonacci([0.5,0.5,0.5],30), [0.5,0.5,0.5,1.5,2.5,4.5,8.5,15.5,28.5,52.5,96.5,177.5,326.5,600.5,1104.5,2031.5,3736.5,6872.5,12640.5,23249.5,42762.5,78652.5,144664.5,266079.5,489396.5,900140.5,1655616.5,3045153.5,5600910.5,10301680.5], 'True') + '\n'
    );

    // the res must end
    res.end('\n');
});

这是我启动服务器时的结果(使用 $ node server.js):

我检查了该函数,它 return 与测试中给出的结果相同。

有人为我找到了解决方案吗?也许我做错了什么?

感谢您以后的帮助:)

assert.* 函数在通过断言时没有输出。他们只会在断言失败时输出失败消息 True

如果单个断言失败,它将 throw 一个错误并停止您的程序。所以你可以这样写你的程序......

// let's start our tests
assert.deepEqual(tribonacci([1,1,1],10), [1,1,1,3,5,9,17,31,57,105])
res.write('tribonacci([1,1,1],10) = [1,1,1,3,5,9,17,31,57,105] ? True' + '\n');

assert.deepEqual(tribonacci([0,0,1],10), [0,0,1,1,2,4,7,13,24,44])
res.write('tribonacci([0,0,1],10) = [0,0,1,1,2,4,7,13,24,44] ? True' + '\n');

// ...

换句话说,res.write 行保证不会 运行 如果断言之一失败,因为将抛出错误。


这对你来说应该是个问题

因为它是。每次您的测试断言之一失败时,在服务器内抛出错误(而不是捕获它)将导致服务器崩溃。希望这向您表明,这是编写程序整体结构的一种非常糟糕的方式。