sku-pg
Version:
Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!
490 lines (380 loc) • 10 kB
HTML
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.
Use of this source code is governed by an Apache 2.0 License.
See the COPYING file for details.
-->
<head>
<title>Closure Unit Tests - goog.structs.PriorityPool</title>
<script src="../base.js"></script>
<script>
goog.require('goog.structs');
goog.require('goog.structs.PriorityPool');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
// Implementation of the Pool class with isObjectDead() always returning TRUE,
// so that the the Pool will not reuse any objects.
function NoObjectReusePriorityPool(opt_min, opt_max) {
goog.structs.PriorityPool.call(this, opt_min, opt_max);
};
goog.inherits(NoObjectReusePriorityPool, goog.structs.PriorityPool);
NoObjectReusePriorityPool.prototype.objectCanBeReused = function(obj) {
return false;
};
function testExceedMax1() {
var p = new goog.structs.PriorityPool(0, 3);
var getCount1 = 0;
var callback1 = function(obj) {
assertNotNull(obj);
getCount1++;
};
var getCount2 = 0;
var callback2 = function(obj) {
getCount2++;
};
p.getObject(callback1);
p.getObject(callback1);
p.getObject(callback1);
p.getObject(callback2);
p.getObject(callback2);
p.getObject(callback2);
assertEquals('getCount for allocated, Should be 3', getCount1, 3);
assertEquals('getCount for unallocated, Should be 0', getCount2, 0);
}
function testExceedMax2() {
var p = new goog.structs.PriorityPool(0, 1);
var getCount1 = 0;
var callback1 = function(obj) {
assertNotNull(obj);
getCount1++;
};
var getCount2 = 0;
var callback2 = function(obj) {
getCount2++;
};
p.getObject(callback1);
p.getObject(callback2);
p.getObject(callback2);
p.getObject(callback2);
p.getObject(callback2);
p.getObject(callback2);
assertEquals('getCount for allocated, Should be 1', getCount1, 1);
assertEquals('getCount for unallocated, Should be 0', getCount2, 0);
}
function testExceedMax3() {
var p = new goog.structs.PriorityPool(0, 2);
var obj1 = null;
var callback1 = function(obj) {
obj1 = obj;
};
var obj2 = null;
var callback2 = function(obj) {
obj2 = obj;
};
var obj3 = null;
var callback3 = function(obj) {
obj3 = obj;
};
p.getObject(callback1);
p.getObject(callback2);
p.getObject(callback3);
assertNotNull(obj1);
assertNotNull(obj2);
assertNull(obj3);
}
function testExceedMax4() {
var p = new goog.structs.PriorityPool(); // default: 10
var objs = [];
var getCount1 = 0;
var callback1 = function(obj) {
assertNotNull(obj);
getCount1++;
};
var getCount2 = 0;
var callback2 = function(obj) {
getCount2++;
};
for (var i = 0; i < 12; i++) {
p.getObject(i < 10 ? callback1 : callback2);
}
assertEquals('getCount for allocated, Should be 10', getCount1, 10);
assertEquals('getCount for unallocated, Should be 0', getCount2, 0);
}
function testReleaseAndGet1() {
var p = new goog.structs.PriorityPool(0, 10);
var o = null;
var callback = function(obj) {
o = obj;
};
p.getObject(callback);
assertEquals(1, p.getCount());
assertEquals(1, p.getInUseCount());
assertEquals(0, p.getFreeCount());
assertTrue('Result should be true', p.releaseObject(o));
assertEquals(1, p.getCount());
assertEquals(0, p.getInUseCount());
assertEquals(1, p.getFreeCount());
}
function testReleaseAndGet2() {
var p = new NoObjectReusePriorityPool(0, 10);
var o = null;
var callback = function(obj) {
o = obj;
};
p.getObject(callback);
assertEquals(1, p.getCount());
assertEquals(1, p.getInUseCount());
assertEquals(0, p.getFreeCount());
assertTrue('Result should be true', p.releaseObject(o));
assertEquals(0, p.getCount());
assertEquals(0, p.getInUseCount());
assertEquals(0, p.getFreeCount());
}
function testReleaseAndGet3() {
var p = new goog.structs.PriorityPool(0, 10);
var o1 = null;
var callback1 = function(obj) {
o1 = obj;
};
var o2 = null;
var callback2 = function(obj) {
o2 = obj;
};
var o3 = null;
var callback3 = function(obj) {
o3 = obj;
};
var o4 = {};
p.getObject(callback1);
p.getObject(callback2);
p.getObject(callback3);
assertEquals(3, p.getCount());
assertEquals(3, p.getInUseCount());
assertEquals(0, p.getFreeCount());
assertTrue('Result should be true', p.releaseObject(o1));
assertTrue('Result should be true', p.releaseObject(o2));
assertFalse('Result should be false', p.releaseObject(o4));
assertEquals(3, p.getCount());
assertEquals(1, p.getInUseCount());
assertEquals(2, p.getFreeCount());
}
function testReleaseAndGet4() {
var p = new NoObjectReusePriorityPool(0, 10);
var o1 = null;
var callback1 = function(obj) {
o1 = obj;
};
var o2 = null;
var callback2 = function(obj) {
o2 = obj;
};
var o3 = null;
var callback3 = function(obj) {
o3 = obj;
};
var o4 = {};
p.getObject(callback1);
p.getObject(callback2);
p.getObject(callback3);
assertEquals(3, p.getCount());
assertEquals(3, p.getInUseCount());
assertEquals(0, p.getFreeCount());
assertTrue('Result should be true', p.releaseObject(o1));
assertTrue('Result should be true', p.releaseObject(o2));
assertFalse('Result should be false', p.releaseObject(o4));
assertEquals(1, p.getCount());
assertEquals(1, p.getInUseCount());
assertEquals(0, p.getFreeCount());
}
function testIsInPool1() {
var p = new goog.structs.PriorityPool();
var o1 = null;
var callback1 = function(obj) {
o1 = obj;
};
var o2 = null;
var callback2 = function(obj) {
o2 = obj;
};
var o3 = null;
var callback3 = function(obj) {
o3 = obj;
};
var o4 = {};
var o5 = {};
p.getObject(callback1);
p.getObject(callback2);
p.getObject(callback3);
var o6 = o1;
assertTrue(p.contains(o1));
assertTrue(p.contains(o2));
assertTrue(p.contains(o3));
assertFalse(p.contains(o4));
assertFalse(p.contains(o5));
assertTrue(p.contains(o6));
}
function testSetMin1() {
var p = new goog.structs.PriorityPool(0, 10);
assertEquals(0, p.getCount());
assertEquals(0, p.getInUseCount());
assertEquals(0, p.getFreeCount());
p.setMinimumCount(10);
assertEquals(10, p.getCount());
assertEquals(0, p.getInUseCount());
assertEquals(10, p.getFreeCount());
}
function testSetMin2() {
var p = new goog.structs.PriorityPool(0, 10);
assertEquals(0, p.getCount());
assertEquals(0, p.getInUseCount());
assertEquals(0, p.getFreeCount());
var o1 = null;
var callback1 = function(obj) {
o1 = obj;
};
p.getObject(callback1);
assertEquals(1, p.getCount());
assertEquals(1, p.getInUseCount());
assertEquals(0, p.getFreeCount());
p.setMinimumCount(10);
assertEquals(10, p.getCount());
assertEquals(1, p.getInUseCount());
assertEquals(9, p.getFreeCount());
}
function testSetMax1() {
var p = new goog.structs.PriorityPool(0, 10);
assertEquals(0, p.getCount());
assertEquals(0, p.getInUseCount());
assertEquals(0, p.getFreeCount());
var o1 = null;
var callback1 = function(obj) {
o1 = obj;
};
var o2 = null;
var callback2 = function(obj) {
o2 = obj;
};
var o3 = null;
var callback3 = function(obj) {
o3 = obj;
};
var o4 = null;
var callback4 = function(obj) {
o4 = obj;
};
var o5 = null;
var callback5 = function(obj) {
o5 = obj;
};
p.getObject(callback1);
p.getObject(callback2);
p.getObject(callback3);
p.getObject(callback4);
p.getObject(callback5);
assertEquals(5, p.getCount());
assertEquals(5, p.getInUseCount());
assertEquals(0, p.getFreeCount());
assertTrue('Result should be true', p.releaseObject(o5));
assertEquals(5, p.getCount());
assertEquals(4, p.getInUseCount());
assertEquals(1, p.getFreeCount());
p.setMaximumCount(4);
assertEquals(4, p.getCount());
assertEquals(4, p.getInUseCount());
assertEquals(0, p.getFreeCount());
}
function testInvalidMinMax1() {
var p = new goog.structs.PriorityPool(0, 10);
assertEquals(0, p.getCount());
assertEquals(0, p.getInUseCount());
assertEquals(0, p.getFreeCount());
try {
p.setMinimumCount(11);
} catch(e) {
assertTrue('Excption caught', true);
return;
}
assertTrue('Exception should of been caught', false);
}
function testInvalidMinMax2() {
var p = new goog.structs.PriorityPool(5, 10);
assertEquals(5, p.getCount());
assertEquals(0, p.getInUseCount());
assertEquals(5, p.getFreeCount());
try {
p.setMaximumCount(4);
} catch(e) {
assertTrue('Excption caught', true);
return;
}
assertTrue('Exception should of been caught', false);
}
function testInvalidMinMax3() {
try {
var p = new goog.structs.PriorityPool(10, 1);
} catch(e) {
assertTrue('Excption caught', true);
return;
}
assertTrue('Exception should of been caught', false);
}
function testQueue1() {
var p = new goog.structs.PriorityPool(0, 2);
var o1 = null;
var callback1 = function(obj) {
o1 = obj;
};
var o2 = null;
var callback2 = function(obj) {
o2 = obj;
};
var o3 = null;
var callback3 = function(obj) {
o3 = obj;
};
p.getObject(callback1);
p.getObject(callback2);
p.getObject(callback3);
assertNotNull(o1);
assertNotNull(o2);
assertNull(o3);
p.releaseObject(o1);
assertNotNull(o3);
}
function testPriority1() {
var p = new goog.structs.PriorityPool(0, 2);
var o1 = null;
var callback1 = function(obj) {
o1 = obj;
};
var o2 = null;
var callback2 = function(obj) {
o2 = obj;
};
var o3 = null;
var callback3 = function(obj) {
o3 = obj;
};
var o4 = null;
var callback4 = function(obj) {
o4 = obj;
};
p.getObject(callback1);
p.getObject(callback2);
p.getObject(callback3, 10);
p.getObject(callback4, 5);
assertNotNull(o1);
assertNotNull(o2);
assertNull(o3);
assertNull(o4);
p.releaseObject(o1);
assertNull(o3);
assertNotNull(o4);
}
</script>
</body>
</html>