| 1 |
// Copyright 2008 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.Coordinate3Test'); |
| 16 |
goog.setTestOnly('goog.math.Coordinate3Test'); |
| 17 |
|
| 18 |
goog.require('goog.math.Coordinate3'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
|
| 21 |
function assertCoordinate3Equals(a, b) { |
| 22 |
assertTrue( |
| 23 |
b + ' should be equal to ' + a, goog.math.Coordinate3.equals(a, b)); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
function testCoordinate3MissingXYZ() { |
| 28 |
var noXYZ = new goog.math.Coordinate3(); |
| 29 |
assertEquals(0, noXYZ.x); |
| 30 |
assertEquals(0, noXYZ.y); |
| 31 |
assertEquals(0, noXYZ.z); |
| 32 |
assertCoordinate3Equals(noXYZ, new goog.math.Coordinate3()); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
function testCoordinate3MissingYZ() { |
| 37 |
var noYZ = new goog.math.Coordinate3(10); |
| 38 |
assertEquals(10, noYZ.x); |
| 39 |
assertEquals(0, noYZ.y); |
| 40 |
assertEquals(0, noYZ.z); |
| 41 |
assertCoordinate3Equals(noYZ, new goog.math.Coordinate3(10)); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
function testCoordinate3MissingZ() { |
| 46 |
var noZ = new goog.math.Coordinate3(10, 20); |
| 47 |
assertEquals(10, noZ.x); |
| 48 |
assertEquals(20, noZ.y); |
| 49 |
assertEquals(0, noZ.z); |
| 50 |
assertCoordinate3Equals(noZ, new goog.math.Coordinate3(10, 20)); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
function testCoordinate3IntegerValues() { |
| 55 |
var intCoord = new goog.math.Coordinate3(10, 20, -19); |
| 56 |
assertEquals(10, intCoord.x); |
| 57 |
assertEquals(20, intCoord.y); |
| 58 |
assertEquals(-19, intCoord.z); |
| 59 |
assertCoordinate3Equals(intCoord, new goog.math.Coordinate3(10, 20, -19)); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
function testCoordinate3FloatValues() { |
| 64 |
var floatCoord = new goog.math.Coordinate3(10.5, 20.897, -71.385); |
| 65 |
assertEquals(10.5, floatCoord.x); |
| 66 |
assertEquals(20.897, floatCoord.y); |
| 67 |
assertEquals(-71.385, floatCoord.z); |
| 68 |
assertCoordinate3Equals( |
| 69 |
floatCoord, new goog.math.Coordinate3(10.5, 20.897, -71.385)); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
function testCoordinate3OneNonNumericValue() { |
| 74 |
var dim5 = new goog.math.Coordinate3('ten', 1000, 85); |
| 75 |
assertTrue(isNaN(dim5.x)); |
| 76 |
assertEquals(1000, dim5.y); |
| 77 |
assertEquals(85, dim5.z); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
function testCoordinate3AllNonNumericValues() { |
| 82 |
var nonNumeric = |
| 83 |
new goog.math.Coordinate3('ten', {woop: 'test'}, Math.sqrt(-1)); |
| 84 |
assertTrue(isNaN(nonNumeric.x)); |
| 85 |
assertTrue(isNaN(nonNumeric.y)); |
| 86 |
assertTrue(isNaN(nonNumeric.z)); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
function testCoordinate3Origin() { |
| 91 |
var origin = new goog.math.Coordinate3(0, 0, 0); |
| 92 |
assertEquals(0, origin.x); |
| 93 |
assertEquals(0, origin.y); |
| 94 |
assertEquals(0, origin.z); |
| 95 |
assertCoordinate3Equals(origin, new goog.math.Coordinate3(0, 0, 0)); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
function testCoordinate3Clone() { |
| 100 |
var c = new goog.math.Coordinate3(); |
| 101 |
assertCoordinate3Equals(c, c.clone()); |
| 102 |
c.x = -12; |
| 103 |
c.y = 13; |
| 104 |
c.z = 5; |
| 105 |
assertCoordinate3Equals(c, c.clone()); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
function testToString() { |
| 110 |
assertEquals('(0, 0, 0)', new goog.math.Coordinate3().toString()); |
| 111 |
assertEquals('(1, 0, 0)', new goog.math.Coordinate3(1).toString()); |
| 112 |
assertEquals('(1, 2, 0)', new goog.math.Coordinate3(1, 2).toString()); |
| 113 |
assertEquals('(0, 0, 0)', new goog.math.Coordinate3(0, 0, 0).toString()); |
| 114 |
assertEquals('(1, 2, 3)', new goog.math.Coordinate3(1, 2, 3).toString()); |
| 115 |
assertEquals('(-4, 5, -3)', new goog.math.Coordinate3(-4, 5, -3).toString()); |
| 116 |
assertEquals( |
| 117 |
'(11.25, -71.935, 2.8)', |
| 118 |
new goog.math.Coordinate3(11.25, -71.935, 2.8).toString()); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
function testEquals() { |
| 123 |
var a = new goog.math.Coordinate3(3, 4, 5); |
| 124 |
var b = new goog.math.Coordinate3(3, 4, 5); |
| 125 |
var c = new goog.math.Coordinate3(-3, 4, -5); |
| 126 |
|
| 127 |
assertTrue(goog.math.Coordinate3.equals(null, null)); |
| 128 |
assertFalse(goog.math.Coordinate3.equals(a, null)); |
| 129 |
assertTrue(goog.math.Coordinate3.equals(a, a)); |
| 130 |
assertTrue(goog.math.Coordinate3.equals(a, b)); |
| 131 |
assertFalse(goog.math.Coordinate3.equals(a, c)); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
function testCoordinate3Distance() { |
| 136 |
var a = new goog.math.Coordinate3(-2, -3, 1); |
| 137 |
var b = new goog.math.Coordinate3(2, 0, 1); |
| 138 |
assertEquals(5, goog.math.Coordinate3.distance(a, b)); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
function testCoordinate3SquaredDistance() { |
| 143 |
var a = new goog.math.Coordinate3(7, 11, 1); |
| 144 |
var b = new goog.math.Coordinate3(3, -1, 1); |
| 145 |
assertEquals(160, goog.math.Coordinate3.squaredDistance(a, b)); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
function testCoordinate3Difference() { |
| 150 |
var a = new goog.math.Coordinate3(7, 11, 1); |
| 151 |
var b = new goog.math.Coordinate3(3, -1, 1); |
| 152 |
assertCoordinate3Equals( |
| 153 |
goog.math.Coordinate3.difference(a, b), |
| 154 |
new goog.math.Coordinate3(4, 12, 0)); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
function testToArray() { |
| 159 |
var a = new goog.math.Coordinate3(7, 11, 1); |
| 160 |
var b = a.toArray(); |
| 161 |
assertEquals(b.length, 3); |
| 162 |
assertEquals(b[0], 7); |
| 163 |
assertEquals(b[1], 11); |
| 164 |
assertEquals(b[2], 1); |
| 165 |
|
| 166 |
var c = new goog.math.Coordinate3('abc', 'def', 'xyz'); |
| 167 |
var result = c.toArray(); |
| 168 |
assertTrue(isNaN(result[0])); |
| 169 |
assertTrue(isNaN(result[1])); |
| 170 |
assertTrue(isNaN(result[2])); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
function testFromArray() { |
| 175 |
var a = [1, 2, 3]; |
| 176 |
var b = goog.math.Coordinate3.fromArray(a); |
| 177 |
assertEquals('(1, 2, 3)', b.toString()); |
| 178 |
|
| 179 |
var c = [1, 2]; |
| 180 |
var d = goog.math.Coordinate3.fromArray(c); |
| 181 |
assertEquals('(1, 2, 0)', d.toString()); |
| 182 |
|
| 183 |
var e = [1]; |
| 184 |
var f = goog.math.Coordinate3.fromArray(e); |
| 185 |
assertEquals('(1, 0, 0)', f.toString()); |
| 186 |
|
| 187 |
var g = []; |
| 188 |
var h = goog.math.Coordinate3.fromArray(g); |
| 189 |
assertEquals('(0, 0, 0)', h.toString()); |
| 190 |
|
| 191 |
var tooLong = [1, 2, 3, 4, 5, 6]; |
| 192 |
assertThrows( |
| 193 |
'Error should be thrown attempting to convert an invalid type.', |
| 194 |
goog.partial(goog.math.Coordinate3.fromArray, tooLong)); |
| 195 |
} |