UNPKG

sku-pg

Version:

Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!

315 lines (275 loc) 10.2 kB
<!DOCTYPE html> <html> <!-- Copyright 2006 The Closure Library Authors. All Rights Reserved. Use of this source code is governed by an Apache 2.0 License. See the COPYING file for details. --> <head> <title>Closure Unit Tests - goog.math.Rect</title> <script src="../base.js"></script> <script> goog.require('goog.math.Box'); goog.require('goog.math.Coordinate'); goog.require('goog.math.Rect'); goog.require('goog.math.Size'); goog.require('goog.testing.jsunit'); </script> </head> <body> <script> /** * Produce legible assertion results. If two rects are not equal, the error * message will be of the form * "Expected <(1, 2 - 10 x 10)> (Object) but was <(3, 4 - 20 x 20)> (Object)" */ function assertRectsEqual(expected, actual) { if (!goog.math.Rect.equals(expected, actual)) { assertEquals(expected, actual); } } function createRect(a) { return a ? new goog.math.Rect(a[0], a[1], a[2] - a[0], a[3] - a[1]) : null; } function testRectClone() { var r = new goog.math.Rect(0, 0, 0, 0); assertRectsEqual(r, r.clone()); r.left = -10; r.top = -20; r.width = 10; r.height = 20; assertRectsEqual(r, r.clone()); } function testRectIntersection() { var tests = [[[10, 10, 20, 20], [15, 15, 25, 25], [15, 15, 20, 20]], [[10, 10, 20, 20], [20, 0, 30, 10], [20, 10, 20, 10]], [[0, 0, 1, 1], [10, 11, 12, 13], null], [[11, 12, 98, 99], [22, 23, 34, 35], [22, 23, 34, 35]]]; for (var i = 0; i < tests.length; ++i) { var t = tests[i]; var r0 = createRect(t[0]); var r1 = createRect(t[1]); var expected = createRect(t[2]); assertRectsEqual(expected, goog.math.Rect.intersection(r0, r1)); assertRectsEqual(expected, goog.math.Rect.intersection(r1, r0)); // Test in place methods. var clone = r0.clone(); assertRectsEqual(expected, clone.intersection(r1) ? clone : null); assertRectsEqual(expected, r1.intersection(r0) ? r1 : null); } } function testRectIntersects() { var r0 = createRect([10, 10, 20, 20]); var r1 = createRect([15, 15, 25, 25]); var r2 = createRect([0, 0, 1, 1]); assertTrue(goog.math.Rect.intersects(r0, r1)); assertTrue(goog.math.Rect.intersects(r1, r0)); assertTrue(r0.intersects(r1)); assertTrue(r1.intersects(r0)); assertFalse(goog.math.Rect.intersects(r0, r2)); assertFalse(goog.math.Rect.intersects(r2, r0)); assertFalse(r0.intersects(r2)); assertFalse(r2.intersects(r0)); } function testRectBoundingRect() { var tests = [[[10, 10, 20, 20], [15, 15, 25, 25], [10, 10, 25, 25]], [[10, 10, 20, 20], [20, 0, 30, 10], [10, 0, 30, 20]], [[0, 0, 1, 1], [10, 11, 12, 13], [0, 0, 12, 13]], [[11, 12, 98, 99], [22, 23, 34, 35], [11, 12, 98, 99]]]; for (var i = 0; i < tests.length; ++i) { var t = tests[i]; var r0 = createRect(t[0]); var r1 = createRect(t[1]); var expected = createRect(t[2]); assertRectsEqual(expected, goog.math.Rect.boundingRect(r0, r1)); assertRectsEqual(expected, goog.math.Rect.boundingRect(r1, r0)); // Test in place methods. var clone = r0.clone(); clone.boundingRect(r1); assertRectsEqual(expected, clone); r1.boundingRect(r0); assertRectsEqual(expected, r1); } } function testRectDifference() { // B is the same as A. assertDifference([10, 10, 20, 20], [10, 10, 20, 20], []); // B does not touch A. assertDifference([10, 10, 20, 20], [0, 0, 5, 5], [[10, 10, 20, 20]]); // B overlaps top half of A. assertDifference([10, 10, 20, 20], [5, 15, 25, 25], [[10, 10, 20, 15]]); // B overlaps bottom half of A. assertDifference([10, 10, 20, 20], [5, 5, 25, 15], [[10, 15, 20, 20]]); // B overlaps right half of A. assertDifference([10, 10, 20, 20], [15, 5, 25, 25], [[10, 10, 15, 20]]); // B overlaps left half of A. assertDifference([10, 10, 20, 20], [5, 5, 15, 25], [[15, 10, 20, 20]]); // B touches A at its bottom right corner assertDifference([10, 10, 20, 20], [20, 20, 30, 30], [[10, 10, 20, 20]]); // B touches A at its top left corner assertDifference([10, 10, 20, 20], [5, 5, 10, 10], [[10, 10, 20, 20]]); // B touches A along its bottom edge assertDifference([10, 10, 20, 20], [12, 20, 17, 25], [[10, 10, 20, 20]]); // B splits A horizontally. assertDifference([10, 10, 20, 20], [5, 12, 25, 18], [[10, 10, 20, 12], [10, 18, 20, 20]]); // B splits A vertically. assertDifference([10, 10, 20, 20], [12, 5, 18, 25], [[10, 10, 12, 20], [18, 10, 20, 20]]); // B subtracts a notch from the top of A. assertDifference([10, 10, 20, 20], [12, 5, 18, 15], [[10, 15, 20, 20], [10, 10, 12, 15], [18, 10, 20, 15]]); // B subtracts a notch from the bottom left of A assertDifference([1, 6, 3, 9], [1, 7, 2, 9], [[1, 6, 3, 7], [2, 7, 3, 9]]); // B subtracts a notch from the bottom right of A assertDifference([1, 6, 3, 9], [2, 7, 3, 9], [[1, 6, 3, 7], [1, 7, 2, 9]]); // B subtracts a notch from the top left of A assertDifference([1, 6, 3, 9], [1, 6, 2, 8], [[1, 8, 3, 9], [2, 6, 3, 8]]); // B subtracts a notch from the top left of A (no coinciding edge) assertDifference([1, 6, 3, 9], [0, 5, 2, 8], [[1, 8, 3, 9], [2, 6, 3, 8]]); // B subtracts a hole from the center of A. assertDifference([-20, -20, -10, -10], [-18, -18, -12, -12], [[-20, -20, -10, -18], [-20, -12, -10, -10], [-20, -18, -18, -12], [-12, -18, -10, -12]]); } function assertDifference(a, b, expected) { var r0 = createRect(a); var r1 = createRect(b); var diff = goog.math.Rect.difference(r0, r1); assertEquals('Wrong number of rectangles in difference ', expected.length, diff.length); for (var j = 0; j < expected.length; ++j) { var e = createRect(expected[j]); if (!goog.math.Rect.equals(e, diff[j])) { alert(j + ": " + e + " != " + diff[j]); } assertRectsEqual(e, diff[j]); } // Test in place version var diff = r0.difference(r1); assertEquals('Wrong number of rectangles in in-place difference ', expected.length, diff.length); for (var j = 0; j < expected.length; ++j) { var e = createRect(expected[j]); if (!goog.math.Rect.equals(e, diff[j])) { alert(j + ": " + e + " != " + diff[j]); } assertRectsEqual(e, diff[j]); } } function testRectToBox() { var r = new goog.math.Rect(0, 0, 0, 0); assertEquals(new goog.math.Box(0, 0, 0, 0).toString(), r.toBox().toString()); r.top = 10; r.left = 10; r.width = 20; r.height = 20; assertEquals(new goog.math.Box(10, 30, 30, 10).toString(), r.toBox().toString()); r.top = -10; r.left = 0; r.width = 10; r.height = 10; assertEquals(new goog.math.Box(-10, 10, 0, 0).toString(), r.toBox().toString()); } function testBoxToRect() { var box = new goog.math.Box(0, 0, 0, 0); assertEquals(new goog.math.Rect(0, 0, 0, 0).toString(), goog.math.Rect.createFromBox(box).toString()); box.top = 10; box.left = 15; box.right = 23; box.bottom = 27; assertEquals(goog.math.Rect.createFromBox(box).toString(), new goog.math.Rect(15, 10, 8, 17).toString()); box.top = -10; box.left = 3; box.right = 12; box.bottom = 7; assertEquals(new goog.math.Rect(3, -10, 9, 17).toString(), goog.math.Rect.createFromBox(box).toString()); } function testBoxToRectAndBack() { rectToBoxAndBackTest(new goog.math.Rect(8, 11, 20, 23)); rectToBoxAndBackTest(new goog.math.Rect(9, 13, NaN, NaN)); rectToBoxAndBackTest(new goog.math.Rect(10, 13, NaN, 21)); rectToBoxAndBackTest(new goog.math.Rect(5, 7, 14, NaN)); } function rectToBoxAndBackTest(rect) { var box = rect.toBox(); var rect2 = goog.math.Rect.createFromBox(box); assertEquals(rect.toString(), rect2.toString()); } function testRectToBoxAndBack() { // This doesn't work if left or top is undefined. boxToRectAndBackTest(new goog.math.Box(11, 13, 20, 17)); boxToRectAndBackTest(new goog.math.Box(10, NaN, NaN, 11)); boxToRectAndBackTest(new goog.math.Box(9, 14, NaN, 11)); boxToRectAndBackTest(new goog.math.Box(10, NaN, 22, 15)); } function boxToRectAndBackTest(box) { var rect = goog.math.Rect.createFromBox(box); var box2 = rect.toBox(); assertEquals(box.toString(), box2.toString()); } function testRectContainsRect() { var r = new goog.math.Rect(-10, 0, 20, 10); assertTrue(r.contains(r)); assertFalse(r.contains(new goog.math.Rect(NaN, NaN, NaN, NaN))); var r2 = new goog.math.Rect(0, 2, 5, 5); assertTrue(r.contains(r2)); assertFalse(r2.contains(r)); r2.left = -11; assertFalse(r.contains(r2)); r2.left = 0; r2.width = 15; assertFalse(r.contains(r2)); r2.width = 5; r2.height = 10; assertFalse(r.contains(r2)); r2.top = 0; assertTrue(r.contains(r2)); } function testRectContainsCoordinate() { var r = new goog.math.Rect(20, 40, 60, 80); // Test middle. assertTrue(r.contains(new goog.math.Coordinate(50, 80))); // Test edges. assertTrue(r.contains(new goog.math.Coordinate(20, 40))); assertTrue(r.contains(new goog.math.Coordinate(50, 40))); assertTrue(r.contains(new goog.math.Coordinate(80, 40))); assertTrue(r.contains(new goog.math.Coordinate(80, 80))); assertTrue(r.contains(new goog.math.Coordinate(80, 120))); assertTrue(r.contains(new goog.math.Coordinate(50, 120))); assertTrue(r.contains(new goog.math.Coordinate(20, 120))); assertTrue(r.contains(new goog.math.Coordinate(20, 80))); // Test outside. assertFalse(r.contains(new goog.math.Coordinate(0, 0))); assertFalse(r.contains(new goog.math.Coordinate(50, 0))); assertFalse(r.contains(new goog.math.Coordinate(100, 0))); assertFalse(r.contains(new goog.math.Coordinate(100, 80))); assertFalse(r.contains(new goog.math.Coordinate(100, 160))); assertFalse(r.contains(new goog.math.Coordinate(50, 160))); assertFalse(r.contains(new goog.math.Coordinate(0, 160))); assertFalse(r.contains(new goog.math.Coordinate(0, 80))); } function testGetSize() { assertEquals(new goog.math.Size(60, 80).toString(), new goog.math.Rect(20, 40, 60, 80).getSize().toString()); } </script> </body> </html>