rangeset_test.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. // Copyright 2009 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.RangeSetTest');
  15. goog.setTestOnly('goog.math.RangeSetTest');
  16. goog.require('goog.iter');
  17. goog.require('goog.math.Range');
  18. goog.require('goog.math.RangeSet');
  19. goog.require('goog.testing.jsunit');
  20. /**
  21. * Produce legible assertion results for comparing ranges. The expected range
  22. * may be defined as a goog.math.Range or as a two-element array of numbers. If
  23. * two ranges are not equal, the error message will be in the format:
  24. * "Expected <[1, 2]> (Object) but was <[3, 4]> (Object)"
  25. *
  26. * @param {!goog.math.Range|!Array<number>|string} a A descriptive string or
  27. * the expected range.
  28. * @param {!goog.math.Range|!Array<number>} b The expected range when a
  29. * descriptive string is present, or the range to compare.
  30. * @param {goog.math.Range=} opt_c The range to compare when a descriptive
  31. * string is present.
  32. */
  33. function assertRangesEqual(a, b, opt_c) {
  34. var message = opt_c ? a : '';
  35. var expected = opt_c ? b : a;
  36. var actual = opt_c ? opt_c : b;
  37. if (goog.isArray(expected)) {
  38. assertEquals(
  39. message + '\n' +
  40. 'Expected ranges must be specified as goog.math.Range ' +
  41. 'objects or as 2-element number arrays. Found [' +
  42. expected.join(', ') + ']',
  43. 2, expected.length);
  44. expected = new goog.math.Range(expected[0], expected[1]);
  45. }
  46. if (!goog.math.Range.equals(
  47. /** @type {!goog.math.Range} */ (expected),
  48. /** @type {!goog.math.Range} */ (actual))) {
  49. if (message) {
  50. assertEquals(message, expected, actual);
  51. } else {
  52. assertEquals(expected, actual);
  53. }
  54. }
  55. }
  56. /**
  57. * Produce legible assertion results for comparing two lists of ranges. Expected
  58. * lists may be specified as a list of goog.math.Ranges, or as a list of
  59. * two-element arrays of numbers.
  60. *
  61. * @param {Array<goog.math.Range|Array<number>>|string} a A help
  62. * string or the list of expected ranges.
  63. * @param {Array<goog.math.Range|Array<number>>} b The list of
  64. * expected ranges when a descriptive string is present, or the list of
  65. * ranges to compare.
  66. * @param {Array<goog.math.Range>=} opt_c The list of ranges to compare when a
  67. * descriptive string is present.
  68. */
  69. function assertRangeListsEqual(a, b, opt_c) {
  70. var message = opt_c ? a + '\n' : '';
  71. var expected = opt_c ? b : a;
  72. var actual = opt_c ? opt_c : b;
  73. assertEquals(
  74. message + 'Array lengths unequal.', expected.length, actual.length);
  75. for (var i = 0; i < expected.length; i++) {
  76. assertRangesEqual(
  77. message + 'Range ' + i + ' mismatch.', expected[i], actual[i]);
  78. }
  79. }
  80. function testClone() {
  81. var r = new goog.math.RangeSet();
  82. var test = new goog.math.RangeSet(r);
  83. assertRangeListsEqual([], test.ranges_);
  84. r.add(new goog.math.Range(-10, -2));
  85. r.add(new goog.math.Range(2.72, 3.14));
  86. r.add(new goog.math.Range(8, 11));
  87. test = r.clone();
  88. assertRangeListsEqual([[-10, -2], [2.72, 3.14], [8, 11]], test.ranges_);
  89. var test2 = r.clone();
  90. assertRangeListsEqual(test.ranges_, test2.ranges_);
  91. assertNotEquals(
  92. 'The clones should not share the same list reference.', test.ranges_,
  93. test2.ranges_);
  94. for (var i = 0; i < test.ranges_.length; i++) {
  95. assertNotEquals(
  96. 'The clones should not share references to ranges.', test.ranges_[i],
  97. test2.ranges_[i]);
  98. }
  99. }
  100. function testAddNoCorruption() {
  101. var r = new goog.math.RangeSet();
  102. var range = new goog.math.Range(1, 2);
  103. r.add(range);
  104. assertNotEquals(
  105. 'Only a copy of the input range should be stored.', range, r.ranges_[0]);
  106. range.end = 5;
  107. assertRangeListsEqual(
  108. 'Modifying an input range after use should not ' +
  109. 'affect the set.',
  110. [[1, 2]], r.ranges_);
  111. }
  112. function testAdd() {
  113. var r = new goog.math.RangeSet();
  114. r.add(new goog.math.Range(7, 12));
  115. assertRangeListsEqual([[7, 12]], r.ranges_);
  116. r.add(new goog.math.Range(1, 3));
  117. assertRangeListsEqual([[1, 3], [7, 12]], r.ranges_);
  118. r.add(new goog.math.Range(13, 18));
  119. assertRangeListsEqual([[1, 3], [7, 12], [13, 18]], r.ranges_);
  120. r.add(new goog.math.Range(5, 5));
  121. assertRangeListsEqual(
  122. 'Zero length ranges should be ignored.', [[1, 3], [7, 12], [13, 18]],
  123. r.ranges_);
  124. var badRange = new goog.math.Range(5, 5);
  125. badRange.end = 4;
  126. r.add(badRange);
  127. assertRangeListsEqual(
  128. 'Negative length ranges should be ignored.', [[1, 3], [7, 12], [13, 18]],
  129. r.ranges_);
  130. r.add(new goog.math.Range(-22, -15));
  131. assertRangeListsEqual(
  132. 'Negative ranges should work fine.',
  133. [[-22, -15], [1, 3], [7, 12], [13, 18]], r.ranges_);
  134. r.add(new goog.math.Range(3.1, 6.9));
  135. assertRangeListsEqual(
  136. 'Non-integer ranges should work fine.',
  137. [[-22, -15], [1, 3], [3.1, 6.9], [7, 12], [13, 18]], r.ranges_);
  138. }
  139. function testAddWithOverlaps() {
  140. var r = new goog.math.RangeSet();
  141. r.add(new goog.math.Range(7, 12));
  142. r.add(new goog.math.Range(5, 8));
  143. assertRangeListsEqual([[5, 12]], r.ranges_);
  144. r.add(new goog.math.Range(15, 20));
  145. r.add(new goog.math.Range(18, 25));
  146. assertRangeListsEqual([[5, 12], [15, 25]], r.ranges_);
  147. r.add(new goog.math.Range(10, 17));
  148. assertRangeListsEqual([[5, 25]], r.ranges_);
  149. r.add(new goog.math.Range(-4, 4.5));
  150. assertRangeListsEqual([[-4, 4.5], [5, 25]], r.ranges_);
  151. r.add(new goog.math.Range(4.2, 5.3));
  152. assertRangeListsEqual([[-4, 25]], r.ranges_);
  153. }
  154. function testAddWithAdjacentSpans() {
  155. var r = new goog.math.RangeSet();
  156. r.add(new goog.math.Range(7, 12));
  157. r.add(new goog.math.Range(13, 19));
  158. assertRangeListsEqual([[7, 12], [13, 19]], r.ranges_);
  159. r.add(new goog.math.Range(4, 6));
  160. assertRangeListsEqual([[4, 6], [7, 12], [13, 19]], r.ranges_);
  161. r.add(new goog.math.Range(6, 7));
  162. assertRangeListsEqual([[4, 12], [13, 19]], r.ranges_);
  163. r.add(new goog.math.Range(12, 13));
  164. assertRangeListsEqual([[4, 19]], r.ranges_);
  165. r.add(new goog.math.Range(19.1, 22));
  166. assertRangeListsEqual([[4, 19], [19.1, 22]], r.ranges_);
  167. r.add(new goog.math.Range(19, 19.1));
  168. assertRangeListsEqual([[4, 22]], r.ranges_);
  169. r.add(new goog.math.Range(-3, -2));
  170. assertRangeListsEqual([[-3, -2], [4, 22]], r.ranges_);
  171. r.add(new goog.math.Range(-2, 4));
  172. assertRangeListsEqual([[-3, 22]], r.ranges_);
  173. }
  174. function testAddWithSubsets() {
  175. var r = new goog.math.RangeSet();
  176. r.add(new goog.math.Range(7, 12));
  177. assertRangeListsEqual([[7, 12]], r.ranges_);
  178. r.add(new goog.math.Range(7, 12));
  179. assertRangeListsEqual([[7, 12]], r.ranges_);
  180. r.add(new goog.math.Range(8, 11));
  181. assertRangeListsEqual([[7, 12]], r.ranges_);
  182. for (var i = 20; i < 30; i += 2) {
  183. r.add(new goog.math.Range(i, i + 1));
  184. }
  185. assertRangeListsEqual(
  186. [[7, 12], [20, 21], [22, 23], [24, 25], [26, 27], [28, 29]], r.ranges_);
  187. r.add(new goog.math.Range(1, 30));
  188. assertRangeListsEqual([[1, 30]], r.ranges_);
  189. }
  190. function testRemove() {
  191. var r = new goog.math.RangeSet();
  192. r.add(new goog.math.Range(1, 3));
  193. r.add(new goog.math.Range(7, 8));
  194. r.add(new goog.math.Range(10, 20));
  195. r.remove(new goog.math.Range(3, 6));
  196. assertRangeListsEqual([[1, 3], [7, 8], [10, 20]], r.ranges_);
  197. r.remove(new goog.math.Range(7, 8));
  198. assertRangeListsEqual([[1, 3], [10, 20]], r.ranges_);
  199. r.remove(new goog.math.Range(1, 3));
  200. assertRangeListsEqual([[10, 20]], r.ranges_);
  201. r.remove(new goog.math.Range(8, 11));
  202. assertRangeListsEqual([[11, 20]], r.ranges_);
  203. r.remove(new goog.math.Range(18, 25));
  204. assertRangeListsEqual([[11, 18]], r.ranges_);
  205. r.remove(new goog.math.Range(15, 16));
  206. assertRangeListsEqual([[11, 15], [16, 18]], r.ranges_);
  207. r.remove(new goog.math.Range(11, 15));
  208. assertRangeListsEqual([[16, 18]], r.ranges_);
  209. r.remove(new goog.math.Range(16, 16));
  210. assertRangeListsEqual(
  211. 'Empty ranges should be ignored.', [[16, 18]], r.ranges_);
  212. r.remove(new goog.math.Range(16, 17));
  213. assertRangeListsEqual([[17, 18]], r.ranges_);
  214. r.remove(new goog.math.Range(17, 18));
  215. assertRangeListsEqual([], r.ranges_);
  216. }
  217. function testRemoveWithNonOverlappingRanges() {
  218. var r = new goog.math.RangeSet();
  219. r.add(new goog.math.Range(10, 20));
  220. r.remove(new goog.math.Range(5, 8));
  221. assertRangeListsEqual(
  222. 'Non-overlapping ranges should be ignored.', [[10, 20]], r.ranges_);
  223. r.remove(new goog.math.Range(20, 30));
  224. assertRangeListsEqual(
  225. 'Non-overlapping ranges should be ignored.', [[10, 20]], r.ranges_);
  226. r.remove(new goog.math.Range(15, 15));
  227. assertRangeListsEqual(
  228. 'Zero-length ranges should be ignored.', [[10, 20]], r.ranges_);
  229. }
  230. function testRemoveWithIdenticalRanges() {
  231. var r = new goog.math.RangeSet();
  232. r.add(new goog.math.Range(10, 20));
  233. r.add(new goog.math.Range(30, 40));
  234. r.add(new goog.math.Range(50, 60));
  235. assertRangeListsEqual([[10, 20], [30, 40], [50, 60]], r.ranges_);
  236. r.remove(new goog.math.Range(30, 40));
  237. assertRangeListsEqual([[10, 20], [50, 60]], r.ranges_);
  238. r.remove(new goog.math.Range(50, 60));
  239. assertRangeListsEqual([[10, 20]], r.ranges_);
  240. r.remove(new goog.math.Range(10, 20));
  241. assertRangeListsEqual([], r.ranges_);
  242. }
  243. function testRemoveWithOverlappingSubsets() {
  244. var r = new goog.math.RangeSet();
  245. r.add(new goog.math.Range(1, 10));
  246. r.remove(new goog.math.Range(1, 4));
  247. assertRangeListsEqual([[4, 10]], r.ranges_);
  248. r.remove(new goog.math.Range(8, 10));
  249. assertRangeListsEqual([[4, 8]], r.ranges_);
  250. }
  251. function testRemoveMultiple() {
  252. var r = new goog.math.RangeSet();
  253. r.add(new goog.math.Range(5, 8));
  254. r.add(new goog.math.Range(10, 20));
  255. r.add(new goog.math.Range(30, 35));
  256. for (var i = 20; i < 30; i += 2) {
  257. r.add(new goog.math.Range(i, i + 1));
  258. }
  259. assertRangeListsEqual(
  260. 'Setting up the test data seems to have failed, how embarrassing.',
  261. [[5, 8], [10, 21], [22, 23], [24, 25], [26, 27], [28, 29], [30, 35]],
  262. r.ranges_);
  263. r.remove(new goog.math.Range(15, 32));
  264. assertRangeListsEqual([[5, 8], [10, 15], [32, 35]], r.ranges_);
  265. }
  266. function testRemoveWithRealNumbers() {
  267. var r = new goog.math.RangeSet();
  268. r.add(new goog.math.Range(2, 4));
  269. r.remove(new goog.math.Range(1.1, 2.72));
  270. assertRangeListsEqual([[2.72, 4]], r.ranges_);
  271. r.remove(new goog.math.Range(3.14, 5));
  272. assertRangeListsEqual([[2.72, 3.14]], r.ranges_);
  273. r.remove(new goog.math.Range(2.8, 3));
  274. assertRangeListsEqual([[2.72, 2.8], [3, 3.14]], r.ranges_);
  275. }
  276. function testEquals() {
  277. var a = new goog.math.RangeSet();
  278. var b = new goog.math.RangeSet();
  279. assertTrue(goog.math.RangeSet.equals(a, b));
  280. a.add(new goog.math.Range(3, 9));
  281. assertFalse(goog.math.RangeSet.equals(a, b));
  282. b.add(new goog.math.Range(4, 9));
  283. assertFalse(goog.math.RangeSet.equals(a, b));
  284. b.add(new goog.math.Range(3, 4));
  285. assertTrue(goog.math.RangeSet.equals(a, b));
  286. a.add(new goog.math.Range(12, 14));
  287. b.add(new goog.math.Range(11, 14));
  288. assertFalse(goog.math.RangeSet.equals(a, b));
  289. a.add(new goog.math.Range(11, 12));
  290. assertTrue(goog.math.RangeSet.equals(a, b));
  291. }
  292. function testContains() {
  293. var r = new goog.math.RangeSet();
  294. assertFalse(r.contains(7, 9));
  295. r.add(new goog.math.Range(5, 6));
  296. r.add(new goog.math.Range(10, 20));
  297. assertFalse(r.contains(new goog.math.Range(7, 9)));
  298. assertFalse(r.contains(new goog.math.Range(9, 11)));
  299. assertFalse(r.contains(new goog.math.Range(18, 22)));
  300. assertTrue(r.contains(new goog.math.Range(17, 19)));
  301. assertTrue(r.contains(new goog.math.Range(5, 6)));
  302. assertTrue(r.contains(new goog.math.Range(5.9, 5.999)));
  303. assertFalse(
  304. 'An empty input range should always return false.',
  305. r.contains(new goog.math.Range(15, 15)));
  306. var badRange = new goog.math.Range(15, 15);
  307. badRange.end = 14;
  308. assertFalse(
  309. 'An invalid range should always return false.', r.contains(badRange));
  310. }
  311. function testContainsValue() {
  312. var r = new goog.math.RangeSet();
  313. assertFalse(r.containsValue(5));
  314. r.add(new goog.math.Range(1, 4));
  315. r.add(new goog.math.Range(10, 20));
  316. assertFalse(r.containsValue(0));
  317. assertFalse(r.containsValue(0.999));
  318. assertFalse(r.containsValue(5));
  319. assertFalse(r.containsValue(25));
  320. assertFalse(r.containsValue(20));
  321. assertTrue(r.containsValue(3));
  322. assertTrue(r.containsValue(10));
  323. assertTrue(r.containsValue(19));
  324. assertTrue(r.containsValue(19.999));
  325. }
  326. function testUnion() {
  327. var a = new goog.math.RangeSet();
  328. a.add(new goog.math.Range(1, 5));
  329. a.add(new goog.math.Range(10, 11));
  330. a.add(new goog.math.Range(15, 20));
  331. var b = new goog.math.RangeSet();
  332. b.add(new goog.math.Range(0, 5));
  333. b.add(new goog.math.Range(8, 18));
  334. var test = a.union(b);
  335. assertRangeListsEqual([[0, 5], [8, 20]], test.ranges_);
  336. var test = b.union(a);
  337. assertRangeListsEqual([[0, 5], [8, 20]], test.ranges_);
  338. var test = a.union(a);
  339. assertRangeListsEqual(a.ranges_, test.ranges_);
  340. }
  341. function testDifference() {
  342. var a = new goog.math.RangeSet();
  343. a.add(new goog.math.Range(1, 5));
  344. a.add(new goog.math.Range(10, 11));
  345. a.add(new goog.math.Range(15, 20));
  346. var b = new goog.math.RangeSet();
  347. b.add(new goog.math.Range(0, 5));
  348. b.add(new goog.math.Range(8, 18));
  349. var test = a.difference(b);
  350. assertRangeListsEqual([[18, 20]], test.ranges_);
  351. var test = b.difference(a);
  352. assertRangeListsEqual([[0, 1], [8, 10], [11, 15]], test.ranges_);
  353. var test = a.difference(a);
  354. assertRangeListsEqual([], test.ranges_);
  355. var test = b.difference(b);
  356. assertRangeListsEqual([], test.ranges_);
  357. }
  358. function testIntersection() {
  359. var a = new goog.math.RangeSet();
  360. a.add(new goog.math.Range(1, 5));
  361. a.add(new goog.math.Range(10, 11));
  362. a.add(new goog.math.Range(15, 20));
  363. var b = new goog.math.RangeSet();
  364. b.add(new goog.math.Range(0, 5));
  365. b.add(new goog.math.Range(8, 18));
  366. var test = a.intersection(b);
  367. assertRangeListsEqual([[1, 5], [10, 11], [15, 18]], test.ranges_);
  368. var test = b.intersection(a);
  369. assertRangeListsEqual([[1, 5], [10, 11], [15, 18]], test.ranges_);
  370. var test = a.intersection(a);
  371. assertRangeListsEqual(a.ranges_, test.ranges_);
  372. }
  373. function testSlice() {
  374. var r = new goog.math.RangeSet();
  375. r.add(new goog.math.Range(2, 4));
  376. r.add(new goog.math.Range(5, 6));
  377. r.add(new goog.math.Range(9, 15));
  378. var test = r.slice(new goog.math.Range(0, 2));
  379. assertRangeListsEqual([], test.ranges_);
  380. test = r.slice(new goog.math.Range(2, 4));
  381. assertRangeListsEqual([[2, 4]], test.ranges_);
  382. test = r.slice(new goog.math.Range(7, 20));
  383. assertRangeListsEqual([[9, 15]], test.ranges_);
  384. test = r.slice(new goog.math.Range(4, 30));
  385. assertRangeListsEqual([[5, 6], [9, 15]], test.ranges_);
  386. test = r.slice(new goog.math.Range(2, 15));
  387. assertRangeListsEqual([[2, 4], [5, 6], [9, 15]], test.ranges_);
  388. test = r.slice(new goog.math.Range(10, 10));
  389. assertRangeListsEqual(
  390. 'An empty range should produce an empty set.', [], test.ranges_);
  391. var badRange = new goog.math.Range(10, 10);
  392. badRange.end = 9;
  393. test = r.slice(badRange);
  394. assertRangeListsEqual(
  395. 'An invalid range should produce an empty set.', [], test.ranges_);
  396. }
  397. function testInverse() {
  398. var r = new goog.math.RangeSet();
  399. r.add(new goog.math.Range(1, 3));
  400. r.add(new goog.math.Range(5, 6));
  401. r.add(new goog.math.Range(8, 10));
  402. var test = r.inverse(new goog.math.Range(10, 20));
  403. assertRangeListsEqual([[10, 20]], test.ranges_);
  404. test = r.inverse(new goog.math.Range(1, 3));
  405. assertRangeListsEqual([], test.ranges_);
  406. test = r.inverse(new goog.math.Range(0, 2));
  407. assertRangeListsEqual([[0, 1]], test.ranges_);
  408. test = r.inverse(new goog.math.Range(9, 12));
  409. assertRangeListsEqual([[10, 12]], test.ranges_);
  410. test = r.inverse(new goog.math.Range(2, 9));
  411. assertRangeListsEqual([[3, 5], [6, 8]], test.ranges_);
  412. test = r.inverse(new goog.math.Range(4, 9));
  413. assertRangeListsEqual([[4, 5], [6, 8]], test.ranges_);
  414. test = r.inverse(new goog.math.Range(9, 9));
  415. assertRangeListsEqual(
  416. 'An empty range should produce an empty set.', [], test.ranges_);
  417. var badRange = new goog.math.Range(9, 9);
  418. badRange.end = 8;
  419. test = r.inverse(badRange);
  420. assertRangeListsEqual(
  421. 'An invalid range should produce an empty set.', [], test.ranges_);
  422. }
  423. function testCoveredLength() {
  424. var r = new goog.math.RangeSet();
  425. assertEquals(0, r.coveredLength());
  426. r.add(new goog.math.Range(5, 9));
  427. assertEquals(4, r.coveredLength());
  428. r.add(new goog.math.Range(0, 3));
  429. r.add(new goog.math.Range(12, 13));
  430. assertEquals(8, r.coveredLength());
  431. r.add(new goog.math.Range(-1, 13));
  432. assertEquals(14, r.coveredLength());
  433. r.add(new goog.math.Range(13, 13.5));
  434. assertEquals(14.5, r.coveredLength());
  435. }
  436. function testGetBounds() {
  437. var r = new goog.math.RangeSet();
  438. assertNull(r.getBounds());
  439. r.add(new goog.math.Range(12, 54));
  440. assertRangesEqual([12, 54], r.getBounds());
  441. r.add(new goog.math.Range(108, 139));
  442. assertRangesEqual([12, 139], r.getBounds());
  443. }
  444. function testIsEmpty() {
  445. var r = new goog.math.RangeSet();
  446. assertTrue(r.isEmpty());
  447. r.add(new goog.math.Range(0, 1));
  448. assertFalse(r.isEmpty());
  449. r.remove(new goog.math.Range(0, 1));
  450. assertTrue(r.isEmpty());
  451. }
  452. function testClear() {
  453. var r = new goog.math.RangeSet();
  454. r.add(new goog.math.Range(1, 2));
  455. r.add(new goog.math.Range(3, 5));
  456. r.add(new goog.math.Range(8, 13));
  457. assertFalse(r.isEmpty());
  458. r.clear();
  459. assertTrue(r.isEmpty());
  460. }
  461. function testIter() {
  462. var r = new goog.math.RangeSet();
  463. r.add(new goog.math.Range(1, 3));
  464. r.add(new goog.math.Range(5, 6));
  465. r.add(new goog.math.Range(8, 10));
  466. assertRangeListsEqual([[1, 3], [5, 6], [8, 10]], goog.iter.toArray(r));
  467. var i = 0;
  468. goog.iter.forEach(r, function(testRange) {
  469. assertRangesEqual(
  470. 'Iterated set values should match the originals.', r.ranges_[i],
  471. testRange);
  472. assertNotEquals(
  473. 'Iterated range should not be a reference to the original.',
  474. r.ranges_[i], testRange);
  475. i++;
  476. });
  477. }