| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- var fs = require('fs');
- var path = require('path');
- var test = require('tape');
- var load = require('load-json-file');
- var isoBands = require('../dist/marchingsquares.js').isoBands;
- var isoLines = require('../dist/marchingsquares.js').isoLines;
- var QuadTree = require('../dist/marchingsquares.js').QuadTree;
- var directories = {
- in: path.join(__dirname, 'data', 'in') + path.sep,
- out: path.join(__dirname, 'data', 'out') + path.sep
- };
- var isoBandsTestCases = fs.readdirSync(directories.in)
- .filter(function (filename) {
- return filename.includes('isoBands');
- })
- .map(function (filename) {
- return {
- name: path.parse(filename).name,
- data: load.sync(directories.in + filename)
- };
- });
- test('isoBands output', function (t) {
- isoBandsTestCases.forEach(function (inputFile) {
- var name = inputFile.name;
- var data = inputFile.data.matrix;
- var outputfile = directories.out + name + '.json';
- var lowerBand = inputFile.data.lowerBand;
- var upperBand = inputFile.data.upperBand;
- var bands = isoBands(data, lowerBand, upperBand - lowerBand);
- // console.log(JSON.stringify(bands));
- t.deepEqual(bands, load.sync(outputfile), name);
- });
- t.end();
- });
- var isoLinesTestCases = fs.readdirSync(directories.in)
- .filter(function (filename) {
- return filename.includes('isoLines');
- })
- .map(function (filename) {
- return {
- name: path.parse(filename).name,
- data: load.sync(directories.in + filename)
- };
- });
- test('isoLines output', function (t) {
- isoLinesTestCases.forEach(function (inputFile) {
- var name = inputFile.name;
- var data = inputFile.data.matrix;
- var outputfile = directories.out + name + '.json';
- var thresholds = inputFile.data.thresholds;
- var lines = isoLines(data, thresholds);
- // console.log(JSON.stringify(lines));
- t.deepEqual(lines, load.sync(outputfile), name);
- });
- t.end();
- });
- test('isoBands input validation', function (t) {
- var dataArr = [[1], [2], [3]];
- t.throws(function(){isoBands(null, 0, 5)}, /data is required/, 'missing data');
- t.throws(function(){isoBands('string', 0, 5)} , /array of arrays/, 'invalid data');
- t.throws(function(){isoBands([1], 0, 5)}, /array of arrays/, 'invalid data again');
- t.throws(function(){isoBands(dataArr, null, 5)}, /lowerBound is required/, 'missing lowerBound');
- t.throws(function(){isoBands(dataArr, 0, null)}, /bandWidth is required/, 'missing bandWidth');
- t.throws(function(){isoBands(dataArr, [0, 1], null)}, /bandWidth is required/, 'missing bandWidth');
- t.throws(function(){isoBands(dataArr, [0, 'foo'], [1, 2])}, /is not a number/, 'invalid lowerBound entry');
- t.throws(function(){isoBands(dataArr, 'number', 3)}, /lowerBound must be a number/, 'invalid lowerBound');
- t.throws(function(){isoBands(dataArr, 23, 'string')}, /bandWidth must be a number/, 'invalid bandWidth');
- t.throws(function(){isoBands(dataArr, 0, [1, 5])}, /bandWidth must be a number/, 'invalid lowerBound-bandWidth combination');
- t.throws(function(){isoBands(dataArr, [0, 5], [3, 1, 5])}, /unequal lengths/, 'invalid lowerBound-bandWidth combination');
- t.throws(function(){isoBands(dataArr, 23, 3, 'string')}, /options must be an object/, 'invalid options');
- t.end();
- });
- test('isoLines input validation', function (t) {
- var dataArr = [[1], [2], [3]];
- t.throws(function(){isoLines(null, 0)}, /data is required/, 'missing data');
- t.throws(function(){isoLines('string', 0)}, /array of arrays/, 'invalid data');
- t.throws(function(){isoLines([1], 0)}, /array of arrays/, 'invalid data again');
- t.throws(function(){isoLines(dataArr, null)}, /threshold is required/, 'missing threshold');
- t.throws(function(){isoLines(dataArr, 'number')}, /threshold must be a number/, 'invalid threshold');
- t.throws(function(){isoLines(dataArr, [0, 'foo'])}, /is not a number/, 'invalid threshold entry');
- t.throws(function(){isoLines(dataArr, 23, 'string')}, /options must be an object/, 'invalid options');
- t.end();
- });
- test('successCallback check', function (t) {
- var data = [[1, 1], [1, 5]];
- var called = false;
- var options = {
- successCallback: function () {
- called = true;
- }
- };
- isoLines(data, 1, options);
- t.true(called);
- called = false;
- isoBands(data, 1, 2, options);
- t.true(called);
- t.end();
- });
- test('QuadTree', function (t) {
- var data = [
- [1, 1, 1, 0],
- [1, 5, 5, 1],
- [0, 5, 7, 1]
- ];
- var prepData = new QuadTree(data);
- t.equal('QuadTree', prepData.constructor.name);
- t.equal('TreeNode', prepData.root.constructor.name);
- t.throws(function(){new QuadTree(null)}, /data is required/, 'missing data');
- t.throws(function(){new QuadTree([ ])}, /array of arrays/, '1D array');
- t.throws(function(){new QuadTree([ [ ] ])}, /two rows/, 'Empty 2D array');
- t.throws(function(){new QuadTree([ [0] ])}, /two rows/, 'Single row');
- t.throws(function(){new QuadTree([ [0], [0] ])}, /two columns/, 'Single column');
- t.throws(function(){new QuadTree([ [0, 1], [0] ])}, /unequal row lengths/, 'Unequal row lengths');
- /* There are only only two cells with threshold 0 */
- t.deepEqual([{x: 2, y: 0},{x: 0, y: 1}], prepData.root.cellsBelowThreshold(0, false));
- /* There are only two cells with threshold 7 */
- t.deepEqual([{x: 1, y: 1},{x: 2, y: 1}], prepData.root.cellsBelowThreshold(7, false));
- /* there is no cell with threshold -2 */
- t.deepEqual([], prepData.root.cellsBelowThreshold(-2, false));
- /* there is no cell with threshold -2 */
- t.deepEqual([], prepData.root.cellsBelowThreshold(10, false));
- /* only two cells with band [7:8] */
- t.deepEqual([{x: 1, y: 1},{x: 2, y: 1}], prepData.root.cellsInBand(7, 8, false));
- t.end();
- });
|