Skip to content

test: add tests to @stdlib/dstructs/compact-adjacency-matrix #2131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isFunction = require( '@stdlib/assert/is-function' );
var instanceOf = require( '@stdlib/assert/instance-of' );
var CompactAdjacencyMatrix = require( './../lib' );


// TESTS //

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

tape( 'attached to the prototype of the main export is a `addEdge` method', function test( t ) {
t.strictEqual( hasOwnProp( CompactAdjacencyMatrix.prototype, 'addEdge' ), true, 'has property' );
t.strictEqual( isFunction( CompactAdjacencyMatrix.prototype.addEdge ), true, 'has method' );
t.end();
});

tape( 'the method throws an error if invoked with a value where both arguments are not a non negative number', function test( t ) {
var values;
var adj;
var i;

adj = new CompactAdjacencyMatrix( 4 );

values = [
[ '5', 2 ],
[ 'abcde', -1 ],
[ -1, 5 ],
[ NaN, NaN ],
[ true, 2 ],
[ false, '2' ],
[ null, 0 ],
[ void 0, 3 ]
];
for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[i][0], values[i][1] ), TypeError, 'throws an error when provided '+values[i] );
}
t.end();

function badValue( arg1, arg2 ) {
return function badValue() {
return adj.addEdge( arg1, arg2 );
};
}
});

tape( 'the method throws an Range error if invoked with value where both arguments exceeds matrix dimensions', function test( t ) {
var values;
var adj;
var i;

adj = new CompactAdjacencyMatrix( 4 );

values = [
[ 6, 2 ],
[ 0, 5 ],
[ 7, 8 ]
];
for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[i][0], values[i][1] ), RangeError, 'throws an error when provided '+values[i] );
}
t.end();

function badValue( arg1, arg2 ) {
return function badValue() {
return adj.addEdge( arg1, arg2 );
};
}
});

tape( 'the method returns an instance of adjcency matrix upon invoking by a valid input', function test( t ) {
var expected;
var values;
var adj;
var i;

adj = new CompactAdjacencyMatrix( 4 );

adj.addEdge( 1, 0 );
adj.addEdge( 1, 2 );
adj.addEdge( 0, 2 );
adj.addEdge( 2, 3 );

values = [
[ 0, 3],
[ 1, 0],
[ 0, 2]
];
for ( i = 0; i < values.length; i++ ) {
expected = adj.addEdge( values[i][0], values[i][1] );
t.strictEqual( instanceOf( expected, CompactAdjacencyMatrix ), true, 'returns expected value' );
}
t.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isEmptyArray = require( '@stdlib/assert/is-empty-array' );
var isArray = require( '@stdlib/assert/is-array' );
var CompactAdjacencyMatrix = require( './../lib' );


// TESTS //

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

tape( 'attached to the prototype of the main export is a `edges` method', function test( t ) {
t.strictEqual( hasOwnProp( CompactAdjacencyMatrix.prototype, 'edges' ), true, 'has property' );
t.end();
});

tape( 'the method returns a list of array when there exists vertices and edges', function test( t ) {
var adj;

adj = new CompactAdjacencyMatrix( 4 );

adj.addEdge( 1, 0 );
adj.addEdge( 1, 2 );
adj.addEdge( 0, 2 );
adj.addEdge( 2, 3 );

t.strictEqual( isArray( adj.edges ), true, 'returns expected value' );
t.end();
});

tape( 'the method returns an empty array in case of vertices but no edges', function test( t ) {
var adj;

adj = new CompactAdjacencyMatrix( 4 );

t.strictEqual( isEmptyArray( adj.edges ), true, 'returns expected value' );
t.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isFunction = require( '@stdlib/assert/is-function' );
var instanceOf = require( '@stdlib/assert/instance-of' );
var CompactAdjacencyMatrix = require( './../lib' );


// TESTS //

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

tape( 'attached to the prototype of the main export is a `fromEdges` method', function test( t ) {
var arr;

t.strictEqual( hasOwnProp( CompactAdjacencyMatrix, 'fromEdges' ), true, 'has property' );
t.strictEqual( isFunction( CompactAdjacencyMatrix.fromEdges ), true, 'has method' );

arr = CompactAdjacencyMatrix.fromEdges( 2, [] );
t.strictEqual( instanceOf( arr, CompactAdjacencyMatrix ), true, 'returns expected value' );
t.end();
});

tape( 'the method throws an error if invoked with a value which is not a non negative number', function test( t ) {
var values;
var edges;
var i;

edges = [ [ 0, 1 ], [ 0, 2 ], [ 1, 2 ], [ 2, 3 ] ];

values = [
'5',
'abcde',
-1,
NaN,
true,
false,
null,
void 0
];
for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[i], edges ), TypeError, 'throws an error when provided '+values[i] );
}
t.end();

function badValue( arg1, arg2 ) {
return function badValue() {
return CompactAdjacencyMatrix.fromEdges( arg1, arg2 );
};
}
});

tape( 'the method throws an error if invoked with a value which is not an array like object', function test( t ) {
var values;
var i;

values = [
'5',
'abcde',
-1,
NaN,
true,
false,
null,
void 0
];
for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( 4, values[i] ), TypeError, 'throws an error when provided '+values[i] );
}
t.end();

function badValue( arg1, arg2 ) {
return function badValue() {
return CompactAdjacencyMatrix.fromEdges( arg1, arg2 );
};
}
});

tape( 'the method throws an error if not provided an iterable or array-like object (callback)', function test( t ) {
var values;
var i;

values = [
'5',
5,
NaN,
true,
false,
null,
void 0,
{},
function noop() {}
];
for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( 4, values[i] ), TypeError, 'throws an error when provided '+values[i] );
}
t.end();

function badValue( arg1, arg2 ) {
return function badValue() {
return CompactAdjacencyMatrix.fromEdges( arg1, arg2, clbk );
};
}

function clbk() {
return [ 1.0, 1.0 ];
}
});

tape( 'the method throws an error if not provided an iterable or array-like object (callback, thisArg)', function test( t ) {
var values;
var i;

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( arg1, arg2 ) {
return function badValue() {
return CompactAdjacencyMatrix.fromEdges( arg1, arg2, clbk, {} );
};
}

function clbk() {
return [ 1.0, 1.0 ];
}
});

tape( 'the method returns an instance of adjcency matrix upon invoking by a valid input', function test( t ) {
var values;
var i;

values = [
[ [ 0, 1 ], [ 2, 3 ] ],
[ [ 1, 3 ], [ 0, 3 ], [ 2, 0 ] ],
[ [ 1, 2 ], [ 0, 1 ] ]
];
for ( i = 0; i < values.length; i++ ) {
t.strictEqual( instanceOf( CompactAdjacencyMatrix.fromEdges( 4, values[ i ] ), CompactAdjacencyMatrix ), true, 'returns expected value' );
}
t.end();
});
Loading
Loading