collection_test.js 1.6 KB

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