range_test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.RangeTest');
  15. goog.setTestOnly('goog.math.RangeTest');
  16. goog.require('goog.math.Range');
  17. goog.require('goog.testing.jsunit');
  18. /**
  19. * Produce legible assertion results. If two ranges are not equal, the error
  20. * message will be of the form
  21. * "Expected <[1, 2]> (Object) but was <[3, 4]> (Object)"
  22. */
  23. function assertRangesEqual(expected, actual) {
  24. if (!goog.math.Range.equals(expected, actual)) {
  25. assertEquals(expected, actual);
  26. }
  27. }
  28. function createRange(a) {
  29. return a ? new goog.math.Range(a[0], a[1]) : null;
  30. }
  31. function testFromPair() {
  32. var range = goog.math.Range.fromPair([1, 2]);
  33. assertEquals(1, range.start);
  34. assertEquals(2, range.end);
  35. range = goog.math.Range.fromPair([2, 1]);
  36. assertEquals(1, range.start);
  37. assertEquals(2, range.end);
  38. }
  39. function testRangeIntersection() {
  40. var tests = [
  41. [[1, 2], [3, 4], null], [[1, 3], [2, 4], [2, 3]], [[1, 4], [2, 3], [2, 3]],
  42. [[-1, 2], [-1, 2], [-1, 2]], [[1, 2], [2, 3], [2, 2]],
  43. [[1, 1], [1, 1], [1, 1]]
  44. ];
  45. for (var i = 0; i < tests.length; ++i) {
  46. var t = tests[i];
  47. var r0 = createRange(t[0]);
  48. var r1 = createRange(t[1]);
  49. var expected = createRange(t[2]);
  50. assertRangesEqual(expected, goog.math.Range.intersection(r0, r1));
  51. assertRangesEqual(expected, goog.math.Range.intersection(r1, r0));
  52. assertEquals(expected != null, goog.math.Range.hasIntersection(r0, r1));
  53. assertEquals(expected != null, goog.math.Range.hasIntersection(r1, r0));
  54. }
  55. }
  56. function testBoundingRange() {
  57. var tests = [
  58. [[1, 2], [3, 4], [1, 4]], [[1, 3], [2, 4], [1, 4]],
  59. [[1, 4], [2, 3], [1, 4]], [[-1, 2], [-1, 2], [-1, 2]],
  60. [[1, 2], [2, 3], [1, 3]], [[1, 1], [1, 1], [1, 1]]
  61. ];
  62. for (var i = 0; i < tests.length; ++i) {
  63. var t = tests[i];
  64. var r0 = createRange(t[0]);
  65. var r1 = createRange(t[1]);
  66. var expected = createRange(t[2]);
  67. assertRangesEqual(expected, goog.math.Range.boundingRange(r0, r1));
  68. assertRangesEqual(expected, goog.math.Range.boundingRange(r1, r0));
  69. }
  70. }
  71. function testRangeContains() {
  72. var tests = [
  73. [[0, 4], [2, 1], true], [[-4, -1], [-2, -3], true], [[1, 3], [2, 4], false],
  74. [[-1, 0], [0, 1], false], [[0, 2], [3, 5], false]
  75. ];
  76. for (var i = 0; i < tests.length; ++i) {
  77. var t = tests[i];
  78. var r0 = createRange(t[0]);
  79. var r1 = createRange(t[1]);
  80. var expected = t[2];
  81. assertEquals(expected, goog.math.Range.contains(r0, r1));
  82. }
  83. }
  84. function testRangeClone() {
  85. var r = new goog.math.Range(5.6, -3.4);
  86. assertRangesEqual(r, r.clone());
  87. }
  88. function testGetLength() {
  89. assertEquals(2, new goog.math.Range(1, 3).getLength());
  90. assertEquals(2, new goog.math.Range(3, 1).getLength());
  91. }
  92. function testRangeContainsPoint() {
  93. var r = new goog.math.Range(0, 1);
  94. assert(goog.math.Range.containsPoint(r, 0));
  95. assert(goog.math.Range.containsPoint(r, 1));
  96. assertFalse(goog.math.Range.containsPoint(r, -1));
  97. assertFalse(goog.math.Range.containsPoint(r, 2));
  98. }
  99. function testIncludePoint() {
  100. var r = new goog.math.Range(0, 2);
  101. r.includePoint(0);
  102. assertObjectEquals(new goog.math.Range(0, 2), r);
  103. r.includePoint(1);
  104. assertObjectEquals(new goog.math.Range(0, 2), r);
  105. r.includePoint(2);
  106. assertObjectEquals(new goog.math.Range(0, 2), r);
  107. r.includePoint(-1);
  108. assertObjectEquals(new goog.math.Range(-1, 2), r);
  109. r.includePoint(3);
  110. assertObjectEquals(new goog.math.Range(-1, 3), r);
  111. }
  112. function testIncludeRange() {
  113. var r = new goog.math.Range(0, 4);
  114. r.includeRange(r);
  115. assertObjectEquals(new goog.math.Range(0, 4), r);
  116. r.includeRange(new goog.math.Range(1, 3));
  117. assertObjectEquals(new goog.math.Range(0, 4), r);
  118. r.includeRange(new goog.math.Range(-1, 2));
  119. assertObjectEquals(new goog.math.Range(-1, 4), r);
  120. r.includeRange(new goog.math.Range(2, 5));
  121. assertObjectEquals(new goog.math.Range(-1, 5), r);
  122. r.includeRange(new goog.math.Range(-2, 6));
  123. assertObjectEquals(new goog.math.Range(-2, 6), r);
  124. }