Mathieu Dfr Mathieu Dfr - 4 months ago 13
Node.js Question

Assertion testing 2 arrays deepEqual

I'm writing a program in nodejs that implement the Tribonacci sequence (https://en.wikipedia.org/wiki/Tribonacci_sequence). This sequence is like the fibonacci, but each number are the sum of the previous 3 words.

My function works great and looks like this :

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


The problem is that when i'm trying to assert testing it, the assertion return everytime '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');
});


this is the result when i'm launching the server (with $ node server.js) :
tribonacci assertion failed

I checked the function, it return the sames results as given in the tests.

Someone has got a solution for me ? Maybe i'm doing something wrong ?

Thanks for your future help :)

Answer

assert.* functions do not have an output in the event of a passed assertion. They will only output the failure message, True, in the event an assertion fails.

If a single assertion fails, it will throw an Error and halt your program. So you could write your program like this …

// 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');

// ...

In other words, the res.write lines are guaranteed not to run if one of the assertions failed because an error will have been thrown.


This should sound like a problem to you

Because it is. Throwing an error within the server (and not catching it) will cause the server to crash every time one of your testing assertion fails. Hopefully that indicates to you that this is a very bad way to write the overall structure of the program.