Skip to content

Commit

Permalink
[TESTS] set and raw set methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Jun 1, 2015
1 parent 8c79a30 commit e66f7a6
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// MODULES //

var isInteger = require( 'validate.io-integer-primitive' ),
var isNonNegativeInteger = require( 'validate.io-nonnegative-integer' ),
isNumber = require( 'validate.io-number-primitive' );


Expand All @@ -19,8 +19,8 @@ var isInteger = require( 'validate.io-integer-primitive' ),
*/
function set( i, j, v ) {
/* jshint validthis: true */
if ( !isInteger( i ) || !isInteger( j ) ) {
throw new TypeError( 'set()::invalid input argument. Row and column indices must be integers. Values: `[' + i + ',' + j + ']`.' );
if ( !isNonNegativeInteger( i ) || !isNonNegativeInteger( j ) ) {
throw new TypeError( 'set()::invalid input argument. Row and column indices must be nonnegative integers. Values: `[' + i + ',' + j + ']`.' );
}
if ( !isNumber( v ) ) {
throw new TypeError( 'set()::invalid input argument. An input value must be a number primitive. Value: `' + v + '`.' );
Expand Down
4 changes: 2 additions & 2 deletions test/test.get.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe( 'matrix#get', function tests() {
expect( mat.get ).to.be.a( 'function' );
});

it( 'should throw an error if a row index which is not a nonnegative integer', function test() {
it( 'should throw an error if provided a row index which is not a nonnegative integer', function test() {
var values = [
'5',
-1,
Expand All @@ -60,7 +60,7 @@ describe( 'matrix#get', function tests() {
}
});

it( 'should throw an error if a column index which is not a nonnegative integer', function test() {
it( 'should throw an error if provided a column index which is not a nonnegative integer', function test() {
var values = [
'5',
-1,
Expand Down
127 changes: 127 additions & 0 deletions test/test.set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/* global require, describe, it */
'use strict';

// MODULES //

var // Expectation library:
chai = require( 'chai' ),

// Matrix class:
matrix = require( './../lib' ),

// Module to be tested:
set = require( './../lib/set.js' );


// VARIABLES //

var expect = chai.expect,
assert = chai.assert;


// TESTS //

describe( 'matrix#set', function tests() {

var mat, data;

data = new Int32Array( 100 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = i;
}
mat = matrix( data, [10,10] );

it( 'should export a function', function test() {
expect( set ).to.be.a( 'function' );
expect( mat.set ).to.be.a( 'function' );
});

it( 'should throw an error if provided a row index which is not a nonnegative integer', function test() {
var values = [
'5',
-1,
Math.PI,
NaN,
null,
true,
undefined,
[],
{},
function(){}
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[ i ] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
mat.set( value, 0, 5 );
};
}
});

it( 'should throw an error if provided a column index which is not a nonnegative integer', function test() {
var values = [
'5',
-1,
Math.PI,
NaN,
null,
true,
undefined,
[],
{},
function(){}
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[ i ] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
mat.set( 0, value, 5 );
};
}
});

it( 'should throw an error if provided a non-numeric value to set', function test() {
var values = [
'5',
NaN,
null,
true,
undefined,
[],
{},
function(){}
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[ i ] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
mat.set( 0, 0, value );
};
}
});

it( 'should set a Matrix element', function test() {
var prev, actual, expected;

prev = mat.get( 5, 6 );
mat.set( 5, 6, 1000 );

actual = mat.get( 5, 6 );
expected = 1000;

assert.notEqual( actual, prev );
assert.strictEqual( actual, expected );
});

it( 'should silently fail if provided an out-of-bounds index', function test() {
mat.set( 500, 100, 1000 );
assert.isUndefined( mat.get( 500, 100 ) );
});

});
57 changes: 57 additions & 0 deletions test/test.set.raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* global require, describe, it */
'use strict';

// MODULES //

var // Expectation library:
chai = require( 'chai' ),

// Matrix class:
matrix = require( './../lib' ).raw,

// Module to be tested:
set = require( './../lib/set.raw.js' );


// VARIABLES //

var expect = chai.expect,
assert = chai.assert;


// TESTS //

describe( 'matrix.raw#set', function tests() {

var mat, data;

data = new Int32Array( 100 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = i;
}
mat = matrix( data, [10,10] );

it( 'should export a function', function test() {
expect( set ).to.be.a( 'function' );
expect( mat.set ).to.be.a( 'function' );
});

it( 'should set a Matrix element', function test() {
var prev, actual, expected;

prev = mat.get( 5, 6 );
mat.set( 5, 6, 1000 );

actual = mat.get( 5, 6 );
expected = 1000;

assert.notEqual( actual, prev );
assert.strictEqual( actual, expected );
});

it( 'should silently fail if provided an out-of-bounds index', function test() {
mat.set( 500, 100, 1000 );
assert.isUndefined( mat.get( 500, 100 ) );
});

});

0 comments on commit e66f7a6

Please sign in to comment.