https://github.com/probmods/webppl
Tip revision: f3504df53a41500f4447a497d0aff6b39e58d12b authored by Andreas Stuhlmüller on 25 May 2016, 22:31:08 UTC
0.7.0
0.7.0
Tip revision: f3504df
test-util.js
'use strict';
var util = require('../src/util');
function testAlmostEqual(test, x, y, epsilon) {
if (x === y) {
test.equal(x, y);
} else {
test.ok(Math.abs(x - y) < epsilon);
}
}
module.exports = {
testLogSumExp: {
test1: function(test) {
var epsilon = 0.0000000001;
var xs = [-Infinity, -100, -30, -1, 0, 1, 10];
xs.forEach(
function(x) {
testAlmostEqual(test, x, util.logsumexp([x]), epsilon);
xs.forEach(
function(y) {
var targetVal = Math.log(Math.exp(x) + Math.exp(y));
var actualVal = util.logsumexp([x, y]);
testAlmostEqual(test, targetVal, actualVal, epsilon);
});
});
test.done();
}
},
testCpsIterate: {
test1: function(test) {
var result = util.cpsIterate(5, 3,
function(k, val) { return k(val + 2); },
function(finalVal) { return finalVal; });
// Trampoline.
while (typeof result === 'function') { result = result(); }
test.equal(result, 13);
test.done();
}
},
testIsObject: {
test1: function(test) {
test.strictEqual(util.isObject({}), true);
test.done();
},
test2: function(test) {
test.strictEqual(util.isObject([]), false);
test.done();
},
test3: function(test) {
test.strictEqual(util.isObject(function() {}), false);
test.done();
},
test4: function(test) {
test.strictEqual(util.isObject(undefined), false);
test.done();
}
}
};