×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Isaac Dettman
Added: Nov 26, 2016 6:04 PM
Views: 6
Fixed 2D Array Test
  1. var test = require('tape');
  2. var FixedArray = require('..');
  3.  
  4. test('Default values', function (t) {
  5.   t.plan(3);
  6.   var fa = new FixedArray(8,9,{abc:123});
  7.   t.equal(fa.get(7,8).abc,123);
  8.   t.equal(fa.get(0,1).abc,123);
  9.   t.throws(function(){new FixedArray(0,1);});
  10. });
  11.  
  12. test('getHeight and getWidth', function (t) {
  13.   t.plan(2);
  14.   var fa = new FixedArray(3,17);
  15.  
  16.   t.equal(fa.getHeight(),3);
  17.   t.equal(fa.getWidth(),17);
  18. });
  19.  
  20. test('get and set', function (t) {
  21.   t.plan(3);
  22.   var fa = new FixedArray(2,3);
  23.   fa.set(1,2,'This is a string!');
  24.   t.equal(fa.get(1,2),'This is a string!');
  25.   t.throws(function(){fa.set(2,3);});
  26.   t.throws(function(){fa.get(9,9);});
  27. });
  28.  
  29. test('get correct row and column',function (t) {
  30.   t.plan(6);
  31.   var fa = new FixedArray(2,3);
  32.  
  33.   t.equal(fa.getRow(0).length,3);
  34.   t.equal(fa.getColumn(1).length,2);
  35.  
  36.   fa.set(0,1,'This is a string!');
  37.   t.equal(fa.getRow(0)[1],'This is a string!');
  38.   t.equal(fa.getColumn(1)[0],'This is a string!');
  39.   t.throws(function(){fa.getRow(2);});
  40.   t.throws(function(){fa.getColumn(3);});
  41. });
  42.  
  43. test('set row and column',function (t) {
  44.   t.plan(8);
  45.   var fa = new FixedArray(5,5,0);
  46.   var newPillar = [1,2,3,4,5];
  47.  
  48.   fa.setRow(0,newPillar);
  49.   t.deepEqual(fa.getRow(0),newPillar);
  50.   fa.setColumn(0,newPillar);
  51.   t.deepEqual(fa.getColumn(0),newPillar);
  52.  
  53.   t.throws(function(){fa.setRow(-1,newPillar);});
  54.   t.throws(function(){fa.setColumn(-1,newPillar);});
  55.  
  56.   var smallPillar = [1,2];
  57.   fa.setRow(0,smallPillar);
  58.   t.equal(fa.getRow(0)[3],undefined);
  59.   fa.setColumn(0,smallPillar);
  60.   t.equal(fa.getColumn(0)[3],undefined);
  61.  
  62.   var largePillar = [1,2,3,4,5,6];
  63.   t.throws(function(){fa.setRow(0,largePillar);});
  64.   t.throws(function(){fa.setColumn(0,largePillar);});
  65. });
  66.  
  67. test('forEach',function (t) {
  68.   t.plan(1);
  69.   var fa = new FixedArray(10,9,0);
  70.  
  71.   function fun(currentValue, index, array){
  72.     array.set(index.x, index.y, currentValue+1);
  73.   }
  74.  
  75.   fa.forEach(fun);
  76.   t.equal(fa.get(0,0),1);
  77. });
  78.  
  79. test('exception on index out of bounds', function (t) {
  80.   t.plan(2);
  81.   var fa = new FixedArray(2,3);
  82.   t.throws(function(){fa.get(2,3);});
  83.   t.throws(function(){fa.set(-1,-1);});
  84. });
  85.  
  86. test('sameSize',function (t) {
  87.   t.plan(3);
  88.   var fa = new FixedArray(2,2);
  89.   var faSameSize = new FixedArray(2,2);
  90.   var faNotSameSize = new FixedArray(2,3);
  91.   var nonfa = [2,2,2,2];
  92.  
  93.   t.true(fa.sameSize(faSameSize));
  94.   t.false(fa.sameSize(faNotSameSize));
  95.   t.throws(function(){fa.sameSize(nonfa);});
  96. });
  97.  
  98. test('get correct number of neighbours',function (t) {
  99.   t.plan(8);
  100.   var fa = new FixedArray(9,10);
  101.   var treatMeLikeANumber = 2;
  102.   t.equal(fa.getNeighbours(5,5).length,8);
  103.   t.equal(fa.getNeighbours(0,0).length,3);
  104.   t.equal(fa.getNeighbours(1,0).length,5);
  105.   t.equal(fa.getNeighbours(0,0,2).length,8);
  106.   t.equal(fa.getNeighbours(2,2,2).length,24);
  107.   t.equal(fa.getNeighbours(treatMeLikeANumber,treatMeLikeANumber,treatMeLikeANumber).length,24);
  108.   t.equal(fa.getNeighbours(2,2,0).length,0);
  109.   t.equal(fa.getNeighbours(2,2,-2).length,0);
  110. });
  111.  
  112. test('areNeighbors',function (t) {
  113.   t.plan(6);
  114.   var fa = new FixedArray(10,10);
  115.   t.true(fa.areNeighbours(0,0,1,1));
  116.   t.false(fa.areNeighbours(0,0,2,2));
  117.   t.true(fa.areNeighbours(0,0,2,2,2));
  118.   t.throws(function(){fa.areNeighbours(-1,-1,0,0);});
  119.   t.throws(function(){fa.areNeighbours(0,0,-1,1);});
  120.   t.equal(fa.areNeighbours(0,0,1,1,0),false);
  121. });
  122.  
  123. test('pushRow',function (t) {
  124.   t.plan(4);
  125.   var fa = new FixedArray(2,3);
  126.   t.equal(fa.pushRow([1,2,3],[1,2]),4);
  127.   t.equal(fa.get(3,2),undefined);
  128.   t.throws(function(){fa.pushRow([1,2,3,4]);});
  129.   t.equal(fa.pushRow(2),4);
  130. });