summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-08-01 18:03:20 -0400
committerJules Laplace <jules@okfoc.us>2014-08-01 18:03:20 -0400
commitbe7412073a5e0b6641f125f29d254315f50bc95c (patch)
tree30bff47776468515e3087ab73110457cb50bd2a6 /test
parentabd50f1cfca26bb8e83275cfb443a2d678625aef (diff)
tests for grouper
Diffstat (limited to 'test')
-rw-r--r--test/06-test-grouper.js121
-rw-r--r--test/mocks/mx.js2
2 files changed, 122 insertions, 1 deletions
diff --git a/test/06-test-grouper.js b/test/06-test-grouper.js
new file mode 100644
index 0000000..1707e4c
--- /dev/null
+++ b/test/06-test-grouper.js
@@ -0,0 +1,121 @@
+var assert = require("assert")
+var vec = require("../public/assets/javascripts/rectangles/models/vec2.js")
+var Rect = require("../public/assets/javascripts/rectangles/models/rect.js")
+var Room = require("../public/assets/javascripts/rectangles/models/room.js")
+var Rooms = require("../public/assets/javascripts/rectangles/engine/rooms/_rooms.js")
+var Clipper = require("../public/assets/javascripts/rectangles/engine/rooms/clipper.js")
+var Builder = require("../public/assets/javascripts/rectangles/engine/rooms/builder.js")
+var Grouper = require("../public/assets/javascripts/rectangles/engine/rooms/grouper.js")
+var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20
+var ALL = FRONT | BACK | LEFT | RIGHT
+function sidesToString(sides){
+ var s = ""
+ if (sides & FRONT) s += "front "
+ if (sides & BACK) s += "back "
+ if (sides & LEFT) s += "left "
+ if (sides & RIGHT) s += "right "
+ if (sides & TOP) s += "top "
+ if (sides & BOTTOM) s += "bottom "
+ return s
+}
+function bitcount(v) {
+ v = v - ((v >>> 1) & 0x55555555);
+ v = (v & 0x33333333) + ((v >>> 2) & 0x33333333);
+ return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24;
+}
+
+var rect = new Rect( new vec(1,5), new vec(1,5) )
+var east = new Rect( new vec(2,6), new vec(1,5) )
+var corner = new Rect( new vec(3,7), new vec(3,7) )
+var peninsula = new Rect( new vec(4,6), new vec(6,8) )
+
+var rect_room = new Room({ id: "rect", rect: rect, height: 2 })
+var east_room = new Room({ id: "east", rect: east, height: 2 })
+var corner_room = new Room({ id: "corner", rect: corner, height: 2 })
+var peninsula_room = new Room({ id: "peninsula", rect: peninsula, height: 2 })
+
+var taller_room = new Room({ id: "taller", rect: rect, height: 3 })
+
+function report(a) {
+ console.log( a.join("\n") )
+}
+function reportSides(walls) {
+ console.log(walls.map(function(w){ return sidesToString(w.side) }).join("\n"))
+}
+function reset(){
+ Rooms.forEach(function(room){
+ room.reset()
+ })
+ Rooms.list = {}
+ Rooms.regions = []
+}
+function rebuild(){
+ Rooms.clipper.solve_rects()
+ Rooms.builder.build()
+}
+
+describe('grouper(rect)', function(){
+ reset()
+ Rooms.add( rect_room )
+ rebuild()
+
+ var collections = Rooms.grouper.collect()
+
+ describe('#collect(rect)', function(){
+ it("should return 4 sets of 1 wall each", function(){
+ assert.equal(1, collections[FRONT].length)
+ assert.equal(1, collections[BACK].length)
+ assert.equal(1, collections[LEFT].length)
+ assert.equal(1, collections[RIGHT].length)
+ })
+ })
+})
+
+describe('grouper(rect,east)', function(){
+ reset()
+ Rooms.add( rect_room )
+ Rooms.add( east_room )
+ rebuild()
+
+ var collections = Rooms.grouper.collect()
+
+ describe('#collect(rect, east)', function(){
+ it("should find 3 walls on front/back, 1 wall on left/right", function(){
+ assert.equal(3, collections[FRONT].length)
+ assert.equal(3, collections[BACK].length)
+ assert.equal(1, collections[LEFT].length)
+ assert.equal(1, collections[RIGHT].length)
+ })
+ })
+
+ describe('#group(rect, east)', function(){
+ var front_walls = Rooms.grouper.group([], collections, FRONT)
+ var back_walls = Rooms.grouper.group([], collections, BACK)
+ var left_walls = Rooms.grouper.group([], collections, LEFT)
+ var right_walls = Rooms.grouper.group([], collections, RIGHT)
+
+ it("each side now has one wall", function(){
+ assert.equal(1, left_walls.length)
+ assert.equal(1, right_walls.length)
+ assert.equal(1, front_walls.length)
+ assert.equal(1, back_walls.length)
+ })
+ it("front wall is now 5 units long, contains 3 mx elements", function(){
+ var wall = front_walls[0]
+ assert.equal(5, wall.vec.length())
+ assert.equal(3, wall.mx.length)
+ })
+ it("back wall is also 5 units long, contains 3 mx elements", function(){
+ var wall = back_walls[0]
+ assert.equal(5, wall.vec.length())
+ assert.equal(3, wall.mx.length)
+ })
+ it("left wall is still 4 units long, contains 1 mx element", function(){
+ var wall = left_walls[0]
+ assert.equal(4, wall.vec.length())
+ assert.equal(1, wall.mx.length)
+ })
+ })
+})
+
+
diff --git a/test/mocks/mx.js b/test/mocks/mx.js
index 2a76ffc..889f4bc 100644
--- a/test/mocks/mx.js
+++ b/test/mocks/mx.js
@@ -16,7 +16,7 @@ MX.Object3D = function (klass) {
MX.Scene = {
els: [],
add: function (el) {
- els.push(el)
+ MX.Scene.els.push(el)
}
}