1// Copyright 2009 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28// Load the Splay tree implementation from <project root>/tools. 29// Files: tools/splaytree.js 30 31 32(function testIsEmpty() { 33 var tree = new goog.structs.SplayTree(); 34 assertTrue(tree.isEmpty()); 35 tree.insert(0, 'value'); 36 assertFalse(tree.isEmpty()); 37})(); 38 39 40(function testExportValues() { 41 var tree = new goog.structs.SplayTree(); 42 assertArrayEquals([], tree.exportValues()); 43 tree.insert(0, 'value'); 44 assertArrayEquals(['value'], tree.exportValues()); 45 tree.insert(0, 'value'); 46 assertArrayEquals(['value'], tree.exportValues()); 47})(); 48 49 50function createSampleTree() { 51 // Creates the following tree: 52 // 50 53 // / \ 54 // 30 60 55 // / \ \ 56 // 10 40 90 57 // \ / \ 58 // 20 70 100 59 // / \ 60 // 15 80 61 // 62 // We can't use the 'insert' method because it also uses 'splay_'. 63 return { key: 50, value: 50, 64 left: { key: 30, value: 30, 65 left: { key: 10, value: 10, left: null, 66 right: { key: 20, value: 20, 67 left: { key: 15, value: 15, 68 left: null, right: null }, 69 right: null } }, 70 right: { key: 40, value: 40, left: null, right: null } }, 71 right: { key: 60, value: 60, left: null, 72 right: { key: 90, value: 90, 73 left: { key: 70, value: 70, left: null, 74 right: { key: 80, value: 80, 75 left: null, right: null } }, 76 right: { key: 100, value: 100, 77 left: null, right: null } } } }; 78}; 79 80 81(function testSplay() { 82 var tree = new goog.structs.SplayTree(); 83 tree.root_ = createSampleTree(); 84 assertArrayEquals(['50', '30', '60', '10', '40', '90', '20', '70', '100', '15', '80'], 85 tree.exportValues()); 86 tree.splay_(50); 87 assertArrayEquals(['50', '30', '60', '10', '40', '90', '20', '70', '100', '15', '80'], 88 tree.exportValues()); 89 tree.splay_(80); 90 assertArrayEquals(['80', '60', '90', '50', '70', '100', '30', '10', '40', '20', '15'], 91 tree.exportValues()); 92})(); 93 94 95(function testInsert() { 96 var tree = new goog.structs.SplayTree(); 97 tree.insert(5, 'root'); 98 tree.insert(3, 'left'); 99 assertArrayEquals(['left', 'root'], tree.exportValues()); 100 tree.insert(7, 'right'); 101 assertArrayEquals(['right', 'root', 'left'], tree.exportValues()); 102})(); 103 104 105(function testFind() { 106 var tree = new goog.structs.SplayTree(); 107 tree.insert(5, 'root'); 108 tree.insert(3, 'left'); 109 tree.insert(7, 'right'); 110 assertEquals('root', tree.find(5).value); 111 assertEquals('left', tree.find(3).value); 112 assertEquals('right', tree.find(7).value); 113 assertEquals(null, tree.find(0)); 114 assertEquals(null, tree.find(100)); 115 assertEquals(null, tree.find(-100)); 116})(); 117 118 119(function testFindMin() { 120 var tree = new goog.structs.SplayTree(); 121 assertEquals(null, tree.findMin()); 122 tree.insert(5, 'root'); 123 tree.insert(3, 'left'); 124 tree.insert(7, 'right'); 125 assertEquals('left', tree.findMin().value); 126})(); 127 128 129(function testFindMax() { 130 var tree = new goog.structs.SplayTree(); 131 assertEquals(null, tree.findMax()); 132 tree.insert(5, 'root'); 133 tree.insert(3, 'left'); 134 tree.insert(7, 'right'); 135 assertEquals('right', tree.findMax().value); 136})(); 137 138 139(function testFindGreatestLessThan() { 140 var tree = new goog.structs.SplayTree(); 141 assertEquals(null, tree.findGreatestLessThan(10)); 142 tree.insert(5, 'root'); 143 tree.insert(3, 'left'); 144 tree.insert(7, 'right'); 145 assertEquals('right', tree.findGreatestLessThan(10).value); 146 assertEquals('right', tree.findGreatestLessThan(7).value); 147 assertEquals('root', tree.findGreatestLessThan(6).value); 148 assertEquals('left', tree.findGreatestLessThan(4).value); 149 assertEquals(null, tree.findGreatestLessThan(2)); 150})(); 151 152 153(function testRemove() { 154 var tree = new goog.structs.SplayTree(); 155 assertThrows('tree.remove(5)'); 156 tree.insert(5, 'root'); 157 tree.insert(3, 'left'); 158 tree.insert(7, 'right'); 159 assertThrows('tree.remove(1)'); 160 assertThrows('tree.remove(6)'); 161 assertThrows('tree.remove(10)'); 162 assertEquals('root', tree.remove(5).value); 163 assertEquals('left', tree.remove(3).value); 164 assertEquals('right', tree.remove(7).value); 165 assertArrayEquals([], tree.exportValues()); 166})(); 167