'use strict';

// MODULES //

var tape = require( 'tape' );
var incrmsum = require( './../lib' );


// TESTS //

tape( 'main export is a function', function test( t ) {
	t.ok( true, __filename );
	t.equal( typeof incrmsum, 'function', 'main export is a function' );
	t.end();
});

tape( 'the function throws an error if not provided a positive integer', function test( t ) {
	var values;
	var i;

	values = [
		'5',
		-5.0,
		0.0,
		Math.PI,
		true,
		null,
		undefined,
		NaN,
		[],
		{},
		function(){}
	];

	for ( i = 0; i < values.length; i++ ) {
		t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
	}
	t.end();

	function badValue( value ) {
		return function badValue() {
			incrmsum( value );
		};
	}
});

tape( 'the function returns an accumulator function', function test( t ) {
	t.equal( typeof incrmsum( 3 ), 'function', 'returns a function' );
	t.end();
});

tape( 'the accumulator function computes a moving sum incrementally', function test( t ) {
	var expected;
	var actual;
	var data;
	var acc;
	var N;
	var i;

	data = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ];
	N = data.length;

	acc = incrmsum( 3 );

	actual = new Array( N );
	for ( i = 0; i < N; i++ ) {
		actual[ i ] = acc( data[ i ] );
	}
	expected = [ 2.0, 5.0, 7.0, 9.0, 9.0, 11.0 ];

	t.deepEqual( actual, expected, 'returns expected incremental results' );
	t.end();
});

tape( 'if not provided an input value, the accumulator function returns the current sum', function test( t ) {
	var data;
	var acc;
	var i;

	data = [ 2.0, 3.0, 5.0 ];
	acc = incrmsum( 2 );
	for ( i = 0; i < data.length; i++ ) {
		acc( data[ i ] );
	}
	t.equal( acc(), 10.0 );
	t.end();
});

tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) {
	var acc = incrmsum( 3 );
	t.equal( acc(), null, 'returns null' );
	t.end();
});

@@ -53,16 +53,73 @@
 ape' );%0A
+var isnan = require( '@stdlib/math/base/utils/is-nan' );%0A
 var incr
@@ -1704,10 +1704,9 @@
 (), 
-10
+8
 .0 )
@@ -1846,24 +1846,24 @@
 rmsum( 3 );%0A
-
 %09t.equal( ac
@@ -1889,24 +1889,416 @@
  null' );%0A%09t.end();%0A%7D);%0A
+%0Atape( 'if provided a %60NaN%60 or a value which when computed evaluates to %60NaN%60, the accumulator returns %60NaN%60 for all future invocations', function test( t ) %7B%0A%09var acc;%0A%09var sum;%0A%09var i;%0A%0A%09acc = incrmsum( 5 );%0A%0A%09acc( 2.0 );%0A%09acc( 3.0 );%0A%09acc( -4.0 );%0A%09acc( NaN );%0A%0A%09for ( i = 0; i %3C 1000; i++ ) %7B%0A%09%09sum = acc( Math.random() );%0A%09%09t.equal( isnan( sum ), true, 'returns NaN' );%0A%09%7D%0A%09t.end();%0A%7D);%0A

Add NaN test and fix broken test