Add message to assertions.

This commit is contained in:
Marc Jansen
2015-03-30 22:50:15 +02:00
parent 2c40d74a15
commit fb9ba22c30
45 changed files with 394 additions and 221 deletions

View File

@@ -47,32 +47,42 @@ ol.structs.LRUCache = function() {
*/
ol.structs.LRUCache.prototype.assertValid = function() {
if (this.count_ === 0) {
goog.asserts.assert(goog.object.isEmpty(this.entries_));
goog.asserts.assert(goog.isNull(this.oldest_));
goog.asserts.assert(goog.isNull(this.newest_));
goog.asserts.assert(goog.object.isEmpty(this.entries_),
'entries must be an empty object (count = 0)');
goog.asserts.assert(goog.isNull(this.oldest_),
'oldest must be null (count = 0)');
goog.asserts.assert(goog.isNull(this.newest_),
'newest must be null (count = 0)');
} else {
goog.asserts.assert(goog.object.getCount(this.entries_) == this.count_);
goog.asserts.assert(!goog.isNull(this.oldest_));
goog.asserts.assert(goog.isNull(this.oldest_.older));
goog.asserts.assert(!goog.isNull(this.newest_));
goog.asserts.assert(goog.isNull(this.newest_.newer));
goog.asserts.assert(goog.object.getCount(this.entries_) == this.count_,
'number of entries matches count');
goog.asserts.assert(!goog.isNull(this.oldest_),
'we have an oldest entry');
goog.asserts.assert(goog.isNull(this.oldest_.older),
'no entry is older than oldest');
goog.asserts.assert(!goog.isNull(this.newest_),
'we have a newest entry');
goog.asserts.assert(goog.isNull(this.newest_.newer),
'no entry is newer than newest');
var i, entry;
var older = null;
i = 0;
for (entry = this.oldest_; !goog.isNull(entry); entry = entry.newer) {
goog.asserts.assert(entry.older === older);
goog.asserts.assert(entry.older === older,
'entry.older links to correct older');
older = entry;
++i;
}
goog.asserts.assert(i == this.count_);
goog.asserts.assert(i == this.count_, 'iterated correct amount of times');
var newer = null;
i = 0;
for (entry = this.newest_; !goog.isNull(entry); entry = entry.older) {
goog.asserts.assert(entry.newer === newer);
goog.asserts.assert(entry.newer === newer,
'entry.newer links to correct newer');
newer = entry;
++i;
}
goog.asserts.assert(i == this.count_);
goog.asserts.assert(i == this.count_, 'iterated correct amount of times');
}
};
@@ -120,7 +130,7 @@ ol.structs.LRUCache.prototype.forEach = function(f, opt_this) {
*/
ol.structs.LRUCache.prototype.get = function(key) {
var entry = this.entries_[key];
goog.asserts.assert(goog.isDef(entry));
goog.asserts.assert(goog.isDef(entry), 'an entry exists for key %s', key);
if (entry === this.newest_) {
return entry.value_;
} else if (entry === this.oldest_) {
@@ -156,7 +166,7 @@ ol.structs.LRUCache.prototype.getKeys = function() {
for (entry = this.newest_; !goog.isNull(entry); entry = entry.older) {
keys[i++] = entry.key_;
}
goog.asserts.assert(i == this.count_);
goog.asserts.assert(i == this.count_, 'iterated correct number of times');
return keys;
};
@@ -171,7 +181,7 @@ ol.structs.LRUCache.prototype.getValues = function() {
for (entry = this.newest_; !goog.isNull(entry); entry = entry.older) {
values[i++] = entry.value_;
}
goog.asserts.assert(i == this.count_);
goog.asserts.assert(i == this.count_, 'iterated correct number of times');
return values;
};
@@ -180,7 +190,7 @@ ol.structs.LRUCache.prototype.getValues = function() {
* @return {T} Last value.
*/
ol.structs.LRUCache.prototype.peekLast = function() {
goog.asserts.assert(!goog.isNull(this.oldest_));
goog.asserts.assert(!goog.isNull(this.oldest_), 'oldest must not be null');
return this.oldest_.value_;
};
@@ -189,7 +199,7 @@ ol.structs.LRUCache.prototype.peekLast = function() {
* @return {string} Last key.
*/
ol.structs.LRUCache.prototype.peekLastKey = function() {
goog.asserts.assert(!goog.isNull(this.oldest_));
goog.asserts.assert(!goog.isNull(this.oldest_), 'oldest must not be null');
return this.oldest_.key_;
};
@@ -198,10 +208,11 @@ ol.structs.LRUCache.prototype.peekLastKey = function() {
* @return {T} value Value.
*/
ol.structs.LRUCache.prototype.pop = function() {
goog.asserts.assert(!goog.isNull(this.oldest_));
goog.asserts.assert(!goog.isNull(this.newest_));
goog.asserts.assert(!goog.isNull(this.oldest_), 'oldest must not be null');
goog.asserts.assert(!goog.isNull(this.newest_), 'newest must not be null');
var entry = this.oldest_;
goog.asserts.assert(entry.key_ in this.entries_);
goog.asserts.assert(entry.key_ in this.entries_,
'oldest is indexed in entries');
delete this.entries_[entry.key_];
if (!goog.isNull(entry.newer)) {
entry.newer.older = null;
@@ -220,8 +231,10 @@ ol.structs.LRUCache.prototype.pop = function() {
* @param {T} value Value.
*/
ol.structs.LRUCache.prototype.set = function(key, value) {
goog.asserts.assert(!(key in {}));
goog.asserts.assert(!(key in this.entries_));
goog.asserts.assert(!(key in {}),
'key is not a standard property of objects (e.g. "__proto__")');
goog.asserts.assert(!(key in this.entries_),
'key is not used already');
var entry = {
key_: key,
newer: null,