forked from probmods/webppl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-types.js
129 lines (104 loc) · 3.73 KB
/
test-types.js
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
'use strict';
var types = require('../src/types');
var Tensor = require('../src/tensor');
var parseInterval = require('../src/math/interval').parse;
function testMembership(type, obj) {
return function(test) {
obj.inside.forEach(function(val) {
test.ok(type.check(val), JSON.stringify(val) + ' was expected to check for type ' + type.desc);
});
obj.outside.forEach(function(val) {
test.ok(!type.check(val), JSON.stringify(val) + ' was not expected to check for type ' + type.desc);
});
test.done();
};
}
function vec(arr) {
return new Tensor([arr.length, 1]).fromFlatArray(arr);
}
function mat(arr) {
return new Tensor([arr.length, arr[0].length]).fromArray(arr);
}
function zeros(dims) {
return new Tensor(dims);
}
var nonNumVals = ['a', true, NaN];
var infVals = [-Infinity, Infinity];
module.exports = {
any: testMembership(types.any, {
inside: nonNumVals.concat(infVals),
outside: []
}),
unboundedInt: testMembership(types.unboundedInt, {
inside: [-1e6, -1, 0, 1, 1e6],
outside: [.1].concat(nonNumVals).concat(infVals)
}),
nonNegativeInt: testMembership(types.nonNegativeInt, {
inside: [0, 1, 1e6],
outside: [-1e6, -1, .1].concat(nonNumVals).concat(infVals)
}),
positiveInt: testMembership(types.positiveInt, {
inside: [1, 1e6],
outside: [-1e6, -1, 0, .1].concat(nonNumVals).concat(infVals)
}),
unboundedReal: testMembership(types.unboundedReal, {
inside: [-1e6, -.1, 0, .1, 1e6],
outside: nonNumVals.concat(infVals)
}),
extendedReal: testMembership(types.extendedReal, {
inside: [-1e6, -.1, 0, .1, 1e6].concat(infVals),
outside: nonNumVals
}),
positiveReal: testMembership(types.positiveReal, {
inside: [.1, 1e6],
outside: [-1, 0].concat(nonNumVals).concat(infVals)
}),
unitInterval: testMembership(types.unitInterval, {
inside: [0, .1, 1],
outside: [-1, 2].concat(nonNumVals).concat(infVals)
}),
anyArray: testMembership(types.array(types.any), {
inside: [[], [0], [.1], ['a'], [true]],
outside: [0, 'a', true]
}),
intArray: testMembership(types.array(types.unboundedInt), {
inside: [[], [0]],
outside: [[.1], ['a'], [true], 0, 'a', true]
}),
vector: testMembership(types.vector(parseInterval('(-Infinity, Infinity)'), true), {
inside: [vec([]), vec([-1e6]), vec([0]), vec([1e6])],
outside: [0, 'a', true, vec([NaN]), vec([-Infinity]), vec([Infinity])]
}),
positiveVector: testMembership(types.vector(parseInterval('(0, Infinity)'), true), {
inside: [vec([]), vec([1e6])],
outside: [0, 'a', true, vec([-1e6]), vec([0]), vec([NaN]), vec([-Infinity]), vec([Infinity])]
}),
vectorOrRealArray: testMembership(types.unboundedVectorOrRealArray, {
inside: [
vec([]), vec([-1e6]), vec([0]), vec([1e6]),
[], [-1e6], [0], [1e6]
],
outside: [
0, 'a', true,
['a'], [true],
[NaN], [-Infinity], [Infinity],
vec([NaN]), vec([-Infinity]), vec([Infinity])
]
}),
posDefMatrix: testMembership(types.posDefMatrix, {
inside: [zeros([1, 1]), zeros([2, 2])],
outside: [zeros([1, 1, 1]), zeros([2, 1]), [], 0, 'a', true]
}),
unboundedTensor: testMembership(types.tensor(parseInterval('(-Infinity, Infinity)'), true), {
inside: [vec([]), mat([[]]), vec([0]), vec([1]), mat([[0]]), mat([[1]]), zeros([1, 1, 1])],
outside: [[], 0, 'a', true]
}),
positiveTensor: testMembership(types.tensor(parseInterval('(0, Infinity)'), true), {
inside: [vec([]), mat([[]]), vec([1]), mat([[1]])],
outside: [vec([0]), mat([[0]]), zeros([1, 1, 1]), [], 0, 'a', true]
}),
probabilityArray: testMembership(types.probabilityArray, {
inside: [[1], [.4, .6], [1 - 9e-9]],
outside: [[], [.999], [.5, .499]].concat(nonNumVals).concat(infVals)
})
};