Skip to content

Instantly share code, notes, and snippets.

@tinesubic
Last active August 16, 2016 11:42
Show Gist options
  • Save tinesubic/59024bef4e8100e1f790 to your computer and use it in GitHub Desktop.
Save tinesubic/59024bef4e8100e1f790 to your computer and use it in GitHub Desktop.
Mocha Unit Tests for qminer JS wrapper
HOW TO - Running Mocha with Chai, unit test files and examples
Install Node:
- "sudo apt-get install npm" to install npm package manager
- "sudo apt-get install uuid-dev" to install dependencies for building node/qminer
- clone "link to be added" Node repository from GitHub
- cd to root directory and run commands "./configure", "make", "make install"
- cd to /test/addons/la
- install node-gyp with "sudo npm install -g node-gyp"
- build qminer with command "node-gyp configure build"
- follow instructions below to install test framework
Install Testing Framework:
1. Install mocha: "sudo npm install -g mocha"
2. If you want to use Chai assertion library install chai: "sudo npm install -g chai"
3. For running tests, use a file named test.js - download one of the files below and rename it.
NOTE: test-assert.js uses standard Node.js assertion library with fewer testing
capabilities (Shown below only as an example) Recommended is the use of Chai library with test-chai.js file.
4. Run command "mocha" to execute all tests
4a. Test for qminer require including file la.node (previously qminer.node)
in path "./build/Release/la.node"
4b. Using Chai assertion library requires importing expect, should and assert
functions from Chai module with following lines of code:
var assert = require('chai').assert
var expect = require('chai').expect
var should = require('chai').should
4c. Describe block in test.js file logically separate block of multiple tests
(Ex. Functions, property tests...). They can be nested indefinitely. It blocks
denote separate unit tests.
4d. Append ".only" to "describe" or "it" keyword (before parentheses) if you
want to run only specific test(s).
4e. Append ".skip" to exclude particular test(s) to avoid commenting them out.
Links:
Chai.js: http://chaijs.com
Mocha: http://visionmedia.github.io/mocha/
//NOTE: Instructions for building the project on Windows to be added.
//This file if maintained only for comparison purposes. It is no longer updated. FOr further examples of unit tests, check file test-chai.js below.
var assert = require("assert")
var qm = require("./build/Release/qminer.node")
describe('Import test', function(){
it('if import of qminer.node succeeds, return true', function(){
assert.equal(1,1);
})
})
var v = new qm.TVec();
describe('Property Tests', function(){
describe('Vector Length Test', function(){
it('should return 0 for vector v', function(){
assert.equal(v.length,0);
})
})
})
describe('Functions Tests', function(){
describe('Push Test', function(){
it('pushing 3.1 and 4 into a vector v, v.length should return 2', function(){
v.push(3.2);
v.push(4);
assert.equal(v.length,2);
})
})
describe('At Test', function(){
it('returns element in with indexes 0 (3.2) and 1 (4)', function(){
assert.equal(3.2,v.at(0));
assert.equal(4,v.at(1));
})
})
describe('Sum Test', function(){
it('should return a sum of 3.2 and 4', function(){
assert.equal(3.2+4,v.sum());
})
})
describe('getMaxIdx Test', function(){
it('should return index of last element in vector, 1.', function(){
assert.equal(v.length-1,v.getMaxIdx());
})
})
//TO ADD: edge cases, ex. IndexOutOfRange, negative index, sum of empty vector
var la = require("./build/Release/la.node")
//importing assert and expect functions from chai assertion library
var assert = require('chai').assert
var expect = require('chai').expect
describe('Import test', function(){
it('if import of la.node succeeds, return true', function(){
expect(1).to.equal(1);
})
})
var v = new la.vector();
var int1 = 3;
var int2 = 2;
var float1 = 18.2
var float2 = 3.6;
var negNum1 = -2;
var negNum2 = -4;
//tests if length property is exposed correctly
describe('Properties Tests', function(){
describe('Vector Length Test', function(){
it('check if v has property length and has 0 elements', function(){
//check if vector v has property length. Also check if it equals 0 (empty vector)
expect(v).to.have.property('length');
expect(v.length).to.equal(0);
})
})
})
describe('Functions Tests', function(){
//tests if pushing elements into a vector works correctly
describe('Push Test', function(){
it('should push two numbers into vector', function(){
v.push(int1);
v.push(float2);
assert(v.length == 2,'Not enough elements');
})
})
describe('At Test', function(){
//testes if recovering elements at given index works correctly
it('returns element in with indexes 0 (3.2) and 1 (4)', function(){
assert.equal(int1,v.at(0));
assert.equal(float2,v.at(1));
})
})
describe('Sum Test', function(){
//tests if summation of vecotr elements works correctly
it('should test integer sum', function(){
var intVector = la.vector();
intVector.push(int1);
intVector.push(int2);
expect(intVector.sum()).to.equal(int1+int2);
})
it('should test float sum', function(){
var floatVector = la.vector();
floatVector.push(float1);
floatVector.push(float2);
expect(floatVector.sum()).to.equal(float1+float2);
})
it('should test mixed sum', function(){
var mixedVector = la.vector();
mixedVector.push(int1);
mixedVector.push(float1);
expect(mixedVector.sum()).to.equal(int1+float1);
})
it('should test sum of negative numbers', function(){
var negVector = la.vector();
negVector.push(negNum2);
negVector.push(negNum1);
expect(negVector.sum()).to.equal(negNum2+negNum1);
})
})
describe('Shuffle Test', function(){
//tests shuffling elemnts inside a vector
//NOTE: test needs to be refined to correctly test vectors with multiple elements of same value
it('should shuffle elements inside a vector randomly', function(){
var vec = la.vector();
var compare = la.vector();
for (var i = 2; i<15; i++) {
compare.push(i);
vec.push(i);
}
vec.shuffle();
expect(vec.length).to.equal(compare.length);
expect(vec.sum()).to.equal(compare.sum());
})
})
describe('Trunc Test', function(){
//tests vector truncating
it('should return shorter vector', function(){
var truncVector = la.vector();
for (var i = 1; i<8; i++) {
truncVector.push(i);
}
expect(truncVector.length).to.equal(7);
truncVector.trunc(3);
expect(truncVector.length).to.equal(3);
})
it('trunc vector to same length', function(){
var truncVector2 = la.vector();
truncVector2.push(3);
truncVector2.trunc(1);
expect(truncVector2.length).to.equal(1);
})
it('trunc vector to zero length', function(){
var truncVector3 = la.vector();
for (var i = 1; i<8; i++) {
truncVector3.push(i);
}
truncVector3.trunc(0);
expect(truncVector3.length).to.equal(0);
})
})
describe('GetMaxIdx Test', function(){
//tests if returning index of last element in vector works correctly
it('should return index of last element in vector.', function(){
v.push(15);
expect(v.length-1).to.equal(v.getMaxIdx());
})
})
describe('Put Test', function(){
it('should put three numbers in the beginning, middle and end of a vector', function(){
var putVector = la.vector();
for (var i = 1; i<21; i++) {
putVector.push(i);
}
putVector.put(0,33);
putVector.put(putVector.length-1,45);
putVector.put(8,44);
expect(putVector.at(0)).to.equal(33);
expect(putVector.at(putVector.getMaxIdx())).to.equal(45);
expect(putVector.at(8)).to.equal(44);
})
})
describe('Sort Test', function(){
//TO ADD: sorting negative values
var sortVector = la.vector();
//adds 9 elements to vector v
for (var i = 1; i<10; i++) {
sortVector.push(i);
}
sortVector.push(3);
sortVector.push(2);
describe('Ascending sort test', function() {
it('should return vector sorted by descending values', function() {
sortVector.sort(true)
for (var i = 0; i < sortVector.length-1; i++) {
expect(sortVector.at(i)).to.be.at.most(sortVector.at(i+1));
}
})
})
describe('Descending sort test', function() {
it('should return vector sorted by ascending values', function() {
sortVector.sort(false)
for (var i = 0; i < sortVector.length-1; i++) {
expect(sortVector.at(i)).to.be.at.least(sortVector.at(i+1));
}
})
})
})
describe('Put Test', function(){
it('should put three numbers in the beginning, middle and end of a vector', function(){
var putVector = la.vector();
for (var i = 1; i<21; i++) {
putVector.push(i);
}
putVector.put(0,33);
putVector.put(putVector.length-1,45);
putVector.put(8,44);
expect(putVector.at(0)).to.equal(33);
expect(putVector.at(putVector.getMaxIdx())).to.equal(45);
expect(putVector.at(8)).to.equal(44);
})
})
describe('Plus Test', function(){
it('should return a sum of two vectors', function(){
var vec1 = la.vector();
var vec2 = la.vector();
for (var i = 0; i<10; i++) {
vec1.push(i);
}
for (var i = 10; i>0; i--) {
vec2.push(i);
}
var vec3 = vec1.plus(vec2);
for (int i = 0; i<vec3.length; i++) {
expect(vec3.at(i)).to.equal(vec1.at(i)+vec2.at(i));
}
})
})
describe('Minus Test', function(){
it('should return difference of two vectors', function(){
var vec1 = la.vector();
var vec2 = la.vector();
for (var i = 0; i<10; i++) {
vec1.push(i);
}
for (var i = 10; i>0; i--) {
vec2.push(i);
}
var vec3 = vec1.minus(vec2);
for (int i = 0; i<vec3.length; i++) {
expect(vec3.at(i)).to.equal(vec1.at(i)-vec2.at(i));
}
})
})
describe('Inner Test', function(){
it('should return dot product of two vectors', function(){
var vec1 = la.vector();
var vec2 = la.vector();
for (var i = 0; i<10; i++) {
vec1.push(i);
}
for (var i = 10; i>0; i--) {
vec2.push(i);
}
var innerProduct = vec1.innner(vec2);
var manualProduct = 0;
for (var i = 0; i<10; i++) {
manualProduct = manualProduct + vec2.at(i)*vec1.at(i);
}
expect(innerProduct).to.equal(manualProduct);
})
})
describe('Multiply Test', function(){
it('should return product of vector with scalar', function(){
var vec1 = la.vector();
var vec2 = la.vector();
for (var i = 0; i<10; i++) {
vec1.push(i);
vec2.push(i*4);
}
var vec3 = vec1.multiply(4),
for (var i = 0; i<vec1.length; i++) {
expect(vec1.at(i)).to.equal(vec3.at(i));
}
})
})
describe('Normalize Test', function(){
it('should return normalized vector', function(){
var vec1 = la.vector();
var vec2 = la.vector();
var squareSum = 0;
for (var i = 0; i<10; i++) {
vec1.push(i);
vec2.push(i);
squareSum = squareSum + i*i;
}
var length = Math.sqrt(squareSum);
vec1.normalize();
for (var i = 0; i<vec1.length; i++) {
expect(vec1.at(i)).to.equal(vec2.at(i)/length);
}
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment