断言测试2个数组deepEqual

时间:2016-07-20 15:29:18

标签: javascript arrays node.js assert

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

我的功能很棒,看起来像这样:

// 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.
};

问题在于,当我试图断言测试时,断言每次都会返回&#39; undefined&#39; ...

// 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): tribonacci assertion failed

我检查了函数,它返回了测试中给出的相同结果。

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

感谢您将来的帮助:)

1 个答案:

答案 0 :(得分:0)

传递断言时,

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行不会运行,因为会抛出错误。

这听起来像是个问题

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