aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/structs/avltree_test.html
blob: 144047dd79abb1769251bcd8bff7b492bfd72f77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html>
<!--
Copyright 2007 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.structs.AvlTree</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.structs');
  goog.require('goog.structs.AvlTree');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

  /**
   * This test verifies that we can insert strings into the AvlTree and have
   * them be stored and sorted correctly by the default comparator.
   */
  function testInsertsWithDefaultComparator() {
    var tree = new goog.structs.AvlTree();
    var values = ['bill', 'blake', 'elliot', 'jacob', 'john', 'myles', 'ted'];

    // Insert strings into tree out of order
    tree.add(values[4]);
    tree.add(values[3]);
    tree.add(values[0]);
    tree.add(values[6]);
    tree.add(values[5]);
    tree.add(values[1]);
    tree.add(values[2]);

    // Verify strings are stored in sorted order
    var i = 0;
    tree.inOrderTraverse(function(value) {
      assertEquals(values[i], value);
      i += 1;
    });
    assertEquals(i, values.length);
  };

  /**
   * This test verifies that we can insert strings into and remove strings from
   * the AvlTree and have the only the non-removed values be stored and sorted
   * correctly by the default comparator.
   */
  function testRemovesWithDefaultComparator() {
    var tree = new goog.structs.AvlTree();
    var values = ['bill', 'blake', 'elliot', 'jacob', 'john', 'myles', 'ted'];

    // Insert strings into tree out of order
    tree.add('frodo');
    tree.add(values[4]);
    tree.add(values[3]);
    tree.add(values[0]);
    tree.add(values[6]);
    tree.add('samwise');
    tree.add(values[5]);
    tree.add(values[1]);
    tree.add(values[2]);
    tree.add('pippin');

    // Remove strings from tree
    assertEquals(tree.remove('samwise'), 'samwise');
    assertEquals(tree.remove('pippin'), 'pippin');
    assertEquals(tree.remove('frodo'), 'frodo');
    assertEquals(tree.remove('merry'), null);


    // Verify strings are stored in sorted order
    var i = 0;
    tree.inOrderTraverse(function(value) {
      assertEquals(values[i], value);
      i += 1;
    });
    assertEquals(i, values.length);
  };

  /**
   * This test verifies that we can insert values into and remove values from
   * the AvlTree and have them be stored and sorted correctly by a custom
   * comparator.
   */
  function testInsertsAndRemovesWithCustomComparator() {
    var tree = new goog.structs.AvlTree(function(a, b) {
      return a - b;
    });

    var NUM_TO_INSERT = 37;
    var valuesToRemove = [1, 0, 6, 7, 36];

    // Insert ints into tree out of order
    var values = [];
    for (var i = 0; i < NUM_TO_INSERT; i += 1) {
      tree.add(i);
      values.push(i);
    }

    for (var i = 0; i < valuesToRemove.length; i += 1) {
      assertEquals(tree.remove(valuesToRemove[i]), valuesToRemove[i]);
      goog.array.remove(values, valuesToRemove[i]);
    }
    assertEquals(tree.remove(-1), null);
    assertEquals(tree.remove(37), null);

    // Verify strings are stored in sorted order
    var i = 0;
    tree.inOrderTraverse(function(value) {
      assertEquals(values[i], value);
      i += 1;
    });
    assertEquals(i, values.length);
  };

  /**
   * This test verifies that we can insert values into and remove values from
   * the AvlTree and have it maintain the AVL-Tree upperbound on its height.
   */
  function testAvlTreeHeight() {
    var tree = new goog.structs.AvlTree(function(a, b) {
      return a - b;
    });

    var NUM_TO_INSERT = 2000;
    var NUM_TO_REMOVE = 500;

    // Insert ints into tree out of order
    for (var i = 0; i < NUM_TO_INSERT; i += 1) {
      tree.add(i);
    }

    // Remove valuse
    for (var i = 0; i < NUM_TO_REMOVE; i += 1) {
      tree.remove(i);
    }

    assertTrue(tree.getHeight() <= 1.4405 *
        (Math.log(NUM_TO_INSERT - NUM_TO_REMOVE + 2) / Math.log(2)) - 1.3277);
  };

  /**
   * This test verifies that we can insert values into and remove values from
   * the AvlTree and have its contains method correctly determine the values it
   * is contains.
   */
  function testAvlTreeContains() {
    var tree = new goog.structs.AvlTree();
    var values = ['bill', 'blake', 'elliot', 'jacob', 'john', 'myles', 'ted'];

    // Insert strings into tree out of order
    tree.add('frodo');
    tree.add(values[4]);
    tree.add(values[3]);
    tree.add(values[0]);
    tree.add(values[6]);
    tree.add('samwise');
    tree.add(values[5]);
    tree.add(values[1]);
    tree.add(values[2]);
    tree.add('pippin');

    // Remove strings from tree
    assertEquals(tree.remove('samwise'), 'samwise');
    assertEquals(tree.remove('pippin'), 'pippin');
    assertEquals(tree.remove('frodo'), 'frodo');

    for (var i = 0; i < values.length; i += 1) {
      assertTrue(tree.contains(values[i]));
    }
    assertFalse(tree.contains('samwise'));
    assertFalse(tree.contains('pippin'));
    assertFalse(tree.contains('frodo'));
  };

  /**
   * This test verifies that we can insert values into and remove values from
   * the AvlTree and have its minValue and maxValue routines return the correct
   * min and max values contained by the tree.
   */
  function testMinAndMaxValues() {
    var tree = new goog.structs.AvlTree(function(a, b) {
      return a - b;
    });

    var NUM_TO_INSERT = 2000;
    var NUM_TO_REMOVE = 500;

    // Insert ints into tree out of order
    for (var i = 0; i < NUM_TO_INSERT; i += 1) {
      tree.add(i);
    }

    // Remove valuse
    for (var i = 0; i < NUM_TO_REMOVE; i += 1) {
      tree.remove(i);
    }

    assertEquals(tree.getMinimum(), NUM_TO_REMOVE);
    assertEquals(tree.getMaximum(), NUM_TO_INSERT - 1);
  };

  /**
   * This test verifies that we can insert values into and remove values from
   * the AvlTree and traverse the tree in reverse order using the
   * reverseOrderTraverse routine.
   */
  function testReverseOrderTraverse() {
    var tree = new goog.structs.AvlTree(function(a, b) {
      return a - b;
    });

    var NUM_TO_INSERT = 2000;
    var NUM_TO_REMOVE = 500;

    // Insert ints into tree out of order
    for (var i = 0; i < NUM_TO_INSERT; i += 1) {
      tree.add(i);
    }

    // Remove valuse
    for (var i = 0; i < NUM_TO_REMOVE; i += 1) {
      tree.remove(i);
    }

    var i = NUM_TO_INSERT - 1;
    tree.reverseOrderTraverse(function(value) {
      assertEquals(value, i);
      i -= 1;
    });
    assertEquals(i, NUM_TO_REMOVE - 1);
  };

  /**
   * Verifies correct behavior of getCount(). See http://b/4347755
   */
  function testGetCountBehavior() {
    var tree = new goog.structs.AvlTree();
    tree.add(1);
    tree.remove(1);
    assertEquals(0, tree.getCount());
  }

</script>
</body>
</html>