|
| 1 | +import { scaleLinear as d3ScaleLinear } from 'd3-scale' |
| 2 | +import { expect } from '@jest/globals' |
| 3 | + |
| 4 | +import builtIn from './builtIn' |
| 5 | + |
| 6 | +const width = 200 |
| 7 | +const height = 100 |
| 8 | +const xDomain = [-5, 5] |
| 9 | +const yDomain = [-5, 5] |
| 10 | +const xScale = d3ScaleLinear().domain(xDomain).range([0, width]) |
| 11 | +const yScale = d3ScaleLinear().domain(yDomain).range([height, 0]) |
| 12 | + |
| 13 | +function toBeCloseTo(got, want, eps = 1e-3) { |
| 14 | + if (!Array.isArray(got) || !Array.isArray(want)) { |
| 15 | + throw new Error('Got and Want must be arrays') |
| 16 | + } |
| 17 | + if (Math.abs(got[0] - want[0]) > eps || Math.abs(got[1] - want[1]) > eps) { |
| 18 | + return { |
| 19 | + message: () => |
| 20 | + `expected ${this.utils.printReceived(got)} to be within range of ${this.utils.printReceived(want)}`, |
| 21 | + pass: false |
| 22 | + } |
| 23 | + } |
| 24 | + return { pass: true } |
| 25 | +} |
| 26 | + |
| 27 | +expect.extend({ toBeCloseTo }) |
| 28 | + |
| 29 | +describe('builtIn sampler', () => { |
| 30 | + describe('with linear sampler', () => { |
| 31 | + it('should render 2 points for x', () => { |
| 32 | + const nSamples = 2 |
| 33 | + // map the screen coordinates [0, width] to the domain [-5, 5] |
| 34 | + const samplerParams = { |
| 35 | + d: { fn: '1000000*x', fnType: 'linear' }, |
| 36 | + range: xDomain, |
| 37 | + xScale, |
| 38 | + yScale, |
| 39 | + xAxis: { type: 'linear' }, |
| 40 | + nSamples |
| 41 | + } |
| 42 | + const data = builtIn(samplerParams) |
| 43 | + expect(data instanceof Array).toEqual(true) |
| 44 | + expect(data.length).toEqual(1) /* we expect 1 group of points (all connected) */ |
| 45 | + expect(data[0].length).toEqual(nSamples) /* the group should have 101 points */ |
| 46 | + expect(data[0][0]).toEqual([-5, -5000000]) |
| 47 | + expect(data[0][1]).toEqual([5, 5000000]) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should render 101 points for x^2', () => { |
| 51 | + const nSamples = 101 |
| 52 | + // map the screen coordinates [0, width] to the domain [-5, 5] |
| 53 | + const samplerParams = { |
| 54 | + d: { fn: 'x^2', fnType: 'linear' }, |
| 55 | + range: xDomain, |
| 56 | + xScale, |
| 57 | + yScale, |
| 58 | + xAxis: { type: 'linear' }, |
| 59 | + nSamples |
| 60 | + } |
| 61 | + const data = builtIn(samplerParams) |
| 62 | + expect(data instanceof Array).toEqual(true) |
| 63 | + expect(data.length).toEqual(1) /* we expect 1 group of points (all connected) */ |
| 64 | + expect(data[0].length).toEqual(nSamples) /* the group should have 101 points */ |
| 65 | + expect(data[0][0]).toEqual([-5, 25]) /* f(-5) = [-5, 25] */ |
| 66 | + expect(data[0][50]).toEqual([0, 0]) /* f(0) = [0, 0] */ |
| 67 | + expect(data[0][100]).toEqual([5, 25]) /* f(5) = [5, 25] */ |
| 68 | + }) |
| 69 | + |
| 70 | + it('should render 2 groups for 1/x', () => { |
| 71 | + const nSamples = 1000 |
| 72 | + // map the screen coordinates [0, width] to the domain [-5, 5] |
| 73 | + const samplerParams = { |
| 74 | + d: { fn: '1/x', fnType: 'linear' }, |
| 75 | + range: xDomain, |
| 76 | + xScale, |
| 77 | + yScale, |
| 78 | + xAxis: { type: 'linear' }, |
| 79 | + nSamples |
| 80 | + } |
| 81 | + const data = builtIn(samplerParams) |
| 82 | + expect(data instanceof Array).toEqual(true) |
| 83 | + expect(data.length).toEqual(2) /* we expect 2 group of points (left of 0, right of 0) */ |
| 84 | + expect(data[0].length).toEqual(nSamples / 2) /* the 1st group should have 50 points */ |
| 85 | + expect(data[1].length).toEqual(nSamples / 2) /* the 2nd group should have 50 points */ |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + describe('with parametric sampler', () => { |
| 90 | + it('should render a circle of radius 1', () => { |
| 91 | + const nSamples = 1001 |
| 92 | + const samplerParams = { |
| 93 | + d: { x: 'cos(t)', y: 'sin(t)', fnType: 'parametric' }, |
| 94 | + range: xDomain, |
| 95 | + xScale, |
| 96 | + yScale, |
| 97 | + xAxis: { type: 'linear' }, |
| 98 | + nSamples |
| 99 | + } |
| 100 | + const data = builtIn(samplerParams) |
| 101 | + expect(data instanceof Array).toEqual(true) |
| 102 | + expect(data.length).toEqual(1) /* we expect 2 group of points (left of 0, right of 0) */ |
| 103 | + expect(data[0].length).toEqual(nSamples) /* the 1st group should have 50 points */ |
| 104 | + expect(data[0][0]).toBeCloseTo([1, 0]) |
| 105 | + expect(data[0][250]).toBeCloseTo([0, 1]) |
| 106 | + expect(data[0][500]).toBeCloseTo([-1, 0]) |
| 107 | + expect(data[0][750]).toBeCloseTo([0, -1]) |
| 108 | + expect(data[0][1000]).toBeCloseTo([1, 0]) |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + describe('with points sampler', () => { |
| 113 | + it('should render a cube', () => { |
| 114 | + const nSamples = 4 |
| 115 | + const samplerParams = { |
| 116 | + d: { |
| 117 | + points: [ |
| 118 | + [1, 1], |
| 119 | + [2, 1], |
| 120 | + [2, 2], |
| 121 | + [1, 2] |
| 122 | + ], |
| 123 | + fnType: 'points' |
| 124 | + }, |
| 125 | + range: xDomain, |
| 126 | + xScale, |
| 127 | + yScale, |
| 128 | + xAxis: { type: 'linear' }, |
| 129 | + nSamples |
| 130 | + } |
| 131 | + const data = builtIn(samplerParams) |
| 132 | + expect(data instanceof Array).toEqual(true) |
| 133 | + expect(data.length).toEqual(1) /* we expect 1 group */ |
| 134 | + expect(data[0].length).toEqual(nSamples) /* to have 4 points */ |
| 135 | + expect(data[0][0]).toBeCloseTo([1, 1]) |
| 136 | + expect(data[0][1]).toBeCloseTo([2, 1]) |
| 137 | + expect(data[0][2]).toBeCloseTo([2, 2]) |
| 138 | + expect(data[0][3]).toBeCloseTo([1, 2]) |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + describe('with vector sampler', () => { |
| 143 | + it('should render a vector', () => { |
| 144 | + const nSamples = 2 |
| 145 | + const samplerParams = { |
| 146 | + d: { |
| 147 | + vector: [2, 1], |
| 148 | + offset: [1, 2], |
| 149 | + fnType: 'vector' |
| 150 | + }, |
| 151 | + range: xDomain, |
| 152 | + xScale, |
| 153 | + yScale, |
| 154 | + xAxis: { type: 'linear' }, |
| 155 | + nSamples |
| 156 | + } |
| 157 | + const data = builtIn(samplerParams) |
| 158 | + expect(data instanceof Array).toEqual(true) |
| 159 | + expect(data.length).toEqual(1) /* we expect 1 group */ |
| 160 | + expect(data[0].length).toEqual(nSamples) /* to have 2 points */ |
| 161 | + expect(data[0][0]).toBeCloseTo([1, 2]) |
| 162 | + expect(data[0][1]).toBeCloseTo([3, 3]) |
| 163 | + }) |
| 164 | + }) |
| 165 | +}) |
0 commit comments