| 1 |
// Copyright 2011 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 |
|
| 15 |
goog.provide('goog.structs.CollectionTest'); |
| 16 |
goog.setTestOnly('goog.structs.CollectionTest'); |
| 17 |
|
| 18 |
goog.require('goog.structs.AvlTree'); |
| 19 |
goog.require('goog.structs.Set'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
function testSet() { |
| 23 |
var set = new goog.structs.Set(); |
| 24 |
exerciseCollection(set); |
| 25 |
} |
| 26 |
|
| 27 |
function testAvlTree() { |
| 28 |
var tree = new goog.structs.AvlTree(); |
| 29 |
exerciseCollection(tree); |
| 30 |
} |
| 31 |
|
| 32 |
// Simple exercise of a collection object. |
| 33 |
function exerciseCollection(collection) { |
| 34 |
assertEquals(0, collection.getCount()); |
| 35 |
|
| 36 |
for (var i = 1; i <= 10; i++) { |
| 37 |
assertFalse(collection.contains(i)); |
| 38 |
collection.add(i); |
| 39 |
assertTrue(collection.contains(i)); |
| 40 |
assertEquals(i, collection.getCount()); |
| 41 |
} |
| 42 |
|
| 43 |
assertEquals(10, collection.getCount()); |
| 44 |
|
| 45 |
for (var i = 10; i > 0; i--) { |
| 46 |
assertTrue(collection.contains(i)); |
| 47 |
collection.remove(i); |
| 48 |
assertFalse(collection.contains(i)); |
| 49 |
assertEquals(i - 1, collection.getCount()); |
| 50 |
} |
| 51 |
|
| 52 |
assertEquals(0, collection.getCount()); |
| 53 |
} |