Skip to content

Commit 0a4a76a

Browse files
committed
Change set testing to special-case float comparisons
1 parent 1f19aa3 commit 0a4a76a

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

test/shared_functions.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,33 @@ export function eavsToComparables(eavs, entities, index = {}) {
4141
return results;
4242
}
4343

44-
export function isSetEqual(as, bs) {
44+
// Tests if the elements of `as` are in `bs` by comparing keys in a list. In
45+
// the case of floating points, we want the comparison to be looser, to account
46+
// for floating point rounding errors, which can vary by platform.
47+
48+
export function isSetEqual(as, bs, assert) {
4549
if(as.length !== bs.length) return false;
4650
for(let a of as) {
47-
if(bs.indexOf(a) === -1) return false;
51+
let avA = a.split(", ");
52+
let attributeA = avA[0];
53+
let valueA = avA[1];
54+
// If the attribute is called "floatTest" we know to compare
55+
// the values to within some delta. Otherwise, we can
56+
// do a straight lookup of the key
57+
let delta = 0.00000000001;
58+
if (attributeA === "floatTest") {
59+
for (let b of bs) {
60+
let avB = b.split(", ");
61+
let attributeB = avB[0];
62+
let valueB = avB[1];
63+
if (attributeB === "floatTest" && Math.abs(valueB - valueA) < delta) {
64+
return true;
65+
}
66+
}
67+
return false;
68+
} else {
69+
if(bs.indexOf(a) === -1) return false;
70+
}
4871
}
4972
return true;
5073
}
@@ -92,7 +115,7 @@ export function resolveActualExpected(assert, actuals, expecteds, entities) {
92115
let expectedIx = 0;
93116
for(let expected of expecteds) {
94117
let listEqual, linkEqual;
95-
if(isSetEqual(expected.list, actual.list)) {
118+
if(isSetEqual(expected.list, actual.list, assert)) {
96119
listEqual = true;
97120
} else {
98121
found = false;
@@ -120,7 +143,7 @@ export function resolveActualExpected(assert, actuals, expecteds, entities) {
120143
expectedIx++;
121144
}
122145
if(found === false) {
123-
assert.true(false, "No matching add found for object: " + JSON.stringify(actual.list))
146+
assert.true(false, "No matching add found for actual record: " + JSON.stringify(actual.list))
124147
}
125148
}
126149
}
@@ -164,26 +187,31 @@ export function evaluate(assert, expected, code, session = new Database()) {
164187
return next;
165188
}
166189

167-
export function testSingleExpressionByList(list:any[]){
190+
export interface valueTest {
191+
expression: string;
192+
expectedValue: number;
193+
};
194+
195+
export function testSingleExpressionByList(list: valueTest[]){
168196
list.forEach((list_item,index) =>{
169-
test(`Is ${list_item.Expression} returning ${list_item.Value}?`, (assert) => {
197+
test(`Is ${list_item.expression} returning ${list_item.expectedValue}?`, (assert) => {
170198
let expected = {
171199
insert: [
172-
["a", "result", list_item.Value],
200+
["a", "floatTest", list_item.expectedValue],
173201
],
174202
remove: [],
175203
};
176204

177205
evaluate(assert, expected, `
178206
~~~
179207
search
180-
x = ${list_item.Expression}
208+
x = ${list_item.expression}
181209
182210
bind
183-
[result: x]
211+
[floatTest: x]
184212
~~~
185213
`);
186214
assert.end();
187215
});
188216
});
189-
}
217+
}

0 commit comments

Comments
 (0)