'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/math/base/random/randu' );
var isArray = require( '@stdlib/assert/is-array' );
var shape2strides = require( '@stdlib/types/ndarray/base/shape2strides' );
var strides2offset = require( '@stdlib/types/ndarray/base/strides2offset' );
var numel = require( '@stdlib/types/ndarray/base/numel' );
var pkg = require( './../package.json' ).name;
var ndarray2array = require( './../lib' );


// MAIN //

bench( pkg+':order=row-major', function benchmark( b ) {
	var strides;
	var buffer;
	var offset;
	var order;
	var shape;
	var len;
	var out;
	var i;

	shape = [ 10, 10, 10 ];
	order = 'row-major';
	len = numel( shape );
	strides = shape2strides( shape, order );
	offset = strides2offset( shape, strides );

	buffer = new Array( len );
	for ( i = 0; i < len; i++ ) {
		buffer[ i ] = i;
	}

	b.tic();
	for ( i = 0; i < b.iterations; i++ ) {
		strides[ 1 ] *= ( randu() < 0.5 ) ? -1 : 1;
		out = ndarray2array( buffer, shape, strides, offset, order );
		if ( out.length !== shape[ 0 ] ) {
			b.fail( 'should have expected length' );
		}
	}
	b.toc();
	if (
		!isArray( out ) ||
		!isArray( out[ 0 ] ) ||
		!isArray( out[ 0 ][ 0 ] )
	) {
		b.fail( 'should return an array of arrays' );
	}
	b.pass( 'benchmark finished' );
	b.end();
});

bench( pkg+':order=column-major', function benchmark( b ) {
	var strides;
	var buffer;
	var offset;
	var order;
	var shape;
	var len;
	var out;
	var i;

	shape = [ 10, 10, 10 ];
	order = 'column-major';
	len = numel( shape );
	strides = shape2strides( shape, order );
	offset = strides2offset( shape, strides );

	buffer = new Array( len );
	for ( i = 0; i < len; i++ ) {
		buffer[ i ] = i;
	}

	b.tic();
	for ( i = 0; i < b.iterations; i++ ) {
		strides[ 1 ] *= ( randu() < 0.5 ) ? -1 : 1;
		out = ndarray2array( buffer, shape, strides, offset, order );
		if ( out.length !== shape[ 0 ] ) {
			b.fail( 'should have expected length' );
		}
	}
	b.toc();
	if (
		!isArray( out ) ||
		!isArray( out[ 0 ] ) ||
		!isArray( out[ 0 ][ 0 ] )
	) {
		b.fail( 'should return an array of arrays' );
	}
	b.pass( 'benchmark finished' );
	b.end();
});

@@ -127,24 +127,29 @@
 %0Avar isArray
+Array
  = require( 
@@ -172,16 +172,22 @@
 is-array
+-array
 ' );%0Avar
@@ -1153,32 +1153,37 @@
 %09if (%0A%09%09!isArray
+Array
 ( out ) %7C%7C%0A%09%09!is
@@ -1187,37 +1187,16 @@
 !isArray
-( out%5B 0 %5D ) %7C%7C%0A%09%09!is
 Array( o
@@ -1190,37 +1190,32 @@
 ArrayArray( out%5B
- 0 %5D%5B
  0 %5D )%0A%09) %7B%0A%09%09b.
@@ -1974,24 +1974,29 @@
 (%0A%09%09!isArray
+Array
 ( out ) %7C%7C%0A%09
@@ -2008,29 +2008,8 @@
 rray
-( out%5B 0 %5D ) %7C%7C%0A%09%09!is
 Arra
@@ -2019,13 +2019,8 @@
 out%5B
- 0 %5D%5B
  0 %5D

Use utility to test for an array of arrays