From a8ed33d8313b093f589a5483cb00c7163b7b8dc3 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 18 Aug 2014 15:01:56 -0400 Subject: surface tests --- test/01-test-vec2.js | 19 ++++ test/07-test-surface.js | 271 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 232 insertions(+), 58 deletions(-) (limited to 'test') diff --git a/test/01-test-vec2.js b/test/01-test-vec2.js index 6104f92..429c537 100644 --- a/test/01-test-vec2.js +++ b/test/01-test-vec2.js @@ -37,6 +37,25 @@ describe('vec2', function(){ }) }) + describe('#intersection()', function(){ + it('intersects from left', function(){ + var inter = vec.intersection( new vec2( 5, 15 ) ) + assert.equal(true, inter.eq( new vec2(5, 10) )) + }) + it('intersects from right', function(){ + var inter = vec.intersection( new vec2( -5, 5 ) ) + assert.equal(true, inter.eq( new vec2(0, 5) )) + }) + it('intersects inner', function(){ + var inter = vec.intersection( new vec2( 2, 5 ) ) + assert.equal(true, inter.eq( new vec2(2, 5) )) + }) + it('intersects outer', function(){ + var inter = vec.intersection( new vec2( -5, 15 ) ) + assert.equal(true, inter.eq( new vec2(0, 10) )) + }) + }) + describe('#contains()', function(){ it('contains itself', function(){ assert.equal(true, vec.contains( 0 )); diff --git a/test/07-test-surface.js b/test/07-test-surface.js index 2a51b7f..fa05d43 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -7,15 +7,19 @@ var Surface = require("../public/assets/javascripts/rectangles/models/surface.js // [[3 4] [2 4]] front back left right // [[4 5] [0 4]] front back left right +var small = new vec2(2, 2) +var wide = new vec2(5, 1) +var tall = new vec2(1, 5) +var large = new vec2(7, 7) +var position = new vec2(2, 1) + describe('basic surface', function(){ var surface = new Surface () surface.add( new Rect( new vec2(1, 6), new vec2(0, 4) ) ) - var small = new vec2(2, 2) - var oblong = new vec2(4, 1) + var position = new vec2(2, 1) describe('#clamp_delta()', function(){ - var position = new vec2(2,1) it("does not alter a zero delta", function(){ var delta = new vec2(0,0) surface.clamp_delta(surface.bounds, small, position, delta) @@ -48,64 +52,152 @@ describe('basic surface', function(){ }) }) - describe('#place()', function(){ - it("fits a small element on the top left", function(){ - var bounds = surface.place(small, new vec2(1,3)) -// console.log(bounds) + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) }) - it("places a small element on the right", function(){ - var bounds = surface.place(small, new vec2(4,6)) -// console.log(bounds) + it("returns false for large image", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, large) + assert.equal(false, bounds) }) }) - - // describe placement - // describe dragging up (clamp at top edge) - // describe dragging down (clamp at bottom edge) - // describe dragging left (clamp at left edge) - // describe dragging right (clamp at right edge) }) describe('double surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) - surface.add( new Rect( new vec2(3, 5), new vec2(0, 4) ) ) - - var small = new vec2(2, 2) - var oblong = new vec2(4, 1) + surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 6), new vec2(0, 4) ) ) - // describe placement - // describe dragging up (clamp at top edge) - // describe dragging down (clamp at bottom edge) - // describe dragging left (clamp at left edge) - // describe dragging right (clamp at right edge) + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("returns false for large image", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, large) + assert.equal(false, bounds) + }) + }) }) describe('two-level surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) - surface.add( new Rect( new vec2(3, 5), new vec2(0, 6) ) ) - - var small = new vec2(2, 2) - var oblong = new vec2(4, 1) - - // describe placement/centering - // describe dragging up (clamp at top edge) - // describe dragging down (clamp at bottom edge) - // describe dragging left (clamp at left edge) - // describe dragging right (clamp at right edge) - // describe dragging up and right (clamp at top edge, then pop into peninsula space) + surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 6), new vec2(0, 6) ) ) + + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(3,0, 6,6))) + }) + it("generates proper bounds with wide", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with tall", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, tall) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(3,0, 6,6))) + }) + it("generates the same bounds with tall from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, tall) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(3,0, 6,6))) + }) + it("returns false for large image", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, large) + assert.equal(false, bounds) + }) + + }) + }) describe('door surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) - surface.add( new Rect( new vec2(3, 4), new vec2(2, 4) ) ) - surface.add( new Rect( new vec2(4, 6), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(1, 4), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(4, 5), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(5, 8), new vec2(0, 4) ) ) - var small = new vec2(2, 2) - var large = new vec2(10, 10) - var oblong = new vec2(4, 1) + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 4,4))) + }) + it("generates proper bounds from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(5,0, 8,4))) + }) + + it("generates proper bounds for wide from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + it("generates proper bounds for wide from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + it("generates proper bounds for wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + + it("returns false for tall", function(){ + assert.equal(false, surface.bounds_at_index_with_dimensions(0, tall)) + assert.equal(false, surface.bounds_at_index_with_dimensions(1, tall)) + assert.equal(false, surface.bounds_at_index_with_dimensions(2, tall)) + }) + it("returns false for large image", function(){ + assert.equal(false, surface.bounds_at_index_with_dimensions(0, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(1, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(2, large)) + }) + }) describe('#fits()', function(){ it("fits something small", function(){ @@ -114,8 +206,8 @@ describe('door surface', function(){ it("doesn't fit something large", function(){ assert.equal(false, !! surface.fits(large)) }) - it("fits something oblong", function(){ - assert.equal(true, !! surface.fits(oblong)) + it("fits something wide", function(){ + assert.equal(true, !! surface.fits(wide)) }) }) describe('#fits_scale()', function(){ @@ -125,24 +217,87 @@ describe('door surface', function(){ it("doesn't fit something small, scaled up", function(){ assert.equal(false, !! surface.fits_scale(small, 10)) }) - it("doesn't fit something oblong, scaled up", function(){ - assert.equal(false, !! surface.fits_scale(oblong, 10)) + it("doesn't fit something wide, scaled up", function(){ + assert.equal(false, !! surface.fits_scale(wide, 10)) }) }) - describe('#place()', function(){ - it("fits a small element on the top left", function(){ - var bounds = surface.place(small, new vec2(1,3)) -// console.log(bounds) +}) + +describe('double door surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(1, 4), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(4, 5), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(5, 8), new vec2(0, 6) ) ) + surface.add( new Rect( new vec2(8, 10), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(10, 14), new vec2(0, 4) ) ) + + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 4,4))) }) - it("places a small element on the right", function(){ - var bounds = surface.place(small, new vec2(4,6)) -// console.log(bounds) + it("generates proper bounds from left door", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) }) - }) + it("generates proper bounds from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + }) + it("generates proper bounds from right door", function(){ + var bounds = surface.bounds_at_index_with_dimensions(3, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(4, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(10,0, 14,4))) + }) + + it("generates proper bounds for wide from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + it("generates proper bounds for wide from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + it("generates proper bounds for wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(4, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + + it("places tall in middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, tall) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + + var bounds = surface.bounds_at_index_with_dimensions(1, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) - describe('#clamp()', function(){ - it("", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + + var bounds = surface.bounds_at_index_with_dimensions(3, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + + var bounds = surface.bounds_at_index_with_dimensions(4, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + }) + it("returns false for large image", function(){ + assert.equal(false, surface.bounds_at_index_with_dimensions(0, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(1, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(2, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(3, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(4, large)) }) }) -- cgit v1.2.3-70-g09d2 From 559799f707a8c7fbd0d6296dfbce81ac6f9b376a Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 18 Aug 2014 18:42:41 -0400 Subject: translation tests --- .../assets/javascripts/rectangles/models/rect.js | 3 + .../javascripts/rectangles/models/surface.js | 23 +++++++- test/07-test-surface.js | 68 ++++++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 5952f6a..f23ab9e 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -95,6 +95,9 @@ Rect.prototype.contains = function(x,y){ return this.x.contains(x) && this.y.contains(y) } + Rect.prototype.contains_point = function(p){ + return this.x.contains(p.x) && this.y.contains(p.y) + } Rect.prototype.containsDisc = function(x,y,r){ return this.x.containsDisc(x,r) && this.y.containsDisc(y,r) } diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 0e0926f..3f43ec2 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -100,12 +100,28 @@ if (top_edge > 0) { delta.b -= top_edge } + + return delta } Surface.prototype.translate = function (old_bounds, dimension, position, delta) { + this.clamp_delta( this.bounds, dimension, position, delta ) + + var new_delta = delta.clone() + if (this.clamp_delta(old_bounds, dimension, position, new_delta).eq(delta)) { + return old_bounds + } + + var center_index = this.index_for_x( position.a + dimension.a/2 + delta.a, 0 ) + var new_bounds = this.bounds_at_index_with_dimensions(center_index, dimension) + + this.clamp_delta(new_bounds, dimension, position, delta) - var left_side = this.index_for_x( position.a + delta.a, 0 ) + return new_bounds + +/* + var left_side = this.index_for_x( position.a + delta.a, 0 ) var right_side = this.index_for_x( position.a + dimension.a + delta.a, left_side ) var bounds = this.sides[left_side].clone() @@ -126,11 +142,12 @@ this.clamp_delta(bounds, dimension, position, delta) return bounds +*/ } Surface.prototype.index_for_x = function(x, min_i){ min_i = min_i || 0 - if (x < 0 || x > width) { + if (x < 0 || x > this.width) { return -1 } for (var i = min_i; i < this.faces.length; i++) { @@ -162,6 +179,8 @@ else if (bounds.width() < width) { intersection = bounds.y.intersection(face.y) if (intersection.length() < height) { + // not totally sure if we can clobber the bounds here since this would prevent + // us from looking right later break } else { diff --git a/test/07-test-surface.js b/test/07-test-surface.js index fa05d43..8a6d3d0 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -210,6 +210,7 @@ describe('door surface', function(){ assert.equal(true, !! surface.fits(wide)) }) }) + describe('#fits_scale()', function(){ it("fits something large, scaled down", function(){ assert.equal(true, !! surface.fits_scale(large, 0.1)) @@ -222,6 +223,73 @@ describe('door surface', function(){ }) }) + var position = new vec2(1, 2) + var bounds = surface.bounds_at_index_with_dimensions(0, small) + + describe('#translate()', function(){ + it("does not alter a zero delta", function(){ + var delta = new vec2(0,0) + var new_bounds = surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, 0 ))) + }) + it("does not alter a minimal delta", function(){ + var delta = new vec2(1, 1) + surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 1, 1 ))) + }) + it("clamps leftward delta", function(){ + var delta = new vec2(-10, 0) + surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, 0 ))) + }) + it("clamps 2px rightward delta", function(){ + var delta = new vec2(2, 0) + surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 1, 0 ))) + }) + it("clamps 3px rightward delta", function(){ + var delta = new vec2(3, 0) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+"") + assert.equal(true, delta.eq(new vec2( 3, 0 ))) + }) + it("clamps 4px rightward delta", function(){ + var delta = new vec2(4, 0) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+"") + assert.equal(true, delta.eq(new vec2( 4, 0 ))) + }) + it("clamps 5px rightward delta to new bounds", function(){ + var delta = new vec2(5, 0) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+"") + assert.equal(true, delta.eq(new vec2( 5, 0 ))) + }) + it("clamps 6px rightward delta", function(){ + var delta = new vec2(6, 0) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+"") + assert.equal(true, delta.eq(new vec2( 5, 0 ))) + }) + it("clamps 20px rightward delta", function(){ + var delta = new vec2(7, 0) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+"") + assert.equal(true, delta.eq(new vec2( 5, 0 ))) + }) + it("clamps upward delta", function(){ + var delta = new vec2(0, 10) + surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, 1 ))) + }) + it("clamps downward delta", function(){ + var delta = new vec2(0, -10) + surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, -1 ))) + }) + + }) + }) describe('double door surface', function(){ -- cgit v1.2.3-70-g09d2