Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerz committed May 24, 2015
1 parent 4f474d1 commit 618649c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ ParticleByResize.prototype.sampling = function (canvas) {

ParticleByResize.prototype.collidedWith = function collidedWith(change, x, y) {
var base = this;

if (x < 0 || x + change.width > base.width || y < 0 || y + change.height > base.height) {
throw new Error('out of bound');
}

var indexInBg = (function (bg, pt, x, y) {
return function (i) {
var xRel = i % pt.width;
Expand Down Expand Up @@ -72,6 +77,11 @@ ParticleByResize.prototype.collidedWith = function collidedWith(change, x, y) {

ParticleByResize.prototype.composite = function composite(change, x, y) {
var base = this;

if (x < 0 || x + change.width > base.width || y < 0 || y + change.height > base.height) {
throw new Error('out of bound');
}

var indexInPt = (function (bg, pt, x, y) {
return function (i) {
var xRel = i % bg.width;
Expand All @@ -84,6 +94,7 @@ ParticleByResize.prototype.composite = function composite(change, x, y) {
return Math.round(xPt + yPt * pt.width);
};
})(base, change, Math.floor(x), Math.floor(y));

var baseData = base.data;
var chngData = change.data;
for (var i = 0; i < baseData.length; i ++) {
Expand Down
2 changes: 1 addition & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8">
<title>Cow tests</title>
<title>tests</title>
<link rel="stylesheet" media="all" href="../node_modules/duo-test/node_modules/mocha/mocha.css">
</head>
<body>
Expand Down
49 changes: 32 additions & 17 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,60 @@ describe('particle', function() {
});

describe('behavior', function () {
var background, particle22, particle33;
var base, change22, change33;
beforeEach(function init() {
background = new Particle();
particle22 = new Particle();
particle33 = new Particle();
base = new Particle();
change22 = new Particle();
change33 = new Particle();

// fake data since resizing result is not easy to predict
background.width = background.height = 3;
background.data = [1, 0, 0, 0, 1, 0, 0, 0, 0];
base.width = base.height = 3;
base.data = [1, 0, 0, 0, 1, 0, 0, 0, 0];

particle22.width = particle22.height = 2;
particle22.data = [0, 1, 1, 0];
change22.width = change22.height = 2;
change22.data = [0, 1, 1, 0];

particle33.width = particle33.height = 3;
particle33.data = [0, 1, 1, 1, 0, 1, 1, 1, 1];
change33.width = change33.height = 3;
change33.data = [0, 1, 1, 1, 0, 1, 1, 1, 1];
});

describe('collision', function () {
it('should detect collision', function () {
expect(background.collidedWith(background, 0, 0)).to.be.true;
expect(base.collidedWith(base, 0, 0)).to.be.true;
});

it('should detect no collision', function () {
expect(background.collidedWith(particle22, 0, 0)).to.be.false;
expect(base.collidedWith(change22, 0, 0)).to.be.false;
});

it('should detect no collision', function () {
expect(background.collidedWith(particle33, 0, 0)).to.be.false;
expect(base.collidedWith(change33, 0, 0)).to.be.false;
});

it('should throw exception when out of bound', function () {
expect(function () {
base.collidedWith(change22, -1, 0);
}).to.throw(Error);
expect(function () {
base.collidedWith(change22, 0, -1);
}).to.throw(Error);
expect(function () {
base.collidedWith(change22, base.width, 0);
}).to.throw(Error);
expect(function () {
base.collidedWith(change22, 0, base.height);
}).to.throw(Error);
});
});

describe('composite', function () {
it('should composite smaller particle', function () {
background.composite(particle22, 0, 0);
expect(background.data).to.eql([1, 1, 0, 1, 1, 0, 0, 0, 0]);
base.composite(change22, 0, 0);
expect(base.data).to.eql([1, 1, 0, 1, 1, 0, 0, 0, 0]);
});
it('should composite same size particle', function () {
background.composite(particle33, 0, 0);
expect(background.data).to.eql([1, 1, 1, 1, 1, 1, 1, 1, 1]);
base.composite(change33, 0, 0);
expect(base.data).to.eql([1, 1, 1, 1, 1, 1, 1, 1, 1]);
});
});
});
Expand Down

0 comments on commit 618649c

Please sign in to comment.