line_test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.LineTest');
  15. goog.setTestOnly('goog.math.LineTest');
  16. goog.require('goog.math.Coordinate');
  17. goog.require('goog.math.Line');
  18. goog.require('goog.testing.jsunit');
  19. function testEquals() {
  20. var input = new goog.math.Line(1, 2, 3, 4);
  21. assert(input.equals(input));
  22. }
  23. function testClone() {
  24. var input = new goog.math.Line(1, 2, 3, 4);
  25. assertNotEquals('Clone returns a new object', input, input.clone());
  26. assertTrue('Contents of clone match original', input.equals(input.clone()));
  27. }
  28. function testGetLength() {
  29. var input = new goog.math.Line(0, 0, Math.sqrt(2), Math.sqrt(2));
  30. assertRoughlyEquals(input.getSegmentLengthSquared(), 4, 1e-10);
  31. assertRoughlyEquals(input.getSegmentLength(), 2, 1e-10);
  32. }
  33. function testGetClosestPoint() {
  34. var input = new goog.math.Line(0, 1, 1, 2);
  35. var point = input.getClosestPoint(0, 3);
  36. assertRoughlyEquals(point.x, 1, 1e-10);
  37. assertRoughlyEquals(point.y, 2, 1e-10);
  38. }
  39. function testGetClosestSegmentPoint() {
  40. var input = new goog.math.Line(0, 1, 2, 3);
  41. var point = input.getClosestSegmentPoint(4, 4);
  42. assertRoughlyEquals(point.x, 2, 1e-10);
  43. assertRoughlyEquals(point.y, 3, 1e-10);
  44. point = input.getClosestSegmentPoint(new goog.math.Coordinate(-1, -10));
  45. assertRoughlyEquals(point.x, 0, 1e-10);
  46. assertRoughlyEquals(point.y, 1, 1e-10);
  47. }