coordinate_test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. goog.provide('goog.math.CoordinateTest');
  15. goog.setTestOnly('goog.math.CoordinateTest');
  16. goog.require('goog.math.Coordinate');
  17. goog.require('goog.testing.jsunit');
  18. function testCoordinate1() {
  19. var dim1 = new goog.math.Coordinate();
  20. assertEquals(0, dim1.x);
  21. assertEquals(0, dim1.y);
  22. assertEquals('(0, 0)', dim1.toString());
  23. }
  24. function testCoordinate2() {
  25. var dim2 = new goog.math.Coordinate(10);
  26. assertEquals(10, dim2.x);
  27. assertEquals(0, dim2.y);
  28. assertEquals('(10, 0)', dim2.toString());
  29. }
  30. function testCoordinate3() {
  31. var dim3 = new goog.math.Coordinate(10, 20);
  32. assertEquals(10, dim3.x);
  33. assertEquals(20, dim3.y);
  34. assertEquals('(10, 20)', dim3.toString());
  35. }
  36. function testCoordinate4() {
  37. var dim4 = new goog.math.Coordinate(10.5, 20.897);
  38. assertEquals(10.5, dim4.x);
  39. assertEquals(20.897, dim4.y);
  40. assertEquals('(10.5, 20.897)', dim4.toString());
  41. }
  42. function testCoordinate5() {
  43. var dim5 = new goog.math.Coordinate(NaN, 1000);
  44. assertTrue(isNaN(dim5.x));
  45. assertEquals(1000, dim5.y);
  46. assertEquals('(NaN, 1000)', dim5.toString());
  47. }
  48. function testCoordinateSquaredDistance() {
  49. var a = new goog.math.Coordinate(7, 11);
  50. var b = new goog.math.Coordinate(3, -1);
  51. assertEquals(160, goog.math.Coordinate.squaredDistance(a, b));
  52. }
  53. function testCoordinateDistance() {
  54. var a = new goog.math.Coordinate(-2, -3);
  55. var b = new goog.math.Coordinate(2, 0);
  56. assertEquals(5, goog.math.Coordinate.distance(a, b));
  57. }
  58. function testCoordinateMagnitude() {
  59. var a = new goog.math.Coordinate(5, 5);
  60. assertEquals(Math.sqrt(50), goog.math.Coordinate.magnitude(a));
  61. }
  62. function testCoordinateAzimuth() {
  63. var a = new goog.math.Coordinate(5, 5);
  64. assertEquals(45, goog.math.Coordinate.azimuth(a));
  65. }
  66. function testCoordinateClone() {
  67. var c = new goog.math.Coordinate();
  68. assertEquals(c.toString(), c.clone().toString());
  69. c.x = -12;
  70. c.y = 13;
  71. assertEquals(c.toString(), c.clone().toString());
  72. }
  73. function testCoordinateEquals() {
  74. var a = new goog.math.Coordinate(1, 2);
  75. assertFalse(a.equals(null));
  76. assertFalse(a.equals({}));
  77. assertFalse(a.equals(new goog.math.Coordinate(1, 3)));
  78. assertFalse(a.equals(new goog.math.Coordinate(2, 2)));
  79. assertTrue(a.equals(a));
  80. assertTrue(a.equals(new goog.math.Coordinate(1, 2)));
  81. }
  82. function testCoordinateDifference() {
  83. assertObjectEquals(
  84. new goog.math.Coordinate(3, -40),
  85. goog.math.Coordinate.difference(
  86. new goog.math.Coordinate(5, 10), new goog.math.Coordinate(2, 50)));
  87. }
  88. function testCoordinateSum() {
  89. assertObjectEquals(
  90. new goog.math.Coordinate(7, 60),
  91. goog.math.Coordinate.sum(
  92. new goog.math.Coordinate(5, 10), new goog.math.Coordinate(2, 50)));
  93. }
  94. function testCoordinateCeil() {
  95. var c = new goog.math.Coordinate(5.2, 7.6);
  96. assertObjectEquals(new goog.math.Coordinate(6, 8), c.ceil());
  97. c = new goog.math.Coordinate(-1.2, -3.9);
  98. assertObjectEquals(new goog.math.Coordinate(-1, -3), c.ceil());
  99. }
  100. function testCoordinateFloor() {
  101. var c = new goog.math.Coordinate(5.2, 7.6);
  102. assertObjectEquals(new goog.math.Coordinate(5, 7), c.floor());
  103. c = new goog.math.Coordinate(-1.2, -3.9);
  104. assertObjectEquals(new goog.math.Coordinate(-2, -4), c.floor());
  105. }
  106. function testCoordinateRound() {
  107. var c = new goog.math.Coordinate(5.2, 7.6);
  108. assertObjectEquals(new goog.math.Coordinate(5, 8), c.round());
  109. c = new goog.math.Coordinate(-1.2, -3.9);
  110. assertObjectEquals(new goog.math.Coordinate(-1, -4), c.round());
  111. }
  112. function testCoordinateTranslateCoordinate() {
  113. var c = new goog.math.Coordinate(10, 20);
  114. var t = new goog.math.Coordinate(5, 10);
  115. // The translate function modifies the coordinate instead of
  116. // returning a new one.
  117. assertEquals(c, c.translate(t));
  118. assertObjectEquals(new goog.math.Coordinate(15, 30), c);
  119. }
  120. function testCoordinateTranslateXY() {
  121. var c = new goog.math.Coordinate(10, 20);
  122. // The translate function modifies the coordinate instead of
  123. // returning a new one.
  124. assertEquals(c, c.translate(25, 5));
  125. assertObjectEquals(new goog.math.Coordinate(35, 25), c);
  126. }
  127. function testCoordinateTranslateX() {
  128. var c = new goog.math.Coordinate(10, 20);
  129. // The translate function modifies the coordinate instead of
  130. // returning a new one.
  131. assertEquals(c, c.translate(5));
  132. assertObjectEquals(new goog.math.Coordinate(15, 20), c);
  133. }
  134. function testCoordinateScaleXY() {
  135. var c = new goog.math.Coordinate(10, 15);
  136. // The scale function modifies the coordinate instead of returning a new one.
  137. assertEquals(c, c.scale(2, 3));
  138. assertObjectEquals(new goog.math.Coordinate(20, 45), c);
  139. }
  140. function testCoordinateScaleFactor() {
  141. var c = new goog.math.Coordinate(10, 15);
  142. // The scale function modifies the coordinate instead of returning a new one.
  143. assertEquals(c, c.scale(2));
  144. assertObjectEquals(new goog.math.Coordinate(20, 30), c);
  145. }
  146. function testCoordinateRotateRadians() {
  147. var c = new goog.math.Coordinate(15, 75);
  148. c.rotateRadians(Math.PI / 2, new goog.math.Coordinate(10, 70));
  149. assertObjectEquals(new goog.math.Coordinate(5, 75), c);
  150. }
  151. function testCoordinateRotateDegrees() {
  152. var c = new goog.math.Coordinate(15, 75);
  153. c.rotateDegrees(90, new goog.math.Coordinate(10, 70));
  154. assertObjectEquals(new goog.math.Coordinate(5, 75), c);
  155. }