| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.math.CoordinateTest'); |
| 16 |
goog.setTestOnly('goog.math.CoordinateTest'); |
| 17 |
|
| 18 |
goog.require('goog.math.Coordinate'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
|
| 21 |
function testCoordinate1() { |
| 22 |
var dim1 = new goog.math.Coordinate(); |
| 23 |
assertEquals(0, dim1.x); |
| 24 |
assertEquals(0, dim1.y); |
| 25 |
assertEquals('(0, 0)', dim1.toString()); |
| 26 |
} |
| 27 |
|
| 28 |
function testCoordinate2() { |
| 29 |
var dim2 = new goog.math.Coordinate(10); |
| 30 |
assertEquals(10, dim2.x); |
| 31 |
assertEquals(0, dim2.y); |
| 32 |
assertEquals('(10, 0)', dim2.toString()); |
| 33 |
} |
| 34 |
|
| 35 |
function testCoordinate3() { |
| 36 |
var dim3 = new goog.math.Coordinate(10, 20); |
| 37 |
assertEquals(10, dim3.x); |
| 38 |
assertEquals(20, dim3.y); |
| 39 |
assertEquals('(10, 20)', dim3.toString()); |
| 40 |
} |
| 41 |
|
| 42 |
function testCoordinate4() { |
| 43 |
var dim4 = new goog.math.Coordinate(10.5, 20.897); |
| 44 |
assertEquals(10.5, dim4.x); |
| 45 |
assertEquals(20.897, dim4.y); |
| 46 |
assertEquals('(10.5, 20.897)', dim4.toString()); |
| 47 |
} |
| 48 |
|
| 49 |
function testCoordinate5() { |
| 50 |
var dim5 = new goog.math.Coordinate(NaN, 1000); |
| 51 |
assertTrue(isNaN(dim5.x)); |
| 52 |
assertEquals(1000, dim5.y); |
| 53 |
assertEquals('(NaN, 1000)', dim5.toString()); |
| 54 |
} |
| 55 |
|
| 56 |
function testCoordinateSquaredDistance() { |
| 57 |
var a = new goog.math.Coordinate(7, 11); |
| 58 |
var b = new goog.math.Coordinate(3, -1); |
| 59 |
assertEquals(160, goog.math.Coordinate.squaredDistance(a, b)); |
| 60 |
} |
| 61 |
|
| 62 |
function testCoordinateDistance() { |
| 63 |
var a = new goog.math.Coordinate(-2, -3); |
| 64 |
var b = new goog.math.Coordinate(2, 0); |
| 65 |
assertEquals(5, goog.math.Coordinate.distance(a, b)); |
| 66 |
} |
| 67 |
|
| 68 |
function testCoordinateMagnitude() { |
| 69 |
var a = new goog.math.Coordinate(5, 5); |
| 70 |
assertEquals(Math.sqrt(50), goog.math.Coordinate.magnitude(a)); |
| 71 |
} |
| 72 |
|
| 73 |
function testCoordinateAzimuth() { |
| 74 |
var a = new goog.math.Coordinate(5, 5); |
| 75 |
assertEquals(45, goog.math.Coordinate.azimuth(a)); |
| 76 |
} |
| 77 |
|
| 78 |
function testCoordinateClone() { |
| 79 |
var c = new goog.math.Coordinate(); |
| 80 |
assertEquals(c.toString(), c.clone().toString()); |
| 81 |
c.x = -12; |
| 82 |
c.y = 13; |
| 83 |
assertEquals(c.toString(), c.clone().toString()); |
| 84 |
} |
| 85 |
|
| 86 |
function testCoordinateEquals() { |
| 87 |
var a = new goog.math.Coordinate(1, 2); |
| 88 |
|
| 89 |
assertFalse(a.equals(null)); |
| 90 |
assertFalse(a.equals({})); |
| 91 |
assertFalse(a.equals(new goog.math.Coordinate(1, 3))); |
| 92 |
assertFalse(a.equals(new goog.math.Coordinate(2, 2))); |
| 93 |
|
| 94 |
assertTrue(a.equals(a)); |
| 95 |
assertTrue(a.equals(new goog.math.Coordinate(1, 2))); |
| 96 |
} |
| 97 |
|
| 98 |
function testCoordinateDifference() { |
| 99 |
assertObjectEquals( |
| 100 |
new goog.math.Coordinate(3, -40), |
| 101 |
goog.math.Coordinate.difference( |
| 102 |
new goog.math.Coordinate(5, 10), new goog.math.Coordinate(2, 50))); |
| 103 |
} |
| 104 |
|
| 105 |
function testCoordinateSum() { |
| 106 |
assertObjectEquals( |
| 107 |
new goog.math.Coordinate(7, 60), |
| 108 |
goog.math.Coordinate.sum( |
| 109 |
new goog.math.Coordinate(5, 10), new goog.math.Coordinate(2, 50))); |
| 110 |
} |
| 111 |
|
| 112 |
function testCoordinateCeil() { |
| 113 |
var c = new goog.math.Coordinate(5.2, 7.6); |
| 114 |
assertObjectEquals(new goog.math.Coordinate(6, 8), c.ceil()); |
| 115 |
c = new goog.math.Coordinate(-1.2, -3.9); |
| 116 |
assertObjectEquals(new goog.math.Coordinate(-1, -3), c.ceil()); |
| 117 |
} |
| 118 |
|
| 119 |
function testCoordinateFloor() { |
| 120 |
var c = new goog.math.Coordinate(5.2, 7.6); |
| 121 |
assertObjectEquals(new goog.math.Coordinate(5, 7), c.floor()); |
| 122 |
c = new goog.math.Coordinate(-1.2, -3.9); |
| 123 |
assertObjectEquals(new goog.math.Coordinate(-2, -4), c.floor()); |
| 124 |
} |
| 125 |
|
| 126 |
function testCoordinateRound() { |
| 127 |
var c = new goog.math.Coordinate(5.2, 7.6); |
| 128 |
assertObjectEquals(new goog.math.Coordinate(5, 8), c.round()); |
| 129 |
c = new goog.math.Coordinate(-1.2, -3.9); |
| 130 |
assertObjectEquals(new goog.math.Coordinate(-1, -4), c.round()); |
| 131 |
} |
| 132 |
|
| 133 |
function testCoordinateTranslateCoordinate() { |
| 134 |
var c = new goog.math.Coordinate(10, 20); |
| 135 |
var t = new goog.math.Coordinate(5, 10); |
| 136 |
// The translate function modifies the coordinate instead of |
| 137 |
// returning a new one. |
| 138 |
assertEquals(c, c.translate(t)); |
| 139 |
assertObjectEquals(new goog.math.Coordinate(15, 30), c); |
| 140 |
} |
| 141 |
|
| 142 |
function testCoordinateTranslateXY() { |
| 143 |
var c = new goog.math.Coordinate(10, 20); |
| 144 |
// The translate function modifies the coordinate instead of |
| 145 |
// returning a new one. |
| 146 |
assertEquals(c, c.translate(25, 5)); |
| 147 |
assertObjectEquals(new goog.math.Coordinate(35, 25), c); |
| 148 |
} |
| 149 |
|
| 150 |
function testCoordinateTranslateX() { |
| 151 |
var c = new goog.math.Coordinate(10, 20); |
| 152 |
// The translate function modifies the coordinate instead of |
| 153 |
// returning a new one. |
| 154 |
assertEquals(c, c.translate(5)); |
| 155 |
assertObjectEquals(new goog.math.Coordinate(15, 20), c); |
| 156 |
} |
| 157 |
|
| 158 |
function testCoordinateScaleXY() { |
| 159 |
var c = new goog.math.Coordinate(10, 15); |
| 160 |
// The scale function modifies the coordinate instead of returning a new one. |
| 161 |
assertEquals(c, c.scale(2, 3)); |
| 162 |
assertObjectEquals(new goog.math.Coordinate(20, 45), c); |
| 163 |
} |
| 164 |
|
| 165 |
function testCoordinateScaleFactor() { |
| 166 |
var c = new goog.math.Coordinate(10, 15); |
| 167 |
// The scale function modifies the coordinate instead of returning a new one. |
| 168 |
assertEquals(c, c.scale(2)); |
| 169 |
assertObjectEquals(new goog.math.Coordinate(20, 30), c); |
| 170 |
} |
| 171 |
|
| 172 |
function testCoordinateRotateRadians() { |
| 173 |
var c = new goog.math.Coordinate(15, 75); |
| 174 |
c.rotateRadians(Math.PI / 2, new goog.math.Coordinate(10, 70)); |
| 175 |
assertObjectEquals(new goog.math.Coordinate(5, 75), c); |
| 176 |
} |
| 177 |
|
| 178 |
function testCoordinateRotateDegrees() { |
| 179 |
var c = new goog.math.Coordinate(15, 75); |
| 180 |
c.rotateDegrees(90, new goog.math.Coordinate(10, 70)); |
| 181 |
assertObjectEquals(new goog.math.Coordinate(5, 75), c); |
| 182 |
} |