Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
332 changes: 332 additions & 0 deletions lib/node_modules/@stdlib/ndarray/count-truthy/test/test.assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var tape = require( 'tape' );
var Float64Array = require( '@stdlib/array/float64' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var empty = require( '@stdlib/ndarray/empty' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var countTruthy = require( './../lib/assign.js' );


// TESTS //

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

tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) {
var values;
var y;
var i;

y = empty( [], {
'dtype': 'int32'
});

values = [
'5',
5,
NaN,
true,
false,
null,
void 0,
{},
[],
function noop() {}
];

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() {
countTruthy( value, y );
};
}
});

tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) {
var values;
var y;
var i;

y = empty( [], {
'dtype': 'int32'
});

values = [
'5',
5,
NaN,
true,
false,
null,
void 0,
{},
[],
function noop() {}
];

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() {
countTruthy( value, y, {} );
};
}
});

tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) {
var values;
var x;
var i;

x = empty( [ 2, 2 ], {
'dtype': 'float64'
});

values = [
'5',
5,
NaN,
true,
false,
null,
void 0,
{},
[],
function noop() {}
];

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() {
countTruthy( x, value );
};
}
});

tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) {
var values;
var x;
var i;

x = empty( [ 2, 2 ], {
'dtype': 'float64'
});

values = [
'5',
5,
NaN,
true,
false,
null,
void 0,
{},
[],
function noop() {}
];

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() {
countTruthy( x, value, {} );
};
}
});

tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) {
var values;
var x;
var y;
var i;

x = empty( [ 2, 2 ], {
'dtype': 'float64'
});
y = empty( [ 2 ], {
'dtype': 'int32'
});

values = [
'5',
5,
NaN,
true,
false,
null,
void 0,
[],
function noop() {}
];

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() {
countTruthy( x, y, value );
};
}
});

tape( 'the function throws an error if provided an options argument with an invalid `dims` property', function test( t ) {
var values;
var x;
var y;
var i;

x = empty( [ 2, 2 ], {
'dtype': 'float64'
});
y = empty( [ 2 ], {
'dtype': 'int32'
});

values = [
'5',
NaN,
true,
false,
null,
void 0,
{}
];

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() {
var opts = {
'dims': value
};
countTruthy( x, y, opts );
};
}
});

tape( 'the function counts the number of truthy elements along one or more ndarray dimensions (row-major)', function test( t ) {
var expected;
var actual;
var x;
var y;

x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' );
y = empty( [], {
'dtype': 'int32'
});

actual = countTruthy( x, y );
expected = 4;

t.strictEqual( actual, y, 'returns expected value' );
t.strictEqual( actual.get(), expected, 'returns expected value' );
t.end();
});

tape( 'the function counts the number of truthy elements along one or more ndarray dimensions (column-major)', function test( t ) {
var expected;
var actual;
var x;
var y;

x = new ndarray( 'float64', new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ), [ 4 ], [ 1 ], 0, 'column-major' );
y = empty( [], {
'dtype': 'int32'
});

actual = countTruthy( x, y );
expected = 4;

t.strictEqual( actual, y, 'returns expected value' );
t.strictEqual( actual.get(), expected, 'returns expected value' );
t.end();
});

tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) {
var expected;
var actual;
var opts;
var x;
var y;

x = new ndarray( 'float64', new Float64Array( [ 1.0, 3.0, 0.0, 0.0, 0.0, -5.0, -7.0, 0.0 ] ), [ 2, 4 ], [ 4, 1 ], 0, 'row-major' );

opts = {
'dims': [ 0 ]
};
y = empty( [ 4 ], {
'dtype': 'int32'
});
actual = countTruthy( x, y, opts );
expected = [ 1, 2, 1, 0 ];

t.strictEqual( actual, y, 'returns expected value' );
t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' );
t.end();
});

tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) {
var expected;
var actual;
var opts;
var x;
var y;

x = new ndarray( 'float64', new Float64Array( [ 1.0, 3.0, 0.0, 0.0, 0.0, -5.0, -7.0, 0.0 ] ), [ 2, 4 ], [ 1, 2 ], 0, 'column-major' );

opts = {
'dims': [ 1 ]
};
y = empty( [ 2 ], {
'dtype': 'int32'
});
actual = countTruthy( x, y, opts );
expected = [ 2, 2 ];

t.strictEqual( actual, y, 'returns expected value' );
t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' );
t.end();
});
Loading