-
Notifications
You must be signed in to change notification settings - Fork 115
/
evaluate-datum.test.js
46 lines (43 loc) · 1.68 KB
/
evaluate-datum.test.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
import { scaleLinear as d3ScaleLinear } from 'd3-scale'
import { builtInEvaluate, intervalEvaluate } from './evaluate-datum.js'
const width = 200
const height = 100
const nSamples = 100
const config = {
options: {
x: {
type: 'linear'
}
},
emit: () => {},
meta: {
xScale: d3ScaleLinear().domain([-5, 5]).range([0, width]),
yScale: d3ScaleLinear().domain([-5, 5]).range([height, 0])
}
}
describe('evaluate', () => {
it('should eval linear functions (builtin)', () => {
const d = { fn: 'x^2', sampler: 'builtIn', fnType: 'linear', nSamples }
const out = builtInEvaluate(config, d)
expect(out instanceof Array).toEqual(true)
expect(out.length).toEqual(1)
expect(out[0].length).toEqual(nSamples)
expect(out[0][0]).toEqual([-5, 25])
expect(out[0][nSamples - 1]).toEqual([5, 25])
expect(out[0][nSamples / 2][0]).toBeCloseTo(0, 0)
expect(out[0][nSamples / 2][1]).toBeCloseTo(0, 0)
})
it('should eval linear functions (interval)', () => {
const d = { fn: 'x^2', sampler: 'interval', fnType: 'linear', nSamples }
const out = intervalEvaluate(config, d)
expect(out.length).toEqual(1)
expect(out[0][0][0].lo <= -5 && out[0][0][0].hi >= -5).toBe(true)
expect(out[0][0][1].lo <= 25 && out[0][0][1].hi >= 25).toBe(true)
expect(out[0][nSamples - 2][0].lo <= 5 && out[0][nSamples - 2][0].hi >= 5).toBe(true)
expect(out[0][nSamples - 2][1].lo <= 25 && out[0][nSamples - 2][1].hi >= 25).toBe(true)
expect(out[0][nSamples / 2][0].lo).toBeCloseTo(0, 0)
expect(out[0][nSamples / 2][0].hi).toBeCloseTo(0, 0)
expect(out[0][nSamples / 2][1].lo).toBeCloseTo(0, 0)
expect(out[0][nSamples / 2][1].hi).toBeCloseTo(0, 0)
})
})