coordinate3_test.js 5.7 KB

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