test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. var fs = require('fs');
  2. var path = require('path');
  3. var test = require('tape');
  4. var load = require('load-json-file');
  5. var isoBands = require('../dist/marchingsquares.js').isoBands;
  6. var isoLines = require('../dist/marchingsquares.js').isoLines;
  7. var QuadTree = require('../dist/marchingsquares.js').QuadTree;
  8. var directories = {
  9. in: path.join(__dirname, 'data', 'in') + path.sep,
  10. out: path.join(__dirname, 'data', 'out') + path.sep
  11. };
  12. var isoBandsTestCases = fs.readdirSync(directories.in)
  13. .filter(function (filename) {
  14. return filename.includes('isoBands');
  15. })
  16. .map(function (filename) {
  17. return {
  18. name: path.parse(filename).name,
  19. data: load.sync(directories.in + filename)
  20. };
  21. });
  22. test('isoBands output', function (t) {
  23. isoBandsTestCases.forEach(function (inputFile) {
  24. var name = inputFile.name;
  25. var data = inputFile.data.matrix;
  26. var outputfile = directories.out + name + '.json';
  27. var lowerBand = inputFile.data.lowerBand;
  28. var upperBand = inputFile.data.upperBand;
  29. var bands = isoBands(data, lowerBand, upperBand - lowerBand);
  30. // console.log(JSON.stringify(bands));
  31. t.deepEqual(bands, load.sync(outputfile), name);
  32. });
  33. t.end();
  34. });
  35. var isoLinesTestCases = fs.readdirSync(directories.in)
  36. .filter(function (filename) {
  37. return filename.includes('isoLines');
  38. })
  39. .map(function (filename) {
  40. return {
  41. name: path.parse(filename).name,
  42. data: load.sync(directories.in + filename)
  43. };
  44. });
  45. test('isoLines output', function (t) {
  46. isoLinesTestCases.forEach(function (inputFile) {
  47. var name = inputFile.name;
  48. var data = inputFile.data.matrix;
  49. var outputfile = directories.out + name + '.json';
  50. var thresholds = inputFile.data.thresholds;
  51. var lines = isoLines(data, thresholds);
  52. // console.log(JSON.stringify(lines));
  53. t.deepEqual(lines, load.sync(outputfile), name);
  54. });
  55. t.end();
  56. });
  57. test('isoBands input validation', function (t) {
  58. var dataArr = [[1], [2], [3]];
  59. t.throws(function(){isoBands(null, 0, 5)}, /data is required/, 'missing data');
  60. t.throws(function(){isoBands('string', 0, 5)} , /array of arrays/, 'invalid data');
  61. t.throws(function(){isoBands([1], 0, 5)}, /array of arrays/, 'invalid data again');
  62. t.throws(function(){isoBands(dataArr, null, 5)}, /lowerBound is required/, 'missing lowerBound');
  63. t.throws(function(){isoBands(dataArr, 0, null)}, /bandWidth is required/, 'missing bandWidth');
  64. t.throws(function(){isoBands(dataArr, [0, 1], null)}, /bandWidth is required/, 'missing bandWidth');
  65. t.throws(function(){isoBands(dataArr, [0, 'foo'], [1, 2])}, /is not a number/, 'invalid lowerBound entry');
  66. t.throws(function(){isoBands(dataArr, 'number', 3)}, /lowerBound must be a number/, 'invalid lowerBound');
  67. t.throws(function(){isoBands(dataArr, 23, 'string')}, /bandWidth must be a number/, 'invalid bandWidth');
  68. t.throws(function(){isoBands(dataArr, 0, [1, 5])}, /bandWidth must be a number/, 'invalid lowerBound-bandWidth combination');
  69. t.throws(function(){isoBands(dataArr, [0, 5], [3, 1, 5])}, /unequal lengths/, 'invalid lowerBound-bandWidth combination');
  70. t.throws(function(){isoBands(dataArr, 23, 3, 'string')}, /options must be an object/, 'invalid options');
  71. t.end();
  72. });
  73. test('isoLines input validation', function (t) {
  74. var dataArr = [[1], [2], [3]];
  75. t.throws(function(){isoLines(null, 0)}, /data is required/, 'missing data');
  76. t.throws(function(){isoLines('string', 0)}, /array of arrays/, 'invalid data');
  77. t.throws(function(){isoLines([1], 0)}, /array of arrays/, 'invalid data again');
  78. t.throws(function(){isoLines(dataArr, null)}, /threshold is required/, 'missing threshold');
  79. t.throws(function(){isoLines(dataArr, 'number')}, /threshold must be a number/, 'invalid threshold');
  80. t.throws(function(){isoLines(dataArr, [0, 'foo'])}, /is not a number/, 'invalid threshold entry');
  81. t.throws(function(){isoLines(dataArr, 23, 'string')}, /options must be an object/, 'invalid options');
  82. t.end();
  83. });
  84. test('successCallback check', function (t) {
  85. var data = [[1, 1], [1, 5]];
  86. var called = false;
  87. var options = {
  88. successCallback: function () {
  89. called = true;
  90. }
  91. };
  92. isoLines(data, 1, options);
  93. t.true(called);
  94. called = false;
  95. isoBands(data, 1, 2, options);
  96. t.true(called);
  97. t.end();
  98. });
  99. test('QuadTree', function (t) {
  100. var data = [
  101. [1, 1, 1, 0],
  102. [1, 5, 5, 1],
  103. [0, 5, 7, 1]
  104. ];
  105. var prepData = new QuadTree(data);
  106. t.equal('QuadTree', prepData.constructor.name);
  107. t.equal('TreeNode', prepData.root.constructor.name);
  108. t.throws(function(){new QuadTree(null)}, /data is required/, 'missing data');
  109. t.throws(function(){new QuadTree([ ])}, /array of arrays/, '1D array');
  110. t.throws(function(){new QuadTree([ [ ] ])}, /two rows/, 'Empty 2D array');
  111. t.throws(function(){new QuadTree([ [0] ])}, /two rows/, 'Single row');
  112. t.throws(function(){new QuadTree([ [0], [0] ])}, /two columns/, 'Single column');
  113. t.throws(function(){new QuadTree([ [0, 1], [0] ])}, /unequal row lengths/, 'Unequal row lengths');
  114. /* There are only only two cells with threshold 0 */
  115. t.deepEqual([{x: 2, y: 0},{x: 0, y: 1}], prepData.root.cellsBelowThreshold(0, false));
  116. /* There are only two cells with threshold 7 */
  117. t.deepEqual([{x: 1, y: 1},{x: 2, y: 1}], prepData.root.cellsBelowThreshold(7, false));
  118. /* there is no cell with threshold -2 */
  119. t.deepEqual([], prepData.root.cellsBelowThreshold(-2, false));
  120. /* there is no cell with threshold -2 */
  121. t.deepEqual([], prepData.root.cellsBelowThreshold(10, false));
  122. /* only two cells with band [7:8] */
  123. t.deepEqual([{x: 1, y: 1},{x: 2, y: 1}], prepData.root.cellsInBand(7, 8, false));
  124. t.end();
  125. });